-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
create-app.ts
203 lines (184 loc) 路 7.13 KB
/
create-app.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// std
import { execSync, spawn, SpawnOptions } from 'child_process';
// 3p
import { cyan, red } from 'colors/safe';
// FoalTS
import {
Generator,
getNames,
initGitRepo,
mkdirIfDoesNotExist,
} from '../../utils';
function isYarnInstalled() {
try {
execSync('yarn --version', { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
function log(msg: string) {
if (process.env.NODE_ENV !== 'test') {
console.log(msg);
}
}
function validateProjectName(name: string) {
// As per npm naming conventions (https://www.npmjs.com/package/validate-npm-package-name)
const specialChars = ['!',
'@',
'#',
'$',
'%',
'^',
'&',
'*',
'(',
')',
'/',
'\\',
'+',
'{',
'}',
'[',
']',
':',
';',
'<',
'>',
',',
'.',
'?'
];
return !specialChars.find(char => name.includes(char));
}
export async function createApp({ name, autoInstall, initRepo, mongodb = false, yaml = false }:
{ name: string, autoInstall?: boolean, initRepo?: boolean, mongodb?: boolean,
yaml?: boolean }) {
const names = getNames(name);
if (process.env.NODE_ENV !== 'test') {
console.log(cyan(
`====================================================================
_______ ________ ____ ___ _________ _______
/ ____/ / ____ / / _ | / / /___ ___/ / _____/
/ /___ / / / / / /_| | / / / / / /____
/ ____/ / / / / / ___ | / / / / /____ /
/ / / /__ / / / / | | / /____ / / _____/ /
/__/ /_______/ /_/ |_| /_______/ /_/ /______/
====================================================================
`
));
}
const locals = names;
// Validating whether if the project-name follows npm naming conventions
if (!validateProjectName(name)) {
console.log(
red(`\n ${red(`${name} doesn't follow the npm naming conventions. Kindly give a vaild project-name`)}`)
);
return;
}
mkdirIfDoesNotExist(names.kebabName);
log(' 馃搨 Creating files...');
const generator = new Generator('app', names.kebabName, { noLogs: true });
generator
.copyFileFromTemplates('gitignore', '.gitignore')
.copyFileFromTemplatesOnlyIf(!mongodb, 'ormconfig.js')
.renderTemplateOnlyIf(!mongodb && !yaml, 'package.json', locals)
.renderTemplateOnlyIf(!mongodb && yaml, 'package.yaml.json', locals, 'package.json')
.renderTemplateOnlyIf(mongodb && !yaml, 'package.mongodb.json', locals, 'package.json')
.renderTemplateOnlyIf(mongodb && yaml, 'package.mongodb.yaml.json', locals, 'package.json')
.copyFileFromTemplates('tsconfig.app.json')
.copyFileFromTemplates('tsconfig.e2e.json')
.copyFileFromTemplates('tsconfig.json')
.copyFileFromTemplatesOnlyIf(!mongodb, 'tsconfig.migrations.json')
.copyFileFromTemplates('tsconfig.scripts.json')
.copyFileFromTemplates('tsconfig.test.json')
.copyFileFromTemplates('.eslintrc.js')
// Config
.mkdirIfDoesNotExist('config')
.renderTemplateOnlyIf(!mongodb && !yaml, 'config/default.json', locals)
.renderTemplateOnlyIf(!mongodb && yaml, 'config/default.yml', locals)
.renderTemplateOnlyIf(mongodb && !yaml, 'config/default.mongodb.json', locals, 'config/default.json')
.renderTemplateOnlyIf(mongodb && yaml, 'config/default.mongodb.yml', locals, 'config/default.yml')
.renderTemplateOnlyIf(!yaml, 'config/development.json', locals)
.renderTemplateOnlyIf(yaml, 'config/development.yml', locals)
.renderTemplateOnlyIf(!mongodb && !yaml, 'config/e2e.json', locals)
.renderTemplateOnlyIf(!mongodb && yaml, 'config/e2e.yml', locals)
.renderTemplateOnlyIf(mongodb && !yaml, 'config/e2e.mongodb.json', locals, 'config/e2e.json')
.renderTemplateOnlyIf(mongodb && yaml, 'config/e2e.mongodb.yml', locals, 'config/e2e.yml')
.renderTemplateOnlyIf(!yaml, 'config/production.json', locals)
.renderTemplateOnlyIf(yaml, 'config/production.yml', locals)
.renderTemplateOnlyIf(!mongodb && !yaml, 'config/test.json', locals)
.renderTemplateOnlyIf(!mongodb && yaml, 'config/test.yml', locals)
.renderTemplateOnlyIf(mongodb && !yaml, 'config/test.mongodb.json', locals, 'config/test.json')
.renderTemplateOnlyIf(mongodb && yaml, 'config/test.mongodb.yml', locals, 'config/test.yml')
// Public
.mkdirIfDoesNotExist('public')
.copyFileFromTemplates('public/index.html')
.copyFileFromTemplates('public/logo.png')
// Src
.mkdirIfDoesNotExist('src')
.copyFileFromTemplates('src/e2e.ts')
.copyFileFromTemplatesOnlyIf(mongodb, 'src/index.mongodb.ts', 'src/index.ts')
.copyFileFromTemplatesOnlyIf(!mongodb, 'src/index.ts')
.copyFileFromTemplates('src/test.ts')
// App
.mkdirIfDoesNotExist('src/app')
.copyFileFromTemplates('src/app/app.controller.ts')
// Controllers
.mkdirIfDoesNotExist('src/app/controllers')
.copyFileFromTemplates('src/app/controllers/index.ts')
.copyFileFromTemplates('src/app/controllers/api.controller.ts')
.copyFileFromTemplates('src/app/controllers/api.controller.spec.ts')
// Entities
.mkdirIfDoesNotExistOnlyIf(!mongodb, 'src/app/entities')
.copyFileFromTemplatesOnlyIf(!mongodb, 'src/app/entities/index.ts')
.copyFileFromTemplatesOnlyIf(!mongodb, 'src/app/entities/user.entity.ts')
// Hooks
.mkdirIfDoesNotExist('src/app/hooks')
.copyFileFromTemplates('src/app/hooks/index.ts')
// Models
.mkdirIfDoesNotExistOnlyIf(mongodb, 'src/app/models')
.copyFileFromTemplatesOnlyIf(mongodb, 'src/app/models/index.ts')
.copyFileFromTemplatesOnlyIf(mongodb, 'src/app/models/user.model.ts')
// Services
.mkdirIfDoesNotExist('src/app/services')
.copyFileFromTemplates('src/app/services/index.ts')
// E2E
.mkdirIfDoesNotExist('src/e2e')
.copyFileFromTemplatesOnlyIf(!mongodb, 'src/e2e/index.ts')
.copyFileFromTemplatesOnlyIf(mongodb, 'src/e2e/index.mongodb.ts', 'src/e2e/index.ts')
// Scripts
.mkdirIfDoesNotExist('src/scripts')
.copyFileFromTemplatesOnlyIf(!mongodb, 'src/scripts/create-user.ts')
.copyFileFromTemplatesOnlyIf(mongodb, 'src/scripts/create-user.mongodb.ts', 'src/scripts/create-user.ts');
log('');
log(' 馃摝 Installing the dependencies...');
if (autoInstall) {
const packageManager = isYarnInstalled() ? 'yarn' : 'npm';
const args = [ 'install' ];
const options: SpawnOptions = {
cwd: names.kebabName,
shell: true,
stdio: 'inherit'
};
await new Promise(resolve => {
spawn(packageManager, args, options)
.on('close', (code: number) => {
if (code !== 0) {
log(red('A problem occurred when installing the dependencies. See above.'));
}
resolve();
});
});
}
log(' 馃摂 Initializing git repository...');
if (initRepo) {
await initGitRepo(names.kebabName);
}
log(`
馃憠 Run the following commands to get started:
$ ${cyan(`cd ${names.kebabName}`)}
$ ${cyan('npm run develop')}
`
);
}