-
Notifications
You must be signed in to change notification settings - Fork 1
/
createSequelizeExpress.js
344 lines (319 loc) · 9.83 KB
/
createSequelizeExpress.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env node
/**
* Copyright (c) 2018-present, Lmntrx Tech.
*
* This source code is licensed under the Apache 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// create-sequelize-express is installed globally on people's computers. This means
// that it is extremely difficult to have them upgrade the version and
// because there's only one global version installed, it is very prone to
// breaking changes.
//
// If you need to add a new command, please add it to the scripts/ folder.
//
// The only reason to modify this file is to add more warnings and
// troubleshooting information for the `create-sequelize-express` command.
//
// Do not make breaking changes! We absolutely don't want to have to
// tell people to update their global version of create-sequelize-express.
//
// Also be careful with new language features.
// This file must work on Node 6+.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"use strict";
const validateProjectName = require("validate-npm-package-name");
const chalk = require("chalk");
const commander = require("commander");
const fs = require("fs-extra");
const Promise = require("bluebird");
const fs_promise = Promise.promisifyAll(require("fs"));
const envinfo = require("envinfo");
const execSync = require("child_process").execSync;
const spawn = require("cross-spawn");
const targz = require("targz");
const os = require("os");
const path = require("path");
const packageJson = require("./package.json");
let projectName;
// These files should be allowed to remain on a failed install,
// but then silently removed during the next create.
const errorLogFilePatterns = [
"npm-debug.log",
"yarn-error.log",
"yarn-debug.log"
];
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments(`<project-directory>`)
.usage(`${chalk.green("<project-directory>")} [options]`)
.action(name => {
projectName = name;
})
.option("--verbose", "print additional logs")
.option("--info", "print environment debug info")
.allowUnknownOption()
.on("--help", () => {
console.log(` Only ${chalk.green("<project-directory>")} is required.`);
console.log();
console.log(
` If you have any problems, do not hesitate to file an issue:`
);
console.log(
` ${chalk.cyan(
"https://github.com/LmntrX/create-sequelize-express/issues/new"
)}`
);
console.log();
})
.parse(process.argv);
if (program.info) {
console.log(chalk.bold("\nEnvironment Info:"));
return envinfo
.run(
{
System: ["OS", "CPU"],
Binaries: ["Node", "npm", "Yarn"],
Browsers: ["Chrome", "Edge", "Internet Explorer", "Firefox", "Safari"],
npmGlobalPackages: ["create-sequelize-express"]
},
{
clipboard: true,
duplicates: true,
showNotFound: true
}
)
.then(console.log)
.then(() => console.log(chalk.green("Copied To Clipboard!\n")));
}
if (typeof projectName === "undefined") {
console.error("Please specify the project directory:");
console.log(
` ${chalk.cyan(program.name())} ${chalk.green("<project-directory>")}`
);
console.log();
console.log("For example:");
console.log(
` ${chalk.cyan(program.name())} ${chalk.green("lx-sequelize-express-app")}`
);
console.log();
console.log(
`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`
);
process.exit(1);
}
function printValidationResults(results) {
if (typeof results !== "undefined") {
results.forEach(error => {
console.error(chalk.red(` * ${error}`));
});
}
}
createApp(projectName, program.verbose);
function moveAllFiles(srcDir, destDir) {
return fs_promise.readdirAsync(srcDir).map(function(file) {
var destFile = path.join(destDir, file);
console.log(destFile);
return fs_promise
.renameAsync(path.join(srcDir, file), destFile)
.then(function() {
return destFile;
});
});
}
function createApp(name, verbose) {
const root = path.resolve(name);
const appName = path.basename(root);
checkAppName(appName);
fs.ensureDirSync(name);
if (!isSafeToCreateProjectIn(root, name)) {
process.exit(1);
}
console.log(`Creating a new Sequelize-Express app in ${chalk.green(root)}.`);
console.log();
const packageJson = {
name: appName,
version: "0.1.0",
private: true,
description: "",
main: "app.js",
scripts: {
test: "nyc mocha --exit --recursive",
start: "node ./bin/www",
deploy: "./deploy.sh",
"start:dev": "nodemon ./bin/www"
},
repository: {
type: "git",
url: ""
},
author: "Author Name",
license: "",
homepage: "",
dependencies: {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.19.0",
cors: "^2.8.5",
express: "^4.17.1",
jsonwebtoken: "^8.5.1",
morgan: "^1.9.0",
pg: "^7.17.1",
"pg-hstore": "^2.3.3",
sequelize: ">=5.3.0"
},
devDependencies: {
chai: "^4.2.0",
"chai-http": "^4.3.0",
mocha: "^5.2.0",
nyc: "^13.1.0"
}
};
fs.writeFileSync(
path.join(root, "package.json"),
JSON.stringify(packageJson, null, 2) + os.EOL
);
const originalDirectory = process.cwd();
process.chdir(root);
fs.copySync(require.resolve("./arch.tar.gz"), path.join(root, "arch.tar.gz"));
targz.decompress(
{
src: path.join(root, "arch.tar.gz"),
dest: root
},
function(err) {
if (err) {
console.log(chalk.red(`Error extracting files...`));
} else {
moveAllFiles(path.join(root, "arch"), root)
.then(files => {
console.log(chalk.blue(`Completed extracting files...`));
console.log(chalk.blue(`Installing dependencies...`));
spawn("npm", ["install"], { stdio: "inherit" });
fs.renameSync(
path.join(root, ".gitignore-skeleton"),
path.join(root, ".gitignore")
);
console.log(chalk.blue(`Cleaning up...`));
spawn("find", [".", "-name", "._*", "-delete"]);
spawn("rm", ["arch.tar.gz"]);
spawn("rm", ["-rf", "arch"]);
spawn("npm", ["install", "-g", "sequelize-cli"]);
console.log("Setup Complete.");
console.log();
console.log(
`NB: See ${chalk.yellow(
"src/config/config.json"
)} to setup your environment`
);
console.log();
console.log(
`After setting up the environment, run ${chalk.yellow(
"sequelize db:migrate"
)} in '${chalk.yellow(
projectName
)}' directory to create user model in the database`
);
console.log();
})
.catch(error => console.error(error));
}
}
);
}
function checkAppName(appName) {
const validationResult = validateProjectName(appName);
if (!validationResult.validForNewPackages) {
console.error(
`Could not create a project called ${chalk.red(
`"${appName}"`
)} because of npm naming restrictions:`
);
printValidationResults(validationResult.errors);
printValidationResults(validationResult.warnings);
process.exit(1);
}
// TODO: there should be a single place that holds the dependencies
const dependencies = [
"sequelize",
"create-sequelize-express",
"express"
].sort();
if (dependencies.indexOf(appName) >= 0) {
console.error(
chalk.red(
`We cannot create a project called ${chalk.green(
appName
)} because a dependency with the same name exists.\n` +
`Due to the way npm works, the following names are not allowed:\n\n`
) +
chalk.cyan(dependencies.map(depName => ` ${depName}`).join("\n")) +
chalk.red("\n\nPlease choose a different project name.")
);
process.exit(1);
}
}
// If project only contains files generated by GH, it’s safe.
// Also, if project contains remnant error logs from a previous
// installation, lets remove them now.
// We also special case IJ-based products .idea because it integrates with CSE
function isSafeToCreateProjectIn(root, name) {
const validFiles = [
".DS_Store",
"Thumbs.db",
".git",
".gitignore",
".idea",
"README.md",
"LICENSE",
"web.iml",
".hg",
".hgignore",
".hgcheck",
".npmignore",
"mkdocs.yml",
"docs",
".travis.yml",
".gitlab-ci.yml",
".gitattributes"
];
console.log();
const conflicts = fs
.readdirSync(root)
.filter(file => !validFiles.includes(file))
// Don't treat log files from previous installation as conflicts
.filter(
file => !errorLogFilePatterns.some(pattern => file.indexOf(pattern) === 0)
);
if (conflicts.length > 0) {
console.log(
`The directory ${chalk.green(name)} contains files that could conflict:`
);
console.log();
for (const file of conflicts) {
console.log(` ${file}`);
}
console.log();
console.log(
"Either try using a new directory name, or remove the files listed above."
);
return false;
}
// Remove any remnant files from a previous installation
const currentFiles = fs.readdirSync(path.join(root));
currentFiles.forEach(file => {
errorLogFilePatterns.forEach(errorLogFilePattern => {
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
if (file.indexOf(errorLogFilePattern) === 0) {
fs.removeSync(path.join(root, file));
}
});
});
return true;
}