forked from alibaba/lowcode-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.ts
More file actions
103 lines (98 loc) · 3.01 KB
/
base.ts
File metadata and controls
103 lines (98 loc) · 3.01 KB
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
import * as fs from 'fs-extra';
import * as path from 'path';
import * as spawn from 'cross-spawn';
import { pascal } from 'case';
import * as ejs from 'ejs';
import * as dayjs from 'dayjs';
import * as glob from 'glob';
import * as chalk from 'chalk'
import { IAnswer } from './interface';
import { getInstallPath, transformFileName } from './utils';
export default class InitFunc {
projectName: string;
installPath: string;
templatePkg: string;
copyPath: string;
answers: IAnswer;
prefix: string;
useBeta: boolean;
constructor({ argv, answers, templatePkg, prefix }) {
this.projectName = argv['_'][0] || './';
this.installPath = getInstallPath();
this.templatePkg = templatePkg;
this.copyPath = path.join(process.cwd(), this.projectName);
this.answers = answers;
this.prefix = prefix;
this.useBeta = argv.beta;
}
addPrefix(name) {
const prefix = this.prefix;
if (new RegExp(`^${prefix}`).test(name)) {
return name;
}
return `${prefix}-${name}`;
}
isDirEmpty(dir) {
return glob.sync('**', {
cwd: dir,
nodir: true,
dot: true,
}).length === 0;
}
ensureInstallPath() {
fs.ensureDirSync(this.installPath);
}
installTpl() {
spawn.sync('npm', [
'install',
`${this.templatePkg}${this.useBeta ? '@beta' : ''}`,
'--no-save',
'--no-package-lock',
'--no-shrinkwrap',
'--registry=https://registry.npmmirror.com',
], { stdio: 'inherit', cwd: this.installPath });
}
renderTpl() {
const templatePath = path.join(this.installPath, 'node_modules', this.templatePkg, 'proj');
const setterName = this.addPrefix(this.answers.projectName);
const setterComponentName = pascal(setterName);
const renderData = {
...this.answers,
name: setterName,
componentName: setterComponentName,
version: '1.0.0',
nowDate: dayjs().format('YYYY-MM-DD'),
};
fs.ensureDirSync(this.copyPath);
if (!this.isDirEmpty(this.copyPath)) {
console.log(chalk.red('需要初始化的项目目录不为空,请清空后重试'));
return;
}
glob.sync('**', {
cwd: templatePath,
nodir: true,
dot: true,
ignore: ['node_modules/**'],
}).forEach((fileName) => {
const filePath = path.join(templatePath, fileName);
const fileTpl = fs.readFileSync(filePath, 'utf-8');
const fileContent = ejs.render(fileTpl, renderData);
const copyPath = path.join(this.copyPath, transformFileName(fileName));
fs.ensureFileSync(copyPath);
fs.writeFileSync(copyPath, fileContent);
});
}
initInstallPathPackageJson() {
const pkgPath = path.join(this.installPath, 'package.json');
const isExist = fs.pathExistsSync(pkgPath);
if (isExist) return;
fs.writeFileSync(pkgPath, JSON.stringify({ private: true }, null, ' '));
}
init() {
console.log(chalk.green('正在为你初始化项目,请稍等...'));
this.ensureInstallPath();
this.initInstallPathPackageJson();
this.installTpl();
this.renderTpl();
}
}