-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·150 lines (125 loc) · 4.37 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
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
#!/usr/bin/env node
import { sync } from "cross-spawn";
import {
mkdirSync,
cpSync,
readdirSync,
renameSync,
readFileSync,
writeFileSync,
} from "fs";
import { resolve, join, dirname } from "path";
import { createInterface } from "node:readline/promises";
import { fileURLToPath } from "url";
import { stdin as input, stdout as output } from "node:process";
const rl = createInterface({ input, output });
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
let [_, __, projectName, type, framework, author, license] = process.argv;
if (!projectName) {
console.log("No project name provided.");
projectName = await rl.question("Enter a project name: ");
if (!projectName) {
console.log("No project name provided.");
console.log("Exiting...");
process.exit(1);
}
}
if (!type) {
console.log("No project type provided.");
type = await rl.question(
"Enter a project type: (typescript|javascript|assemblyscript) "
);
}
if (!["typescript", "javascript", "assemblyscript"].includes(type)) {
console.log("Invalid project type.");
console.log("defaulting to typescript");
type = "typescript";
}
if (!framework) {
console.log("No framework provided.");
framework = await rl.question("Enter a framework: (dreamland|none) ");
}
if (type === "javascript" && framework === "dreamland") {
console.log("Dreamland is not supported for javascript projects.");
console.log("defaulting to none");
framework = "none";
}
if (!["dreamland", "none"].includes(framework)) {
console.log("Invalid framework.");
console.log("defaulting to none");
framework = "none";
}
if (!author) {
console.log("No author provided.");
author = await rl.question("Enter the author's name: ");
}
if (!license) {
console.log("No license provided.");
license = await rl.question("Enter a license: ");
}
// Create directory for the new project
const currentDir = process.cwd();
const projectDir = resolve(currentDir, projectName);
mkdirSync(projectDir, { recursive: true });
// Copy the static template files to the project directory
function copyTemplateFiles(templateDir) {
cpSync(templateDir, projectDir, { recursive: true });
// Dotfiles have the _DOT prefix so that they are not picked up by this
// project's configuration.
readdirSync(projectDir).forEach((file) => {
if (file.startsWith("_DOT")) {
const dotFile = file.replace("_DOT", ".");
renameSync(join(projectDir, file), join(projectDir, dotFile));
}
});
}
copyTemplateFiles(resolve(__dirname, "template"));
if (framework === "none" && type === "javascript") {
copyTemplateFiles(resolve(__dirname, "template_javascript"));
}
if (framework === "none" && type === "typescript") {
copyTemplateFiles(resolve(__dirname, "template_typescript"));
}
if (framework !== "none" && type === "typescript") {
copyTemplateFiles(resolve(__dirname, "template_ts_" + framework));
}
// Modify manifest.json
const manifestPath = join(projectDir, "public", "manifest.json");
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
manifest.package =
author.split(" ")[0].toLowerCase() +
"." +
projectName.toLowerCase().replace(/\s|\_/g, ".");
manifest.name = projectName;
manifest.wininfo.title = projectName;
writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
// Read the template information file
const info = readFileSync(join(__dirname, "info.json"), "utf8");
const { template, overrides } = JSON.parse(info);
// Update the project's package.json with the new project name
Object.assign(template, {
name: projectName,
author,
license,
description: `A new ${type} anura app`,
});
if (framework === "dreamland") {
template.dependencies["dreamland"] = "^0.0.17";
template.description += " using dreamland";
}
// Merge the template and overrides
const projectPackageJson = Object.assign({}, template, overrides[type]);
console.log(projectPackageJson);
writeFileSync(
join(projectDir, "package.json"),
JSON.stringify(projectPackageJson, null, 2)
);
// Run `npm install` in the project directory to install
// the dependencies. We are using a third-party library
// called `cross-spawn` for cross-platform support.
// (Node has issues spawning child processes in Windows).
sync("npm", ["install", "--prefix", projectName], { stdio: "inherit" });
console.log("Success! Your new project is ready.");
console.log(`Created ${projectName} at ${projectDir}`);
rl.close();