-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.ts
181 lines (139 loc) · 4.29 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node
import * as inquirer from 'inquirer';
import * as fs from 'fs';
import * as path from 'path';
import * as shell from 'shelljs';
import * as template from './utils/template';
import chalk from 'chalk';
import * as yargs from 'yargs';
const CHOICES = fs.readdirSync(path.join(__dirname, 'templates'));
const QUESTIONS = [
{
name: 'template',
type: 'list',
message: 'What project template would you like to generate?',
choices: CHOICES,
when: () => !yargs.argv['template']
},
{
name: 'name',
type: 'input',
message: 'Project name:',
when: () => !yargs.argv['name'],
validate: (input: string) => {
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
else return 'Project name may only include letters, numbers, underscores and hashes.';
}
}
];
const CURR_DIR = process.cwd();
export interface TemplateConfig {
files?: string[]
postMessage?: string
}
export interface CliOptions {
projectName: string
templateName: string
templatePath: string
tartgetPath: string
config: TemplateConfig
}
inquirer.prompt(QUESTIONS)
.then(answers => {
answers = Object.assign({}, answers, yargs.argv);
const projectChoice = answers['template'];
const projectName = answers['name'];
const templatePath = path.join(__dirname, 'templates', projectChoice);
const tartgetPath = path.join(CURR_DIR, projectName);
const templateConfig = getTemplateConfig(templatePath);
const options: CliOptions = {
projectName,
templateName: projectChoice,
templatePath,
tartgetPath,
config: templateConfig
}
if (!createProject(tartgetPath)) {
return;
}
createDirectoryContents(templatePath, projectName, templateConfig);
if (!postProcess(options)) {
return;
}
showMessage(options);
});
function showMessage(options: CliOptions) {
console.log('');
console.log(chalk.green('Done.'));
console.log(chalk.green(`Go into the project: cd ${options.projectName}`));
const message = options.config.postMessage;
if (message) {
console.log('');
console.log(chalk.yellow(message));
console.log('');
}
}
function getTemplateConfig(templatePath: string): TemplateConfig {
const configPath = path.join(templatePath, '.template.json');
if (!fs.existsSync(configPath)) return {};
const templateConfigContent = fs.readFileSync(configPath);
if (templateConfigContent) {
return JSON.parse(templateConfigContent.toString());
}
return {};
}
function createProject(projectPath: string) {
if (fs.existsSync(projectPath)) {
console.log(chalk.red(`Folder ${projectPath} exists. Delete or use another name.`));
return false;
}
fs.mkdirSync(projectPath);
return true;
}
function postProcess(options: CliOptions) {
if (isNode(options)) {
return postProcessNode(options);
}
return true;
}
function isNode(options: CliOptions) {
return fs.existsSync(path.join(options.templatePath, 'package.json'));
}
function postProcessNode(options: CliOptions) {
shell.cd(options.tartgetPath);
let cmd = '';
if (shell.which('yarn')) {
cmd = 'yarn';
} else if (shell.which('npm')) {
cmd = 'npm install';
}
if (cmd) {
const result = shell.exec(cmd);
if (result.code !== 0) {
return false;
}
} else {
console.log(chalk.red('No yarn or npm found. Cannot run installation.'));
}
return true;
}
const SKIP_FILES = ['node_modules', '.template.json'];
function createDirectoryContents(templatePath: string, projectName: string, config: TemplateConfig) {
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach(file => {
const origFilePath = path.join(templatePath, file);
// get stats about the current file
const stats = fs.statSync(origFilePath);
if (SKIP_FILES.indexOf(file) > -1) return;
if (stats.isFile()) {
let contents = fs.readFileSync(origFilePath, 'utf8');
contents = template.render(contents, { projectName });
const writePath = path.join(CURR_DIR, projectName, file);
fs.writeFileSync(writePath, contents, 'utf8');
} else if (stats.isDirectory()) {
fs.mkdirSync(path.join(CURR_DIR, projectName, file));
// recursive call
createDirectoryContents(path.join(templatePath, file), path.join(projectName, file), config);
}
});
}