Skip to content

Commit

Permalink
Replace (most) shelljs with fs-extra
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed May 19, 2018
1 parent c700a5d commit d84c28a
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 104 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bplist-parser": "^0.1.0",
"cordova-registry-mapper": "^1.1.8",
"elementtree": "0.1.7",
"fs-extra": "^6.0.1",
"glob": "^7.1.2",
"minimatch": "^3.0.0",
"plist": "^3.0.1",
Expand Down
87 changes: 43 additions & 44 deletions spec/ConfigChanges/ConfigChanges.spec.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion spec/ConfigChanges/ConfigFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

var rewire = require('rewire');
var configFile = rewire('../../src/ConfigChanges/ConfigFile');
var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var projectDir = path.join('project_dir', 'app', 'src', 'main');

Expand Down
2 changes: 1 addition & 1 deletion spec/ConfigParser/ConfigParser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

var path = require('path');
var fs = require('fs');
var fs = require('fs-extra');
var ConfigParser = require('../../src/ConfigParser/ConfigParser');
var xml = path.join(__dirname, '../fixtures/test-config.xml');
var xml_contents = fs.readFileSync(xml, 'utf-8');
Expand Down
32 changes: 16 additions & 16 deletions spec/CordovaCheck.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

var shell = require('shelljs');
var fs = require('fs-extra');
var path = require('path');
var CordovaCheck = require('../src/CordovaCheck');

Expand All @@ -31,12 +31,12 @@ describe('findProjectRoot method', function () {
process.chdir(cwd);
});
function removeDir (someDirectory) {
shell.rm('-rf', someDirectory);
fs.removeSync(someDirectory);
}
it('Test 001 : should return false if it hits the home directory', function () {
var somedir = path.join(home, 'somedir');
removeDir(somedir);
shell.mkdir(somedir);
fs.ensureDirSync(somedir);
expect(CordovaCheck.findProjectRoot(somedir)).toEqual(false);
});
it('Test 002 : should return false if it cannot find a .cordova directory up the directory tree', function () {
Expand All @@ -47,27 +47,27 @@ describe('findProjectRoot method', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
shell.mkdir('-p', anotherdir);
shell.mkdir('-p', path.join(somedir, 'www', 'config.xml'));
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www', 'config.xml'));
expect(CordovaCheck.findProjectRoot(somedir)).toEqual(somedir);
});
it('Test 004 : should ignore PWD when its undefined', function () {
delete process.env.PWD;
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
shell.mkdir('-p', anotherdir);
shell.mkdir('-p', path.join(somedir, 'www'));
shell.mkdir('-p', path.join(somedir, 'config.xml'));
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www'));
fs.ensureDirSync(path.join(somedir, 'config.xml'));
process.chdir(anotherdir);
expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
});
it('Test 005 : should use PWD when available', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
shell.mkdir('-p', anotherdir);
shell.mkdir('-p', path.join(somedir, 'www', 'config.xml'));
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www', 'config.xml'));
process.env.PWD = anotherdir;
process.chdir(path.sep);
expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
Expand All @@ -76,8 +76,8 @@ describe('findProjectRoot method', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
shell.mkdir('-p', anotherdir);
shell.mkdir('-p', path.join(somedir, 'www', 'config.xml'));
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www', 'config.xml'));
process.env.PWD = path.sep;
process.chdir(anotherdir);
expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
Expand All @@ -86,10 +86,10 @@ describe('findProjectRoot method', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
shell.mkdir('-p', anotherdir);
shell.mkdir('-p', path.join(anotherdir, 'www', 'config.xml'));
shell.mkdir('-p', path.join(somedir, 'www'));
shell.mkdir('-p', path.join(somedir, 'config.xml'));
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(anotherdir, 'www', 'config.xml'));
fs.ensureDirSync(path.join(somedir, 'www'));
fs.ensureDirSync(path.join(somedir, 'config.xml'));
expect(CordovaCheck.findProjectRoot(anotherdir)).toEqual(somedir);
});
});
9 changes: 4 additions & 5 deletions spec/FileUpdater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function mockDirStats () {
};
}

// Create a mock to replace the fs and shelljs modules used by the FileUpdater,
// Create a mock to replace the fs-extra module used by the FileUpdater,
// so the tests don't have to actually touch the filesystem.
var mockFs = {
mkdirPaths: [],
Expand Down Expand Up @@ -86,20 +86,19 @@ var mockFs = {
return result;
},

mkdir: function (flags, path) {
ensureDirSync: function (path) {
this.mkdirPaths.push(path);
},

cp: function (flags, sourcePath, targetPath) {
copySync: function (sourcePath, targetPath) {
this.cpPaths.push([sourcePath, targetPath]);
},

rm: function (flags, path) {
removeSync: function (path) {
this.rmPaths.push(path);
}
};
FileUpdater.__set__('fs', mockFs);
FileUpdater.__set__('shell', mockFs);

// Define some constants used in the test cases.
var testRootDir = 'testRootDir';
Expand Down
5 changes: 2 additions & 3 deletions spec/PluginManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
// require('promise-matchers');

var Q = require('q');
var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var shell = require('shelljs');
var rewire = require('rewire');
var PluginManager = rewire('../src/PluginManager');
var PluginInfo = require('../src/PluginInfo/PluginInfo');
Expand All @@ -42,7 +41,7 @@ describe('PluginManager class', function () {
beforeEach(function () {
spyOn(ConfigChanges, 'PlatformMunger');
spyOn(fs, 'writeFileSync');
spyOn(shell, 'mkdir');
spyOn(fs, 'ensureDirSync');
});

it('Test 001 : should be constructable', function () {
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigChanges/ConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/* eslint no-control-regex: 0 */

var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');

var modules = {};
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigParser/ConfigParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
var et = require('elementtree');
var xml = require('../util/xml-helpers');
var CordovaError = require('../CordovaError/CordovaError');
var fs = require('fs');
var fs = require('fs-extra');
var events = require('../events');

/** Wraps a config.xml file */
Expand Down
2 changes: 1 addition & 1 deletion src/CordovaCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');

function isRootDir (dir) {
Expand Down
33 changes: 11 additions & 22 deletions src/FileUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

'use strict';

var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var shell = require('shelljs');
var minimatch = require('minimatch');

/**
Expand Down Expand Up @@ -68,14 +67,9 @@ function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats,

if (targetStats) {
// The target exists. But if the directory status doesn't match the source, delete it.
if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
log('rmdir ' + targetPath + ' (source is a file)');
shell.rm('-rf', targetFullPath);
targetStats = null;
updated = true;
} else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
log('delete ' + targetPath + ' (source is a directory)');
shell.rm('-f', targetFullPath);
if ((targetStats.isDirectory() && !sourceStats.isDirectory()) || (!targetStats.isDirectory() && sourceStats.isDirectory())) {
log('delete ' + targetPath);
fs.removeSync(targetFullPath);
targetStats = null;
updated = true;
}
Expand All @@ -85,20 +79,20 @@ function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats,
if (sourceStats.isDirectory()) {
// The target directory does not exist, so it should be created.
log('mkdir ' + targetPath);
shell.mkdir('-p', targetFullPath);
fs.ensureDirSync(targetFullPath);
updated = true;
} else if (sourceStats.isFile()) {
// The target file does not exist, so it should be copied from the source.
log('copy ' + sourcePath + ' ' + targetPath + (copyAll ? '' : ' (new file)'));
shell.cp('-f', sourceFullPath, targetFullPath);
fs.copySync(sourceFullPath, targetFullPath);
updated = true;
}
} else if (sourceStats.isFile() && targetStats.isFile()) {
// The source and target paths both exist and are files.
if (copyAll) {
// The caller specified all files should be copied.
log('copy ' + sourcePath + ' ' + targetPath);
shell.cp('-f', sourceFullPath, targetFullPath);
fs.copySync(sourceFullPath, targetFullPath);
updated = true;
} else {
// Copy if the source has been modified since it was copied to the target, or if
Expand All @@ -108,20 +102,15 @@ function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats,
if (sourceStats.mtime.getTime() >= targetStats.mtime.getTime() ||
sourceStats.size !== targetStats.size) {
log('copy ' + sourcePath + ' ' + targetPath + ' (updated file)');
shell.cp('-f', sourceFullPath, targetFullPath);
fs.copySync(sourceFullPath, targetFullPath);
updated = true;
}
}
}
} else if (targetStats) {
// The target exists but the source is null, so the target should be deleted.
if (targetStats.isDirectory()) {
log('rmdir ' + targetPath + (copyAll ? '' : ' (no source)'));
shell.rm('-rf', targetFullPath);
} else {
log('delete ' + targetPath + (copyAll ? '' : ' (no source)'));
shell.rm('-f', targetFullPath);
}
log('delete ' + targetPath + (copyAll ? '' : ' (no source)'));
fs.removeSync(targetFullPath);
updated = true;
}

Expand Down Expand Up @@ -150,7 +139,7 @@ function updatePathInternal (sourcePath, targetPath, options, log) {
// Create the target's parent directory if it doesn't exist.
var parentDir = path.dirname(targetFullPath);
if (!fs.existsSync(parentDir)) {
shell.mkdir('-p', parentDir);
fs.ensureDirSync(parentDir);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/PlatformJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
*
*/

var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var shelljs = require('shelljs');
var mungeutil = require('./ConfigChanges/munge-util');
var pluginMappernto = require('cordova-registry-mapper').newToOld;
var pluginMapperotn = require('cordova-registry-mapper').oldToNew;
Expand All @@ -37,7 +36,7 @@ PlatformJson.load = function (plugins_dir, platform) {
};

PlatformJson.prototype.save = function () {
shelljs.mkdir('-p', path.dirname(this.filePath));
fs.ensureDirSync(path.dirname(this.filePath));
fs.writeFileSync(this.filePath, JSON.stringify(this.root, null, 2), 'utf-8');
};

Expand Down Expand Up @@ -212,7 +211,7 @@ PlatformJson.prototype.generateMetadata = function () {
*/
PlatformJson.prototype.generateAndSaveMetadata = function (destination) {
var meta = this.generateMetadata();
shelljs.mkdir('-p', path.dirname(destination));
fs.ensureDirSync(path.dirname(destination));
fs.writeFileSync(destination, meta, 'utf-8');

return this;
Expand Down
2 changes: 1 addition & 1 deletion src/PluginInfo/PluginInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TODO (kamrik): refactor this to not use sync functions and return promises.
*/

var path = require('path');
var fs = require('fs');
var fs = require('fs-extra');
var xml_helpers = require('../util/xml-helpers');
var CordovaError = require('../CordovaError/CordovaError');

Expand Down
2 changes: 1 addition & 1 deletion src/PluginInfo/PluginInfoProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

/* jshint sub:true, laxcomma:true, laxbreak:true */

var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var PluginInfo = require('./PluginInfo');
var events = require('../events');
Expand Down
2 changes: 1 addition & 1 deletion src/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

var Q = require('q');
var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');

var ActionStack = require('./ActionStack');
Expand Down
2 changes: 1 addition & 1 deletion src/superspawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

var child_process = require('child_process');
var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var _ = require('underscore');
var Q = require('q');
Expand Down
2 changes: 1 addition & 1 deletion src/util/xml-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* contains XML utility functions, some of which are specific to elementtree
*/

var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var _ = require('underscore');
var et = require('elementtree');
Expand Down

0 comments on commit d84c28a

Please sign in to comment.