UNPKG

4.22 kBPlain TextView Raw
1import { _h__param } from '@ctx-core/cli-args'
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'
9const exec = promisify(require('child_process').exec)
10let a1__piped
11interface Param {
12 help:string
13 dir:string
14 build:string
15 compile:string
16 clean:string
17 parallel:string
18 watch:string
19}
20export async function cli() {
21 const {
22 help,
23 dir: dir_param,
24 build: build_param,
25 compile: compile_param,
26 clean: clean_param,
27 parallel: parallel_param,
28 watch: watch_param,
29 } = _h__param(process.argv.slice(2), {
30 help: '-h, --help',
31 dir: '-d, --dir',
32 build: '-b, --build',
33 compile: '-c, --compile',
34 clean: '-l, --clean',
35 parallel: '-p, --parallel',
36 watch: '-w, --watch',
37 }) as Param
38 if (help) {
39 console.info(_help_msg())
40 process.exit(0)
41 }
42 const dir = dir_param || process.cwd()
43 const parallel = parseInt(parallel_param)
44 const opts = {
45 dir,
46 parallel,
47 }
48 a1__piped = await _a1__piped()
49 if (build_param) {
50 await enueue__fn(script, opts)
51 } else if (clean_param) {
52 await enueue__fn(clean, opts)
53 } else if (compile_param) {
54 await enueue__fn(compile, opts)
55 } else if (watch_param) {
56 await enueue__fn(compile, opts)
57 await watch(dir)
58 } else {
59 await enueue__fn(compile, opts)
60 }
61}
62function _help_msg() {
63 return `
64Usage: ${process.argv[0]} [-d] [-b] [-c] [-p <threads>] [-w]
65
66Options:
67
68-h, --help This help message
69-d, --dir Root directory
70-b, --build Rebuild the packages
71-c, --compile Compile the packages
72-p --parallel <threads> Runs in parallel with threads
73-w --watch Watch files
74 `.trim()
75}
76async function _a1__src(dir) {
77 return globby(_a1__pattern(dir), { gitignore: true })
78}
79async function enueue__fn(fn, { dir, parallel }) {
80 const a1__path__package_json = await _a1__path__package_json(dir)
81 if (parallel) {
82 const queue = _queue(parallel)
83 return Promise.all(
84 a1__path__package_json.map(
85 path__package_json=>
86 queue.add(async ()=>{
87 const ret = await fn(path__package_json)
88 console.debug(path__package_json)
89 return ret
90 })))
91 } else {
92 const a1__out = []
93 for (let i = 0; i < a1__path__package_json.length; i++) {
94 const path__package_json = a1__path__package_json[i]
95 console.debug(path__package_json)
96 a1__out.push(
97 await fn(path__package_json)
98 )
99 }
100 return a1__out
101 }
102}
103async function script(path__package_json) {
104 return await run(path__package_json, 'build')
105}
106async function clean(path__package_json) {
107 return await run(path__package_json, 'clean')
108}
109async function compile(path__package_json) {
110 return await run(path__package_json, 'compile')
111}
112async function _a1__path__package_json(dir) {
113 const a1__src =
114 a1__piped
115 ? a1__piped
116 : await _a1__src(dir)
117 const set = new Set()
118 await Promise.all(a1__src.map(async src=>{
119 const path__package_json = await _path__package_json(src)
120 if (path__package_json) {
121 set.add(path__package_json)
122 }
123 }))
124 return Array.from(set)
125}
126async function run(path__package_json, script) {
127 if (path__package_json && await exists(path__package_json)) {
128 const { stdout, stderr } =
129 await exec(`cd ${dirname(path__package_json)}; npm run ${script} --if-present`)
130 if (stdout) console.info(stdout)
131 if (stderr) console.error(stderr)
132 }
133}
134async function watch(dir) {
135 const a1__dir = await globby(_a1__pattern(dir), { gitignore: true })
136 const watcher = require('chokidar').watch(a1__dir)
137 watcher.on(
138 'change',
139 async path=>
140 compile(await _path__package_json(path)))
141}
142async function _path__package_json(path) {
143 const path__dirname = dirname(path)
144 if (path === path__dirname) return
145 const path__package_json = `${path}/package.json`
146 const path__tsconfig = `${path}/tsconfig.json`
147 if (await exists(path__package_json) && await exists(path__tsconfig)) {
148 return path__package_json
149 }
150 return await _path__package_json(path__dirname)
151}
152function _a1__pattern(dir) {
153 return [
154 `${dir}/**/*.ts`,
155 `${dir}/**/rollup.config.js`,
156 `${dir}/**/tsconfig.json`,
157 `${dir}/**/package.json`,
158 `${dir}/**/*.svelte`,
159 ]
160}