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

breaking: replace shelljs with fs-extra #851

Merged
merged 19 commits into from
May 27, 2020
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
30 changes: 0 additions & 30 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -224,33 +224,3 @@ CordovaLib/classes/NSData+Base64.*
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source
// distribution.

================================================================================
bin/node_modules/shelljs:
================================================================================
Copyright (c) 2012, Artur Adib <aadib@mozilla.com>
All rights reserved.

You may use this project under the terms of the New BSD license as follows:

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Artur Adib nor the
names of the contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL ARTUR ADIB BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
154 changes: 77 additions & 77 deletions bin/lib/create.js

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions bin/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

/*
Provides a set of utility methods, which can also be spied on during unit tests.
*/

// TODO: Perhaps this should live in cordova-common?

const fs = require('fs-extra');
const { events } = require('cordova-common');

/**
* Reads, searches, and replaces the found occurences with replacementString and then writes the file back out.
* A backup is not made.
*
* @param {string} file A file path to a readable & writable file
* @param {RegExp} searchRegex The search regex
* @param {string} replacementString The string to replace the found occurences
* @returns {void}
*/
exports.replaceFileContents = function (file, searchRegex, replacementString) {
let contents;
try {
contents = fs.readFileSync(file).toString();
} catch (ex) {
events.emit('verbose', `Trying to read file: ${file}`);
throw ex;
}
contents = contents.replace(searchRegex, replacementString);
fs.writeFileSync(file, contents);
};

/**
* Reads a file and scans for regex. Returns the line of the first occurence or null if no occurences are found.
*
* @param {string} file A file path
* @param {RegExp} regex A search regex
* @returns string|null
*/
exports.grep = function (file, regex) {
const contents = fs.readFileSync(file).toString().replace(/\\r/g, '').split('\n');
for (let i = 0; i < contents.length; i++) {
const line = contents[i];
if (regex.test(line)) {
return line;
}
}
return null;
};
8 changes: 4 additions & 4 deletions bin/templates/scripts/cordova/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const Q = require('q');
const path = require('path');
const shell = require('shelljs');
const which = require('which');
const superspawn = require('cordova-common').superspawn;
const fs = require('fs-extra');
const plist = require('plist');
Expand Down Expand Up @@ -222,7 +222,7 @@ module.exports.run = buildOpts => {
const buildOutputDir = path.join(projectPath, 'build', (buildOpts.device ? 'device' : 'emulator'));

// remove the build/device folder before building
shell.rm('-rf', buildOutputDir);
fs.removeSync(buildOutputDir);

const xcodebuildArgs = getXcodeBuildArgs(projectName, projectPath, configuration, buildOpts.device, buildOpts.buildFlag, emulatorTarget, buildOpts.automaticProvisioning);
return superspawn.spawn('xcodebuild', xcodebuildArgs, { cwd: projectPath, printCommand: true, stdio: 'inherit' });
Expand Down Expand Up @@ -262,7 +262,7 @@ module.exports.run = buildOpts => {
const buildOutputDir = path.join(projectPath, 'build', 'device');

function checkSystemRuby () {
const ruby_cmd = shell.which('ruby');
const ruby_cmd = which.sync('ruby', { nothrow: true });

if (ruby_cmd !== '/usr/bin/ruby') {
events.emit('warn', 'Non-system Ruby in use. This may cause packaging to fail.\n' +
Expand All @@ -289,7 +289,7 @@ module.exports.run = buildOpts => {
*/
function findXCodeProjectIn (projectPath) {
// 'Searching for Xcode project in ' + projectPath);
const xcodeProjFiles = shell.ls(projectPath).filter(name => path.extname(name) === '.xcodeproj');
const xcodeProjFiles = fs.readdirSync(projectPath).filter(name => path.extname(name) === '.xcodeproj');

if (xcodeProjFiles.length === 0) {
return Q.reject(`No Xcode project found in ${projectPath}`);
Expand Down
4 changes: 2 additions & 2 deletions bin/templates/scripts/cordova/lib/check_reqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'use strict';

const Q = require('q');
const shell = require('shelljs');
const which = require('which');
const versions = require('./versions');

const SUPPORTED_OS_PLATFORMS = ['darwin'];
Expand Down Expand Up @@ -83,7 +83,7 @@ function checkTool (tool, minVersion, message, toolFriendlyName) {
toolFriendlyName = toolFriendlyName || tool;

// Check whether tool command is available at all
const tool_command = shell.which(tool);
const tool_command = which.sync(tool, { nothrow: true });
if (!tool_command) {
return Q.reject(`${toolFriendlyName} was not found. ${message || ''}`);
}
Expand Down
6 changes: 3 additions & 3 deletions bin/templates/scripts/cordova/lib/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

const Q = require('q');
const path = require('path');
const shell = require('shelljs');
const fs = require('fs-extra');
const superspawn = require('cordova-common').superspawn;

const projectPath = path.join(__dirname, '..', '..');

module.exports.run = () => {
const projectName = shell.ls(projectPath).filter(name => path.extname(name) === '.xcodeproj')[0];
const projectName = fs.readdirSync(projectPath).filter(name => path.extname(name) === '.xcodeproj');

if (!projectName) {
return Q.reject(`No Xcode project found in ${projectPath}`);
Expand All @@ -41,5 +41,5 @@ module.exports.run = () => {

return xcodebuildClean('Debug')
.then(() => xcodebuildClean('Release'))
.then(() => shell.rm('-rf', path.join(projectPath, 'build')));
.then(() => fs.removeSync(path.join(projectPath, 'build')));
};
26 changes: 11 additions & 15 deletions bin/templates/scripts/cordova/lib/plugman/pluginHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
'use strict';
const fs = require('fs-extra');
const path = require('path');
const shell = require('shelljs');
const util = require('util');
const events = require('cordova-common').events;
const CordovaError = require('cordova-common').CordovaError;
Expand Down Expand Up @@ -77,7 +76,7 @@ const handlers = {
const destFile = path.resolve(project.resources_dir, target);

project.xcode.removeResourceFile(path.join('Resources', target));
shell.rm('-rf', destFile);
fs.removeSync(destFile);
}
},
framework: { // CB-5238 custom frameworks only
Expand Down Expand Up @@ -149,7 +148,7 @@ const handlers = {
if (pbxFile) {
project.xcode.removeFromPbxEmbedFrameworksBuildPhase(pbxFile);
}
shell.rm('-rf', targetDir);
fs.removeSync(targetDir);
}
},
'lib-file': {
Expand Down Expand Up @@ -201,11 +200,11 @@ const handlers = {
scriptContent = `cordova.define("${moduleName}", function(require, exports, module) {\n${scriptContent}\n});\n`;

const moduleDestination = path.resolve(project.www, 'plugins', plugin.id, obj.src);
shell.mkdir('-p', path.dirname(moduleDestination));
fs.ensureDirSync(path.dirname(moduleDestination));
fs.writeFileSync(moduleDestination, scriptContent, 'utf-8');
if (options && options.usePlatformWww) {
const platformWwwDestination = path.resolve(project.platformWww, 'plugins', plugin.id, obj.src);
shell.mkdir('-p', path.dirname(platformWwwDestination));
fs.ensureDirSync(path.dirname(platformWwwDestination));
fs.writeFileSync(platformWwwDestination, scriptContent, 'utf-8');
}
},
Expand Down Expand Up @@ -287,7 +286,7 @@ function uninstallHelper (type, obj, project_dir, plugin_id, options, project) {
project_ref = `Plugins/${fixPathSep(path.relative(project.plugins_dir, destFile))}`;
}

shell.rm('-rf', targetDir);
fs.removeSync(targetDir);

if (type === 'header-file') {
project.xcode.removeHeaderFile(project_ref);
Expand Down Expand Up @@ -319,15 +318,12 @@ function copyFile (plugin_dir, src, project_dir, dest, link) {
// check that dest path is located in project directory
if (dest.indexOf(project_dir) !== 0) { throw new CordovaError(`Destination "${dest}" for source file "${src}" is located outside the project`); }

shell.mkdir('-p', path.dirname(dest));
fs.ensureDirSync(path.dirname(dest));

if (link) {
linkFileOrDirTree(src, dest);
} else if (fs.statSync(src).isDirectory()) {
// XXX shelljs decides to create a directory when -R|-r is used which sucks. http://goo.gl/nbsjq
shell.cp('-Rf', path.join(src, '/*'), dest);
} else {
shell.cp('-f', src, dest);
fs.copySync(src, dest);
}
}

Expand All @@ -341,11 +337,11 @@ function copyNewFile (plugin_dir, src, project_dir, dest, link) {

function linkFileOrDirTree (src, dest) {
if (fs.existsSync(dest)) {
shell.rm('-Rf', dest);
fs.removeSync(dest);
}

if (fs.statSync(src).isDirectory()) {
shell.mkdir('-p', dest);
fs.ensureDirSync(dest);
fs.readdirSync(src).forEach(entry => {
linkFileOrDirTree(path.join(src, entry), path.join(dest, entry));
});
Expand All @@ -357,12 +353,12 @@ function linkFileOrDirTree (src, dest) {
// checks if file exists and then deletes. Error if doesn't exist
function removeFile (project_dir, src) {
const file = path.resolve(project_dir, src);
shell.rm('-Rf', file);
fs.removeSync(file);
}

// deletes file/directory without checking
function removeFileF (file) {
shell.rm('-Rf', file);
fs.removeSync(file);
}

function removeFileAndParents (baseDir, destFile, stopper) {
Expand Down
3 changes: 1 addition & 2 deletions bin/templates/scripts/cordova/lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
const Q = require('q');
const fs = require('fs-extra');
const path = require('path');
const shell = require('shelljs');
const unorm = require('unorm');
const plist = require('plist');
const URL = require('url');
Expand Down Expand Up @@ -103,7 +102,7 @@ function updateConfigFile (sourceConfig, configMunger, locations) {

// First cleanup current config and merge project's one into own
// Overwrite platform config.xml with defaults.xml.
shell.cp('-f', locations.defaultConfigXml, locations.configXml);
fs.copySync(locations.defaultConfigXml, locations.configXml);

// Then apply config changes from global munge to all config files
// in project (including project's config)
Expand Down
3 changes: 1 addition & 2 deletions bin/templates/scripts/cordova/lib/projectFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const plist = require('plist');
const _ = require('underscore');
const path = require('path');
const fs = require('fs-extra');
const shell = require('shelljs');

const pluginHandlers = require('./plugman/pluginHandlers');
const CordovaError = require('cordova-common').CordovaError;
Expand Down Expand Up @@ -72,7 +71,7 @@ function parseProjectFile (locations) {
fs.writeFileSync(pbxPath, xcodeproj.writeSync());
if (Object.keys(this.frameworks).length === 0) {
// If there is no framework references remain in the project, just remove this file
shell.rm('-rf', frameworks_file);
fs.removeSync(frameworks_file);
return;
}
fs.writeFileSync(frameworks_file, JSON.stringify(this.frameworks, null, 4));
Expand Down
5 changes: 2 additions & 3 deletions bin/templates/scripts/cordova/lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
const Q = require('q');
const path = require('path');
const build = require('./build');
const shell = require('shelljs');
const superspawn = require('cordova-common').superspawn;
const check_reqs = require('./check_reqs');
const fs = require('fs-extra');
Expand Down Expand Up @@ -86,9 +85,9 @@ module.exports.run = runOptions => {
// delete the existing platform/ios/build/device/appname.app
fs.removeSync(appFile);
// move the platform/ios/build/device/Payload/appname.app to parent
shell.mv('-f', appFileInflated, buildOutputDir);
fs.moveSync(appFileInflated, buildOutputDir);
// delete the platform/ios/build/device/Payload folder
shell.rm('-rf', payloadFolder);
fs.removeSync(payloadFolder);

return null;
})
Expand Down
Loading