Skip to content

Commit 1a5cbf8

Browse files
committed
fix(cli): fix templates & move some utils to base generator
1 parent bac8d8c commit 1a5cbf8

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
patch,
88
del,
99
requestBody
10-
} from '@loopback/openapi-v3';
10+
} from '@loopback/rest';
1111
import {<%= modelName %>} from '../models';
1212
import {<%= repositoryName %>} from '../repositories';
1313

packages/cli/generators/controller/templates/src/controllers/controller-template.ts.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Uncomment these imports to begin using these cool features!
22

3-
// import {inject} from @loopback/context;
3+
// import {inject} from '@loopback/context';
44

55

66
export class <%= name %>Controller {

packages/cli/lib/artifact-generator.js

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const utils = require('./utils');
1010
const updateIndex = require('./update-index');
1111
const path = require('path');
1212
const chalk = require('chalk');
13-
const StatusConflicter = utils.StatusConflicter;
1413

1514
module.exports = class ArtifactGenerator extends BaseGenerator {
1615
// Note: arguments and options should be defined in the constructor.
@@ -36,41 +35,7 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
3635
this.destinationPath(),
3736
this.artifactInfo.outDir,
3837
);
39-
this.conflicter = new StatusConflicter(
40-
this.env.adapter,
41-
this.options.force,
42-
);
43-
}
44-
45-
/**
46-
* Checks if current directory is a LoopBack project by checking for
47-
* keyword 'loopback' under 'keywords' attribute in package.json.
48-
* 'keywords' is an array
49-
*/
50-
checkLoopBackProject() {
51-
debug('Checking for loopback project');
52-
if (this.shouldExit()) return false;
53-
const pkg = this.fs.readJSON(this.destinationPath('package.json'));
54-
const key = 'loopback';
55-
if (!pkg) {
56-
const err = new Error(
57-
'No package.json found in ' +
58-
this.destinationRoot() +
59-
'. ' +
60-
'The command must be run in a LoopBack project.',
61-
);
62-
this.exit(err);
63-
return;
64-
}
65-
if (!pkg.keywords || !pkg.keywords.includes(key)) {
66-
const err = new Error(
67-
'No `loopback` keyword found in ' +
68-
this.destinationPath('package.json') +
69-
'. ' +
70-
'The command must be run in a LoopBack project.',
71-
);
72-
this.exit(err);
73-
}
38+
super._setupGenerator();
7439
}
7540

7641
promptArtifactName() {
@@ -111,7 +76,7 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
11176
}
11277

11378
async end() {
114-
const success = super.end();
79+
const success = await super.end();
11580
if (!success) return false;
11681

11782
let generationStatus = true;
@@ -128,7 +93,7 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
12893
*
12994
* Both those properties must be present for this to happen. Optionally,
13095
* this can be disabled even if the properties are present by setting:
131-
* this.artifactInfo.disableIndexUdpdate = true;
96+
* this.artifactInfo.disableIndexUpdate = true;
13297
*/
13398
if (
13499
this.artifactInfo.outDir &&

packages/cli/lib/base-generator.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,31 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
'use strict';
7+
78
const Generator = require('yeoman-generator');
89
const chalk = require('chalk');
9-
10+
const debug = require('./debug')('artifact-generator');
11+
const utils = require('./utils');
12+
const StatusConflicter = utils.StatusConflicter;
1013
/**
1114
* Base Generator for LoopBack 4
1215
*/
1316
module.exports = class BaseGenerator extends Generator {
1417
// Note: arguments and options should be defined in the constructor.
1518
constructor(args, opts) {
1619
super(args, opts);
20+
this.conflicter = new StatusConflicter(
21+
this.env.adapter,
22+
this.options.force,
23+
);
1724
this._setupGenerator();
1825
}
1926

2027
/**
2128
* Subclasses can extend _setupGenerator() to set up the generator
2229
*/
2330
_setupGenerator() {
24-
this.artifactInfo = {
31+
this.artifactInfo = this.artifactInfo || {
2532
rootDir: 'src',
2633
};
2734
}
@@ -46,6 +53,37 @@ module.exports = class BaseGenerator extends Generator {
4653
this.exitGeneration = reason;
4754
}
4855

56+
/**
57+
* Checks if current directory is a LoopBack project by checking for
58+
* keyword 'loopback' under 'keywords' attribute in package.json.
59+
* 'keywords' is an array
60+
*/
61+
checkLoopBackProject() {
62+
debug('Checking for loopback project');
63+
if (this.shouldExit()) return false;
64+
const pkg = this.fs.readJSON(this.destinationPath('package.json'));
65+
const key = 'loopback';
66+
if (!pkg) {
67+
const err = new Error(
68+
'No package.json found in ' +
69+
this.destinationRoot() +
70+
'. ' +
71+
'The command must be run in a LoopBack project.',
72+
);
73+
this.exit(err);
74+
return;
75+
}
76+
if (!pkg.keywords || !pkg.keywords.includes(key)) {
77+
const err = new Error(
78+
'No `loopback` keyword found in ' +
79+
this.destinationPath('package.json') +
80+
'. ' +
81+
'The command must be run in a LoopBack project.',
82+
);
83+
this.exit(err);
84+
}
85+
}
86+
4987
/**
5088
* Check if the generator should exit
5189
*/

packages/cli/lib/update-index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
const path = require('path');
77
const util = require('util');
88
const fs = require('fs');
9-
const appendFileAsync = util.promisify(fs.appendFile);
9+
const appendFile = util.promisify(fs.appendFile);
10+
const readFile = util.promisify(fs.readFile);
11+
const exists = util.promisify(fs.exists);
1012

1113
/**
1214
*
@@ -18,6 +20,14 @@ module.exports = async function(dir, file) {
1820
if (!file.endsWith('.ts')) {
1921
throw new Error(`${file} must be a TypeScript (.ts) file`);
2022
}
23+
24+
let index = '';
25+
const indexExists = await exists(indexFile);
26+
if (indexExists) {
27+
index = await readFile(indexFile);
28+
}
2129
const content = `export * from './${file.slice(0, -3)}';\n`;
22-
await appendFileAsync(indexFile, content);
30+
if (!index.includes(content)) {
31+
await appendFile(indexFile, content);
32+
}
2333
};

0 commit comments

Comments
 (0)