-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
executable file
·88 lines (67 loc) · 2.44 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
#!/usr/bin/env node
const { spawn } = require('child_process');
const { Select } = require('enquirer');
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const recursiveCopy = require('recursive-copy');
const isBun = (typeof(Bun) !== "undefined" || (process.env.npm_config_user_agent && process.env.npm_config_user_agent.indexOf("bun") >= 0));
function exec(args, onclose) {
const child = spawn(args.shift(), args, { shell: true });
child.stdout.on('data', function(data) {
console.log(data.toString());
});
child.on("close", onclose);
}
const prompt = new Select({
name: 'language',
message: "Which template you'd like to use?",
choices: ['TypeScript (recommended)', 'JavaScript - ESM', 'JavaScript - CommonJS', 'Haxe (experimental)']
});
prompt.run().then(language => {
let outputDir = '.';
let templateName = 'typescript';
if (language.indexOf("ESM") !== -1) {
templateName = 'esm';
} else if (language.indexOf("CommonJS") !== -1) {
templateName = 'javascript';
} else if (language.indexOf("Haxe") !== -1) {
templateName = 'haxe';
}
if (process.argv.length >= 3) {
outputDir = process.argv[2];
}
outputDir = path.resolve(outputDir);
// ensure outputFolder exists.
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
rimraf.sync(path.resolve(outputDir, '.git'));
const options = { dot: true, };
recursiveCopy(path.resolve(__dirname, "templates", templateName), outputDir, options, function (err, results) {
if (err) {
console.log(err);
console.log(err.stack);
return console.error('Copy failed: ' + err);
}
console.info(`✂️ Created ${results.length} files.`);
const pkgManager = (isBun)
? 'bun'
: (/yarn/.test(process.env.npm_execpath))
? 'yarn'
: 'npm';
const pkgManagerCmd = (/^win/.test(process.platform) && pkgManager !== "bun") ? `${pkgManager}.cmd` : pkgManager;
console.log(`📦 Installing dependencies... (${pkgManager} install)`);
// npm install with --prefix causes issues on Windows. need to enter the directory first.
process.chdir(outputDir);
process.cwd();
exec([pkgManagerCmd, "install"], function (code) {
console.log("");
console.log(`All set! ${templateName} server created at:`, outputDir);
console.log("");
console.log("⚔️ It's time to kick ass and chew bubblegum!");
});
});
}).catch(e => {
// cancelled
});