8000 added verbosity flag (-v) for cp, mv and rm commands · shelljs/shelljs@1e3df2a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e3df2a

Browse files
committed
added verbosity flag (-v) for cp, mv and rm commands
1 parent f7a7c75 commit 1e3df2a

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/cp.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ common.register('cp', _cp, {
1212
'L': 'followsymlink',
1313
'P': 'noFollowsymlink',
1414
'p': 'preserve',
15+
'v': 'verbose',
1516
},
1617
wrapOutput: false,
1718
});
@@ -84,6 +85,10 @@ function copyFileSync(srcFile, destFile, options) {
8485
fs.closeSync(fdr);
8586
fs.closeSync(fdw);
8687
}
88+
89+
if (options.verbose) {
90+
console.log("copied '" + srcFile + "' -> '" + destFile + "'");
91+
}
8792
}
8893

8994
// Recursively copies 'sourceDir' into 'destDir'
@@ -108,6 +113,9 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) {
108113
var checkDir = common.statFollowLinks(sourceDir);
109114
try {
110115
fs.mkdirSync(destDir);
116+
if (opts.verbose) {
117+
console.log("created directory '" + sourceDir + "' -> '" + destDir + "'");
118+
}
111119
} catch (e) {
112120
// if the directory already exists, that's okay
113121
if (e.code !== 'EEXIST') throw e;

src/mv.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ common.register('mv', _mv, {
88
cmdOptions: {
99
'f': '!no_force',
1010
'n': 'no_force',
11+
'v': 'verbose',
1112
},
1213
});
1314

@@ -102,15 +103,18 @@ function _mv(options, sources, dest) {
102103

103104
try {
104105
fs.renameSync(src, thisDest);
106+
if (options.verbose) {
107+
console.log("renamed '" + src + "' -> '" + thisDest + "'");
108+
}
105109
} catch (e) {
106110
/* istanbul ignore next */
107111
if (e.code === 'EXDEV') {
108112
// If we're trying to `mv` to an external partition, we'll actually need
109113
// to perform a copy and then clean up the original file. If either the
110114
// copy or the rm fails with an exception, we should allow this
111115
// exception to pass up to the top level.
112-
cp({ recursive: true }, src, thisDest);
113-
rm({ recursive: true, force: true }, src);
116+
cp({ recursive: true, verbose: options.verbose }, src, thisDest);
117+
rm({ recursive: true, force: true, verbose: options.verbose }, src);
114118
}
115119
}
116120
}); // forEach(src)

src/rm.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ common.register('rm', _rm, {
66
'f': 'force',
77
'r': 'recursive',
88
'R': 'recursive',
9+
'v': 'verbose',
910
},
1011
});
1112

@@ -17,7 +18,7 @@ common.register('rm', _rm, {
1718
//
1819
// Licensed under the MIT License
1920
// http://www.opensource.org/licenses/mit-license.php
20-
function rmdirSyncRecursive(dir, force, fromSymlink) {
21+
function rmdirSyncRecursive(dir, force, verbose, fromSymlink) {
2122
var files;
2223

2324
files = fs.readdirSync(dir);
@@ -28,11 +29,14 @@ function rmdirSyncRecursive(dir, force, fromSymlink) {
2829
var currFile = common.statNoFollowLinks(file);
2930

3031
if (currFile.isDirectory()) { // Recursive function back to the beginning
31-
rmdirSyncRecursive(file, force);
32+
rmdirSyncRecursive(file, force, verbose);
3233
} else if (force || isWriteable(file)) {
3334
// Assume it's a file - perhaps a try/catch belongs here?
3435
try {
3536
common.unlinkSync(file);
37+
if (verbose) {
38+
console.log("removed '" + file + "'");
39+
}
3640
} catch (e) {
3741
/* istanbul ignore next */
3842
common.error('could not remove file (code ' + e.code + '): ' + file, {
@@ -59,6 +63,9 @@ function rmdirSyncRecursive(dir, force, fromSymlink) {
5963
try {
6064
result = fs.rmdirSync(dir);
6165
if (fs.existsSync(dir)) throw { code: 'EAGAIN' };
66+
if (verbose) {
67+
console.log("removed directory'" + dir + "'");
68+
}
6269
break;
6370
} catch (er) {
6471
/* istanbul ignore next */
@@ -98,6 +105,9 @@ function handleFile(file, options) {
98105
if (options.force || isWriteable(file)) {
99106
// -f was passed, or file is writable, so it can be removed
100107
common.unlinkSync(file);
108+
if (options.verbose) {
109+
console.log("removed '" + file + "'");
110+
}
101111
} else {
102112
common.error('permission denied: ' + file, { continue: true });
103113
}
@@ -106,43 +116,53 @@ function handleFile(file, options) {
106116
function handleDirectory(file, options) {
107117
if (options.recursive) {
108118
// -r was passed, so directory can be removed
109-
rmdirSyncRecursive(file, options.force);
119+
rmdirSyncRecursive(file, options.force, options.verbose);
110120
} else {
111121
common.error('path is a directory', { continue: true });
112122
}
113123
}
114124

115125
function handleSymbolicLink(file, options) {
126+
function _unlink() {
127+
common.unlinkSync(file);
128+
if (options.verbose) {
129+
console.log("removed '" + file + "'");
130+
}
131+
}
132+
116133
var stats;
117134
try {
118135
stats = common.statFollowLinks(file);
119136
} catch (e) {
120137
// symlink is broken, so remove the symlink itself
121-
common.unlinkSync(file);
138+
_unlink();
122139
return;
123140
}
124141

125142
if (stats.isFile()) {
126-
common.unlinkSync(file);
143+
_unlink();
127144
} else if (stats.isDirectory()) {
128145
if (file[file.length - 1] === '/') {
129146
// trailing separator, so remove the contents, not the link
130147
if (options.recursive) {
131148
// -r was passed, so directory can be removed
132149
var fromSymlink = true;
133-
rmdirSyncRecursive(file, options.force, fromSymlink);
150+
rmdirSyncRecursive(file, options.force, options.verbose, fromSymlink);
134151
} else {
135152
common.error('path is a directory', { continue: true });
136153
}
137154
} else {
138155
// no trailing separator, so remove the link
139-
common.unlinkSync(file);
156+
_unlink();
140157
}
141158
}
142159
}
143160

144-
function handleFIFO(file) {
161+
function handleFIFO(file, options) {
145162
common.unlinkSync(file);
163+
if (options.verbose) {
164+
console.log("removed '" + file + "'");
165+
}
146166
}
147167

148168
//@
@@ -193,7 +213,7 @@ function _rm(options, files) {
193213
} else if (lstats.isSymbolicLink()) {
194214
handleSymbolicLink(file, options);
195215
} else if (lstats.isFIFO()) {
196-
handleFIFO(file);
216+
handleFIFO(file, options);
197217
}
198218
}); // forEach(file)
199219
return '';

0 commit comments

Comments
 (0)
0