-
Notifications
You must be signed in to change notification settings - Fork 0
/
.reinit
executable file
·206 lines (194 loc) · 6.38 KB
/
.reinit
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
#!/usr/bin/env node
// tslint:disable: tsr-detect-non-literal-fs-filename no-var-requires tsr-detect-child-process
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const cp = require("child_process");
const pkgFname = "package.json";
const pkgJson = JSON.parse(fs.readFileSync(pkgFname, "utf8"));
const resetPackageJsonPreInstall = (ctx) => {
const pkg = JSON.parse(fs.readFileSync(pkgFname, "utf8"));
const origname = pkg.name;
delete pkg.scripts.preinstall;
pkg.name = `@${ctx.org}/${ctx.app}`;
pkg.description = "";
pkg.version = "0.0.1";
pkg.keywords = [];
pkg.repository = {
type: "git",
url: `git+https://github.com/${ctx.org}/${ctx.app}.git`,
};
pkg.homepage = `https://github.com/${ctx.org}/${ctx.app}#readme`;
pkg.bugs = {
url: `https://github.com/${ctx.org}/${ctx.app}/issues`,
};
// remove scripts from distributed files
pkg.files = [
"dist",
"docs",
];
fs.writeFileSync(pkgFname, JSON.stringify(pkg, null, 2), "utf8");
return {
...ctx,
origname,
};
};
const resetPackageJsonPostInstall = () => {
const pkg = JSON.parse(fs.readFileSync(pkgFname, "utf8"));
delete pkg.scripts.postinstall;
fs.writeFileSync(pkgFname, JSON.stringify(pkg, null, 2), "utf8");
return pkg;
};
// Since this is a pre-install configuration script, meant to be run
// one-time, it's not right to install dependencies into target repo.
// We have simple local implementations of needed functionality.
// This file self-deletes after successful execution.
function sed(fname, match_re, replacement) {
const content = fs.readFileSync(fname, "utf8");
fs.writeFileSync(fname, content.replace(match_re, replacement), "utf8");
}
function rimraf(dir) {
if (dir === "/") { return; }
if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach((file, index) => {
const curdir = path.join(dir, file);
if (fs.lstatSync(curdir).isDirectory()) { // recurse
rimraf(curdir);
} else { // delete file
fs.unlinkSync(curdir);
}
});
fs.rmdirSync(dir);
}
}
function spawn(_cmd, args, opts) {
opts = opts || {
cwd: process.cwd(),
stdio: "inherit",
};
// tslint:disable-next-line: no-console
console.log([_cmd].concat(args).join(" "));
return new Promise((resolve, reject) => {
const proc = cp.spawn(_cmd, args, opts);
proc.on("close", (code) => {
code ? reject(code) : resolve();
});
});
}
const isSourceRepo = path.basename(__dirname) === pkgJson.name.split("/").pop();
const [exe, script, cmd, ...rest] = process.argv;
// tslint:disable-next-line: no-use-before-declare
if (require.main !== module) {
// this file is being included as a library
// export the utility functions defined within
module.exports = {
resetPackageJsonPostInstall,
resetPackageJsonPreInstall,
rimraf,
sed,
spawn,
};
} else if (!isSourceRepo && cmd === "preinstall") {
// this is the purpose
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// defaults
const result = {
app: `@tufan-io/${path.basename(process.cwd())}`,
org: "tufan-io",
};
// tslint:disable: no-shadowed-variable
return new Promise((resolve, reject) => {
// Q1 ----
rl.question(`Git organization (${result.org}): `, (answer1) => {
result.org = answer1 || result.org;
// Q2 ----
rl.question(`Name of the project (@${result.org}/${result.app.replace(/.*\//, "")}): `, (answer2) => {
result.app = answer2 || `@${result.org}/${result.app.replace(/.*\//, "")}`;
return resolve(result);
});
});
}).then((result) => {
result.app = result.app.replace(/.*\//, "");
const ctx = resetPackageJsonPreInstall(result);
fs.writeFileSync("./.reinit-lock", JSON.stringify(ctx), "utf8");
// reset changelog.md
fs.writeFileSync("CHANGELOG.md", "", "utf8");
sed("./README.md", /tufan-io/g, result.org);
sed("./README.md", /action-starter/g, result.app);
sed("./action.yml", /action-starter/g, result.app);
sed("./src/index.ts", /action-starter/g, result.app);
return result;
}).then((result) => {
// delete old git repo and initialize a new one.
rimraf(path.join(process.cwd(), ".git"));
return result;
}).then((result) => {
// initialize new git repo
return spawn("git", ["init"]).then(() => result);
}).then((result) => {
return spawn("git", ["add", "."]).then(() => result);
}).then((result) => {
process.exit(0);
}).catch((err) => {
// tslint:disable: no-console
console.error(err);
process.exit(-1);
});
} else if (isSourceRepo && cmd === "postinstall") {
// need to split pre and post install here.
return Promise.resolve().then(() => {
try {
const raw = fs.readFileSync("./.reinit-lock", "utf8");
const ctx = JSON.parse(raw);
} catch (err) {
// invoking post install on unmodified starter module
const raw = fs.readFileSync("./package.json", "utf8");
const pkg = JSON.parse(raw);
if (pkg.name.match(/starter/)) {
throw new Error(`This is a starter repo. Ignore`);
} else {
throw new Error(`Invoked '.reinit postinstall' without a './.reinit-lock' present.`);
}
}
}).then(() => {
const pkg = resetPackageJsonPostInstall();
fs.unlinkSync(".reinit");
fs.unlinkSync(".reinit-lock");
return pkg;
}).then((pkg) => {
return spawn(`npm`, [`run`, `build`]).then(() => pkg);
}).then((pkg) => {
return spawn(`git`, [`add`, `.`, `--all`]).then(() => pkg);
}).then((pkg) => {
return spawn(`git`, [
`remote`,
`add`,
`origin`,
pkg.repository.url,
]).then(() => pkg);
}).then((pkg) => {
console.log(``);
console.log(`TODO: configure git username and email before committing`);
console.log(``);
console.log(` git config user.name \"John Doe\"`);
console.log(` git config user.email [email protected]`);
console.log(``);
console.log(`---`);
console.log(`The repo has been properly re-initialized.`);
console.log(`Go forth and make a brave new npm package...`);
console.log(`---`);
process.exit(0);
}).catch((err) => {
if (err.message.match(/This is a starter repo. Ignore/)) {
process.exit(0);
} else {
console.error(err);
process.exit(-1);
}
});
} else if (!isSourceRepo) {
console.error(`Usage: .reinit <preinstall|postinstall>.\n Missing required argument`);
}