-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinstall.js
executable file
·102 lines (90 loc) · 2.37 KB
/
install.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
#!/usr/bin/env node
const path = require("path");
const fs = require('fs').promises;
const mvdir = require("mvdir");
const prompts = require("prompts");
const replace = require("replace-in-file");
const slugify = require("slugify");
main();
async function main() {
const ts = Boolean(process.argv.includes("--typescript"));
const codebase = ts ? "ts" : "js";
const res = await prompts([
{
type: "text",
name: "app",
message: "What should we call your application?",
},
{
type: async (prev) =>
(await hasDirectory(getDir(prev))) ? "text" : null,
name: "dir",
message: async (prev) =>
`Looks like a directory already exists called "${slugify(prev, {
lower: true,
})}". Where should your app be placed instead?`,
},
{
type: "text",
name: "url",
message: "What URL do you use to access Urbit?",
},
]);
const name = res.app.trim();
const slug = slugify(name, { lower: true });
const dir = res.dir || path.join(".", slug);
try {
await mvdir(path.join(__dirname, codebase), dir, { copy: true });
await mvdir(
path.join(dir, "ui", "_gitignore"),
path.join(dir, "ui", ".gitignore")
);
const prefixPath = (p) => path.join(dir, p);
await replace({
files: prefixPath("ui/.env.local"),
from: "%URBITURL%",
to: res.url,
});
await replace({
files: [
"README.md",
"ui/index.html",
`ui/src/app.${ts ? "tsx" : "jsx"}`,
"ui/src/assets/manifest.json",
"desk/desk.docket-0",
].map(prefixPath),
from: /%APPNAME%/g,
to: name,
});
await replace({
files: [
"README.md",
"ui/package.json",
`ui/vite.config.${ts ? "ts" : "js"}`,
"desk/desk.docket-0",
].map(prefixPath),
from: /%APPSLUG%/g,
to: slug,
});
console.log(
`All done, switch to the "${res.dir || slug}" directory to get started.`
);
console.log("Happy hacking!");
} catch (err) {
console.log(
`Something went wrong when generating your app. You may need to delete the folder at ${dir}`
);
}
}
async function hasDirectory(dir) {
try {
await fs.access(dir);
return true;
} catch (err) {
return false;
}
}
function getDir(appName) {
const slug = slugify(appName, { lower: true });
return path.join(".", slug);
}