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

Continue fs-extra migration #1

Closed
Show file tree
Hide file tree
Changes from 6 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
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'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make it more clear about what this function actually does? Wonder if we should just remove the separate function altogether?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the function would also work for plain files. But for that case the name makes no sense.

I'd keep it for now. It will have to be changed anyway in case some of apache/cordova-discuss#89 is accepted.

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'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering why this problem does not show up when I run the spec?

Copy link
Author

@raphinesse raphinesse May 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't check, but this case might not be covered or not sufficiently tested. Or I'm just plainly wrong 😉

But as far as I understand it, with copySync you always have the full file path in either argument. Whether you copy file or folder. This leads to less surprising if a bit wordier copying IMO.

} 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"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",
"shelljs": "^0.8.2",
"valid-identifier": "0.0.1"
},
"devDependencies": {
Expand Down
12 changes: 6 additions & 6 deletions spec/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
under the License.
*/

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

var path = require('path');

var shell = require('shelljs');
var requireFresh = require('import-fresh');

var create = require('..');
Expand All @@ -35,12 +35,12 @@ var project = path.join(tmpDir, appName);

// Setup and teardown test dirs
beforeEach(function () {
shell.rm('-rf', project);
shell.mkdir('-p', tmpDir);
fs.removeSync(project);
fs.ensureDirSync(tmpDir);
});
afterEach(function () {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
fs.removeSync(tmpDir);
});

describe('cordova create checks for valid-identifier', function () {
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('create end-to-end', function () {
});

it('should successfully run with existing, empty destination', function () {
shell.mkdir('-p', project);
fs.ensureDirSync(project);
return create(project, appId, appName, {}, events)
.then(checkProject);
});
Expand Down
5 changes: 2 additions & 3 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
under the License.
*/

const fs = require('fs');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');

const rewire = require('rewire');
const shell = require('shelljs');

// Disable regular console output during tests
const CordovaLogger = require('cordova-common').CordovaLogger;
Expand All @@ -44,7 +43,7 @@ function createWithMockFetch (dir, id, name, cfg, events) {
const fetchSpy = jasmine.createSpy('fetchSpy')
.and.callFake(() => Promise.resolve(mockFetchDest));

shell.cp('-R', templateDir, mockFetchDest);
fs.copySync(templateDir, mockFetchDest);
return createWith({fetch: fetchSpy})(dir, id, name, cfg, events)
.then(() => fetchSpy);
}
Expand Down