Skip to content

Commit 93ea025

Browse files
gyandeepsnfischer
authored andcommitted
Revert "refactor: replace fs.existsSync" (fixes shelljs#531) (shelljs#532)
This reverts commit ab8cf5a.
1 parent 9bf98de commit 93ea025

39 files changed

Lines changed: 241 additions & 254 deletions

src/cat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function _cat(options, files) {
2828
files = [].slice.call(arguments, 1);
2929

3030
files.forEach(function (file) {
31-
if (!common.existsSync(file)) {
31+
if (!fs.existsSync(file)) {
3232
common.error('no such file or directory: ' + file);
3333
}
3434

src/chmod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function _chmod(options, mode, filePattern) {
102102

103103
files.forEach(function innerChmod(file) {
104104
file = path.resolve(file);
105-
if (!common.existsSync(file)) {
105+
if (!fs.existsSync(file)) {
106106
common.error('File not found: ' + file);
107107
}
108108

src/common.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ function error(msg, _code, _continue) {
8181
}
8282
exports.error = error;
8383

84-
function existsSync(file) {
85-
try {
86-
fs.statSync(file);
87-
return true;
88-
} catch (e) {
89-
return false;
90-
}
91-
}
92-
exports.existsSync = existsSync;
93-
9484
//@
9585
//@ ### ShellString(str)
9686
//@

src/cp.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ common.register('cp', _cp, {
2020
// (Using readFileSync() + writeFileSync() could easily cause a memory overflow
2121
// with large files)
2222
function copyFileSync(srcFile, destFile, options) {
23-
if (!common.existsSync(srcFile)) {
23+
if (!fs.existsSync(srcFile)) {
2424
common.error('copyFileSync: no such file or directory: ' + srcFile);
2525
}
2626

@@ -145,7 +145,7 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
145145
}
146146
} else {
147147
/* At this point, we've hit a file actually worth copying... so copy it on over. */
148-
if (common.existsSync(destFile) && opts.no_force) {
148+
if (fs.existsSync(destFile) && opts.no_force) {
149149
common.log('skipping existing file: ' + files[i]);
150150
} else {
151151
copyFileSync(srcFile, destFile, opts);
@@ -215,7 +215,7 @@ function _cp(options, sources, dest) {
215215
dest = arguments[arguments.length - 1];
216216
}
217217

218-
var destExists = common.existsSync(dest);
218+
var destExists = fs.existsSync(dest);
219219
var destStat = destExists && fs.statSync(dest);
220220

221221
// Dest is not existing dir, but multiple sources given
@@ -229,7 +229,7 @@ function _cp(options, sources, dest) {
229229
}
230230

231231
sources.forEach(function (src) {
232-
if (!common.existsSync(src)) {
232+
if (!fs.existsSync(src)) {
233233
common.error('no such file or directory: ' + src, true);
234234
return; // skip file
235235
}
@@ -262,7 +262,7 @@ function _cp(options, sources, dest) {
262262
thisDest = path.normalize(dest + '/' + path.basename(src));
263263
}
264264

265-
if (common.existsSync(thisDest) && options.no_force) {
265+
if (fs.existsSync(thisDest) && options.no_force) {
266266
return; // skip file
267267
}
268268

src/exec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function execSync(cmd, opts, pipe) {
3737
var previousStderrContent = '';
3838
// Echoes stdout and stderr changes from running process, if not silent
3939
function updateStream(streamFile) {
40-
if (opts.silent || !common.existsSync(streamFile)) {
40+
if (opts.silent || !fs.existsSync(streamFile)) {
4141
return;
4242
}
4343

@@ -61,10 +61,10 @@ function execSync(cmd, opts, pipe) {
6161
previousStreamContent = streamContent;
6262
}
6363

64-
if (common.existsSync(scriptFile)) common.unlinkSync(scriptFile);
65-
if (common.existsSync(stdoutFile)) common.unlinkSync(stdoutFile);
66-
if (common.existsSync(stderrFile)) common.unlinkSync(stderrFile);
67-
if (common.existsSync(codeFile)) common.unlinkSync(codeFile);
64+
if (fs.existsSync(scriptFile)) common.unlinkSync(scriptFile);
65+
if (fs.existsSync(stdoutFile)) common.unlinkSync(stdoutFile);
66+
if (fs.existsSync(stderrFile)) common.unlinkSync(stderrFile);
67+
if (fs.existsSync(codeFile)) common.unlinkSync(codeFile);
6868

6969
var execCommand = JSON.stringify(process.execPath) + ' ' + JSON.stringify(scriptFile);
7070
var script;
@@ -134,9 +134,9 @@ function execSync(cmd, opts, pipe) {
134134
// sleepFile is used as a dummy I/O op to mitigate unnecessary CPU usage
135135
// (tried many I/O sync ops, writeFileSync() seems to be only one that is effective in reducing
136136
// CPU usage, though apparently not so much on Windows)
137-
while (!common.existsSync(codeFile)) { updateStream(stdoutFile); fs.writeFileSync(sleepFile, 'a'); }
138-
while (!common.existsSync(stdoutFile)) { updateStream(stdoutFile); fs.writeFileSync(sleepFile, 'a'); }
139-
while (!common.existsSync(stderrFile)) { updateStream(stderrFile); fs.writeFileSync(sleepFile, 'a'); }
137+
while (!fs.existsSync(codeFile)) { updateStream(stdoutFile); fs.writeFileSync(sleepFile, 'a'); }
138+
while (!fs.existsSync(stdoutFile)) { updateStream(stdoutFile); fs.writeFileSync(sleepFile, 'a'); }
139+
while (!fs.existsSync(stderrFile)) { updateStream(stderrFile); fs.writeFileSync(sleepFile, 'a'); }
140140
try { common.unlinkSync(sleepFile); } catch (e) {}
141141
}
142142

src/grep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function _grep(options, regex, files) {
4141

4242
var grep = [];
4343
files.forEach(function (file) {
44-
if (!common.existsSync(file) && file !== '-') {
44+
if (!fs.existsSync(file) && file !== '-') {
4545
common.error('no such file or directory: ' + file, 2, true);
4646
return;
4747
}

src/head.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function _head(options, files) {
7272

7373
var shouldAppendNewline = false;
7474
files.forEach(function (file) {
75-
if (!common.existsSync(file) && file !== '-') {
75+
if (!fs.existsSync(file) && file !== '-') {
7676
common.error('no such file or directory: ' + file, true);
7777
return;
7878
}

src/ln.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function _ln(options, source, dest) {
3434
var isAbsolute = (path.resolve(source) === sourcePath);
3535
dest = path.resolve(process.cwd(), String(dest));
3636

37-
if (common.existsSync(dest)) {
37+
if (fs.existsSync(dest)) {
3838
if (!options.force) {
3939
common.error('Destination file exists', true);
4040
}
@@ -46,7 +46,7 @@ function _ln(options, source, dest) {
4646
var isWindows = common.platform === 'win';
4747
var linkType = isWindows ? 'file' : null;
4848
var resolvedSourcePath = isAbsolute ? sourcePath : path.resolve(process.cwd(), path.dirname(dest), source);
49-
if (!common.existsSync(resolvedSourcePath)) {
49+
if (!fs.existsSync(resolvedSourcePath)) {
5050
common.error('Source file does not exist', true);
5151
} else if (isWindows && fs.statSync(resolvedSourcePath).isDirectory()) {
5252
linkType = 'junction';
@@ -58,7 +58,7 @@ function _ln(options, source, dest) {
5858
common.error(err.message);
5959
}
6060
} else {
61-
if (!common.existsSync(source)) {
61+
if (!fs.existsSync(source)) {
6262
common.error('Source file does not exist', true);
6363
}
6464
try {

src/mkdir.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function mkdirSyncRecursive(dir) {
2020
}
2121

2222
// Base dir exists, no recursion necessary
23-
if (common.existsSync(baseDir)) {
23+
if (fs.existsSync(baseDir)) {
2424
fs.mkdirSync(dir, parseInt('0777', 8));
2525
return;
2626
}
@@ -68,7 +68,7 @@ function _mkdir(options, dirs) {
6868

6969
// Base dir does not exist, and no -p option given
7070
var baseDir = path.dirname(dir);
71-
if (!common.existsSync(baseDir) && !options.fullpath) {
71+
if (!fs.existsSync(baseDir) && !options.fullpath) {
7272
common.error('no such file or directory: ' + baseDir, true);
7373
return; // skip dir
7474
}

src/mv.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function _mv(options, sources, dest) {
4141
common.error('invalid arguments');
4242
}
4343

44-
var exists = common.existsSync(dest);
44+
var exists = fs.existsSync(dest);
4545
var stats = exists && fs.statSync(dest);
4646

4747
// Dest is not existing dir, but multiple sources given
@@ -55,7 +55,7 @@ function _mv(options, sources, dest) {
5555
}
5656

5757
sources.forEach(function (src) {
58-
if (!common.existsSync(src)) {
58+
if (!fs.existsSync(src)) {
5959
common.error('no such file or directory: ' + src, true);
6060
return; // skip file
6161
}
@@ -65,11 +65,11 @@ function _mv(options, sources, dest) {
6565
// When copying to '/path/dir':
6666
// thisDest = '/path/dir/file1'
6767
var thisDest = dest;
68-
if (common.existsSync(dest) && fs.statSync(dest).isDirectory()) {
68+
if (fs.existsSync(dest) && fs.statSync(dest).isDirectory()) {
6969
thisDest = path.normalize(dest + '/' + path.basename(src));
7070
}
7171

72-
if (common.existsSync(thisDest) && options.no_force) {
72+
if (fs.existsSync(thisDest) && options.no_force) {
7373
common.error('dest file already exists: ' + thisDest, true);
7474
return; // skip file
7575
}

0 commit comments

Comments
 (0)