-
Notifications
You must be signed in to change notification settings - Fork 236
/
index.js
94 lines (75 loc) · 2.56 KB
/
index.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
const crypto = require("crypto");
const fs = require("fs/promises");
const path = require("path");
const toml = require("@iarna/toml");
const sort = require("sort-package-json");
function escapeRegExp(string) {
// $& means the whole matched string
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function getRandomString(length) {
return crypto.randomBytes(length).toString("hex");
}
async function main({ rootDirectory }) {
const README_PATH = path.join(rootDirectory, "README.md");
const FLY_TOML_PATH = path.join(rootDirectory, "fly.toml");
const EXAMPLE_ENV_PATH = path.join(rootDirectory, ".env.example");
const ENV_PATH = path.join(rootDirectory, ".env");
const PACKAGE_JSON_PATH = path.join(rootDirectory, "package.json");
const REPLACER = "blues-stack-template";
const DIR_NAME = path.basename(rootDirectory);
const SUFFIX = getRandomString(2);
const APP_NAME = (DIR_NAME + "-" + SUFFIX)
// get rid of anything that's not allowed in an app name
.replace(/[^a-zA-Z0-9-_]/g, "-");
const [prodContent, readme, env, packageJson] = await Promise.all([
fs.readFile(FLY_TOML_PATH, "utf-8"),
fs.readFile(README_PATH, "utf-8"),
fs.readFile(EXAMPLE_ENV_PATH, "utf-8"),
fs.readFile(PACKAGE_JSON_PATH, "utf-8"),
]);
const newEnv = env.replace(
/^SESSION_SECRET=.*$/m,
`SESSION_SECRET="${getRandomString(16)}"`
);
const prodToml = toml.parse(prodContent);
prodToml.app = prodToml.app.replace(REPLACER, APP_NAME);
const newReadme = readme.replace(
new RegExp(escapeRegExp(REPLACER), "g"),
APP_NAME
);
const newPackageJson =
JSON.stringify(
sort({ ...JSON.parse(packageJson), name: APP_NAME }),
null,
2
) + "\n";
await Promise.all([
fs.writeFile(FLY_TOML_PATH, toml.stringify(prodToml)),
fs.writeFile(README_PATH, newReadme),
fs.writeFile(ENV_PATH, newEnv),
fs.writeFile(PACKAGE_JSON_PATH, newPackageJson),
fs.copyFile(
path.join(rootDirectory, "remix.init", "gitignore"),
path.join(rootDirectory, ".gitignore")
),
fs.rm(path.join(rootDirectory, ".github/ISSUE_TEMPLATE"), {
recursive: true,
}),
fs.rm(path.join(rootDirectory, ".github/PULL_REQUEST_TEMPLATE.md")),
]);
console.log(
`
Setup is almost complete. Follow these steps to finish initialization:
- Start the database:
npm run docker
- Run setup (this updates the database):
npm run setup
- Run the first build (this generates the server you will run):
npm run build
- You're now ready to rock and roll 🤘
npm run dev
`.trim()
);
}
module.exports = main;