Repository navigation
Expand file tree
/
Copy pathinit-game.js
More file actions
executable file
·64 lines (50 loc) · 2.13 KB
/
Copy pathinit-game.js
File metadata and controls
executable file
·64 lines (50 loc) · 2.13 KB
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
#!/usr/bin/env node
'use strict';
const path = require("path");
const fs = require("fs");
const expectedNodeMinVersionMajor = 18;
const currentNodeVersionMajor = process.versions.node?.split('.')?.[0];
if (isNaN(Number(currentNodeVersionMajor)) || Number(currentNodeVersionMajor) < expectedNodeMinVersionMajor) {
console.warn(
`This tool is supposed to be run under Node.js ${expectedNodeMinVersionMajor} or newer. Currently run under Node.js version: ${process.version}.`
);
process.exit(1);
}
if (process.argv.length < 3) {
console.warn("Project name has to be provided as a first argument.");
process.exit(1);
}
const projectName = process.argv[2];
const projectNameConstraints = /^[-_0-9a-zA-Z]+$/;
if (!projectNameConstraints.test(projectName)) {
console.warn(`Project name has to match a following RegExp: ${projectNameConstraints.toString()}`);
process.exit(1);
}
const projectPath = path.resolve(process.cwd(), projectName);
if (fs.existsSync(projectPath)) {
console.warn(`Cannot create the project. Its folder already exists: ${projectPath}`);
process.exit(1);
}
console.log(`Creating a new BeetPx project in: ${projectPath} ...`);
fs.mkdirSync(projectPath);
fs.cpSync(
path.resolve(__dirname, "initial-project-files"),
projectPath,
{recursive: true},
);
// A fix for `npm publish` not including the `.gitignore`: let's name it differently
// (here: `_gitignore`) and then rename back to `.gitignore` during the project init.
fs.renameSync(
path.resolve(projectPath, "_gitignore"),
path.resolve(projectPath, ".gitignore"),
);
injectProjectName(projectName, path.resolve(projectPath, "package.json"));
injectProjectName(projectName, path.resolve(projectPath, "README.md"));
injectProjectName(projectName, path.resolve(projectPath, "src/main.ts"));
console.log(`Done!`);
/////////////////////////////////////////////////////////////////////////////
function injectProjectName(projectName, filePath) {
let content = fs.readFileSync(filePath, {encoding: "utf8"});
content = content.replace(/__PROJECT_NAME__/g, projectName);
fs.writeFileSync(filePath, content, {encoding: "utf8"});
}