Skip to content

Commit 77f15c7

Browse files
committed
feat(cli): add --format to run lint:fix for generated code
1 parent 5c11b6c commit 77f15c7

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

packages/cli/generators/app/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ module.exports = class AppGenerator extends ProjectGenerator {
8282
return super.install();
8383
}
8484

85-
end() {
86-
if (!super.end()) return false;
85+
async end() {
86+
await super.end();
87+
if (this.shouldExit()) return;
8788
this.log();
8889
this.log(
8990
'Application %s was created in %s.',

packages/cli/lib/artifact-generator.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
8181
}
8282

8383
async end() {
84-
const success = await super.end();
85-
if (!success) return false;
84+
if (this.shouldExit()) {
85+
await super.end();
86+
return;
87+
}
8688

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

127-
return false;
129+
await super.end();
128130
}
129131
};

packages/cli/lib/base-generator.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const path = require('path');
1212
const fs = require('fs');
1313
const readline = require('readline');
1414
const debug = require('./debug')('base-generator');
15+
const assert = require('assert');
1516

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

48+
this.option('format', {
49+
type: Boolean,
50+
alias: 'f',
51+
description: 'Format generated code using npm run lint:fix',
52+
});
53+
4754
this.artifactInfo = this.artifactInfo || {
4855
rootDir: 'src',
4956
};
@@ -280,6 +287,20 @@ module.exports = class BaseGenerator extends Generator {
280287
);
281288
this.exit(err);
282289
}
290+
this.packageJson = pkg;
291+
}
292+
293+
_runNpmScript(projectDir, args) {
294+
return new Promise((resolve, reject) => {
295+
this.spawnCommand('npm', args, {
296+
// Disable stdout
297+
stdio: [process.stdin, 'ignore', process.stderr],
298+
cwd: projectDir,
299+
}).on('close', code => {
300+
if (code === 0) resolve();
301+
else reject(new Error('npm exit code: ' + code));
302+
});
303+
});
283304
}
284305

285306
/**
@@ -292,14 +313,17 @@ module.exports = class BaseGenerator extends Generator {
292313
/**
293314
* Print out the exit reason if this generator is told to exit before it ends
294315
*/
295-
end() {
316+
async end() {
296317
if (this.shouldExit()) {
297318
debug(this.exitGeneration);
298319
this.log(chalk.red('Generation is aborted:', this.exitGeneration));
299320
// Fail the process
300321
process.exitCode = 1;
301-
return false;
322+
return;
323+
}
324+
if (this.options.format) {
325+
this.log('Running npm run lint:fix to format the code...');
326+
await this._runNpmScript(this.destinationRoot(), ['run', 'lint:fix']);
302327
}
303-
return true;
304328
}
305329
};

packages/cli/test/acceptance/app-run.acceptance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('app-generator (SLOW)', function() {
2424
};
2525

2626
before('scaffold a new application', async function createAppProject() {
27-
// Increase the timeout to 1 minute to accomodate slow CI build machines
27+
// Increase the timeout to 1 minute to accommodate slow CI build machines
2828
this.timeout(60 * 1000);
2929
await helpers
3030
.run(generator)

packages/cli/test/integration/lib/artifact-generator.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ module.exports = function(artiGenerator) {
5858
});
5959

6060
describe('checkLoopBackProject', () => {
61+
let gen;
62+
63+
beforeEach(() => {
64+
gen = testUtils.testSetUpGen(artiGenerator);
65+
gen.fs.readJSON = sinon.stub(fs, 'readJSON');
66+
});
67+
68+
afterEach(() => {
69+
if (gen) gen.fs.readJSON.restore();
70+
});
71+
6172
testCheckLoopBack(
6273
'throws an error if no package.json is present',
6374
undefined,
@@ -75,23 +86,18 @@ module.exports = function(artiGenerator) {
7586
);
7687

7788
it('passes if "keywords" maps to "loopback"', () => {
78-
let gen = testUtils.testSetUpGen(artiGenerator);
79-
gen.fs.readJSON = sinon.stub(fs, 'readJSON');
8089
gen.fs.readJSON.returns({keywords: ['test', 'loopback']});
8190
assert.doesNotThrow(() => {
8291
gen.checkLoopBackProject();
8392
}, Error);
84-
gen.fs.readJSON.restore();
8593
});
8694

8795
function testCheckLoopBack(testName, obj, expected) {
8896
it(testName, () => {
89-
let gen = testUtils.testSetUpGen(artiGenerator);
9097
let logs = [];
9198
gen.log = function(...args) {
9299
logs = logs.concat(args);
93100
};
94-
gen.fs.readJSON = sinon.stub(fs, 'readJSON');
95101
gen.fs.readJSON.returns(obj);
96102
gen.checkLoopBackProject();
97103
assert(gen.exitGeneration instanceof Error);
@@ -100,18 +106,25 @@ module.exports = function(artiGenerator) {
100106
assert.deepEqual(logs, [
101107
chalk.red('Generation is aborted:', gen.exitGeneration),
102108
]);
103-
gen.fs.readJSON.restore();
104109
});
105110
}
106111
});
107112

108113
describe('promptArtifactName', () => {
109-
it('incorporates user input into artifactInfo', () => {
110-
let gen = testUtils.testSetUpGen(artiGenerator);
114+
let gen;
115+
116+
beforeEach(() => {
117+
gen = testUtils.testSetUpGen(artiGenerator);
111118
gen.prompt = sinon.stub(gen, 'prompt');
119+
});
120+
121+
afterEach(() => {
122+
if (gen) gen.prompt.restore();
123+
});
124+
125+
it('incorporates user input into artifactInfo', () => {
112126
gen.prompt.resolves({name: 'foobar'});
113127
return gen.promptArtifactName().then(() => {
114-
gen.prompt.restore();
115128
assert(gen.artifactInfo.name);
116129
assert(gen.artifactInfo.name === 'foobar');
117130
});

0 commit comments

Comments
 (0)