-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
76 lines (61 loc) · 2.13 KB
/
bin.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
#!/usr/bin/env node
const { defineCommand, runMain } = require("citty")
const path = require("node:path")
const fs = require("node:fs")
const pkgJson = require("./package.json")
const getFlavorPath = (flavor = "") =>
path.resolve(__dirname, "examples", flavor ? `${flavor}.eslintrc.js` : "")
const files = fs
.readdirSync(getFlavorPath())
.filter((s) => s.includes(".eslintrc.js"))
const flavors = files.map((s) => s.replace(".eslintrc.js", ""))
const flavorsString = flavors.join(", ")
const addDependency = (packageName, version, dev = false) => {
const packageJsonPath = "package.json"
const packageJsonObj = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"))
const depKey = dev ? "devDependencies" : "dependencies"
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
;(packageJsonObj[depKey] ??= {})[packageName] = `^${version}`
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJsonObj, null, 2))
}
const setupEslint = (flavor, { force, js }) => {
const filename = `.eslintrc.${js ? "js" : "cjs"}`
if (fs.existsSync(filename) && !force)
throw new Error(
"There is already an .eslintrc.js in the directory! Use -f to overwrite.",
)
fs.copyFileSync(getFlavorPath(flavor), filename)
addDependency("eslint-config-gev", pkgJson.version, true)
console.log(
'eslint-config-gev was added as a devDependency! Run "npm/yarn/pnpm/bun install" to install it!',
)
}
const main = defineCommand({
meta: {
name: pkgJson.name,
version: pkgJson.version,
description: pkgJson.description,
},
args: {
force: {
type: "boolean",
alias: "f",
description: "Overwrites the existing '.eslintrc'",
},
js: {
type: "boolean",
description: "Use '.eslintrc.js' instead of '.eslintrc.cjs'",
},
flavor: {
type: "positional",
required: true,
description: `Your project kind. One of these: ${flavorsString}`,
},
},
run: ({ args: { flavor, force, js } }) => {
if (!flavors.includes(flavor))
throw new Error(`The flavor must be one of these: ${flavorsString}`)
setupEslint(flavor, { force, js })
},
})
runMain(main)