UNPKG

3.65 kBJavaScriptView Raw
1import commander from 'commander'
2const fs = require('fs')
3import { promisify } from 'util'
4import { dirname } from 'path'
5const exists = promisify(fs.exists)
6const globby = require('globby')
7import { _queue } from '@ctx-core/queue'
8import { _a1__piped } from '@ctx-core/pipe'
9import { info, error } from '@ctx-core/logger'
10const exec = promisify(require('child_process').exec)
11const a1__pattern = [
12 `${_dir()}/**/*.ts`,
13 `${_dir()}/**/rollup.config.js`,
14 `${_dir()}/**/tsconfig.json`,
15 `${_dir()}/**/package.json`,
16 `${_dir()}/**/*.svelte`,
17]
18let a1__piped
19export 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}
45async function _a1__src() {
46 return globby(a1__pattern, { gitignore: true })
47}
48async 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}
73async function script(path__package_json) {
74 return await run(path__package_json, 'build')
75}
76async function clean(path__package_json) {
77 return await run(path__package_json, 'clean')
78}
79async function compile(path__package_json) {
80 return await run(path__package_json, 'compile')
81}
82async 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}
96async 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}
104async 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}
112async 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}
122function _dir() {
123 return commander.dir || process.cwd()
124}
125