forked from webpack/webpack-cli
/
addon-generator.ts
83 lines (75 loc) · 2.66 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
81
82
83
import * as mkdirp from "mkdirp";
import * as path from "path";
import Generator = require("yeoman-generator");
import * as copyUtils from "@webpack-cli/utils/copy-utils";
import { InquirerScaffoldObject } from "@webpack-cli/webpack-scaffold";
import { YeoGenerator } from "../generate-loader/types/Yeoman";
/**
* Creates a Yeoman Generator that generates a project conforming
* to webpack-defaults.
*
* @param {any[]} 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
*/
export default function addonGenerator(
prompts: InquirerScaffoldObject[],
templateDir: string,
copyFiles: string[],
copyTemplateFiles: string[],
templateFn: Function
): YeoGenerator {
return class AddOnGenerator extends Generator {
public props: InquirerScaffoldObject;
public copy: (value: string, index: number, array: string[]) => void;
public copyTpl: (value: string, index: number, array: string[]) => void;
public async prompting(): void {
const scaffoldObject: InquirerScaffoldObject = await this.prompt(prompts);
this.props = scaffoldObject;
}
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"]);
}
};
}