@jujulego/jill 3.0.0-alpha.3 → 3.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -1,33 +1,2110 @@
1
- ;{try{(function(){var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="8a2485f3-b592-4c76-b67b-06ad641e7844",e._sentryDebugIdIdentifier="sentry-dbid-8a2485f3-b592-4c76-b67b-06ad641e7844");})();}catch(e){}};!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};e.SENTRY_RELEASE={id:"3.0.0-alpha.3"};}catch(e){}}();import { inject$ } from '@kyrielle/injector';
2
- import { startSpan, captureMessage, captureException } from '@sentry/node';
3
- import process from 'node:process';
1
+ ;{try{(function(){var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="3715da3f-f37a-4cd7-8874-575f86356b2f",e._sentryDebugIdIdentifier="sentry-dbid-3715da3f-f37a-4cd7-8874-575f86356b2f");})();}catch(e){}};!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};e.SENTRY_RELEASE={id:"3.0.0-alpha.4"};}catch(e){}}();import { token$, inject$, asyncScope$ } from '@kyrielle/injector';
2
+ import { startSpan, getActiveSpan, updateSpanName, getRootSpan, startInactiveSpan, captureException } from '@sentry/node';
3
+ import process$1 from 'node:process';
4
4
  import { hideBin } from 'yargs/helpers';
5
- import { e as executeParser, L as LOGGER, C as ClientError } from './parser.js';
6
- import '@jujulego/tasks';
7
- import 'kyrielle';
8
- import '@kyrielle/logger';
9
- import 'node:path';
10
- import 'node:child_process';
11
- import 'chalk';
12
- import 'semver';
13
- import 'slugify';
14
- import 'yargs';
15
- import 'node:fs';
16
- import 'path-scurry';
17
- import '@swc/helpers/_/_apply_decs_2203_r';
18
- import 'glob';
19
- import 'normalize-package-data';
20
- import 'moo';
21
- import '@jujulego/quick-tag';
22
- import 'ajv';
23
- import 'node:os';
24
- import 'cosmiconfig';
25
- import 'chalk-template';
5
+ import { pipe$, waitFor$, filter$, observable$, off$, once$, map$, collect$, asyncIterator$, var$, flow$ } from 'kyrielle';
6
+ import yargs from 'yargs';
7
+ import { TaskManager, plan, SpawnTask, GroupTask, TaskSet, ParallelGroup, FallbackGroup, SequenceGroup } from '@jujulego/tasks';
8
+ import { logger$, withTimestamp, withLabel, qLogDelay, LogLevel, LogGateway, logDelay$, toStderr } from '@kyrielle/logger';
9
+ import fs from 'node:fs';
10
+ import { PathScurry } from 'path-scurry';
11
+ import { _ } from '@swc/helpers/_/_apply_decs_2203_r';
12
+ import path from 'node:path';
13
+ import { Glob } from 'glob';
14
+ import normalize from 'normalize-package-data';
15
+ import { satisfies, compare, parse } from 'semver';
16
+ import moo from 'moo';
17
+ import cp from 'node:child_process';
18
+ import chalk from 'chalk';
19
+ import slugify from 'slugify';
20
+ import { qjson, qprop, q$, defineQuickFormat, qerror, qarg, qwrap } from '@jujulego/quick-tag';
21
+ import Ajv from 'ajv';
22
+ import os from 'node:os';
23
+ import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
24
+ import { chalkTemplateStderr } from 'chalk-template';
25
+
26
+ var version = "3.0.0-alpha.4";
27
+
28
+ function trace(fun, opts) {
29
+ const name = typeof opts === 'string' ? opts : opts.name;
30
+ const use = typeof opts === 'object' ? opts.use : (_, r)=>r;
31
+ return function(...args) {
32
+ return pipe$(startSpan({
33
+ name
34
+ }, ()=>fun.call(this, ...args)), (r)=>use(name, r));
35
+ };
36
+ }
37
+ function instrument(opts) {
38
+ return (target, context)=>{
39
+ const name = typeof opts === 'string' ? opts : opts?.name ?? context.name.toString();
40
+ const use = typeof opts === 'object' ? opts.use : (_, r)=>r;
41
+ return trace(target, {
42
+ name,
43
+ use
44
+ });
45
+ };
46
+ }
47
+ function traceAsyncGenerator(name, generator) {
48
+ const instrumented = {
49
+ ...generator,
50
+ next: async ()=>startSpan({
51
+ name,
52
+ op: 'iterator.next'
53
+ }, ()=>generator.next()),
54
+ [Symbol.asyncIterator]: ()=>instrumented
55
+ };
56
+ return instrumented;
57
+ }
58
+ function traceImport(name, loader) {
59
+ return startSpan({
60
+ name: `load ${name}`,
61
+ op: 'resource.script'
62
+ }, loader);
63
+ }
64
+
65
+ function command$5(module) {
66
+ const name = getCommandName(module);
67
+ const handler = trace(module.handler, 'cli.handler');
68
+ return (parser)=>parser.command({
69
+ ...module,
70
+ async handler (args) {
71
+ const activeSpan = getActiveSpan();
72
+ if (activeSpan) {
73
+ updateSpanName(getRootSpan(activeSpan), `jill ${name}`);
74
+ }
75
+ await handler(args);
76
+ }
77
+ });
78
+ }
79
+ function getCommandName(module) {
80
+ if (!module.command) {
81
+ return '[unknown]';
82
+ }
83
+ if (typeof module.command === 'string') {
84
+ return module.command;
85
+ }
86
+ return module.command[0];
87
+ }
88
+
89
+ // Tokens
90
+ const CONFIG = token$('Config', async ()=>{
91
+ const { ConfigService } = await Promise.resolve().then(function () { return config_service; });
92
+ return waitFor$(inject$(ConfigService, asyncScope$()).config$);
93
+ });
94
+ const CWD = token$('cwd', ()=>process$1.cwd());
95
+ const LOGGER = token$('Logger', ()=>logger$(withTimestamp()));
96
+ const PATH_SCURRY = token$('PathScurry', ()=>new PathScurry('/', {
97
+ fs
98
+ }));
99
+ const TASK_MANAGER = token$('TaskManager', async ()=>{
100
+ const manager = new TaskManager({
101
+ jobs: (await inject$(CONFIG, asyncScope$())).jobs,
102
+ logger: inject$(LOGGER)
103
+ });
104
+ let rootSpan = getActiveSpan();
105
+ if (rootSpan) rootSpan = getRootSpan(rootSpan);
106
+ const spans = new Map();
107
+ manager.events$.on('added', (task)=>{
108
+ // Main span
109
+ const span = startInactiveSpan({
110
+ name: task.name,
111
+ parentSpan: (task.group && spans.get(task.group.id)) ?? rootSpan,
112
+ op: 'task'
113
+ });
114
+ task.events$.on('completed', ({ status })=>{
115
+ span.setStatus({
116
+ code: status === 'done' ? 1 : 2
117
+ });
118
+ span.end();
119
+ });
120
+ spans.set(task.id, span);
121
+ // Status spans
122
+ let statusSpan = startInactiveSpan({
123
+ name: task.status,
124
+ parentSpan: span,
125
+ op: 'task.status'
126
+ });
127
+ task.events$.on('status', ({ status })=>{
128
+ statusSpan.end();
129
+ if (!task.completed) {
130
+ statusSpan = startInactiveSpan({
131
+ name: status,
132
+ parentSpan: span,
133
+ op: 'task.status'
134
+ });
135
+ }
136
+ });
137
+ });
138
+ return manager;
139
+ });
140
+
141
+ // Utils
142
+ function printJson(data, stream = process.stdout) {
143
+ if (stream.isTTY) {
144
+ stream.write(JSON.stringify(data, null, 2));
145
+ } else {
146
+ stream.write(JSON.stringify(data));
147
+ }
148
+ }
149
+
150
+ // Utils
151
+ function executeCommand(module) {
152
+ const prepare = trace(module.prepare, 'cli.prepare');
153
+ const execute = module.execute && trace(module.execute, 'cli.execute');
154
+ return command$5({
155
+ ...module,
156
+ builder (base) {
157
+ const parser = withPlanMode(base);
158
+ if (module.builder) {
159
+ return module.builder(parser);
160
+ } else {
161
+ return parser;
162
+ }
163
+ },
164
+ async handler (args) {
165
+ const tasks = await prepare(args);
166
+ if (args.plan) {
167
+ if (args.planMode === 'json') {
168
+ printJson(Array.from(plan(tasks)));
169
+ } else {
170
+ const { default: TaskPlanInk } = await traceImport('TaskPlanInk', ()=>import('./task-plan.ink.js'));
171
+ await TaskPlanInk({
172
+ tasks
173
+ });
174
+ }
175
+ } else {
176
+ if (execute) {
177
+ await execute(args, tasks);
178
+ } else if (tasks.tasks.length > 0) {
179
+ const { default: TaskExecInk } = await traceImport('TaskExecInk', ()=>import('./task-exec.ink.js'));
180
+ await TaskExecInk({
181
+ tasks,
182
+ verbose: [
183
+ 'verbose',
184
+ 'debug'
185
+ ].includes(args.verbose)
186
+ });
187
+ } else {
188
+ const logger = inject$(LOGGER);
189
+ logger.warning('No task found');
190
+ process$1.exitCode = 1;
191
+ }
192
+ }
193
+ }
194
+ });
195
+ }
196
+ function planCommand(module, tasks$) {
197
+ if ('prepare' in module) {
198
+ const prepare = trace(module.prepare, 'cli.prepare');
199
+ return command$5({
200
+ ...module,
201
+ builder (base) {
202
+ const parser = withPlanMode(base);
203
+ if (module.builder) {
204
+ return module.builder(parser);
205
+ } else {
206
+ return parser;
207
+ }
208
+ },
209
+ async handler (args) {
210
+ tasks$.mutate(await prepare(args));
211
+ }
212
+ });
213
+ } else {
214
+ return command$5({
215
+ ...module,
216
+ handler: ()=>undefined
217
+ });
218
+ }
219
+ }
220
+ // Utils
221
+ function withPlanMode(parser) {
222
+ return parser.option('plan', {
223
+ type: 'boolean',
224
+ default: false,
225
+ describe: 'Only prints tasks to be run'
226
+ }).option('plan-mode', {
227
+ type: 'string',
228
+ desc: 'Plan output mode',
229
+ choices: [
230
+ 'json',
231
+ 'list'
232
+ ],
233
+ default: 'list'
234
+ });
235
+ }
236
+
237
+ // Filter
238
+ function hasSomeScript$(scripts) {
239
+ return filter$((wks)=>{
240
+ if (!wks.manifest.scripts) {
241
+ return false;
242
+ }
243
+ return scripts.some((script)=>script in wks.manifest.scripts);
244
+ });
245
+ }
246
+ function hasEveryScript$(scripts) {
247
+ return filter$((wks)=>{
248
+ if (!wks.manifest.scripts) {
249
+ return false;
250
+ }
251
+ return scripts.every((script)=>script in wks.manifest.scripts);
252
+ });
253
+ }
254
+
255
+ // Utils
256
+ async function* combine(...generators) {
257
+ for (const gen of generators){
258
+ yield* gen;
259
+ }
260
+ }
261
+ function streamLines$(task, stream = 'stdout') {
262
+ return observable$((observer, signal)=>{
263
+ const off = off$();
264
+ let current = '';
265
+ // End
266
+ off.add(once$(task.events$, 'completed', ()=>{
267
+ if (current) observer.next(current);
268
+ observer.complete();
269
+ off.unsubscribe();
270
+ }));
271
+ // Abort
272
+ signal.addEventListener('abort', ()=>off.unsubscribe(), {
273
+ once: true
274
+ });
275
+ // Steam
276
+ off.add(task.events$.on(`stream.${stream}`, (chunk)=>{
277
+ const data = current + chunk.data.toString('utf-8');
278
+ const lines = data.split(/\r?\n/);
279
+ current = lines.pop() ?? '';
280
+ for (const line of lines){
281
+ observer.next(line);
282
+ }
283
+ }));
284
+ });
285
+ }
286
+
287
+ var _dec$4, _dec1$3, _dec2$1, _initProto$4;
288
+ _dec$4 = instrument('GitService.isAffected'), _dec1$3 = instrument('GitService.listBranches'), _dec2$1 = instrument('GitService.listTags');
289
+ class GitService {
290
+ static{
291
+ ({ e: [_initProto$4] } = _(this, [
292
+ [
293
+ _dec$4,
294
+ 2,
295
+ "isAffected"
296
+ ],
297
+ [
298
+ _dec1$3,
299
+ 2,
300
+ "listBranches"
301
+ ],
302
+ [
303
+ _dec2$1,
304
+ 2,
305
+ "listTags"
306
+ ]
307
+ ], []));
308
+ }
309
+ // Attributes
310
+ _manager = (_initProto$4(this), inject$(TASK_MANAGER));
311
+ _logger = inject$(LOGGER);
312
+ // Methods
313
+ /**
314
+ * Runs a git command inside a SpawnTask
315
+ *
316
+ * @param cmd
317
+ * @param args
318
+ * @param options
319
+ */ async command(cmd, args, options = {}) {
320
+ const opts = {
321
+ logger: this._logger,
322
+ ...options
323
+ };
324
+ // Create task
325
+ const task = new SpawnTask('git', [
326
+ cmd,
327
+ ...args
328
+ ], {
329
+ command: cmd,
330
+ hidden: true
331
+ }, opts);
332
+ task.events$.on('stream', ({ data })=>opts.logger.debug(data.toString('utf-8')));
333
+ (await this._manager).add(task);
334
+ return task;
335
+ }
336
+ /**
337
+ * Runs git branch
338
+ *
339
+ * @param args
340
+ * @param options
341
+ */ branch(args, options) {
342
+ return this.command('branch', args, options);
343
+ }
344
+ /**
345
+ * Runs git diff
346
+ *
347
+ * @param args
348
+ * @param options
349
+ */ diff(args, options) {
350
+ return this.command('diff', args, options);
351
+ }
352
+ /**
353
+ * Runs git tag
354
+ *
355
+ * @param args
356
+ * @param options
357
+ */ tag(args, options) {
358
+ return this.command('tag', args, options);
359
+ }
360
+ /**
361
+ * Uses git diff to detect if given files have been affected since given reference
362
+ *
363
+ * @param reference
364
+ * @param files
365
+ * @param opts
366
+ */ async isAffected(reference, files = [], opts) {
367
+ const task = await this.diff([
368
+ '--quiet',
369
+ reference,
370
+ '--',
371
+ ...files
372
+ ], opts);
373
+ return new Promise((resolve, reject)=>{
374
+ once$(task.events$, 'status.done', ()=>resolve(false));
375
+ once$(task.events$, 'status.failed', ()=>{
376
+ if (task.exitCode) {
377
+ resolve(true);
378
+ } else {
379
+ reject(new Error(`Task ${task.name} failed`));
380
+ }
381
+ });
382
+ });
383
+ }
384
+ /**
385
+ * List git branches
386
+ *
387
+ * @param args
388
+ * @param opts
389
+ */ async listBranches(args = [], opts) {
390
+ const task = await this.branch([
391
+ '-l',
392
+ ...args
393
+ ], opts);
394
+ return waitFor$(pipe$(streamLines$(task), map$((line)=>line.replace(/^[ *] /, '')), collect$()));
395
+ }
396
+ /**
397
+ * List git tags
398
+ *
399
+ * @param args
400
+ * @param opts
401
+ */ async listTags(args = [], opts) {
402
+ const task = await this.tag([
403
+ '-l',
404
+ ...args
405
+ ], opts);
406
+ return waitFor$(pipe$(streamLines$(task), collect$()));
407
+ }
408
+ }
409
+
410
+ // Filter
411
+ function isAffected$(opts) {
412
+ return (origin)=>{
413
+ return asyncIterator$(async function*() {
414
+ for await (const wks of origin){
415
+ const revision = await formatRevision(opts, wks);
416
+ if (await wks.isAffected(revision)) {
417
+ yield wks;
418
+ }
419
+ }
420
+ }());
421
+ };
422
+ }
423
+ async function formatRevision(opts, workspace) {
424
+ const git = inject$(GitService);
425
+ const logger = inject$(LOGGER).child(withLabel(workspace.name));
426
+ // Format revision
427
+ let result = opts.format;
428
+ result = result.replace(/(?<!\\)((?:\\\\)*)%name/g, `$1${workspace.name}`);
429
+ result = result.replace(/\\(.)/g, '$1');
430
+ // Ask git to complete it
431
+ const sortArgs = opts.sort ? [
432
+ '--sort',
433
+ opts.sort
434
+ ] : [];
435
+ // - search in branches
436
+ if (result.includes('*')) {
437
+ const branches = await git.listBranches([
438
+ ...sortArgs,
439
+ result
440
+ ], {
441
+ cwd: workspace.root,
442
+ logger: logger
443
+ });
444
+ if (branches.length > 0) {
445
+ result = branches[branches.length - 1];
446
+ }
447
+ }
448
+ // - search in tags
449
+ if (result.includes('*')) {
450
+ const tags = await git.listTags([
451
+ ...sortArgs,
452
+ result
453
+ ], {
454
+ cwd: workspace.root,
455
+ logger: logger
456
+ });
457
+ if (tags.length > 0) {
458
+ result = tags[tags.length - 1];
459
+ }
460
+ }
461
+ if (result !== opts.format) {
462
+ logger.verbose(`resolved ${opts.format} into ${result}`);
463
+ }
464
+ if (result.includes('*')) {
465
+ logger.warning(`no revision found matching ${result}, using fallback ${opts.fallback}`);
466
+ return opts.fallback;
467
+ }
468
+ return result;
469
+ }
470
+
471
+ // Filter
472
+ function isPrivate$(value) {
473
+ return filter$((wks)=>(wks.manifest.private ?? false) === value);
474
+ }
475
+
476
+ function mutex$() {
477
+ const count$ = var$(0);
478
+ return {
479
+ async acquire () {
480
+ let cnt = count$.defer();
481
+ while(cnt > 0){
482
+ cnt = await waitFor$(count$);
483
+ }
484
+ count$.mutate(cnt + 1);
485
+ },
486
+ release () {
487
+ const cnt = count$.defer();
488
+ if (cnt > 0) {
489
+ count$.mutate(cnt - 1);
490
+ }
491
+ }
492
+ };
493
+ }
494
+ async function with$(lock, fn) {
495
+ try {
496
+ await lock.acquire();
497
+ return await fn();
498
+ } finally{
499
+ lock.release();
500
+ }
501
+ }
502
+
503
+ // Class
504
+ class CommandTask extends SpawnTask {
505
+ workspace;
506
+ // Constructor
507
+ constructor(workspace, command, args, opts = {}){
508
+ let cmd = command;
509
+ if (opts.superCommand) {
510
+ if (typeof opts.superCommand === 'string') {
511
+ opts.superCommand = [
512
+ opts.superCommand
513
+ ];
514
+ }
515
+ if (opts.superCommand.length > 0) {
516
+ cmd = opts.superCommand[0];
517
+ args = [
518
+ ...opts.superCommand.slice(1),
519
+ command,
520
+ ...args
521
+ ];
522
+ }
523
+ }
524
+ super(cmd, args, {
525
+ workspace,
526
+ command
527
+ }, {
528
+ ...opts,
529
+ cwd: workspace.root,
530
+ env: {
531
+ FORCE_COLOR: '1',
532
+ ...opts.env
533
+ }
534
+ }), this.workspace = workspace;
535
+ this._logStreams();
536
+ }
537
+ // Methods
538
+ _logStreams() {
539
+ const off = off$(streamLines$(this, 'stdout').subscribe((line)=>this.logger$.info(line)), streamLines$(this, 'stderr').subscribe((line)=>this.logger$.info(line)));
540
+ once$(this.events$, 'completed', ()=>{
541
+ off.unsubscribe();
542
+ });
543
+ }
544
+ }
545
+ // Utils
546
+ function isCommandCtx(ctx) {
547
+ return 'workspace' in ctx && 'command' in ctx;
548
+ }
549
+
550
+ class ClientError extends Error {
551
+ name = 'ClientError';
552
+ constructor(message){
553
+ super(message);
554
+ }
555
+ }
556
+
557
+ // Utils
558
+ function capitalize(txt) {
559
+ return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase();
560
+ }
561
+ function splitCommandLine(line) {
562
+ line = line.trim();
563
+ const parts = [];
564
+ let current_cote = '';
565
+ let last = 0;
566
+ for(let i = 1; i < line.length; ++i){
567
+ const c = line[i];
568
+ if (current_cote) {
569
+ if (c === current_cote) {
570
+ current_cote = '';
571
+ }
572
+ } else {
573
+ if ([
574
+ '"',
575
+ '\''
576
+ ].includes(c)) {
577
+ current_cote = c;
578
+ } else if (c === ' ') {
579
+ parts.push(line.slice(last, i));
580
+ last = i + 1;
581
+ }
582
+ }
583
+ }
584
+ parts.push(line.slice(last));
585
+ return parts;
586
+ }
587
+
588
+ // Class
589
+ class ScriptTask extends GroupTask {
590
+ workspace;
591
+ script;
592
+ args;
593
+ // Attributes
594
+ _preHookTasks = null;
595
+ _postHookTasks = null;
596
+ _scriptTasks = null;
597
+ _runHooks;
598
+ // Constructor
599
+ constructor(workspace, script, args, opts){
600
+ super(script, {
601
+ workspace,
602
+ script
603
+ }, opts), this.workspace = workspace, this.script = script, this.args = args;
604
+ this._runHooks = opts?.runHooks ?? true;
605
+ }
606
+ // Methods
607
+ async _runScript(script, args) {
608
+ const line = this.workspace.getScript(script);
609
+ if (!line) {
610
+ return null;
611
+ }
612
+ // Create command task for script
613
+ const [command, ...commandArgs] = splitCommandLine(line);
614
+ const set = new TaskSet();
615
+ if (!command) {
616
+ return set;
617
+ }
618
+ if (command === 'jill') {
619
+ const argv = commandArgs.map((arg)=>arg.replace(/^["'](.+)["']$/, '$1'));
620
+ const { PlannerService } = await import('./planner.service.js');
621
+ const plannerService = inject$(PlannerService);
622
+ const tasks = await plannerService.plan(argv, this.workspace.root);
623
+ if (tasks) {
624
+ return tasks;
625
+ }
626
+ }
627
+ const pm = await this.workspace.project.packageManager();
628
+ set.add(new CommandTask(this.workspace, command, [
629
+ ...commandArgs,
630
+ ...args
631
+ ], {
632
+ logger: this.logger$,
633
+ superCommand: pm === 'yarn' ? [
634
+ 'yarn',
635
+ 'exec'
636
+ ] : undefined
637
+ }));
638
+ return set;
639
+ }
640
+ async prepare() {
641
+ // Prepare script run
642
+ this._scriptTasks = await this._runScript(this.script, this.args);
643
+ if (!this._scriptTasks) {
644
+ throw new ScriptNotFound(`No script ${this.script} in ${this.workspace.name}`);
645
+ }
646
+ // Prepare hooks run
647
+ if (this._runHooks) {
648
+ this._preHookTasks = await this._runScript(`pre${this.script}`, []);
649
+ this._postHookTasks = await this._runScript(`post${this.script}`, []);
650
+ }
651
+ // Add tasks to group
652
+ if (this._preHookTasks) {
653
+ this.logger$.verbose(`found pre-hook script "pre${this.script}"`);
654
+ for (const tsk of this._preHookTasks){
655
+ this.add(tsk);
656
+ }
657
+ }
658
+ for (const tsk of this._scriptTasks){
659
+ this.add(tsk);
660
+ }
661
+ if (this._postHookTasks) {
662
+ this.logger$.verbose(`found post-hook script "post${this.script}"`);
663
+ for (const tsk of this._postHookTasks){
664
+ this.add(tsk);
665
+ }
666
+ }
667
+ }
668
+ async *onOrchestrate() {
669
+ if (!this._scriptTasks) {
670
+ throw new Error('ScriptTask needs to be prepared. Call prepare before starting it');
671
+ }
672
+ // Run pre-hook
673
+ if (this._preHookTasks) {
674
+ yield* this._preHookTasks;
675
+ if (await this._hasFailed(this._preHookTasks)) {
676
+ return this.setStatus('failed');
677
+ }
678
+ }
679
+ // Run script
680
+ yield* this._scriptTasks;
681
+ if (await this._hasFailed(this._scriptTasks)) {
682
+ return this.setStatus('failed');
683
+ }
684
+ // Run post-hook
685
+ if (this._postHookTasks) {
686
+ yield* this._postHookTasks;
687
+ if (await this._hasFailed(this._postHookTasks)) {
688
+ return this.setStatus('failed');
689
+ }
690
+ }
691
+ this.setStatus('done');
692
+ }
693
+ async _hasFailed(set) {
694
+ const results = await waitFor$(set.events$, 'finished');
695
+ return results.failed > 0;
696
+ }
697
+ async onStop() {
698
+ if (!this._scriptTasks) return;
699
+ for (const tsk of this._scriptTasks){
700
+ await tsk.stop();
701
+ }
702
+ }
703
+ complexity(cache = new Map()) {
704
+ let complexity = super.complexity(cache);
705
+ if (this._scriptTasks) {
706
+ complexity += this._scriptTasks.tasks.reduce((cpl, tsk)=>cpl + tsk.complexity(cache), 0);
707
+ }
708
+ cache.set(this.id, complexity);
709
+ return complexity;
710
+ }
711
+ // Properties
712
+ get project() {
713
+ return this.workspace.project;
714
+ }
715
+ }
716
+ // Utils
717
+ function isScriptCtx(ctx) {
718
+ return 'workspace' in ctx && 'script' in ctx;
719
+ }
720
+ class ScriptNotFound extends ClientError {
721
+ name = 'ScriptNotFound';
722
+ }
723
+
724
+ var _dec$3, _dec1$2, _initProto$3;
725
+ _dec$3 = instrument({
726
+ name: 'Workspace.dependencies',
727
+ use: traceAsyncGenerator
728
+ }), _dec1$2 = instrument({
729
+ name: 'Workspace.devDependencies',
730
+ use: traceAsyncGenerator
731
+ });
732
+ class Workspace {
733
+ manifest;
734
+ project;
735
+ static{
736
+ ({ e: [_initProto$3] } = _(this, [
737
+ [
738
+ _dec$3,
739
+ 2,
740
+ "dependencies"
741
+ ],
742
+ [
743
+ _dec1$2,
744
+ 2,
745
+ "devDependencies"
746
+ ]
747
+ ], []));
748
+ }
749
+ // Attributes
750
+ _affectedCache = (_initProto$3(this), new Map());
751
+ _logger;
752
+ _git = inject$(GitService);
753
+ _root;
754
+ _tasks = new Map();
755
+ // Constructor
756
+ constructor(root, manifest, project){
757
+ this.manifest = manifest;
758
+ this.project = project;
759
+ this._root = root;
760
+ this._logger = inject$(LOGGER).child(withLabel(manifest.name));
761
+ }
762
+ // Methods
763
+ async _buildDependencies(task, opts) {
764
+ const generators = [];
765
+ switch(opts.buildDeps ?? 'all'){
766
+ case 'all':
767
+ generators.unshift(this.devDependencies());
768
+ // eslint-disable-next no-fallthrough
769
+ case 'prod':
770
+ generators.unshift(this.dependencies());
771
+ }
772
+ // Build deps
773
+ for await (const dep of combine(...generators)){
774
+ const build = await dep.build(opts);
775
+ if (build) {
776
+ task.dependsOn(build);
777
+ }
778
+ }
779
+ }
780
+ async *_loadDependencies(dependencies, kind) {
781
+ for (const [dep, range] of Object.entries(dependencies)){
782
+ const ws = await this.project.workspace(dep);
783
+ if (ws) {
784
+ if (ws._satisfies(this, range)) {
785
+ yield ws;
786
+ } else {
787
+ this._logger.warning(`ignoring ${kind} ${ws.reference} as it does not match requirement ${range}`);
788
+ }
789
+ }
790
+ }
791
+ }
792
+ async _isAffected(reference) {
793
+ const isAffected = await this._git.isAffected(reference, [
794
+ this.root
795
+ ], {
796
+ cwd: this.project.root,
797
+ logger: this._logger
798
+ });
799
+ if (isAffected) {
800
+ return true;
801
+ }
802
+ // Test dependencies
803
+ const proms = [];
804
+ for await (const dep of combine(this.dependencies(), this.devDependencies())){
805
+ proms.push(dep.isAffected(reference));
806
+ }
807
+ const results = await Promise.all(proms);
808
+ return results.some((r)=>r);
809
+ }
810
+ _satisfies(from, range) {
811
+ if (range.startsWith('file:')) {
812
+ return path.resolve(from.root, range.substring(5)) === this.root;
813
+ }
814
+ if (range.startsWith('workspace:')) {
815
+ range = range.substring(10);
816
+ }
817
+ return !this.version || satisfies(this.version, range);
818
+ }
819
+ async isAffected(reference) {
820
+ let isAffected = this._affectedCache.get(reference);
821
+ if (!isAffected) {
822
+ isAffected = this._isAffected(reference);
823
+ this._affectedCache.set(reference, isAffected);
824
+ }
825
+ return await isAffected;
826
+ }
827
+ async *dependencies() {
828
+ if (!this.manifest.dependencies) return;
829
+ for await (const ws of this._loadDependencies(this.manifest.dependencies, 'dependency')){
830
+ yield ws;
831
+ }
832
+ }
833
+ async *devDependencies() {
834
+ if (!this.manifest.devDependencies) return;
835
+ for await (const ws of this._loadDependencies(this.manifest.devDependencies, 'devDependency')){
836
+ yield ws;
837
+ }
838
+ }
839
+ async build(opts = {}) {
840
+ const script = opts.buildScript ?? 'build';
841
+ const task = await this.run(script, [], opts);
842
+ if (!task) {
843
+ this._logger.warning(`will not be built (no "${script}" script found)`);
844
+ }
845
+ return task;
846
+ }
847
+ async exec(command, args = [], opts = {}) {
848
+ const pm = await this.project.packageManager();
849
+ const task = new CommandTask(this, command, args, {
850
+ ...opts,
851
+ logger: this._logger.child(withLabel(`${this.name}$${command}`)),
852
+ superCommand: pm === 'yarn' ? [
853
+ 'yarn',
854
+ 'exec'
855
+ ] : undefined
856
+ });
857
+ await this._buildDependencies(task, opts);
858
+ return task;
859
+ }
860
+ getScript(script) {
861
+ const { scripts = {} } = this.manifest;
862
+ return scripts[script] || null;
863
+ }
864
+ async run(script, args = [], opts = {}) {
865
+ // Script not found
866
+ if (!this.getScript(script)) {
867
+ return null;
868
+ }
869
+ // Create task if it doesn't exist yet
870
+ let task = this._tasks.get(script);
871
+ if (!task) {
872
+ const config = await inject$(CONFIG, asyncScope$());
873
+ task = new ScriptTask(this, script, args, {
874
+ ...opts,
875
+ logger: this._logger.child(withLabel(`${this.name}#${script}`)),
876
+ runHooks: config.hooks
877
+ });
878
+ await task.prepare();
879
+ await this._buildDependencies(task, opts);
880
+ this._tasks.set(script, task);
881
+ }
882
+ return task;
883
+ }
884
+ toJSON() {
885
+ return {
886
+ name: this.name,
887
+ version: this.version,
888
+ root: this.root
889
+ };
890
+ }
891
+ // Properties
892
+ get name() {
893
+ return this.manifest.name;
894
+ }
895
+ get reference() {
896
+ return this.version ? `${this.name}@${this.version}` : this.name;
897
+ }
898
+ get root() {
899
+ return path.resolve(this.project.root, this._root);
900
+ }
901
+ get version() {
902
+ return this.manifest.version;
903
+ }
904
+ }
905
+
906
+ var _dec$2, _dec1$1, _dec2, _dec3, _dec4, _initProto$2;
907
+ _dec$2 = instrument('Project.currentWorkspace'), _dec1$1 = instrument('Project.mainWorkspace'), _dec2 = instrument('Project.packageManager'), _dec3 = instrument('Project.workspace'), _dec4 = instrument({
908
+ name: 'Project.workspaces',
909
+ use: traceAsyncGenerator
910
+ });
911
+ class Project {
912
+ static{
913
+ ({ e: [_initProto$2] } = _(this, [
914
+ [
915
+ _dec$2,
916
+ 2,
917
+ "currentWorkspace"
918
+ ],
919
+ [
920
+ _dec1$1,
921
+ 2,
922
+ "mainWorkspace"
923
+ ],
924
+ [
925
+ _dec2,
926
+ 2,
927
+ "packageManager"
928
+ ],
929
+ [
930
+ _dec3,
931
+ 2,
932
+ "workspace"
933
+ ],
934
+ [
935
+ _dec4,
936
+ 2,
937
+ "workspaces"
938
+ ]
939
+ ], []));
940
+ }
941
+ // Attributes
942
+ _isFullyLoaded = (_initProto$2(this), false);
943
+ _lock = mutex$();
944
+ _mainWorkspace;
945
+ _packageManager;
946
+ _workspaceGlob;
947
+ _names = new Map();
948
+ _logger = inject$(LOGGER).child(withLabel('project'));
949
+ _root;
950
+ _scurry = inject$(PATH_SCURRY);
951
+ _workspaces = new Map();
952
+ // Constructor
953
+ constructor(root, opts = {}){
954
+ this._root = root;
955
+ if (opts.packageManager) {
956
+ this._logger.verbose`Forced use of ${opts.packageManager} in ${root}`;
957
+ this._packageManager = opts.packageManager;
958
+ }
959
+ }
960
+ // Methods
961
+ async _loadManifest(dir) {
962
+ const file = path.resolve(this.root, dir, 'package.json');
963
+ const relative = path.relative(this.root, path.dirname(file));
964
+ const logger = this._logger.child(withLabel(relative ? `project@${relative}` : 'project'));
965
+ logger.debug('loading package.json ...');
966
+ const data = await fs.promises.readFile(file, 'utf-8');
967
+ const mnf = JSON.parse(data);
968
+ normalize(mnf, (msg)=>logger.verbose(msg));
969
+ return mnf;
970
+ }
971
+ _loadWorkspace(dir) {
972
+ return with$(this._lock, async ()=>{
973
+ let wks = this._workspaces.get(dir);
974
+ if (!wks) {
975
+ const manifest = await this._loadManifest(dir);
976
+ wks = new Workspace(dir, manifest, this);
977
+ this._workspaces.set(dir, wks);
978
+ this._names.set(wks.name, wks);
979
+ }
980
+ return wks;
981
+ });
982
+ }
983
+ async currentWorkspace(cwd = inject$(CWD, asyncScope$())) {
984
+ let workspace = null;
985
+ cwd = path.resolve(cwd);
986
+ for await (const wks of this.workspaces()){
987
+ if (cwd.startsWith(wks.root)) {
988
+ workspace = wks;
989
+ if (wks.root !== this.root) return wks;
990
+ }
991
+ }
992
+ return workspace;
993
+ }
994
+ async mainWorkspace() {
995
+ if (!this._mainWorkspace) {
996
+ const manifest = await this._loadManifest('.');
997
+ this._mainWorkspace = new Workspace('.', manifest, this);
998
+ this._names.set(this._mainWorkspace.name, this._mainWorkspace);
999
+ }
1000
+ return this._mainWorkspace;
1001
+ }
1002
+ async packageManager() {
1003
+ if (!this._packageManager) {
1004
+ this._logger.debug`searching lockfile in ${this.root}`;
1005
+ const files = await this._scurry.readdir(this.root, {
1006
+ withFileTypes: false
1007
+ });
1008
+ if (files.includes('yarn.lock')) {
1009
+ this._logger.debug`detected yarn in ${this.root}`;
1010
+ this._packageManager = 'yarn';
1011
+ } else if (files.includes('package-lock.json')) {
1012
+ this._logger.debug`detected npm in ${this.root}`;
1013
+ this._packageManager = 'npm';
1014
+ } else {
1015
+ this._logger.debug`no package manager recognized in ${this.root}, defaults to npm`;
1016
+ this._packageManager = 'npm';
1017
+ }
1018
+ }
1019
+ return this._packageManager;
1020
+ }
1021
+ async workspace(name) {
1022
+ // With current directory
1023
+ if (!name) {
1024
+ const dir = path.relative(this.root, inject$(CWD, asyncScope$()));
1025
+ return this._loadWorkspace(dir);
1026
+ }
1027
+ // Try name index
1028
+ const wks = this._names.get(name);
1029
+ if (wks) {
1030
+ return wks;
1031
+ }
1032
+ // Load workspaces
1033
+ if (!this._isFullyLoaded) {
1034
+ for await (const ws of this.workspaces()){
1035
+ if (ws.name === name) {
1036
+ return ws;
1037
+ }
1038
+ }
1039
+ this._isFullyLoaded = true;
1040
+ }
1041
+ return null;
1042
+ }
1043
+ async *workspaces() {
1044
+ const main = await this.mainWorkspace();
1045
+ yield main;
1046
+ if (this._isFullyLoaded) {
1047
+ for (const wks of this._names.values()){
1048
+ if (wks.name !== main.name) yield wks;
1049
+ }
1050
+ } else {
1051
+ // Load child workspaces
1052
+ const patterns = main.manifest.workspaces ?? [];
1053
+ this._scurry.chdir(this.root);
1054
+ this._workspaceGlob ??= new Glob(patterns, {
1055
+ scurry: this._scurry,
1056
+ withFileTypes: true
1057
+ });
1058
+ for await (const dir of this._workspaceGlob){
1059
+ try {
1060
+ // Check if dir is a directory
1061
+ if (dir.isDirectory()) {
1062
+ yield await this._loadWorkspace(dir.fullpath());
1063
+ }
1064
+ } catch (error) {
1065
+ if (error.code === 'ENOENT') {
1066
+ continue;
1067
+ }
1068
+ throw error;
1069
+ }
1070
+ }
1071
+ this._isFullyLoaded = true;
1072
+ }
1073
+ }
1074
+ // Properties
1075
+ get root() {
1076
+ return path.resolve(this._root);
1077
+ }
1078
+ }
1079
+
1080
+ var _dec$1, _initProto$1;
1081
+ _dec$1 = instrument();
1082
+ /**
1083
+ * Helps detecting projects folders
1084
+ */ class ProjectsRepository {
1085
+ static{
1086
+ ({ e: [_initProto$1] } = _(this, [
1087
+ [
1088
+ _dec$1,
1089
+ 2,
1090
+ "searchProjectRoot"
1091
+ ]
1092
+ ], []));
1093
+ }
1094
+ // Attributes
1095
+ _cache = (_initProto$1(this), new Map());
1096
+ _logger = inject$(LOGGER).child(withLabel('projects'));
1097
+ _scurry = inject$(PATH_SCURRY);
1098
+ // Methods
1099
+ async isProjectRoot(dir) {
1100
+ this._logger.debug`testing ${dir}`;
1101
+ const files = await this._scurry.readdir(dir, {
1102
+ withFileTypes: false
1103
+ });
1104
+ return {
1105
+ hasManifest: files.includes(MANIFEST),
1106
+ hasLockFile: LOCK_FILES.some((lock)=>files.includes(lock))
1107
+ };
1108
+ }
1109
+ async searchProjectRoot(directory) {
1110
+ directory = path.resolve(directory);
1111
+ let foundManifest = false;
1112
+ let projectRoot = directory;
1113
+ let dir = directory;
1114
+ let prev = dir;
1115
+ do {
1116
+ // Look for files
1117
+ const { hasManifest, hasLockFile } = await this.isProjectRoot(dir);
1118
+ if (hasManifest) {
1119
+ projectRoot = dir;
1120
+ foundManifest = true;
1121
+ }
1122
+ if (hasLockFile) {
1123
+ break;
1124
+ }
1125
+ prev = dir;
1126
+ dir = path.dirname(dir);
1127
+ }while (prev !== dir);
1128
+ // Log it
1129
+ if (foundManifest) {
1130
+ this._logger.verbose`project root found at ${projectRoot}`;
1131
+ } else {
1132
+ this._logger.verbose`project root not found, keeping ${projectRoot}`;
1133
+ }
1134
+ return projectRoot;
1135
+ }
1136
+ getProject(root, opts) {
1137
+ let project = this._cache.get(root);
1138
+ if (!project) {
1139
+ project = new Project(root, opts);
1140
+ this._cache.set(root, project);
1141
+ }
1142
+ return project;
1143
+ }
1144
+ }
1145
+ // Constants
1146
+ const MANIFEST = 'package.json';
1147
+ const LOCK_FILES = [
1148
+ 'package-lock.json',
1149
+ 'yarn.lock'
1150
+ ];
1151
+
1152
+ /**
1153
+ * Adds arguments to load a project.
1154
+ */ function withProject(parser) {
1155
+ return parser.option('project', {
1156
+ alias: 'p',
1157
+ type: 'string',
1158
+ default: '',
1159
+ defaultDescription: 'current working directory',
1160
+ description: 'Project root directory',
1161
+ normalize: true
1162
+ }).option('package-manager', {
1163
+ choices: [
1164
+ 'yarn',
1165
+ 'npm'
1166
+ ],
1167
+ type: 'string',
1168
+ description: 'Force package manager'
1169
+ }).middleware((args)=>startSpan({
1170
+ name: 'project',
1171
+ op: 'cli.middleware'
1172
+ }, async ()=>{
1173
+ const repository = inject$(ProjectsRepository);
1174
+ const directory = path.resolve(inject$(CWD, asyncScope$()), args.project);
1175
+ args.project = await repository.searchProjectRoot(directory);
1176
+ }));
1177
+ }
1178
+ /**
1179
+ * Loads a project, based on arguments.
1180
+ */ function loadProject(args) {
1181
+ const repository = inject$(ProjectsRepository);
1182
+ return repository.getProject(args.project, {
1183
+ packageManager: args.packageManager
1184
+ });
1185
+ }
1186
+
1187
+ class TaskExpressionError extends ClientError {
1188
+ name = 'TaskExpressionError';
1189
+ }
1190
+ class TaskSyntaxError extends ClientError {
1191
+ name = 'TaskSyntaxError';
1192
+ }
1193
+
1194
+ var _dec, _dec1, _initProto;
1195
+ _dec = instrument('TaskParserService.parse'), _dec1 = instrument('TaskParserService.buildTask');
1196
+ // Service
1197
+ class TaskParserService {
1198
+ static{
1199
+ ({ e: [_initProto] } = _(this, [
1200
+ [
1201
+ _dec,
1202
+ 2,
1203
+ "parse"
1204
+ ],
1205
+ [
1206
+ _dec1,
1207
+ 2,
1208
+ "buildTask"
1209
+ ]
1210
+ ], []));
1211
+ }
1212
+ // Attributes
1213
+ _logger = (_initProto(this), inject$(LOGGER).child(withLabel('task-parser')));
1214
+ // Statics
1215
+ static isTaskNode(node) {
1216
+ return 'script' in node;
1217
+ }
1218
+ static _sequenceOperatorWarn = true;
1219
+ // Methods
1220
+ _lexer() {
1221
+ return moo.states({
1222
+ task: {
1223
+ lparen: '(',
1224
+ whitespace: /[ \t]+/,
1225
+ script: {
1226
+ match: /[-_:a-zA-Z0-9]+/,
1227
+ push: 'operatorOrArgument'
1228
+ },
1229
+ string: [
1230
+ {
1231
+ match: /'(?:\\['\\]|[^\r\n'\\])+'/,
1232
+ push: 'operator',
1233
+ value: (x)=>x.slice(1, -1).replace(/\\(['\\])/g, '$1')
1234
+ },
1235
+ {
1236
+ match: /"(?:\\["\\]|[^\r\n"\\])+"/,
1237
+ push: 'operator',
1238
+ value: (x)=>x.slice(1, -1).replace(/\\(["\\])/g, '$1')
1239
+ }
1240
+ ]
1241
+ },
1242
+ operator: {
1243
+ rparen: ')',
1244
+ whitespace: /[ \t]+/,
1245
+ operator: {
1246
+ match: [
1247
+ '->',
1248
+ '&&',
1249
+ '//',
1250
+ '||'
1251
+ ],
1252
+ pop: 1
1253
+ }
1254
+ },
1255
+ operatorOrArgument: {
1256
+ rparen: ')',
1257
+ whitespace: /[ \t]+/,
1258
+ operator: {
1259
+ match: [
1260
+ '->',
1261
+ '&&',
1262
+ '//',
1263
+ '||'
1264
+ ],
1265
+ pop: 1
1266
+ },
1267
+ argument: [
1268
+ {
1269
+ match: /[-_:a-zA-Z0-9]+/
1270
+ },
1271
+ {
1272
+ match: /'(?:\\['\\]|[^\r\n'\\])+'/,
1273
+ value: (x)=>x.slice(1, -1).replace(/\\(['\\])/g, '$1')
1274
+ },
1275
+ {
1276
+ match: /"(?:\\["\\]|[^\r\n"\\])+"/,
1277
+ value: (x)=>x.slice(1, -1).replace(/\\(["\\])/g, '$1')
1278
+ }
1279
+ ]
1280
+ }
1281
+ });
1282
+ }
1283
+ _nextNode(lexer, i = 0) {
1284
+ let node = null;
1285
+ for (const token of lexer){
1286
+ // Ignore whitespaces
1287
+ if (token.type === 'whitespace') {
1288
+ continue;
1289
+ }
1290
+ // rparen = end of group
1291
+ if (token.type === 'rparen') {
1292
+ break;
1293
+ }
1294
+ // Handle argument
1295
+ if (token.type === 'argument') {
1296
+ if (!node) {
1297
+ throw new TaskSyntaxError(lexer.formatError(token, 'Unexpected argument'));
1298
+ } else if (TaskParserService.isTaskNode(node)) {
1299
+ node.args.push(token.value);
1300
+ } else {
1301
+ const lastTask = node.tasks[node.tasks.length - 1];
1302
+ if (!lastTask || !TaskParserService.isTaskNode(lastTask)) {
1303
+ throw new TaskSyntaxError(lexer.formatError(token, 'Unexpected argument'));
1304
+ } else {
1305
+ lastTask.args.push(token.value);
1306
+ }
1307
+ }
1308
+ continue;
1309
+ }
1310
+ // Handle operator
1311
+ if (token.type === 'operator') {
1312
+ const operator = token.value;
1313
+ if (!node) {
1314
+ throw new TaskSyntaxError(lexer.formatError(token, 'Unexpected operator'));
1315
+ } else if (TaskParserService.isTaskNode(node)) {
1316
+ node = {
1317
+ operator,
1318
+ tasks: [
1319
+ node
1320
+ ]
1321
+ };
1322
+ continue;
1323
+ } else {
1324
+ if (node.operator !== operator) {
1325
+ node = {
1326
+ operator,
1327
+ tasks: [
1328
+ node
1329
+ ]
1330
+ };
1331
+ }
1332
+ continue;
1333
+ }
1334
+ }
1335
+ // Build "child"
1336
+ let child;
1337
+ if (token.type === 'script') {
1338
+ child = {
1339
+ script: token.value,
1340
+ args: []
1341
+ };
1342
+ } else if (token.type === 'string') {
1343
+ const [script, ...args] = token.value.split(/ +/);
1344
+ child = {
1345
+ script,
1346
+ args
1347
+ };
1348
+ } else if (token.type === 'lparen') {
1349
+ const res = this._nextNode(lexer, i + 1);
1350
+ if (!res) {
1351
+ throw new TaskSyntaxError(lexer.formatError(token, 'Empty group found'));
1352
+ }
1353
+ child = res;
1354
+ } else {
1355
+ throw new TaskSyntaxError(lexer.formatError(token, 'Unexpected token'));
1356
+ }
1357
+ if (!node) {
1358
+ node = child;
1359
+ } else if (TaskParserService.isTaskNode(node)) {
1360
+ throw new TaskSyntaxError(lexer.formatError(token, 'Unexpected token, expected an operator'));
1361
+ } else {
1362
+ node.tasks.push(child);
1363
+ }
1364
+ }
1365
+ return node;
1366
+ }
1367
+ parse(expr) {
1368
+ const lexer = this._lexer().reset(expr);
1369
+ const tree = {
1370
+ roots: []
1371
+ };
1372
+ while(true){
1373
+ const node = this._nextNode(lexer);
1374
+ if (node) {
1375
+ tree.roots.push(node);
1376
+ } else {
1377
+ break;
1378
+ }
1379
+ }
1380
+ return tree;
1381
+ }
1382
+ *extractScripts(node) {
1383
+ if ('roots' in node) {
1384
+ for (const child of node.roots){
1385
+ yield* this.extractScripts(child);
1386
+ }
1387
+ } else if (TaskParserService.isTaskNode(node)) {
1388
+ yield node.script;
1389
+ } else {
1390
+ for (const child of node.tasks){
1391
+ yield* this.extractScripts(child);
1392
+ }
1393
+ }
1394
+ }
1395
+ async buildTask(node, workspace, opts) {
1396
+ if (TaskParserService.isTaskNode(node)) {
1397
+ const task = await workspace.run(node.script, node.args, opts);
1398
+ if (!task) {
1399
+ throw new TaskExpressionError(`Workspace ${workspace.name} have no ${node.script} script`);
1400
+ }
1401
+ return task;
1402
+ } else {
1403
+ let group;
1404
+ if (node.operator === '//') {
1405
+ group = new ParallelGroup('In parallel', {
1406
+ workspace
1407
+ }, {
1408
+ logger: this._logger
1409
+ });
1410
+ } else if (node.operator === '||') {
1411
+ group = new FallbackGroup('Fallbacks', {
1412
+ workspace
1413
+ }, {
1414
+ logger: this._logger
1415
+ });
1416
+ } else {
1417
+ if (node.operator === '->' && TaskParserService._sequenceOperatorWarn) {
1418
+ this._logger.warn('Sequence operator -> is deprecated in favor of &&. It will be removed in a next major release.');
1419
+ TaskParserService._sequenceOperatorWarn = true;
1420
+ }
1421
+ group = new SequenceGroup('In sequence', {
1422
+ workspace
1423
+ }, {
1424
+ logger: this._logger
1425
+ });
1426
+ }
1427
+ for (const child of node.tasks){
1428
+ group.add(await this.buildTask(child, workspace, opts));
1429
+ }
1430
+ return group;
1431
+ }
1432
+ }
1433
+ }
1434
+
1435
+ function pipeline$() {
1436
+ const steps = [];
1437
+ return {
1438
+ add (step) {
1439
+ steps.push(step);
1440
+ return this;
1441
+ },
1442
+ build () {
1443
+ return (value)=>steps.reduce((val, step)=>step(val), value);
1444
+ }
1445
+ };
1446
+ }
1447
+
1448
+ // Command
1449
+ const command$4 = {
1450
+ command: 'each <expr>',
1451
+ describe: 'Run a task expression in many workspace, after having built all theirs dependencies.',
1452
+ builder: (parser)=>withProject(parser).positional('expr', {
1453
+ type: 'string',
1454
+ demandOption: true,
1455
+ desc: 'Script or task expression'
1456
+ }).option('affected', {
1457
+ alias: 'a',
1458
+ type: 'string',
1459
+ coerce: (rev)=>rev === '' ? 'master' : rev,
1460
+ group: 'Filters:',
1461
+ desc: 'Print only affected workspaces towards given git revision. If no revision is given, it will check towards master. Replaces %name by workspace name.'
1462
+ }).option('affected-rev-fallback', {
1463
+ type: 'string',
1464
+ default: 'master',
1465
+ group: 'Filters:',
1466
+ desc: 'Fallback revision, used if no revision matching the given format is found'
1467
+ }).option('affected-rev-sort', {
1468
+ type: 'string',
1469
+ group: 'Filters:',
1470
+ desc: 'Sort applied to git tag / git branch command'
1471
+ }).option('allow-no-workspaces', {
1472
+ type: 'boolean',
1473
+ default: false,
1474
+ desc: 'Allow no matching workspaces. Without it jill will exit with code 1 if no workspace matches'
1475
+ }).option('build-script', {
1476
+ default: 'build',
1477
+ desc: 'Script to use to build dependencies'
1478
+ }).option('deps-mode', {
1479
+ alias: 'd',
1480
+ choice: [
1481
+ 'all',
1482
+ 'prod',
1483
+ 'none'
1484
+ ],
1485
+ default: 'all',
1486
+ desc: 'Dependency selection mode:\n' + ' - all = dependencies AND devDependencies\n' + ' - prod = dependencies\n' + ' - none = nothing'
1487
+ }).option('private', {
1488
+ type: 'boolean',
1489
+ group: 'Filters:',
1490
+ describe: 'Print only private workspaces'
1491
+ })// Config
1492
+ .strict(false).parserConfiguration({
1493
+ 'unknown-options-as-args': true
1494
+ }),
1495
+ async prepare (args) {
1496
+ // Extract expression
1497
+ const expr = args._.map((arg)=>arg.toString());
1498
+ if (expr[0] === 'each') {
1499
+ expr.splice(0, 1);
1500
+ }
1501
+ expr.unshift(args.expr);
1502
+ // Prepare filters
1503
+ let filters = pipeline$();
1504
+ if (args.private !== undefined) {
1505
+ filters = filters.add(isPrivate$(args.private));
1506
+ }
1507
+ if (args.affected !== undefined) {
1508
+ filters = filters.add(isAffected$({
1509
+ format: args.affected,
1510
+ fallback: args.affectedRevFallback,
1511
+ sort: args.affectedRevSort
1512
+ }));
1513
+ }
1514
+ // Parse task expression
1515
+ const taskParser = inject$(TaskParserService);
1516
+ const tree = taskParser.parse(expr.join(' '));
1517
+ const scripts = Array.from(taskParser.extractScripts(tree));
1518
+ // Load workspaces
1519
+ const project = loadProject(args);
1520
+ const workspaces = pipe$(asyncIterator$(project.workspaces()), hasEveryScript$(scripts), filters.build());
1521
+ // Prepare tasks
1522
+ const tasks = new TaskSet();
1523
+ for await (const wks of workspaces){
1524
+ tasks.add(await taskParser.buildTask(tree.roots[0], wks, {
1525
+ buildScript: args.buildScript,
1526
+ buildDeps: args.depsMode
1527
+ }));
1528
+ }
1529
+ return tasks;
1530
+ }
1531
+ };
1532
+
1533
+ /**
1534
+ * Adds arguments to load a workspace.
1535
+ */ function withWorkspace(parser) {
1536
+ return withProject(parser).option('workspace', {
1537
+ alias: 'w',
1538
+ type: 'string',
1539
+ desc: 'Workspace to use'
1540
+ });
1541
+ }
1542
+ /**
1543
+ * Loads a workspace, based on arguments.
1544
+ */ async function loadWorkspace(args) {
1545
+ const logger = inject$(LOGGER);
1546
+ const project = loadProject(args);
1547
+ let workspace;
1548
+ if (args.workspace) {
1549
+ logger.debug(`loading workspace "${args.workspace}"`);
1550
+ workspace = await project.workspace(args.workspace);
1551
+ } else if (inject$(CWD, asyncScope$()).startsWith(project.root)) {
1552
+ logger.debug('loading workspace containing current directory');
1553
+ workspace = await project.currentWorkspace(inject$(CWD, asyncScope$()));
1554
+ } else {
1555
+ logger.debug('loading main workspace');
1556
+ workspace = await project.mainWorkspace();
1557
+ }
1558
+ if (!workspace) {
1559
+ throw new WorkspaceNotFound(args.workspace || '.');
1560
+ }
1561
+ return workspace;
1562
+ }
1563
+ class WorkspaceNotFound extends ClientError {
1564
+ name = 'WorkspaceNotFound';
1565
+ constructor(workspace){
1566
+ super(`Workspace "${workspace}" not found`);
1567
+ }
1568
+ }
1569
+
1570
+ // Command
1571
+ const command$3 = {
1572
+ command: 'exec <command>',
1573
+ aliases: [
1574
+ '$0'
1575
+ ],
1576
+ describe: 'Run command inside workspace, after all its dependencies has been built.',
1577
+ builder: (args)=>withWorkspace(args).positional('command', {
1578
+ type: 'string',
1579
+ demandOption: true
1580
+ }).option('build-script', {
1581
+ default: 'build',
1582
+ desc: 'Script to use to build dependencies'
1583
+ }).option('deps-mode', {
1584
+ alias: 'd',
1585
+ choice: [
1586
+ 'all',
1587
+ 'prod',
1588
+ 'none'
1589
+ ],
1590
+ default: 'all',
1591
+ desc: 'Dependency selection mode:\n' + ' - all = dependencies AND devDependencies\n' + ' - prod = dependencies\n' + ' - none = nothing'
1592
+ })// Documentation
1593
+ .example('jill exec eslint', '').example('jill exec eslint --env-info', 'Unknown arguments are passed down to command. Here it will run "eslint --env-info"').example('jill exec eslint -- -v', 'You can use -- to stop argument parsing. Here it will run "eslint -v"')// Config
1594
+ .strict(false).parserConfiguration({
1595
+ 'unknown-options-as-args': true
1596
+ }),
1597
+ async prepare (args) {
1598
+ const workspace = await loadWorkspace(args);
1599
+ // Extract arguments
1600
+ const rest = args._.map((arg)=>arg.toString());
1601
+ if (rest[0] === 'exec') {
1602
+ rest.splice(0, 1);
1603
+ }
1604
+ // Run script in workspace
1605
+ const tasks = new TaskSet();
1606
+ tasks.add(await workspace.exec(args.command, rest, {
1607
+ buildScript: args.buildScript,
1608
+ buildDeps: args.depsMode
1609
+ }));
1610
+ return tasks;
1611
+ },
1612
+ async execute (args, tasks) {
1613
+ const task = tasks.tasks[0];
1614
+ if (task.dependencies.length > 0) {
1615
+ const dependencies = new TaskSet();
1616
+ for (const dep of task.dependencies){
1617
+ dependencies.add(dep);
1618
+ }
1619
+ // Run dependencies first with spinners
1620
+ const { default: TaskExecInk } = await traceImport('TaskExecInk', ()=>import('./task-exec.ink.js'));
1621
+ await TaskExecInk({
1622
+ tasks: dependencies,
1623
+ verbose: [
1624
+ 'verbose',
1625
+ 'debug'
1626
+ ].includes(args.verbose)
1627
+ });
1628
+ } else {
1629
+ const logger = inject$(LOGGER);
1630
+ logger.verbose('No dependency to build');
1631
+ }
1632
+ const child = cp.spawn(task.cmd, task.args, {
1633
+ stdio: 'inherit',
1634
+ cwd: task.cwd,
1635
+ env: {
1636
+ ...process$1.env,
1637
+ ...task.env
1638
+ },
1639
+ shell: true,
1640
+ windowsHide: true
1641
+ });
1642
+ process$1.exitCode = await new Promise((resolve)=>{
1643
+ child.on('close', (code)=>{
1644
+ resolve(code ?? 0);
1645
+ });
1646
+ });
1647
+ }
1648
+ };
1649
+
1650
+ // Command
1651
+ const command$2 = {
1652
+ command: 'list',
1653
+ aliases: [
1654
+ 'ls'
1655
+ ],
1656
+ describe: 'List project workspaces',
1657
+ builder: (parser)=>withProject(parser).option('affected', {
1658
+ alias: 'a',
1659
+ type: 'string',
1660
+ coerce: (rev)=>rev === '' ? 'master' : rev,
1661
+ group: 'Filters:',
1662
+ desc: `Print only affected workspaces towards given git revision. If no revision is given, it will check towards master. Replaces ${chalk.bold('%name')} by workspace name.`
1663
+ }).option('affected-rev-fallback', {
1664
+ type: 'string',
1665
+ default: 'master',
1666
+ group: 'Filters:',
1667
+ desc: 'Fallback revision, used if no revision matching the given format is found'
1668
+ }).option('affected-rev-sort', {
1669
+ type: 'string',
1670
+ group: 'Filters:',
1671
+ desc: 'Sort applied to git tag / git branch command'
1672
+ }).option('attribute', {
1673
+ alias: [
1674
+ 'attr',
1675
+ 'attrs'
1676
+ ],
1677
+ type: 'array',
1678
+ choices: [
1679
+ 'name',
1680
+ 'version',
1681
+ 'root',
1682
+ 'slug'
1683
+ ],
1684
+ group: 'Format:',
1685
+ default: [],
1686
+ description: 'Select printed attributes',
1687
+ defaultDescription: '"name" only'
1688
+ }).option('headers', {
1689
+ type: 'boolean',
1690
+ group: 'Format:',
1691
+ default: false,
1692
+ desc: 'Prints columns headers'
1693
+ }).option('long', {
1694
+ alias: 'l',
1695
+ type: 'boolean',
1696
+ group: 'Format:',
1697
+ default: false,
1698
+ desc: 'Prints name, version and root of all workspaces'
1699
+ }).option('json', {
1700
+ type: 'boolean',
1701
+ group: 'Format:',
1702
+ default: false,
1703
+ desc: 'Prints data as a JSON array'
1704
+ }).option('private', {
1705
+ type: 'boolean',
1706
+ group: 'Filters:',
1707
+ describe: 'Print only private workspaces'
1708
+ }).option('sort-by', {
1709
+ alias: 's',
1710
+ type: 'array',
1711
+ choices: [
1712
+ 'name',
1713
+ 'version',
1714
+ 'root',
1715
+ 'slug'
1716
+ ],
1717
+ group: 'Sort:',
1718
+ default: [],
1719
+ description: 'Sort output by given attribute',
1720
+ defaultDescription: 'first attribute'
1721
+ }).option('sort-order', {
1722
+ alias: [
1723
+ 'o',
1724
+ 'order'
1725
+ ],
1726
+ type: 'string',
1727
+ choices: [
1728
+ 'asc',
1729
+ 'desc'
1730
+ ],
1731
+ default: 'asc',
1732
+ group: 'Sort:',
1733
+ desc: 'Sort order'
1734
+ }).option('with-script', {
1735
+ type: 'array',
1736
+ string: true,
1737
+ group: 'Filters:',
1738
+ desc: 'Print only workspaces having the given script'
1739
+ }).middleware((argv)=>{
1740
+ // Compute attributes
1741
+ if (!argv.attribute.length) {
1742
+ if (argv.json) {
1743
+ argv.attrs = argv.attr = argv.attribute = [
1744
+ 'name',
1745
+ 'version',
1746
+ 'slug',
1747
+ 'root'
1748
+ ];
1749
+ } else if (argv.long) {
1750
+ argv.attrs = argv.attr = argv.attribute = [
1751
+ 'name',
1752
+ 'version',
1753
+ 'root'
1754
+ ];
1755
+ } else if (argv.sortBy?.length) {
1756
+ argv.attrs = argv.attr = argv.attribute = argv.sortBy;
1757
+ } else {
1758
+ argv.attrs = argv.attr = argv.attribute = [
1759
+ 'name'
1760
+ ];
1761
+ }
1762
+ }
1763
+ }, true).check((argv)=>{
1764
+ if (argv.attribute.length > 0 && argv['sort-by']?.length) {
1765
+ const miss = argv['sort-by'].filter((attr)=>!argv.attribute.includes(attr));
1766
+ if (miss.length > 0) {
1767
+ throw new ClientError(`Cannot sort by non printed attributes. Missing ${miss.join(', ')}.`);
1768
+ }
1769
+ }
1770
+ if (!argv['sort-by']?.length && argv.attribute.length > 0) {
1771
+ argv['sort-by'] = argv.sortBy = argv.s = [
1772
+ argv.attribute[0]
1773
+ ];
1774
+ }
1775
+ return true;
1776
+ }),
1777
+ async handler (args) {
1778
+ // Prepare filters
1779
+ let filters = pipeline$();
1780
+ if (args.private !== undefined) {
1781
+ filters = filters.add(isPrivate$(args.private));
1782
+ }
1783
+ if (args.withScript) {
1784
+ filters = filters.add(hasSomeScript$(args.withScript));
1785
+ }
1786
+ if (args.affected !== undefined) {
1787
+ filters = filters.add(isAffected$({
1788
+ format: args.affected,
1789
+ fallback: args.affectedRevFallback,
1790
+ sort: args.affectedRevSort
1791
+ }));
1792
+ }
1793
+ // Load workspaces
1794
+ const project = loadProject(args);
1795
+ const workspaces = await waitFor$(pipe$(asyncIterator$(project.workspaces()), filters.build(), map$(buildExtractor(args)), collect$()));
1796
+ if (args.sortBy.length > 0) {
1797
+ workspaces.sort(buildComparator(args));
1798
+ }
1799
+ if (args.json) {
1800
+ printJson(workspaces);
1801
+ } else if (workspaces.length > 0) {
1802
+ for (const data of workspaces){
1803
+ if (data.root) {
1804
+ data.root = path.relative(process.cwd(), data.root) || '.';
1805
+ }
1806
+ }
1807
+ const { default: ListInk } = await traceImport('ListInk', ()=>import('./list.ink.js'));
1808
+ await ListInk({
1809
+ attributes: args.attribute,
1810
+ headers: args.headers,
1811
+ workspaces
1812
+ });
1813
+ }
1814
+ }
1815
+ };
1816
+ const EXTRACTORS = {
1817
+ name: (wks)=>wks.name,
1818
+ version: (wks, json)=>wks.manifest.version || (json ? undefined : chalk.grey('unset')),
1819
+ root: (wks)=>wks.root,
1820
+ slug: (wks)=>slugify(wks.name)
1821
+ };
1822
+ const COMPARATORS = {
1823
+ name: (a = '', b = '')=>a.localeCompare(b),
1824
+ version: (a, b)=>compare(parse(a) ?? '0.0.0', parse(b) ?? '0.0.0'),
1825
+ root: (a = '', b = '')=>a.localeCompare(b),
1826
+ slug: (a = '', b = '')=>a.localeCompare(b)
1827
+ };
1828
+ function buildExtractor(args) {
1829
+ return (wks)=>{
1830
+ const data = {};
1831
+ for (const attr of args.attribute){
1832
+ data[attr] = EXTRACTORS[attr](wks, args.json);
1833
+ }
1834
+ return data;
1835
+ };
1836
+ }
1837
+ function buildComparator(args) {
1838
+ const factor = args.sortOrder === 'asc' ? 1 : -1;
1839
+ return (a, b)=>{
1840
+ for (const attr of args.sortBy){
1841
+ const diff = COMPARATORS[attr](a[attr], b[attr]);
1842
+ if (diff !== 0) {
1843
+ return diff * factor;
1844
+ }
1845
+ }
1846
+ return 0;
1847
+ };
1848
+ }
1849
+
1850
+ // Command
1851
+ const command$1 = {
1852
+ command: 'run <expr>',
1853
+ describe: 'Run a task expression in a workspace, after having built all its dependencies.',
1854
+ builder: (parser)=>withWorkspace(parser).positional('expr', {
1855
+ type: 'string',
1856
+ demandOption: true,
1857
+ desc: 'Script or task expression'
1858
+ }).option('build-script', {
1859
+ default: 'build',
1860
+ desc: 'Script to use to build dependencies'
1861
+ }).option('deps-mode', {
1862
+ alias: 'd',
1863
+ choice: [
1864
+ 'all',
1865
+ 'prod',
1866
+ 'none'
1867
+ ],
1868
+ default: 'all',
1869
+ desc: 'Dependency selection mode:\n' + ' - all = dependencies AND devDependencies\n' + ' - prod = dependencies\n' + ' - none = nothing'
1870
+ })// Config
1871
+ .strict(false).parserConfiguration({
1872
+ 'unknown-options-as-args': true
1873
+ }),
1874
+ async prepare (args) {
1875
+ const workspace = await loadWorkspace(args);
1876
+ // Extract expression
1877
+ const expr = args._.map((arg)=>arg.toString());
1878
+ if (expr[0] === 'run') {
1879
+ expr.splice(0, 1);
1880
+ }
1881
+ expr.unshift(args.expr);
1882
+ // Parse task expression
1883
+ const taskParser = inject$(TaskParserService);
1884
+ const tree = taskParser.parse(expr.join(' '));
1885
+ const tasks = new TaskSet();
1886
+ tasks.add(await taskParser.buildTask(tree.roots[0], workspace, {
1887
+ buildScript: args.buildScript,
1888
+ buildDeps: args.depsMode
1889
+ }));
1890
+ return tasks;
1891
+ }
1892
+ };
1893
+
1894
+ // Command
1895
+ const command = {
1896
+ command: 'tree',
1897
+ describe: 'Print workspace dependency tree',
1898
+ builder: withWorkspace,
1899
+ async handler (args) {
1900
+ const workspace = await loadWorkspace(args);
1901
+ const { default: TreeInk } = await traceImport('TreeInk', ()=>import('./tree.ink.js'));
1902
+ await TreeInk({
1903
+ workspace
1904
+ });
1905
+ }
1906
+ };
1907
+
1908
+ // Utils
1909
+ function dynamicImport(filepath) {
1910
+ return import(/* webpackIgnore: true */ process.platform === 'win32' ? `file://${filepath}` : filepath);
1911
+ }
1912
+
1913
+ const ConfigExplorer = token$('ConfigExplorer', ()=>cosmiconfig('jill', {
1914
+ searchStrategy: 'global',
1915
+ loaders: {
1916
+ '.cjs': (filepath)=>dynamicImport(filepath).then((mod)=>mod.default),
1917
+ '.js': (filepath)=>dynamicImport(filepath).then((mod)=>mod.default),
1918
+ '.json': defaultLoaders['.json'],
1919
+ '.yaml': defaultLoaders['.yaml'],
1920
+ '.yml': defaultLoaders['.yml'],
1921
+ noExt: defaultLoaders.noExt
1922
+ }
1923
+ }));
1924
+
1925
+ var $schema = "http://json-schema.org/draft-07/schema";
1926
+ var type = "object";
1927
+ var additionalProperties = false;
1928
+ var properties = {
1929
+ hooks: {
1930
+ type: "boolean",
1931
+ "default": true,
1932
+ description: "Instructs jill to run hook scripts."
1933
+ },
1934
+ jobs: {
1935
+ type: "number",
1936
+ "default": 0,
1937
+ description: "Number of allowed parallel tasks, defaults to CPU number."
1938
+ }
1939
+ };
1940
+ var schema = {
1941
+ $schema: $schema,
1942
+ type: type,
1943
+ additionalProperties: additionalProperties,
1944
+ properties: properties
1945
+ };
1946
+
1947
+ // Constants
1948
+ const CPU_COUNT = os.cpus().length;
1949
+ /**
1950
+ * Loads and make configuration accessible
1951
+ */ class ConfigService {
1952
+ // Attributes
1953
+ _filepath;
1954
+ _config = var$();
1955
+ _logger = inject$(LOGGER).child(withLabel('config'));
1956
+ _explorer = inject$(ConfigExplorer);
1957
+ // Constructor
1958
+ constructor(state = {}){
1959
+ if (state.filepath) {
1960
+ this._filepath = state.filepath;
1961
+ }
1962
+ if (state.config) {
1963
+ this._config.mutate(state.config);
1964
+ }
1965
+ }
1966
+ // Methods
1967
+ _validateConfig(config) {
1968
+ // Validate config
1969
+ const ajv = new Ajv({
1970
+ allErrors: true,
1971
+ useDefaults: true,
1972
+ logger: this._logger.child(withLabel('ajv')),
1973
+ strict: process$1.env.NODE_ENV === 'development' ? 'log' : true
1974
+ });
1975
+ const validator = ajv.compile(schema);
1976
+ if (!validator(config)) {
1977
+ const errors = ajv.errorsText(validator.errors, {
1978
+ separator: '\n- ',
1979
+ dataVar: 'config'
1980
+ });
1981
+ this._logger.error(`errors in config file:\n- ${errors}`);
1982
+ throw new Error('Error in config file');
1983
+ }
1984
+ // Correct jobs value
1985
+ if (config.jobs <= 0) {
1986
+ Object.assign(config, {
1987
+ jobs: Math.max(CPU_COUNT - 1, 1)
1988
+ });
1989
+ }
1990
+ this._logger.debug`loaded config:\n${qjson(config, {
1991
+ pretty: true
1992
+ })}`;
1993
+ return config;
1994
+ }
1995
+ async searchConfig() {
1996
+ const loaded = await this._explorer.search(inject$(CWD, asyncScope$()));
1997
+ if (loaded) {
1998
+ this._logger.verbose`loaded file ${loaded.filepath}`;
1999
+ this._filepath = loaded.filepath;
2000
+ const config = this._validateConfig(loaded.config);
2001
+ this._config.mutate(config);
2002
+ }
2003
+ }
2004
+ async loadConfig(filepath) {
2005
+ const loaded = await this._explorer.load(filepath);
2006
+ if (loaded) {
2007
+ this._logger.verbose`loaded file ${loaded.filepath}`;
2008
+ this._filepath = loaded.filepath;
2009
+ const config = this._validateConfig(loaded.config);
2010
+ this._config.mutate(config);
2011
+ }
2012
+ }
2013
+ // Attributes
2014
+ get baseDir() {
2015
+ return this._filepath ? path.dirname(this._filepath) : inject$(CWD, asyncScope$());
2016
+ }
2017
+ get config$() {
2018
+ return this._config;
2019
+ }
2020
+ get config() {
2021
+ return this._config.defer();
2022
+ }
2023
+ get state() {
2024
+ return {
2025
+ filepath: this._filepath,
2026
+ config: this.config
2027
+ };
2028
+ }
2029
+ }
2030
+
2031
+ var config_service = /*#__PURE__*/Object.freeze({
2032
+ __proto__: null,
2033
+ ConfigService: ConfigService
2034
+ });
2035
+
2036
+ // Middleware
2037
+ function withConfig(parser) {
2038
+ return parser.option('config-file', {
2039
+ alias: 'c',
2040
+ type: 'string',
2041
+ description: 'Configuration file'
2042
+ }).middleware((args)=>startSpan({
2043
+ name: 'config',
2044
+ op: 'cli.middleware'
2045
+ }, async ()=>{
2046
+ const configService = inject$(ConfigService, asyncScope$());
2047
+ if (args.configFile) {
2048
+ await configService.loadConfig(args.configFile);
2049
+ } else {
2050
+ await configService.searchConfig();
2051
+ }
2052
+ }));
2053
+ }
2054
+
2055
+ const LEVEL_COLORS = {
2056
+ [LogLevel.debug]: 'grey',
2057
+ [LogLevel.verbose]: 'blue',
2058
+ [LogLevel.info]: 'reset',
2059
+ [LogLevel.warning]: 'yellow',
2060
+ [LogLevel.error]: 'red'
2061
+ };
2062
+ const logColor = defineQuickFormat((level)=>LEVEL_COLORS[level])(qprop('level'));
2063
+ const logFormat = qwrap(chalkTemplateStderr).fun`#?:${qprop('label')}{grey [${q$}]} ?#{${logColor} ${qprop('message')} {grey +${qLogDelay(qarg())}}#?:${qerror(qprop('error'))}${os.EOL}${q$}?#}`;
2064
+
2065
+ // Utils
2066
+ const VERBOSITY_LEVEL = {
2067
+ 1: 'verbose',
2068
+ 2: 'debug'
2069
+ };
2070
+ // Middleware
2071
+ function withLogger(parser) {
2072
+ return parser.option('verbose', {
2073
+ alias: 'v',
2074
+ default: 'info',
2075
+ type: 'count',
2076
+ description: 'Set verbosity level',
2077
+ coerce: (cnt)=>VERBOSITY_LEVEL[Math.min(cnt, 2)]
2078
+ }).middleware((args)=>startSpan({
2079
+ name: 'logger',
2080
+ op: 'cli.middleware'
2081
+ }, ()=>{
2082
+ const logLevel = args.verbose ? LogLevel[args.verbose] : LogLevel.info;
2083
+ const logGateway = inject$(LogGateway);
2084
+ flow$(inject$(LOGGER), filter$((log)=>log.level >= logLevel), logDelay$(), logGateway);
2085
+ logGateway.connect('console', toStderr(logFormat));
2086
+ }));
2087
+ }
2088
+
2089
+ // Utils
2090
+ function baseParser() {
2091
+ return pipe$(yargs().scriptName('jill').version(version).demandCommand().recommendCommands(), withLogger, withConfig);
2092
+ }
2093
+ /**
2094
+ * Prepare parser executing commands
2095
+ */ function executeParser() {
2096
+ return pipe$(baseParser(), executeCommand(command$4), executeCommand(command$3), command$5(command$2), executeCommand(command$1), command$5(command));
2097
+ }
2098
+ /**
2099
+ * Prepare parser planning commands
2100
+ */ function planParser(tasks$) {
2101
+ return pipe$(baseParser(), planCommand(command$4, tasks$), planCommand(command$3, tasks$), planCommand(command$2, tasks$), planCommand(command$1, tasks$), planCommand(command, tasks$));
2102
+ }
26
2103
 
27
2104
  // Bootstrap
28
- const argv = hideBin(process.argv);
2105
+ const argv = hideBin(process$1.argv);
29
2106
  const parser = executeParser();
30
- await startSpan({
2107
+ void startSpan({
31
2108
  name: 'jill',
32
2109
  op: 'cli.main',
33
2110
  attributes: {
@@ -37,9 +2114,6 @@ await startSpan({
37
2114
  const logger = inject$(LOGGER);
38
2115
  if (msg) {
39
2116
  logger.error(msg);
40
- captureMessage(msg, {
41
- level: 'error'
42
- });
43
2117
  } else if (err instanceof ClientError) {
44
2118
  logger.warning(err.message);
45
2119
  } else {
@@ -50,6 +2124,8 @@ await startSpan({
50
2124
  });
51
2125
  logger.error(err.message);
52
2126
  }
53
- process.exitCode = 1;
2127
+ process$1.exitCode = 1;
54
2128
  }).parseAsync(argv).catch(()=>{}));
2129
+
2130
+ export { CommandTask as C, LOGGER as L, ScriptTask as S, TASK_MANAGER as T, isScriptCtx as a, CWD as b, capitalize as c, ConfigService as d, instrument as e, isCommandCtx as i, logFormat as l, planParser as p };
55
2131
  //# sourceMappingURL=main.js.map