jiek 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,7 +8,8 @@ import { execaCommand } from 'execa'
8
8
 
9
9
  import { actionDone, actionRestore } from '../inner'
10
10
  import type { RollupProgressEvent, TemplateOptions } from '../rollup/base'
11
- import { getSelectedProjectsGraph } from '../utils/filterSupport'
11
+ import type { ProjectsGraph } from '../utils/filterSupport'
12
+ import { filterPackagesGraph } from '../utils/filterSupport'
12
13
  import { loadConfig } from '../utils/loadConfig'
13
14
  import { tsRegisterName } from '../utils/tsRegister'
14
15
 
@@ -59,127 +60,135 @@ program
59
60
  actionRestore()
60
61
  const { build } = loadConfig()
61
62
  silent = silent ?? build?.silent ?? false
62
- const {
63
- wd,
64
- value = {}
65
- } = await getSelectedProjectsGraph() ?? {}
66
-
67
- if (Object.keys(value).length === 0) {
68
- throw new Error('no package found')
69
- }
70
- const wdNodeModules = path.resolve(wd, 'node_modules')
71
- if (!fs.existsSync(wdNodeModules)) {
72
- fs.mkdirSync(wdNodeModules)
73
- }
74
- const jiekTempDir = (...paths: string[]) => path.resolve(wdNodeModules, '.jiek', ...paths)
75
- if (!fs.existsSync(jiekTempDir())) {
76
- fs.mkdirSync(jiekTempDir())
77
- }
78
63
 
79
- const rollupBinaryPath = require.resolve('rollup')
80
- .replace(/dist\/rollup.js$/, 'dist/bin/rollup')
81
64
  const multiBars = new MultiBar({
82
65
  clearOnComplete: false,
83
66
  hideCursor: true,
84
- format: '- {bar} | {status} | {input} | {message}'
67
+ format: '- {bar} | {status} | {pkgName} | {input} | {message}'
85
68
  }, Presets.shades_classic)
86
- let i = 0
87
- await Promise.all(
88
- Object.entries(value).map(async ([dir, manifest]) => {
89
- // TODO support auto build child packages in workspaces
90
- const escapeManifestName = manifest.name?.replace(/^@/g, '').replace(/\//g, '+')
91
- const configFile = jiekTempDir(
92
- `${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
93
- )
94
- fs.writeFileSync(configFile, FILE_TEMPLATE(manifest))
95
- let prefix = ''
96
- if (tsRegisterName) {
97
- prefix = `node -r ${tsRegisterName} `
98
- }
99
- const command = `${prefix}${rollupBinaryPath} --silent -c ${configFile}`
100
- const child = execaCommand(command, {
101
- ipc: true,
102
- cwd: dir,
103
- env: {
104
- ...process.env,
105
- JIEK_ROOT: wd,
106
- JIEK_ENTRIES: entries,
107
- JIEK_WITHOUT_JS: String(withoutJs),
108
- JIEK_WITHOUT_DTS: String(withoutDts)
69
+
70
+ const buildPackage = async ({
71
+ wd,
72
+ value = {}
73
+ }: ProjectsGraph) => {
74
+ if (Object.keys(value).length === 0) {
75
+ throw new Error('no package found')
76
+ }
77
+ const wdNodeModules = path.resolve(wd, 'node_modules')
78
+ if (!fs.existsSync(wdNodeModules)) {
79
+ fs.mkdirSync(wdNodeModules)
80
+ }
81
+ const jiekTempDir = (...paths: string[]) => path.resolve(wdNodeModules, '.jiek', ...paths)
82
+ if (!fs.existsSync(jiekTempDir())) {
83
+ fs.mkdirSync(jiekTempDir())
84
+ }
85
+
86
+ const rollupBinaryPath = require.resolve('rollup')
87
+ .replace(/dist\/rollup.js$/, 'dist/bin/rollup')
88
+ let i = 0
89
+ await Promise.all(
90
+ Object.entries(value).map(async ([dir, manifest]) => {
91
+ // TODO support auto build child packages in workspaces
92
+ const escapeManifestName = manifest.name?.replace(/^@/g, '').replace(/\//g, '+')
93
+ const configFile = jiekTempDir(
94
+ `${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
95
+ )
96
+ fs.writeFileSync(configFile, FILE_TEMPLATE(manifest))
97
+ let prefix = ''
98
+ if (tsRegisterName) {
99
+ prefix = `node -r ${tsRegisterName} `
109
100
  }
110
- })
111
- const bars: Record<string, ReturnType<typeof multiBars.create>> = {}
112
- let inputMaxLen = 10
113
- child.on('message', (e: RollupProgressEvent) => {
114
- if (e.type === 'debug') console.log(...(Array.isArray(e.data) ? e.data : [e.data]))
115
- })
116
- !silent && child.on('message', (e: RollupProgressEvent) => {
117
- if (e.type === 'init') {
118
- const { leafMap, targetsLength } = e.data
119
- const leafs = Array
120
- .from(leafMap.entries())
121
- .flatMap(([input, pathAndCondiions]) =>
122
- pathAndCondiions.map(([path, ...conditions]) => ({
123
- input,
124
- path,
125
- conditions
126
- }))
127
- )
128
- console.log(`Package '${manifest.name}' has ${targetsLength} targets to build`)
129
- leafs.forEach(({ input }) => {
130
- inputMaxLen = Math.max(inputMaxLen, input.length)
131
- })
132
- leafs.forEach(({ input, path }) => {
133
- const key = `${input}:${path}`
134
- if (bars[key]) return
135
- bars[key] = multiBars.create(50, 0, {
136
- input: input.padEnd(inputMaxLen),
137
- status: 'waiting'.padEnd(10)
138
- }, {
139
- barsize: 20,
140
- linewrap: true
101
+ const command = `${prefix}${rollupBinaryPath} --silent -c ${configFile}`
102
+ const child = execaCommand(command, {
103
+ ipc: true,
104
+ cwd: dir,
105
+ env: {
106
+ ...process.env,
107
+ JIEK_ROOT: wd,
108
+ JIEK_ENTRIES: entries,
109
+ JIEK_WITHOUT_JS: String(withoutJs),
110
+ JIEK_WITHOUT_DTS: String(withoutDts)
111
+ }
112
+ })
113
+ const bars: Record<string, ReturnType<typeof multiBars.create>> = {}
114
+ let inputMaxLen = 10
115
+ child.on('message', (e: RollupProgressEvent) => {
116
+ if (e.type === 'debug') console.log(...(Array.isArray(e.data) ? e.data : [e.data]))
117
+ })
118
+ !silent && child.on('message', (e: RollupProgressEvent) => {
119
+ if (e.type === 'init') {
120
+ const { leafMap, targetsLength } = e.data
121
+ const leafs = Array
122
+ .from(leafMap.entries())
123
+ .flatMap(([input, pathAndCondiions]) =>
124
+ pathAndCondiions.map(([path, ...conditions]) => ({
125
+ input,
126
+ path,
127
+ conditions
128
+ }))
129
+ )
130
+ console.log(`Package '${manifest.name}' has ${targetsLength} targets to build`)
131
+ leafs.forEach(({ input }) => {
132
+ inputMaxLen = Math.max(inputMaxLen, input.length)
133
+ })
134
+ leafs.forEach(({ input, path }) => {
135
+ const key = `${input}:${path}`
136
+ if (bars[key]) return
137
+ bars[key] = multiBars.create(50, 0, {
138
+ pkgName: manifest.name,
139
+ input: input.padEnd(inputMaxLen),
140
+ status: 'waiting'.padEnd(10)
141
+ }, {
142
+ barsize: 20,
143
+ linewrap: true
144
+ })
141
145
  })
146
+ }
147
+ if (e.type === 'progress') {
148
+ const {
149
+ path,
150
+ tags,
151
+ input,
152
+ event,
153
+ message
154
+ } = e.data
155
+ const bar = bars[`${input}:${path}`]
156
+ if (!bar) return
157
+ bar.update(
158
+ {
159
+ start: 0,
160
+ resolve: 20,
161
+ end: 50
162
+ }[event ?? 'start'] ?? 0,
163
+ {
164
+ input: input.padEnd(inputMaxLen),
165
+ status: event?.padEnd(10),
166
+ message: `${tags?.join(', ')}: ${message}`
167
+ }
168
+ )
169
+ }
170
+ })
171
+ await new Promise<void>((resolve, reject) => {
172
+ let errorStr = ''
173
+ child.stderr?.on('data', (data) => {
174
+ errorStr += data
142
175
  })
143
- }
144
- if (e.type === 'progress') {
145
- const {
146
- path,
147
- tags,
148
- input,
149
- event,
150
- message
151
- } = e.data
152
- const bar = bars[`${input}:${path}`]
153
- if (!bar) return
154
- bar.update(
155
- {
156
- start: 0,
157
- resolve: 20,
158
- end: 50
159
- }[event ?? 'start'] ?? 0,
160
- {
161
- input: input.padEnd(inputMaxLen),
162
- status: event?.padEnd(10),
163
- message: `${tags?.join(', ')}: ${message}`
164
- }
165
- )
166
- }
167
- })
168
- await new Promise<void>((resolve, reject) => {
169
- let errorStr = ''
170
- child.stderr?.on('data', (data) => {
171
- errorStr += data
176
+ child.once('exit', (code) =>
177
+ code === 0
178
+ ? resolve()
179
+ : reject(new Error(`rollup build failed:\n${errorStr}`)))
180
+ verbose && child.stdout?.pipe(process.stdout)
172
181
  })
173
- child.once('exit', (code) =>
174
- code === 0
175
- ? resolve()
176
- : reject(new Error(`rollup build failed:\n${errorStr}`)))
177
- verbose && child.stdout?.pipe(process.stdout)
178
182
  })
183
+ )
184
+ }
185
+ const filters: string[] = program.getOptionValue('filter').split(',')
186
+ const packages = await filterPackagesGraph(filters)
187
+ await Promise
188
+ .all(packages.map(buildPackage))
189
+ .finally(() => {
190
+ multiBars.stop()
179
191
  })
180
- ).finally(() => {
181
- multiBars.stop()
182
- })
183
192
 
184
193
  actionDone()
185
194
  })
@@ -18,10 +18,10 @@ try {
18
18
  } catch { /* empty */ }
19
19
  if (type !== '') {
20
20
  program
21
- .option('-f, --filter <filter>', 'filter packages')
21
+ .option('-f, --filter <filter>', 'filter packages, support fuzzy match and array. e.g. -f core,utils')
22
22
  }
23
23
 
24
- interface ProjectsGraph {
24
+ export interface ProjectsGraph {
25
25
  wd: string
26
26
  root: string
27
27
  value?: Record<string, {
@@ -31,8 +31,13 @@ interface ProjectsGraph {
31
31
  }>
32
32
  }
33
33
 
34
- export async function getSelectedProjectsGraph(): Promise<ProjectsGraph> {
35
- let filter = program.getOptionValue('filter')
34
+ export function filterPackagesGraph(filters: string[]): Promise<ProjectsGraph[]> {
35
+ return Promise.all(filters.map(async filter => getSelectedProjectsGraph(filter)))
36
+ }
37
+
38
+ export async function getSelectedProjectsGraph(
39
+ filter = program.getOptionValue('filter')
40
+ ): Promise<ProjectsGraph> {
36
41
  const root = getRoot()
37
42
  const { wd, notWorkspace } = getWD()
38
43
  if (!notWorkspace && type === 'pnpm') {