-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubik.cli.mjs
executable file
·169 lines (165 loc) · 4.96 KB
/
ubik.cli.mjs
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
#!/usr/bin/env node
/** This is file is part of "Ubik", (c) 2023 Hack.bg, available under GNU AGPL v3.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
import { resolve, relative, dirname } from 'node:path'
import { readFileSync, statSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import fastGlob from 'fast-glob'
import { console, bold, colors } from './package.mjs'
import * as Ubik from './ubik.mjs'
console
.log(colors.green('█ █ █▀▀▄ █ █ ▄▀ 2022-∞'))
.log(colors.green('█ █ █▀▀▄ █ █▀▄ '))
.log(colors.green('▀▀▀▀ ▀▀▀ ▀ ▀ ▀ '))
const argv = [...process.argv]
let interpreter = argv.shift()
let entrypoint = argv.shift()
// Dry run flag:
let dryRun = false
setDryRun() // works before command
let command = argv.shift()
setDryRun() // works after command
function setDryRun () {
if (argv[0] === '--dry') {
if (dryRun === false) {
console.info('This is a dry run. No files will be modified.')
}
dryRun = true
argv.shift()
}
}
try {
process.exit(await dispatch(command))
} catch (e) {
if (e.message) {
console.br().error(`Ubik failed:\n\n${bold(e.message)}`).br()
}
console.error(`${e.stack.slice(e.stack.indexOf('\n'))}\n`)
process.exit(2)
}
console.br()
console.log('Done.')
process.exit(0)
// Command dispatch:
async function dispatch (command) {
switch (command) {
case '--help': {
Ubik.printHelp()
return 1
}
case 'split-types': {
if (argv.length === 0) {
console.error('You did not provide any input directories (try "." or "./src").')
return 1
}
const resolver = new Ubik.Resolver()
resolver.load(argv).patch().save(dryRun)
return 0
}
case 'split-stars': {
if (argv.length === 0) {
console.error('You did not provide any arguments.')
return 1
}
const split = argv.indexOf('--')
if (split === -1) {
console.error('The command line did not contain the "--" separator.')
return 1
}
const pkgs = argv.slice(0, split)
if (pkgs.length < 1) {
console.error('You did not provide any packages.')
return 1
}
const srcs = argv.slice(split + 1)
if (srcs.length < 1) {
console.error('You did not provide any sources to process.')
return 1
}
const targets = new Set()
for (const target of srcs) {
const stats = statSync(target)
if (stats.isFile()) {
targets.add(target)
} else if (stats.isDirectory()) {
for (const file of await fastGlob(resolve(target, '**', '*.ts'))) {
targets.add(relative(process.cwd(), file))
}
} else {
throw new Error(`${target} is neither file nor directory`)
}
}
console.log('Patching:')
for (const target of targets) {
console.log(' -', target)
}
console.log(`Patching ${targets.size} files.`)
for (const path of targets) {
console.br()
console.log('Patching', bold(path))
for (const packageName of pkgs) {
Ubik.separateNamespaceImport({ path, packageName, dryRun })
}
}
return 0
}
case 'fix-import-dirs': {
if (argv.length === 0) {
console.error('You did not provide any input directories.')
return 1
}
const resolver = new Ubik.Resolver().load(argv)
Ubik.fixImportDirs(resolver, dryRun)
return 0
}
case 'merge-package': {
if (argv.length === 0) {
Ubik.printUsageOfMerge()
console.error('You did not provide any inputs.')
return 1
}
const split = argv.indexOf('--')
if (split === -1) {
Ubik.printUsageOfMerge()
console.error('The command line did not contain the "--" separator.')
return 1
}
const pkgs = argv.slice(0, split)
const dirs = argv.slice(split + 1)
if (dirs.length < 1) {
Ubik.printUsageOfMerge()
console.error('You did not provide any packages to merge.')
return 1
}
if (dirs.length < 1) {
Ubik.printUsageOfMerge()
console.error('You did not provide any sources to process.')
return 1
}
const resolver = new Ubik.Resolver().load(dirs).load(pkgs)
Ubik.redirectToRelative(resolver, pkgs, dryRun)
return 0
}
case 'make-import-map': {
Ubik.ImportMap.fromPNPM()
return 0
}
case 'compile': {
await new Ubik.Compiler(process.cwd(), { dryRun, keep: true }).compileAndPatch()
return 0
}
case 'release': {
await new Ubik.Publisher(process.cwd(), { dryRun, args: argv }).releasePackage()
return 0
}
default: {
Ubik.printUsage()
if (command) {
console.error(bold(command), 'is not a supported command. Try one of above.')
}
return 1
}
}
return 0
}