-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathaddon-generator.ts
80 lines (71 loc) · 2.53 KB
/
addon-generator.ts
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
import * as mkdirp from "mkdirp";
import * as path from "path";
import * as Generator from "yeoman-generator";
import * as copyUtils from "@webpack-cli/utils/copy-utils";
/**
* Creates a Yeoman Generator that generates a project conforming
* to webpack-defaults.
*
* @param {Generator.Questions} prompts An array of Yeoman prompt objects
*
* @param {string} templateDir Absolute path to template directory
*
* @param {string[]} copyFiles An array of file paths (relative to `./templates`)
* of files to be copied to the generated project. File paths should be of the
* form `path/to/file.js.tpl`.
*
* @param {string[]} copyTemplateFiles An array of file paths (relative to
* `./templates`) of files to be copied to the generated project. Template
* file paths should be of the form `path/to/_file.js.tpl`.
*
* @param {Function} templateFn A function that is passed a generator instance and
* returns an object containing data to be supplied to the template files.
*
* @returns {Generator} A class extending Generator
*/
const addonGenerator = (
prompts: Generator.Questions,
templateDir: string,
copyFiles: string[],
copyTemplateFiles: string[],
templateFn: Function
): typeof Generator =>
class AddonGenerator extends Generator {
public props: Generator.Question;
public copy: (value: string, index: number, array: string[]) => void;
public copyTpl: (value: string, index: number, array: string[]) => void;
public prompting(): Promise<void | {}> {
return this.prompt(prompts).then((props: Generator.Question): void => {
this.props = props;
});
}
public default(): void {
const currentDirName = path.basename(this.destinationPath());
if (currentDirName !== this.props.name) {
this.log(`
Your project must be inside a folder named ${this.props.name}
I will create this folder for you.
`);
mkdirp(this.props.name, (err: object): void => {
console.error("Failed to create directory", err);
});
const pathToProjectDir: string = this.destinationPath(this.props.name);
this.destinationRoot(pathToProjectDir);
}
}
public writing(): void {
this.copy = copyUtils.generatorCopy(this, templateDir);
this.copyTpl = copyUtils.generatorCopyTpl(this, templateDir, templateFn(this));
copyFiles.forEach(this.copy);
copyTemplateFiles.forEach(this.copyTpl);
}
public install(): void {
this.npmInstall(["webpack-defaults", "bluebird"], {
"save-dev": true
});
}
public end(): void {
this.spawnCommand("npm", ["run", "defaults"]);
}
};
export default addonGenerator;