Skip to content

Commit

Permalink
feat(cli): add --format to run lint:fix for generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jul 10, 2018
1 parent 5c11b6c commit 77f15c7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
5 changes: 3 additions & 2 deletions packages/cli/generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ module.exports = class AppGenerator extends ProjectGenerator {
return super.install();
}

end() {
if (!super.end()) return false;
async end() {
await super.end();
if (this.shouldExit()) return;
this.log();
this.log(
'Application %s was created in %s.',
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/lib/artifact-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
}

async end() {
const success = await super.end();
if (!success) return false;
if (this.shouldExit()) {
await super.end();
return;
}

let generationStatus = true;
// Check all files being generated to ensure they succeeded
Expand Down Expand Up @@ -124,6 +126,6 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
this.log();
}

return false;
await super.end();
}
};
30 changes: 27 additions & 3 deletions packages/cli/lib/base-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const path = require('path');
const fs = require('fs');
const readline = require('readline');
const debug = require('./debug')('base-generator');
const assert = require('assert');

/**
* Base Generator for LoopBack 4
Expand Down Expand Up @@ -44,6 +45,12 @@ module.exports = class BaseGenerator extends Generator {
'Skip all confirmation prompts with default or provided value',
});

this.option('format', {
type: Boolean,
alias: 'f',
description: 'Format generated code using npm run lint:fix',
});

this.artifactInfo = this.artifactInfo || {
rootDir: 'src',
};
Expand Down Expand Up @@ -280,6 +287,20 @@ module.exports = class BaseGenerator extends Generator {
);
this.exit(err);
}
this.packageJson = pkg;
}

_runNpmScript(projectDir, args) {
return new Promise((resolve, reject) => {
this.spawnCommand('npm', args, {
// Disable stdout
stdio: [process.stdin, 'ignore', process.stderr],
cwd: projectDir,
}).on('close', code => {
if (code === 0) resolve();
else reject(new Error('npm exit code: ' + code));
});
});
}

/**
Expand All @@ -292,14 +313,17 @@ module.exports = class BaseGenerator extends Generator {
/**
* Print out the exit reason if this generator is told to exit before it ends
*/
end() {
async end() {
if (this.shouldExit()) {
debug(this.exitGeneration);
this.log(chalk.red('Generation is aborted:', this.exitGeneration));
// Fail the process
process.exitCode = 1;
return false;
return;
}
if (this.options.format) {
this.log('Running npm run lint:fix to format the code...');
await this._runNpmScript(this.destinationRoot(), ['run', 'lint:fix']);
}
return true;
}
};
2 changes: 1 addition & 1 deletion packages/cli/test/acceptance/app-run.acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('app-generator (SLOW)', function() {
};

before('scaffold a new application', async function createAppProject() {
// Increase the timeout to 1 minute to accomodate slow CI build machines
// Increase the timeout to 1 minute to accommodate slow CI build machines
this.timeout(60 * 1000);
await helpers
.run(generator)
Expand Down
31 changes: 22 additions & 9 deletions packages/cli/test/integration/lib/artifact-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ module.exports = function(artiGenerator) {
});

describe('checkLoopBackProject', () => {
let gen;

beforeEach(() => {
gen = testUtils.testSetUpGen(artiGenerator);
gen.fs.readJSON = sinon.stub(fs, 'readJSON');
});

afterEach(() => {
if (gen) gen.fs.readJSON.restore();
});

testCheckLoopBack(
'throws an error if no package.json is present',
undefined,
Expand All @@ -75,23 +86,18 @@ module.exports = function(artiGenerator) {
);

it('passes if "keywords" maps to "loopback"', () => {
let gen = testUtils.testSetUpGen(artiGenerator);
gen.fs.readJSON = sinon.stub(fs, 'readJSON');
gen.fs.readJSON.returns({keywords: ['test', 'loopback']});
assert.doesNotThrow(() => {
gen.checkLoopBackProject();
}, Error);
gen.fs.readJSON.restore();
});

function testCheckLoopBack(testName, obj, expected) {
it(testName, () => {
let gen = testUtils.testSetUpGen(artiGenerator);
let logs = [];
gen.log = function(...args) {
logs = logs.concat(args);
};
gen.fs.readJSON = sinon.stub(fs, 'readJSON');
gen.fs.readJSON.returns(obj);
gen.checkLoopBackProject();
assert(gen.exitGeneration instanceof Error);
Expand All @@ -100,18 +106,25 @@ module.exports = function(artiGenerator) {
assert.deepEqual(logs, [
chalk.red('Generation is aborted:', gen.exitGeneration),
]);
gen.fs.readJSON.restore();
});
}
});

describe('promptArtifactName', () => {
it('incorporates user input into artifactInfo', () => {
let gen = testUtils.testSetUpGen(artiGenerator);
let gen;

beforeEach(() => {
gen = testUtils.testSetUpGen(artiGenerator);
gen.prompt = sinon.stub(gen, 'prompt');
});

afterEach(() => {
if (gen) gen.prompt.restore();
});

it('incorporates user input into artifactInfo', () => {
gen.prompt.resolves({name: 'foobar'});
return gen.promptArtifactName().then(() => {
gen.prompt.restore();
assert(gen.artifactInfo.name);
assert(gen.artifactInfo.name === 'foobar');
});
Expand Down

0 comments on commit 77f15c7

Please sign in to comment.