-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
artifact-generator.js
104 lines (96 loc) · 3.04 KB
/
artifact-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const BaseGenerator = require('./base-generator');
const debug = require('./debug')('artifact-generator');
const utils = require('./utils');
const StatusConflicter = utils.StatusConflicter;
module.exports = class ArtifactGenerator extends BaseGenerator {
// Note: arguments and options should be defined in the constructor.
constructor(args, opts) {
super(args, opts);
}
_setupGenerator() {
debug('Setting up generator');
this.argument('name', {
type: String,
required: false,
description: 'Name for the ' + this.artifactInfo.type,
});
// argument validation
if (this.args.length) {
const validationMsg = utils.validateClassName(this.args[0]);
if (typeof validationMsg === 'string') throw new Error(validationMsg);
}
this.artifactInfo.name = this.args[0];
this.artifactInfo.defaultName = 'new';
this.conflicter = new StatusConflicter(
this.env.adapter,
this.options.force
);
}
/**
* Checks if current directory is a LoopBack project by checking for
* keyword 'loopback' under 'keywords' attribute in package.json.
* 'keywords' is an array
*/
checkLoopBackProject() {
debug('Checking for loopback project');
if (this.shouldExit()) return false;
const pkg = this.fs.readJSON(this.destinationPath('package.json'));
const key = 'loopback';
if (!pkg) {
const err = new Error(
'No package.json found in ' +
this.destinationRoot() +
'. ' +
'The command must be run in a LoopBack project.'
);
this.exit(err);
return;
}
if (!pkg.keywords || !pkg.keywords.includes(key)) {
const err = new Error(
'No `loopback` keyword found in ' +
this.destinationPath('package.json') +
'. ' +
'The command must be run in a LoopBack project.'
);
this.exit(err);
}
}
promptArtifactName() {
debug('Prompting for artifact name');
if (this.shouldExit()) return false;
const prompts = [
{
type: 'input',
name: 'name',
// capitalization
message: utils.toClassName(this.artifactInfo.type) + ' class name:',
when: this.artifactInfo.name === undefined,
validate: utils.validateClassName,
},
];
return this.prompt(prompts).then(props => {
Object.assign(this.artifactInfo, props);
});
}
scaffold() {
debug('Scaffolding artifact(s)');
if (this.shouldExit()) return false;
// Capitalize class name
this.artifactInfo.name = utils.toClassName(this.artifactInfo.name);
// Copy template files from ./templates
// Renaming of the files should be done in the generator inheriting from this one
this.fs.copyTpl(
this.templatePath('**/*'),
this.destinationPath(),
this.artifactInfo,
{},
{globOptions: {dot: true}}
);
}
};