-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·112 lines (88 loc) · 2.66 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
#!/usr/bin/env node
// @ts-check
// A fork of https://github.com/c8r/initit/blob/master/index.js
// which is MIT licensed
// Changes:
// - Less dependencies
// - Ability for me to define the branch and the depth to extract from a zip
// - yarn not npm
const path = require("path")
const os = require("os")
const fs = require("fs")
const exec = require("child_process").execSync
const spawn = require("cross-spawn")
const install = () => {
return new Promise((resolve, reject) => {
const child = spawn("yarn", ["install"], {
stdio: "inherit",
})
child.on("close", code => {
if (code !== 0) {
reject()
return
}
resolve()
})
})
}
const gitInit = () => {
exec("git --version", { stdio: "inherit" })
exec("git init", { stdio: "inherit" })
exec("git add .", { stdio: "inherit" })
exec('git commit -am "Init"', { stdio: "inherit" })
return true
}
const branch = "v2"
const vLess = 2
const getTar = ({ user, repo, path = "", name }) => {
const url = `https://codeload.github.com/${user}/${repo}/tar.gz/${branch}`
const cmd = `curl ${url} | tar -xz -C ${name} --strip=4 ${repo}-${vLess}/${path}`
exec(cmd, { stdio: "inherit" })
}
const create = async (opts = {}) => {
if (!opts.name) {
throw new Error("name argument required")
}
if (!opts.template) {
throw new Error("template argument required")
}
const dirname = path.resolve(opts.name)
const name = path.basename(dirname)
const [user, repo, ...paths] = opts.template.split("/")
if (!fs.existsSync(name)) {
fs.mkdirSync(name)
}
getTar(
Object.assign({}, opts, {
name,
user,
repo,
path: paths.join("/"),
})
)
const templatePkg = require(path.join(dirname, "package.json"))
const pkg = Object.assign({}, templatePkg, {
name,
version: "0.0.1",
})
fs.writeFileSync(path.join(dirname, "package.json"), JSON.stringify(pkg, null, 2) + os.EOL)
process.chdir(dirname)
await install()
gitInit()
const readmePath = path.join(dirname, "README.md")
const README = fs.readFileSync(readmePath, "utf8")
README.replace(/\[name\]/g, opts.name)
fs.writeFileSync(readmePath, README)
console.log("\nAlright, you're good to go!\n")
console.log("To get started:")
console.log(` - code ${name}`)
console.log(` - cd ${name}`)
console.log(` - yarn start`)
console.log(
"\nCome and ask questions to other plugin authors in the TypeScript Community Discord: https://discord.gg/typescript"
)
return { name, dirname }
}
const [name] = process.argv.slice(2)
const template = "microsoft/TypeScript-Website/packages/create-typescript-playground-plugin/template/"
create({ name, template })