1 | import commander from 'commander'
|
2 | const fs = require('fs')
|
3 | import { promisify } from 'util'
|
4 | import { dirname } from 'path'
|
5 | const exists = promisify(fs.exists)
|
6 | const globby = require('globby')
|
7 | import { _queue } from '@ctx-core/queue'
|
8 | import { _a1__piped } from '@ctx-core/pipe'
|
9 | import { info, error } from '@ctx-core/logger'
|
10 | const exec = promisify(require('child_process').exec)
|
11 | const a1__pattern = [
|
12 | `${_dir()}/**/*.ts`,
|
13 | `${_dir()}/**/rollup.config.js`,
|
14 | `${_dir()}/**/tsconfig.json`,
|
15 | `${_dir()}/**/package.json`,
|
16 | `${_dir()}/**/*.svelte`,
|
17 | ]
|
18 | let a1__piped
|
19 | export async function cli(opts = {}) {
|
20 | const {
|
21 | _path__package_json = _path__package_json,
|
22 | } = opts
|
23 | commander
|
24 | .option('-d --dir <root-directory>', 'Root directory')
|
25 | .option('-b --build', 'rebuild the packages')
|
26 | .option('-c --compile', 'compile the packages')
|
27 | .option('-l --clean', 'clean the packages')
|
28 | .option('-p --parallel <threads>', 'runs in parallel with threads')
|
29 | .option('-w --watch', 'Watch files')
|
30 | .parse(process.argv)
|
31 | a1__piped = await _a1__piped()
|
32 | if (commander.build) {
|
33 | await enueue__fn(script)
|
34 | } else if (commander.clean) {
|
35 | await enueue__fn(clean)
|
36 | } else if (commander.compile) {
|
37 | await enueue__fn(compile)
|
38 | } else if (commander.watch) {
|
39 | await enueue__fn(compile)
|
40 | await watch()
|
41 | } else {
|
42 | await enueue__fn(compile)
|
43 | }
|
44 | }
|
45 | async function _a1__src() {
|
46 | return globby(a1__pattern, { gitignore: true })
|
47 | }
|
48 | async function enueue__fn(fn) {
|
49 | const a1__path__package_json = await _a1__path__package_json()
|
50 | const parallel = parseInt(commander.parallel)
|
51 | if (parallel) {
|
52 | const queue = _queue(parallel)
|
53 | return Promise.all(
|
54 | a1__path__package_json.map(
|
55 | path__package_json =>
|
56 | queue.add(async () => {
|
57 | const ret = await fn(path__package_json)
|
58 | console.debug(path__package_json)
|
59 | return ret
|
60 | })))
|
61 | } else {
|
62 | const a1__out = []
|
63 | for (let i = 0; i < a1__path__package_json.length; i++) {
|
64 | const path__package_json = a1__path__package_json[i]
|
65 | console.debug(path__package_json)
|
66 | a1__out.push(
|
67 | await fn(path__package_json)
|
68 | )
|
69 | }
|
70 | return a1__out
|
71 | }
|
72 | }
|
73 | async function script(path__package_json) {
|
74 | return await run(path__package_json, 'build')
|
75 | }
|
76 | async function clean(path__package_json) {
|
77 | return await run(path__package_json, 'clean')
|
78 | }
|
79 | async function compile(path__package_json) {
|
80 | return await run(path__package_json, 'compile')
|
81 | }
|
82 | async function _a1__path__package_json() {
|
83 | const a1__src =
|
84 | a1__piped
|
85 | ? a1__piped
|
86 | : await _a1__src()
|
87 | const set = new Set()
|
88 | await Promise.all(a1__src.map(async src => {
|
89 | const path__package_json = await _path__package_json(src)
|
90 | if (path__package_json) {
|
91 | set.add(path__package_json)
|
92 | }
|
93 | }))
|
94 | return Array.from(set)
|
95 | }
|
96 | async function run(path__package_json, script) {
|
97 | if (path__package_json && await exists(path__package_json)) {
|
98 | const { stdout, stderr } =
|
99 | await exec(`cd ${dirname(path__package_json)}; npm run ${script} --if-present`)
|
100 | if (stdout) console.info(stdout)
|
101 | if (stderr) console.error(stderr)
|
102 | }
|
103 | }
|
104 | async function watch() {
|
105 | const a1__dir = await globby(a1__pattern, { gitignore: true })
|
106 | const watcher = require('chokidar').watch(a1__dir)
|
107 | watcher.on(
|
108 | 'change',
|
109 | async path =>
|
110 | compile(await _path__package_json(path)))
|
111 | }
|
112 | async function _path__package_json(path) {
|
113 | const path__dirname = dirname(path)
|
114 | if (path === path__dirname) return
|
115 | const path__package_json = `${path}/package.json`
|
116 | const path__tsconfig = `${path}/tsconfig.json`
|
117 | if (await exists(path__package_json) && await exists(path__tsconfig)) {
|
118 | return path__package_json
|
119 | }
|
120 | return await _path__package_json(path__dirname)
|
121 | }
|
122 | function _dir() {
|
123 | return commander.dir || process.cwd()
|
124 | }
|
125 |
|