Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CB-14140] Replace shelljs calls with fs-extra & which #21

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
"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",
"q": "^1.4.1",
"shelljs": "^0.8.1",
"strip-bom": "^3.0.0",
"underscore": "^1.8.3"
"underscore": "^1.8.3",
"which": "^1.3.0"
},
"devDependencies": {
"eslint": "^4.0.0",
Expand Down
105 changes: 61 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
44 changes: 20 additions & 24 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 @@ -30,13 +30,10 @@ describe('findProjectRoot method', function () {
process.env.PWD = origPWD;
process.chdir(cwd);
});
function removeDir (someDirectory) {
shell.rm('-rf', 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.emptyDirSync(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 @@ -46,50 +43,49 @@ describe('findProjectRoot method', function () {
it('Test 003 : should return the first directory it finds with a .cordova folder in it', 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.removeSync(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureFileSync(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.removeSync(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www'));
fs.ensureFileSync(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.removeSync(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureFileSync(path.join(somedir, 'www', 'config.xml'));
process.env.PWD = anotherdir;
process.chdir(path.sep);
expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
});
it('Test 006 : should use cwd as a fallback when PWD is not a cordova dir', 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.removeSync(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureFileSync(path.join(somedir, 'www', 'config.xml'));
process.env.PWD = path.sep;
process.chdir(anotherdir);
expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
});
it('Test 007 : should ignore platform www/config.xml', 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.removeSync(somedir);
fs.ensureFileSync(path.join(anotherdir, 'www', 'config.xml'));
fs.ensureDirSync(path.join(somedir, 'www'));
fs.ensureFileSync(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
16 changes: 16 additions & 0 deletions spec/PlatformJson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ describe('PlatformJson class', function () {
expect(meta).toMatch(JSON.stringify(platformJson.root.plugin_metadata, null, 2));
});
});

describe('generateAndSaveMetadata method', function () {
it('should save generated metadata', function () {
// Needs to use graceful-fs, since that is used by fs-extra
const spy = spyOn(require('graceful-fs'), 'writeFileSync');

const dest = require('path').join(__dirname, 'test-destination');
platformJson.addPluginMetadata(fakePlugin).generateAndSaveMetadata(dest);

expect(spy).toHaveBeenCalledTimes(1);
const [file, data] = spy.calls.argsFor(0);
expect(file).toBe(dest);
expect(data.indexOf(JSON.stringify(platformJson.root.modules, null, 2))).toBeGreaterThan(0);
expect(data).toMatch(JSON.stringify(platformJson.root.plugin_metadata, null, 2));
});
});
});
});

Expand Down
6 changes: 3 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 @@ -41,8 +40,9 @@ describe('PluginManager class', function () {

beforeEach(function () {
spyOn(ConfigChanges, 'PlatformMunger');
spyOn(fs, 'outputJsonSync');
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
39 changes: 13 additions & 26 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 @@ -66,39 +65,32 @@ function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats,
if (sourceStats) {
var sourceFullPath = path.join(rootDir || '', sourcePath);

if (targetStats) {
if (targetStats && (targetStats.isDirectory() !== sourceStats.isDirectory())) {
// 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);
targetStats = null;
updated = true;
}
log('delete ' + targetPath);
fs.removeSync(targetFullPath);
targetStats = null;
updated = true;
}

if (!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 +100,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 +137,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
10 changes: 3 additions & 7 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,8 +36,7 @@ PlatformJson.load = function (plugins_dir, platform) {
};

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

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

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
Loading