This repository has been archived by the owner on Jul 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject.js
120 lines (99 loc) · 3.08 KB
/
Project.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
// Require dependencies
const fs = require("fs");
const path = require("path");
const exec = require("child_process").execSync;
const chalk = require("chalk");
const { splitPackages, readFile, writeJson, addFile } = require("./functions");
class Project {
constructor({
userDir,
name,
type,
packages,
scripts,
templates,
folders,
mainFile,
manager
}) {
this.userDir = userDir;
this.name = name;
this.type = type;
this.packages = packages ? packages : undefined;
this.scripts = scripts ? [...scripts] : undefined;
this.templates = templates ? [...templates] : undefined;
this.folders = folders ? [...folders] : undefined;
this.mainFile = mainFile;
this.manager = manager;
}
create() {
// Create variable of base url
const baseUrl = this.userDir + "/" + this.name;
// Check folder of project name for existing
if (fs.existsSync(baseUrl)) {
return console.log(`Folder with name \"${this.name}\" already exists.`);
}
// Create a project folder
fs.mkdirSync(baseUrl);
// Cd to a new project folder and initialize it
exec(`cd ${this.name} && ${this.manager} init -y`);
console.log(
chalk.green("Now, script is installing dependencies to your new project.")
);
// Install usual dependencies
exec(splitPackages(this.packages.usual, this.name, false, this.manager));
console.log(
chalk.green(
"Usual dependencies was installed and dev dependencies will install now."
)
);
// Install dev dependencies
if (typeof this.packages.dev !== "undefined") {
exec(splitPackages(this.packages.dev, this.name, true, this.manager));
}
console.log(
chalk.green(
"All dependencies was installed and now script is setting up your new project."
)
);
// Read package.json file in new directory
const packageJSON = JSON.parse(readFile(`${baseUrl}/package.json`));
// Add scripts object if user uses yarn as package manager
if (this.manager === "yarn") {
packageJSON.scripts = {};
}
// Add scripts to package.json file in new directory
if (this.scripts) {
this.scripts.forEach(script => {
const name = script.name;
const value = script.value;
packageJSON.scripts[name] = value;
});
}
// Change main file in project
packageJSON["main"] = this.mainFile;
// Write changed package.json file
writeJson(`${baseUrl}/package.json`, packageJSON);
// Create folders
if (this.folders) {
this.folders.forEach(folder => {
fs.mkdirSync(baseUrl + folder);
});
}
// Add project files
if (this.templates) {
this.templates.forEach(template => {
addFile(baseUrl + template.path, template.file);
});
}
}
bye() {
let message = fs
.readFileSync(path.join(__dirname, "bye-message.txt"))
.toString();
message = message.replace(/--PROJECT_NAME--/, this.name);
message = message.replace(/--MANAGER--/, this.manager);
console.log(chalk.blue(message));
}
}
module.exports = Project;