refactor: small syntax changes (#64)
* refactor: transform var to let & const
* refactor: small formatting changes
* refactor: transform arrow functions
diff --git a/index.js b/index.js
index 5b76133..1013f33 100644
--- a/index.js
+++ b/index.js
@@ -18,20 +18,16 @@
*/
const fs = require('fs-extra');
-
-var path = require('path');
-
-var tmp = require('tmp');
+const path = require('path');
+const tmp = require('tmp');
const npa = require('npm-package-arg');
const globby = require('globby');
-var isObject = require('isobject');
-var pathIsInside = require('path-is-inside');
-var requireFresh = require('import-fresh');
-var validateIdentifier = require('valid-identifier');
-
-var fetch = require('cordova-fetch');
-var CordovaError = require('cordova-common').CordovaError;
-var ConfigParser = require('cordova-common').ConfigParser;
+const isObject = require('isobject');
+const pathIsInside = require('path-is-inside');
+const requireFresh = require('import-fresh');
+const validateIdentifier = require('valid-identifier');
+const fetch = require('cordova-fetch');
+const { CordovaError, ConfigParser } = require('cordova-common');
module.exports = cordovaCreate;
@@ -47,7 +43,7 @@
// TODO this is to avoid having a huge diff. Remove later.
let dir = dest;
- return Promise.resolve().then(function () {
+ return Promise.resolve().then(() => {
if (!dir) {
throw new CordovaError('Directory not specified. See `cordova help`.');
}
@@ -84,13 +80,13 @@
);
}
})
- .then(function () {
+ .then(() => {
// Finally, Ready to start!
emit('log', 'Creating a new cordova project.');
// Use cordova-fetch to obtain npm or git templates
if (needsToBeFetched(opts.template)) {
- var target = opts.template;
+ const target = opts.template;
emit('verbose', 'Using cordova-fetch for ' + target);
return fetch(target, getSelfDestructingTempDir(), {});
} else {
@@ -98,8 +94,8 @@
return path.resolve(opts.template);
}
})
- .then(function (templatePath) {
- var import_from_path;
+ .then(templatePath => {
+ let import_from_path;
try {
import_from_path = requireFresh(templatePath).dirname;
@@ -112,7 +108,7 @@
import_from_path);
}
- var dirAlreadyExisted = fs.existsSync(dir);
+ const dirAlreadyExisted = fs.existsSync(dir);
if (!dirAlreadyExisted) {
fs.mkdirSync(dir);
}
diff --git a/spec/create.spec.js b/spec/create.spec.js
index 3892972..06fa97a 100644
--- a/spec/create.spec.js
+++ b/spec/create.spec.js
@@ -19,14 +19,10 @@
const fs = require('fs-extra');
const rewire = require('rewire');
-
-var path = require('path');
-
-var requireFresh = require('import-fresh');
-
-var create = require('..');
-var CordovaError = require('cordova-common').CordovaError;
-var ConfigParser = require('cordova-common').ConfigParser;
+const path = require('path');
+const requireFresh = require('import-fresh');
+const create = require('..');
+const { CordovaError, ConfigParser } = require('cordova-common');
const { tmpDir, createWith, createWithMockFetch, expectRejection } = require('./helpers');
const appName = 'TestBase';
@@ -36,30 +32,30 @@
let opts;
-beforeEach(function () {
+beforeEach(() => {
fs.emptyDirSync(tmpDir);
opts = { name: appName, id: appId };
});
-afterAll(function () {
+afterAll(() => {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
fs.removeSync(tmpDir);
});
-describe('cordova create checks for valid-identifier', function () {
+describe('cordova create checks for valid-identifier', () => {
const error = new CordovaError('is not a valid identifier');
- it('should reject reserved words from start of id', function () {
+ it('should reject reserved words from start of id', () => {
opts.id = 'int.bob';
return expectRejection(create(project, opts), error);
});
- it('should reject reserved words from end of id', function () {
+ it('should reject reserved words from end of id', () => {
opts.id = 'bob.class';
return expectRejection(create(project, opts), error);
});
});
-describe('create end-to-end', function () {
+describe('create end-to-end', () => {
function checkCommonArtifacts () {
// Check that www dir exist
expect(path.join(project, 'www')).toExist();
@@ -79,7 +75,7 @@
expect(path.join(project, 'www', 'config.xml')).not.toExist();
// Check that config.xml was updated correctly
- var configXml = new ConfigParser(path.join(project, 'config.xml'));
+ const configXml = new ConfigParser(path.join(project, 'config.xml'));
expect(configXml.packageName()).toEqual(appId);
expect(configXml.name()).toEqual(appName);
}
@@ -120,14 +116,14 @@
checkDefaultTemplate();
}
- it('should successfully run without template and use default hello-world app', function () {
+ it('should successfully run without template and use default hello-world app', () => {
// Create a real project with no template
// use default cordova-app-hello-world app
return create(project, opts)
.then(checkProjectCreatedWithDefaultTemplate);
});
- it('should successfully run with Git URL', function () {
+ it('should successfully run with Git URL', () => {
// Create a real project with git URL as template
opts.template = 'https://github.com/apache/cordova-app-hello-world';
return createWithMockFetch(project, opts)
@@ -138,7 +134,7 @@
.then(checkProjectCreatedWithDefaultTemplate);
});
- it('should successfully run with NPM package (specific version)', function () {
+ it('should successfully run with NPM package (specific version)', () => {
// Create a real project with npm module as template
opts.template = 'phonegap-template-vue-f7-tabs@1';
return createWithMockFetch(project, opts)
@@ -149,7 +145,7 @@
.then(checkProjectCreatedWithDefaultTemplate);
});
- it('should successfully run with NPM package (no specific version)', function () {
+ it('should successfully run with NPM package (no specific version)', () => {
// Create a real project with npm module as template
opts.template = 'phonegap-template-vue-f7-tabs';
return createWithMockFetch(project, opts)
@@ -160,7 +156,7 @@
.then(checkProjectCreatedWithDefaultTemplate);
});
- it('should successfully run with local template having no package.json in template dir', function () {
+ it('should successfully run with local template having no package.json in template dir', () => {
opts.template = path.join(__dirname, 'templates/withsubdirectory');
return create(project, opts)
.then(checkCommonArtifacts)
@@ -168,7 +164,7 @@
.then(checkNotDefaultTemplate);
});
- it('should successfully run with local template having package.json in template dir', function () {
+ it('should successfully run with local template having package.json in template dir', () => {
opts.template = path.join(__dirname, 'templates/withsubdirectory_package_json');
return create(project, opts)
.then(checkCommonArtifacts)
@@ -176,7 +172,7 @@
.then(checkNotDefaultTemplate);
});
- it('should successfully run with existing, empty destination', function () {
+ it('should successfully run with existing, empty destination', () => {
fs.ensureDirSync(project);
return create(project, opts)
.then(checkProjectCreatedWithDefaultTemplate);
@@ -210,22 +206,22 @@
});
});
-describe('when shit happens', function () {
- it('should fail when dir is missing', function () {
+describe('when shit happens', () => {
+ it('should fail when dir is missing', () => {
return expectRejection(
create(null, opts),
new CordovaError('Directory not specified')
);
});
- it('should fail when dir already exists', function () {
+ it('should fail when dir already exists', () => {
return expectRejection(
create(__dirname, opts),
new CordovaError('Path already exists and is not empty')
);
});
- it('should fail when destination is inside template', function () {
+ it('should fail when destination is inside template', () => {
opts.template = path.join(tmpDir, 'template');
return expectRejection(
create(path.join(opts.template, 'destination'), opts),
@@ -233,7 +229,7 @@
);
});
- it('should fail when fetch fails', function () {
+ it('should fail when fetch fails', () => {
const fetchError = new Error('Fetch fail');
const failingFetch = jasmine.createSpy('failingFetch')
.and.callFake(() => Promise.reject(fetchError));
@@ -246,7 +242,7 @@
});
// FIXME: we need to improve isRemote to make this different from the test above
- xit('should fail when template does not exist', function () {
+ xit('should fail when template does not exist', () => {
opts.template = path.join(__dirname, 'doesnotexist');
return expectRejection(
create(project, opts),
diff --git a/spec/helpers.js b/spec/helpers.js
index 7aa6665..f2033dc 100644
--- a/spec/helpers.js
+++ b/spec/helpers.js
@@ -20,7 +20,6 @@
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
-
const rewire = require('rewire');
// Temporary directory to use for all tests
@@ -28,6 +27,7 @@
// Returns a version of create with its local scope rewired
const create = rewire('..');
+
function createWith (rewiring) {
return (...args) => create.__with__(rewiring)(() => create(...args));
}
@@ -70,12 +70,12 @@
};
// Add the toExist matcher.
-beforeEach(function () {
+beforeEach(() => {
jasmine.addMatchers({
toExist: function () {
return {
compare: function (testPath) {
- var result = {};
+ const result = {};
result.pass = fs.existsSync(testPath);
if (result.pass) {
diff --git a/spec/templates/withsubdirectory/index.js b/spec/templates/withsubdirectory/index.js
index d48190e..a3cab90 100644
--- a/spec/templates/withsubdirectory/index.js
+++ b/spec/templates/withsubdirectory/index.js
@@ -17,7 +17,8 @@
under the License.
*/
-var path = require('path');
+const path = require('path');
+
module.exports = {
'dirname': path.join(__dirname, 'template')
};
diff --git a/spec/templates/withsubdirectory_package_json/index.js b/spec/templates/withsubdirectory_package_json/index.js
index d48190e..a3cab90 100644
--- a/spec/templates/withsubdirectory_package_json/index.js
+++ b/spec/templates/withsubdirectory_package_json/index.js
@@ -17,7 +17,8 @@
under the License.
*/
-var path = require('path');
+const path = require('path');
+
module.exports = {
'dirname': path.join(__dirname, 'template')
};