-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·265 lines (244 loc) · 6.89 KB
/
index.mjs
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
#! /usr/bin/env node
import http from "http";
import fs from "fs";
import path from "path";
import { spawn } from "child_process";
import chalk from "chalk";
import clone from "gh-clone";
import { rimraf } from "rimraf";
import { simpleGit } from "simple-git";
import argvPackage from "process-argv";
import open from "open";
const argv = argvPackage();
const httpRepo = "https://github.com/";
const sshRepo = "[email protected]";
const repoName = "readymade-ui/starter";
const pkg = await import(`./package.json`, {
assert: { type: "json" },
});
const args = Object.assign(argv, {
http: argv.options
? isNull(argv.options.host)
? true
: isString(argv.options.host)
? argv.options.host
: null
: null,
npm: argv.options ? isNull(argv.options.npm) : false,
repo: argv.options
? argv.options.repo
? argv.options.repo
: repoName
: repoName,
script: argv.options
? isString(argv.options.script)
? argv.options.script
: "start"
: "start",
ssh: argv.options
? isNull(argv.options.ssh)
? true
: isString(argv.options.ssh)
? argv.options.ssh
: null
: null,
open: argv.options
? isNull(argv.options.open)
? true
: isString(argv.options.open)
? true
: false
: false,
version: pkg.version,
});
function helpItem(key, value) {
process.stdout.write(`${chalk.white(key)} ${chalk.dim.white(value)} \n`);
}
function exampleItem(key, value) {
process.stdout.write(`${chalk.blueBright(key)}\n ${chalk.white(value)} \n\n`);
}
function help(args) {
process.stdout.write(`\n`);
process.stdout.write(chalk.magenta(`primr ${args.version}\n`));
process.stdout.write(`\n`);
process.stdout.write(
`${chalk.dim.white("Example usage:")} ${chalk.white("npx primr my-app")} \n`
);
process.stdout.write(
chalk.blueBright(
`^^^ generates a new Readymade project in the my-app directory \n`
)
);
process.stdout.write(`\n`);
process.stdout.write(chalk.whiteBright(`Options \n`));
process.stdout.write(chalk.whiteBright(`------- \n`));
helpItem(
"http",
"use https instead of ssh for git clone (default = https://github.com/)"
);
helpItem("repo", "repo name (default = readymade-ui/starter)");
helpItem("script", "run script after install (default = npm run dev)");
helpItem(
"ssh",
"use ssh instead of https for git clone (default = [email protected])"
);
helpItem("npm", "use npm instead of yarn (default is yarn)");
process.stdout.write(`\n`);
process.stdout.write(chalk.whiteBright(`Examples \n`));
process.stdout.write(chalk.whiteBright(`-------- \n`));
exampleItem(
"Clone the default readymade-ui/starter repository into a directory called my-app over ssh from a custom git server, install dependencies and run scripts with npm",
"npx primr my-app --ssh [email protected] --npm"
);
exampleItem(
"Clone another repository into a directory called foo-server, run the serve script post install.",
"npx primr foo-server --repo steveblue/bazel-typescript-starter --script serve"
);
process.stdout.write(`\n`);
}
function exists(args) {
process.stdout.write(`\n`);
process.stdout.write(
chalk.red(`${args.command} already exists.\n`) +
chalk.dim.white(
`Please remove the directory or install ${args.command} in another location.\n`
)
);
process.stdout.write(`\n`);
}
function init(args) {
const server = spawn(
args.npm === true ? "npm" : "yarn",
args.npm === true ? [args.script] : ["run", args.script],
{
cwd: args.command,
shell: true,
stdio: "inherit",
}
).on("exit", () => {
process.stdout.write("🪄 " + chalk.green(args.command + " is ready\n"));
});
process.on("exit", () => {
server.kill();
});
if (args.open) {
const options = {
hostname: "localhost",
port: 4443,
path: "/",
method: "GET",
};
const pingServer = () => {
const req = http.request(options, (res) => {
if (res.statusCode === 200) {
open("http://localhost:4443");
clearInterval(pingInterval);
}
});
req.on("error", (error) => {
// console.error(`Error: ${error.message}`);
});
req.end();
};
const pingInterval = setInterval(pingServer, 100);
}
}
function install(args) {
spawn(args.npm === true ? "npm" : "yarn", ["install"], {
cwd: args.command,
shell: true,
stdio: "inherit",
}).on("exit", () => {
process.stdout.write("🪄 " + chalk.green(args.command + " is ready\n"));
init(args);
});
}
function processPackage(args, rootPackage, git) {
rimraf.sync(path.resolve(`./${args.command}/yarn.lock`));
rimraf.sync(path.resolve(`./${args.command}/package-lock.json`));
rimraf.sync(path.resolve(`./${args.command}/.yarnrc.yml`));
args.user = {};
const name = spawn("git", ["config", "--global", "user.name"]);
name.stdout.setEncoding("utf8");
name.stdout.on("data", (data) => {
args.user.name = data;
const email = spawn("git", ["config", "--global", "user.email"]);
email.stdout.setEncoding("utf8");
email.stdout.on("data", (data) => {
const author = `${args.user.name} <${data}>`.replace(/\n/g, "");
args.user.email = data;
rootPackage.description = "";
rootPackage.repository = "";
rootPackage.name = args.command;
rootPackage.author = author;
rootPackage.version = "1.0.0";
delete rootPackage.packageManager;
fs.writeFile(
path.resolve(`./${args.command}/package.json`),
JSON.stringify(rootPackage, null, 4),
(err, data) => {
if (err) {
throw err;
}
install(args);
}
);
});
});
}
function customize(args) {
rimraf.sync(path.resolve(`./${args.command}/.git`));
const git = simpleGit({
baseDir: args.command,
});
git.init().then(() => {
fs.readFile(
path.resolve(`./${args.command}/package.json`),
"utf-8",
(err, data) => {
if (err) {
throw err;
}
processPackage(args, JSON.parse(data), git);
}
);
});
}
function make(args) {
let hostName = "";
if (argv.http === true) {
hostName = httpRepo;
} else if (isString(argv.http)) {
hostName = argv.http + "/";
} else if (args.ssh === true) {
hostName = sshRepo + ":";
} else if (isString(argv.ssh)) {
hostName = args.ssh + ":";
} else {
hostName = httpRepo;
}
clone(`${hostName}${args.repo}.git`, { dest: args.command })
.then(() => {
customize(args);
})
.catch((err) => process.stderr.write(err));
}
function start(args) {
if (args.options && isNull(args.options.help)) {
help(args);
}
if (args.command) {
if (fs.existsSync(path.resolve("./" + args.command))) {
exists(args);
} else {
make(args);
}
}
}
function isString(arg) {
return typeof arg === "string";
}
function isNull(arg) {
return arg === null;
}
start(args);