generated from markthree/deno-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
172 lines (161 loc) · 4.17 KB
/
mod.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {
brightGreen,
brightYellow,
Command,
emptyDir,
ensureFile,
EnumType,
gray,
yellow,
} from "./src/deps.ts"
import {
ensurePackageJson,
existsFile,
findUpDetectPM,
findUpLock,
findUpNodeModulesPath,
getPackageCommands,
PM_LOCKS,
} from "./src/pm.ts"
import { execa, execaInstall } from "./src/process.ts"
import { version } from "./src/version.ts"
import type { Options, PM } from "./src/type.ts"
function formatOptions(originOptions: Options) {
const options = Object.keys(originOptions).filter((k) => {
if (typeof originOptions[k] === "boolean") {
return true
}
return false
})
if (originOptions.dir) {
options.push(`--dir=${originOptions.dir}`)
}
return options.map((o) => {
if (o === "dev") {
return "-D"
}
if (o === "prod") {
return "-P"
}
return o
})
}
if (import.meta.main) {
const pm = await findUpDetectPM()
const commander = new Command()
.name("n")
.version(version)
.description(`Command line tool created by deno to manage node projects`)
.action(async () => {
await ensurePackageJson()
execaInstall(pm)
})
const packageCommands = await getPackageCommands()
if (packageCommands) {
Object.keys(packageCommands).forEach((ck) => {
const cv = packageCommands[ck]
const runCommand = new Command().description(
`${gray(cv)}`,
).action(async () => {
await execa([pm, "run", ck])
})
commander.command(ck, runCommand)
})
}
const install = new Command()
.alias("install")
.description(`${brightGreen(pm)} install deps`)
.option("-g, --global", "Global installation")
.option("-C, --dir <dir:string>", "Change to directory <dir>")
.option(
`-P, --prod`,
`Packages in ${brightYellow(`devDependencies`)} won't be installed`,
)
.option(
"-w, --workspace-root",
`Run the command on the root workspace project ${
brightYellow("(only pnpm)")
}`,
)
.option(
`-D, --dev`,
`Only ${
brightYellow(`devDependencies`)
} are installed regardless of the ${brightGreen(`NODE_ENV`)}`,
{
conflicts: [`prod`],
},
)
.option(
"-r, --recursive",
`Run the command for each project in the workspace ${
brightYellow("(only pnpm)")
}`,
)
.arguments("[...deps:string]")
.action(
async (options, ...deps) => {
await execaInstall(pm, deps, formatOptions(options))
},
)
const reinstall = new Command().alias("reinstall")
.description(`${brightGreen(pm)} reinstall deps`).option(
"-w, --withLock",
"with lock",
{
default: false,
},
).action(
async ({ withLock }) => {
if (withLock) {
const lock = await findUpLock(pm)
if (lock) {
await Deno.remove(lock)
}
}
const node_modules_path = await findUpNodeModulesPath()
if (node_modules_path) {
await emptyDir(node_modules_path)
console.log(
`\n${brightGreen("√ clean")} ${gray(node_modules_path)} \n`,
)
}
await execaInstall(pm)
},
)
const PMS = Object.keys(PM_LOCKS)
const PM_TYPE = new EnumType(PMS)
const _switch = new Command().alias("switch").description(
`switch ${brightGreen(pm)} to ${
PMS.filter((p) => p !== pm).map((p) => yellow(p)).join(" or ")
}`,
).type(
"PM_TYPE",
PM_TYPE,
).arguments("<pm:PM_TYPE>").action(async (_, newPM) => {
const existedLock = PM_LOCKS[pm]
if (await existsFile(existedLock)) {
await Deno.remove(existedLock)
}
const newLock = PM_LOCKS[newPM as PM]
await ensureFile(newLock)
})
const init = new Command().alias("init").description("init new project")
.type(
"PM_TYPE",
PM_TYPE,
).arguments("<pm:PM_TYPE>")
.action(async (_, newPM) => {
const newLock = PM_LOCKS[newPM as PM]
await Promise.all([
ensureFile(newLock),
ensurePackageJson(),
])
})
await commander
.command("in", init)
.command("i", install)
.command("ri", reinstall)
.command("sw", _switch)
.parse(Deno.args)
}