Skip to content

Commit

Permalink
index.js use fs-extra instead of shelljs
Browse files Browse the repository at this point in the history
Co-authored-by: Christopher J. Brody <[email protected]>
Co-authored-by: Raphael von der Grün <[email protected]>
  • Loading branch information
Christopher J. Brody and raphinesse committed May 29, 2018
1 parent 5435b9a commit 3b3ad05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
31 changes: 14 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
under the License.
*/

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

var os = require('os');
var path = require('path');

var Promise = require('q');
var isUrl = require('is-url');
var shell = require('shelljs');
var requireFresh = require('import-fresh');
var validateIdentifier = require('valid-identifier');

Expand Down Expand Up @@ -232,11 +232,11 @@ module.exports = function (dir, optionalId, optionalName, cfg, extEvents) {
copyIfNotExists(stockAssetPath('hooks'), path.join(dir, 'hooks'));
var configXmlExists = projectConfig(dir); // moves config to root if in www
if (!configXmlExists) {
shell.cp(stockAssetPath('config.xml'), path.join(dir, 'config.xml'));
fs.copySync(stockAssetPath('config.xml'), path.join(dir, 'config.xml'));
}
} catch (e) {
if (!dirAlreadyExisted) {
shell.rm('-rf', dir);
fs.removeSync(dir);
}
if (process.platform.slice(0, 3) === 'win' && e.code === 'EPERM') {
throw new CordovaError('Symlinks on Windows require Administrator privileges');
Expand Down Expand Up @@ -266,8 +266,8 @@ module.exports = function (dir, optionalId, optionalName, cfg, extEvents) {
}

// Create basic project structure.
shell.mkdir('-p', path.join(dir, 'platforms'));
shell.mkdir('-p', path.join(dir, 'plugins'));
fs.ensureDirSync(path.join(dir, 'platforms'));
fs.ensureDirSync(path.join(dir, 'plugins'));

var configPath = path.join(dir, 'config.xml');
// only update config.xml if not a symlink
Expand All @@ -290,8 +290,7 @@ module.exports = function (dir, optionalId, optionalName, cfg, extEvents) {
*/
function copyIfNotExists (src, dst) {
if (!fs.existsSync(dst) && src) {
shell.mkdir(dst);
shell.cp('-R', path.join(src, '*'), dst);
fs.copySync(src, dst);
}
}

Expand All @@ -310,7 +309,7 @@ function copyTemplateFiles (templateDir, projectDir, isSubDir) {
// if template is a www dir
if (path.basename(templateDir) === 'www') {
copyPath = path.resolve(templateDir);
shell.cp('-R', copyPath, projectDir);
fs.copySync(copyPath, path.resolve(projectDir, 'www'));
} else {
var templateFiles = fs.readdirSync(templateDir);
// Remove directories, and files that are unwanted
Expand All @@ -321,10 +320,10 @@ function copyTemplateFiles (templateDir, projectDir, isSubDir) {
});
}
// Copy each template file after filter
for (var i = 0; i < templateFiles.length; i++) {
copyPath = path.resolve(templateDir, templateFiles[i]);
shell.cp('-R', copyPath, projectDir);
}
templateFiles.forEach(f => {
copyPath = path.resolve(templateDir, f);
fs.copySync(copyPath, path.resolve(projectDir, f));
});
}
}

Expand Down Expand Up @@ -373,9 +372,7 @@ function linkFromTemplate (templateDir, projectDir) {
var linkSrc, linkDst, linkFolders, copySrc, copyDst;
function rmlinkSync (src, dst, type) {
if (src && dst) {
if (fs.existsSync(dst)) {
shell.rm('-rf', dst);
}
fs.removeSync(dst);
if (fs.existsSync(src)) {
fs.symlinkSync(src, dst, type);
}
Expand Down Expand Up @@ -403,7 +400,7 @@ function linkFromTemplate (templateDir, projectDir) {
// if template/www/config.xml then copy to project/config.xml
copyDst = path.join(projectDir, 'config.xml');
if (!fs.existsSync(copyDst) && fs.existsSync(copySrc)) {
shell.cp(copySrc, projectDir);
fs.copySync(copySrc, copyDst);
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cordova-app-hello-world": "^3.11.0",
"cordova-common": "^2.2.0",
"cordova-fetch": "^1.3.0",
"fs-extra": "^6.0.1",
"import-fresh": "^2.0.0",
"is-url": "^1.2.4",
"q": "^1.5.1",
Expand Down

0 comments on commit 3b3ad05

Please sign in to comment.