Skip to content

Commit

Permalink
refactor: small syntax changes (#64)
Browse files Browse the repository at this point in the history
* refactor: transform var to let & const
* refactor: small formatting changes
* refactor: transform arrow functions
  • Loading branch information
erisu committed Apr 17, 2020
1 parent a399529 commit 5f24bdb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 51 deletions.
32 changes: 14 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,7 +43,7 @@ function cordovaCreate (dest, opts = {}) {
// 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`.');
}
Expand Down Expand Up @@ -84,22 +80,22 @@ function cordovaCreate (dest, opts = {}) {
);
}
})
.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 {
// If assets are not online, resolve as a relative path on local computer
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;
Expand All @@ -112,7 +108,7 @@ function cordovaCreate (dest, opts = {}) {
import_from_path);
}

var dirAlreadyExisted = fs.existsSync(dir);
const dirAlreadyExisted = fs.existsSync(dir);
if (!dirAlreadyExisted) {
fs.mkdirSync(dir);
}
Expand Down
52 changes: 24 additions & 28 deletions spec/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -36,30 +32,30 @@ const project = path.join(tmpDir, appName);

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();
Expand All @@ -79,7 +75,7 @@ describe('create end-to-end', function () {
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);
}
Expand Down Expand Up @@ -120,14 +116,14 @@ describe('create end-to-end', function () {
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)
Expand All @@ -138,7 +134,7 @@ describe('create end-to-end', function () {
.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)
Expand All @@ -149,7 +145,7 @@ describe('create end-to-end', function () {
.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)
Expand All @@ -160,23 +156,23 @@ describe('create end-to-end', function () {
.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)
.then(checkNoPackageJson)
.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)
.then(checkPackageJson)
.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);
Expand Down Expand Up @@ -210,30 +206,30 @@ describe('create end-to-end', function () {
});
});

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),
new CordovaError('inside the template')
);
});

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));
Expand All @@ -246,7 +242,7 @@ describe('when shit happens', function () {
});

// 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),
Expand Down
6 changes: 3 additions & 3 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
const fs = require('fs-extra');
const os = require('os');
const path = require('path');

const rewire = require('rewire');

// Temporary directory to use for all tests
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cordova-create-tests-'));

// Returns a version of create with its local scope rewired
const create = rewire('..');

function createWith (rewiring) {
return (...args) => create.__with__(rewiring)(() => create(...args));
}
Expand Down Expand Up @@ -70,12 +70,12 @@ module.exports = {
};

// 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) {
Expand Down
3 changes: 2 additions & 1 deletion spec/templates/withsubdirectory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
under the License.
*/

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

module.exports = {
'dirname': path.join(__dirname, 'template')
};
3 changes: 2 additions & 1 deletion spec/templates/withsubdirectory_package_json/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
under the License.
*/

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

module.exports = {
'dirname': path.join(__dirname, 'template')
};

0 comments on commit 5f24bdb

Please sign in to comment.