@jujulego/jill 2.4.0-alpha.2 → 2.4.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.
@@ -0,0 +1,1177 @@
1
+ import { withTimestamp, LogLevel, quick, qlevelColor, toStderr, Logger, logger$, withLabel } from '@jujulego/logger';
2
+ import { Container, decorate, injectable, inject, ContainerModule } from 'inversify';
3
+ import { AsyncLocalStorage } from 'node:async_hooks';
4
+ import { SpawnTask, GroupTask, TaskSet, TaskManager } from '@jujulego/tasks';
5
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
6
+ import { Box, Text, useStderr, render } from 'ink';
7
+ import { useState, useLayoutEffect, useMemo, Fragment as Fragment$1 } from 'react';
8
+ import os from 'node:os';
9
+ import path from 'node:path';
10
+ import Ajv from 'ajv';
11
+ import wt, { BroadcastChannel, setEnvironmentData, getEnvironmentData } from 'node:worker_threads';
12
+ import yargs from 'yargs';
13
+ import { hideBin } from 'yargs/helpers';
14
+ import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
15
+ import { source$, once$, waitFor$ } from '@jujulego/event-tree';
16
+ import Spinner from 'ink-spinner';
17
+ import symbols from 'log-symbols';
18
+ import ms from 'pretty-ms';
19
+ import getDecorators from 'inversify-inject-decorators';
20
+ import 'reflect-metadata';
21
+ import { flow$, filter$, var$ } from '@jujulego/aegis';
22
+ import { qprop } from '@jujulego/quick-tag';
23
+ import { chalkTemplateStderr } from 'chalk-template';
24
+
25
+ // Utils
26
+ async function dynamicImport(filepath) {
27
+ return import(/* webpackIgnore: true */ process.platform === "win32" ? `file://${filepath}` : filepath);
28
+ }
29
+ function fixDefaultExport(mod) {
30
+ return "default" in mod ? mod.default : mod;
31
+ }
32
+
33
+ // Container
34
+ const container = new Container();
35
+ // Utilities
36
+ const { lazyInject, lazyInjectNamed } = fixDefaultExport(getDecorators)(container);
37
+
38
+ // Decorators
39
+ /**
40
+ * Register class as a service
41
+ */ function Service() {
42
+ return (cls)=>{
43
+ decorate(injectable(), cls);
44
+ container.bind(cls).toSelf().inSingletonScope().onActivation((ctx, service)=>{
45
+ if ("onServiceActivate" in service) {
46
+ service.onServiceActivate(ctx);
47
+ }
48
+ return service;
49
+ });
50
+ return cls;
51
+ };
52
+ }
53
+
54
+ // Constants
55
+ const LOG_BROADCAST_CHANNEL = Symbol.for("jujulego:jill:log-broadcast-channel");
56
+ // Parameters
57
+ container.bind(LOG_BROADCAST_CHANNEL).toConstantValue("jujulego:jill:logger");
58
+
59
+ function _ts_decorate$4(decorators, target, key, desc) {
60
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
61
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
62
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
63
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
64
+ }
65
+ function _ts_param$2(paramIndex, decorator) {
66
+ return function(target, key) {
67
+ decorator(target, key, paramIndex);
68
+ };
69
+ }
70
+ let ThreadGateway = class ThreadGateway {
71
+ // Constructor
72
+ constructor(channel){
73
+ this._source = source$();
74
+ this.subscribe = this._source.subscribe;
75
+ this.unsubscribe = this._source.unsubscribe;
76
+ this.clear = this._source.clear;
77
+ this.channel = new BroadcastChannel(channel);
78
+ this.channel.unref();
79
+ this.channel.onmessage = (log)=>{
80
+ this._source.next(log.data);
81
+ };
82
+ this.channel.onmessageerror = (data)=>{
83
+ this._source.next(withTimestamp()({
84
+ level: LogLevel.error,
85
+ message: quick.string`Unable to read message: #!json:${data.data}`
86
+ }));
87
+ };
88
+ }
89
+ // Methods
90
+ next(data) {
91
+ this.channel.postMessage(data);
92
+ }
93
+ };
94
+ ThreadGateway = _ts_decorate$4([
95
+ Service(),
96
+ _ts_param$2(0, inject(LOG_BROADCAST_CHANNEL))
97
+ ], ThreadGateway);
98
+
99
+ function _ts_decorate$3(decorators, target, key, desc) {
100
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
101
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
102
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
103
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
104
+ }
105
+ // Utils
106
+ const jillLogFormat = qlevelColor(quick.wrap(chalkTemplateStderr).function`#?:${qprop("label")}{grey [#$]} ?#${qprop("message")}#?:${qprop("error")}\n#!error$?#`);
107
+ let LogGateway = class LogGateway {
108
+ // Lifecycle
109
+ onServiceActivate({ container }) {
110
+ const threadGtw = container.get(ThreadGateway);
111
+ if (wt.isMainThread) {
112
+ // Redirect logs to stderr
113
+ flow$(this._source, toStderr(jillLogFormat));
114
+ // Add thread gateway as input
115
+ this.connect(threadGtw);
116
+ } else {
117
+ // Redirect logs to thread gateway
118
+ flow$(this._source, threadGtw);
119
+ }
120
+ }
121
+ connect(origin) {
122
+ return flow$(origin, filter$((log)=>log.level >= this.level), this._source);
123
+ }
124
+ // Properties
125
+ get listeners() {
126
+ return Array.from(this._source.listeners);
127
+ }
128
+ get level() {
129
+ return this.level$.read();
130
+ }
131
+ set level(level) {
132
+ this.level$.mutate(level);
133
+ }
134
+ constructor(){
135
+ // Attributes
136
+ this.level$ = var$(LogLevel.info);
137
+ this._source = source$();
138
+ // Methods
139
+ this.subscribe = this._source.subscribe;
140
+ this.unsubscribe = this._source.unsubscribe;
141
+ this.clear = this._source.clear;
142
+ }
143
+ };
144
+ LogGateway = _ts_decorate$3([
145
+ Service()
146
+ ], LogGateway);
147
+
148
+ // Service
149
+ container.bind(Logger).toDynamicValue(()=>logger$(withTimestamp())).inSingletonScope().onActivation(({ container }, logger)=>{
150
+ const gateway = container.get(LogGateway);
151
+ gateway.connect(logger);
152
+ return logger;
153
+ });
154
+
155
+ function _ts_decorate$2(decorators, target, key, desc) {
156
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
157
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
158
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
159
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
160
+ }
161
+ function _ts_param$1(paramIndex, decorator) {
162
+ return function(target, key) {
163
+ decorator(target, key, paramIndex);
164
+ };
165
+ }
166
+ let ContextService = class ContextService {
167
+ // Constructor
168
+ constructor(logger){
169
+ this._storage = new AsyncLocalStorage();
170
+ this._logger = logger.child(withLabel("context"));
171
+ this.reset();
172
+ }
173
+ // Methods
174
+ reset(context = {}) {
175
+ this._storage.enterWith(context);
176
+ }
177
+ _getContext() {
178
+ const ctx = this._storage.getStore();
179
+ if (!ctx) {
180
+ this._logger.warning("Trying to access uninitialized context");
181
+ return {};
182
+ }
183
+ return ctx;
184
+ }
185
+ _updateContext(update) {
186
+ Object.assign(this._getContext(), update);
187
+ }
188
+ // Properties
189
+ get application() {
190
+ return this._getContext().application;
191
+ }
192
+ set application(application) {
193
+ this._updateContext({
194
+ application
195
+ });
196
+ }
197
+ get project() {
198
+ return this._getContext().project;
199
+ }
200
+ set project(project) {
201
+ this._updateContext({
202
+ project
203
+ });
204
+ }
205
+ get workspace() {
206
+ return this._getContext().workspace;
207
+ }
208
+ set workspace(workspace) {
209
+ this._updateContext({
210
+ workspace
211
+ });
212
+ }
213
+ };
214
+ ContextService = _ts_decorate$2([
215
+ Service(),
216
+ _ts_param$1(0, inject(Logger))
217
+ ], ContextService);
218
+
219
+ // Symbols
220
+ const CONFIG_OPTIONS = Symbol("jujulego:jill:config-options");
221
+ // Constants
222
+ const VERBOSITY_LEVEL = {
223
+ 1: "verbose",
224
+ 2: "debug"
225
+ };
226
+ // Options
227
+ function applyConfigOptions(parser) {
228
+ return parser.option("verbose", {
229
+ alias: "v",
230
+ type: "count",
231
+ description: "Set verbosity level",
232
+ coerce: (cnt)=>VERBOSITY_LEVEL[Math.min(cnt, 2)]
233
+ }).option("jobs", {
234
+ alias: "j",
235
+ type: "number",
236
+ description: "Set maximum parallel job number"
237
+ }).option("hooks", {
238
+ type: "boolean",
239
+ description: "Run hook scripts"
240
+ });
241
+ }
242
+ container.bind(CONFIG_OPTIONS).toDynamicValue(()=>{
243
+ const parser = yargs(hideBin(process.argv)).help(false).version(false);
244
+ applyConfigOptions(parser);
245
+ return parser.parse();
246
+ }).inSingletonScope();
247
+
248
+ // Constants
249
+ const CURRENT = "current";
250
+
251
+ // Symbols
252
+ const MODULE = Symbol("jujulego:jill:module");
253
+ const REGISTRY = Symbol("jujulego:jill:registry");
254
+ // Utils
255
+ function getRegistry(target) {
256
+ const registry = Reflect.getMetadata(REGISTRY, target);
257
+ if (typeof registry !== "function") {
258
+ throw new Error(`No registry found in ${target.name}`);
259
+ }
260
+ return registry;
261
+ }
262
+ function setRegistry(target, registry) {
263
+ Reflect.defineMetadata(REGISTRY, registry, target);
264
+ }
265
+ function getModule(target, assert = false) {
266
+ let module = Reflect.getMetadata(MODULE, target);
267
+ if (!module || !(module instanceof ContainerModule)) {
268
+ const registry = Reflect.getMetadata(REGISTRY, target);
269
+ if (typeof registry !== "function") {
270
+ if (assert) {
271
+ throw new Error(`No module found in ${target.name}`);
272
+ }
273
+ return null;
274
+ }
275
+ module = new ContainerModule(registry);
276
+ setModule(target, module);
277
+ }
278
+ return module;
279
+ }
280
+ function setModule(target, module) {
281
+ Reflect.defineMetadata(MODULE, module, target);
282
+ }
283
+
284
+ // Class
285
+ class ExitException extends Error {
286
+ // Constructor
287
+ constructor(code, message){
288
+ super(message);
289
+ this.code = code;
290
+ }
291
+ }
292
+
293
+ // Decorator
294
+ function Middleware() {
295
+ return (target)=>{
296
+ decorate(injectable(), target);
297
+ container.bind(target).toSelf().inSingletonScope();
298
+ };
299
+ }
300
+ // Utils
301
+ function applyMiddlewares(parser, middlewares) {
302
+ let tmp = parser;
303
+ for (const cls of middlewares){
304
+ const middleware = container.get(cls);
305
+ if (middleware.builder) {
306
+ tmp = middleware.builder(tmp);
307
+ }
308
+ tmp.middleware((args)=>middleware.handler(args));
309
+ }
310
+ return tmp;
311
+ }
312
+
313
+ // Symbols
314
+ const COMMAND_OPTS = Symbol("jujulego:jill:command-opts");
315
+ const COMMAND = Symbol("jujulego:jill:command");
316
+ const COMMAND_MODULE = Symbol("jujulego:jill:command-module");
317
+ // Utils
318
+ function getCommandOpts(target) {
319
+ const opts = Reflect.getMetadata(COMMAND_OPTS, target);
320
+ if (typeof opts !== "object") {
321
+ throw new Error(`No command options found in ${target.name}`);
322
+ }
323
+ return opts;
324
+ }
325
+ function buildCommandModule(cmd, opts) {
326
+ return {
327
+ command: opts.command,
328
+ aliases: opts.aliases,
329
+ describe: opts.describe,
330
+ deprecated: opts.deprecated,
331
+ builder (parser) {
332
+ if (opts.middlewares) {
333
+ parser = applyMiddlewares(parser, opts.middlewares);
334
+ }
335
+ if (cmd.builder) {
336
+ parser = cmd.builder(parser);
337
+ }
338
+ return parser;
339
+ },
340
+ handler: async (args)=>{
341
+ try {
342
+ await cmd.handler(args);
343
+ } catch (err) {
344
+ if (err.message) {
345
+ const logger = container.get(Logger);
346
+ logger.error("Error while running command:", err);
347
+ }
348
+ throw new ExitException(1);
349
+ }
350
+ }
351
+ };
352
+ }
353
+ // Decorator
354
+ function Command(opts) {
355
+ return (target)=>{
356
+ decorate(injectable(), target);
357
+ Reflect.defineMetadata(COMMAND_OPTS, opts, target);
358
+ const cmd = opts.command.split(" ")[0];
359
+ setRegistry(target, (bind)=>{
360
+ bind(target).toSelf();
361
+ bind(COMMAND).toDynamicValue(({ container })=>container.getAsync(target)).whenTargetNamed(cmd);
362
+ bind(COMMAND_MODULE).toDynamicValue(async ({ container })=>{
363
+ const cmd = await container.getAsync(target);
364
+ return buildCommandModule(cmd, opts);
365
+ }).whenTargetNamed(cmd);
366
+ });
367
+ };
368
+ }
369
+
370
+ // Symbols
371
+ const AJV = Symbol("jujulego:jill:Ajv");
372
+ // Setup
373
+ container.bind(AJV).toDynamicValue(({ container })=>{
374
+ const logger = container.get(Logger);
375
+ return new Ajv.default({
376
+ allErrors: true,
377
+ logger: logger.child(withLabel("ajv")),
378
+ strict: process.env.NODE_ENV === "development" ? "log" : true
379
+ });
380
+ }).inSingletonScope();
381
+
382
+ function isCacheUpdate(msg) {
383
+ return typeof msg === "object" && msg !== null && "key" in msg && "value" in msg;
384
+ }
385
+ // Chanel
386
+ const channel = new BroadcastChannel("jujulego:jill:worker-cache");
387
+ channel.unref();
388
+ channel.onmessage = (arg)=>{
389
+ const msg = arg;
390
+ if (isCacheUpdate(msg.data)) {
391
+ setEnvironmentData(msg.data.key, msg.data.value);
392
+ }
393
+ };
394
+ // Utils
395
+ async function workerCache(key, compute) {
396
+ const cache = getEnvironmentData(key);
397
+ if (cache !== undefined) return cache;
398
+ // Compute it
399
+ const result = await compute();
400
+ setEnvironmentData(key, result);
401
+ channel.postMessage({
402
+ key,
403
+ value: result
404
+ });
405
+ return result;
406
+ }
407
+
408
+ var $schema = "http://json-schema.org/draft-07/schema";
409
+ var type = "object";
410
+ var additionalProperties = false;
411
+ var properties = {
412
+ jobs: {
413
+ type: "number"
414
+ },
415
+ hooks: {
416
+ type: "boolean",
417
+ "default": true,
418
+ description: "Run hook scripts"
419
+ },
420
+ plugins: {
421
+ type: "array",
422
+ items: {
423
+ type: "string"
424
+ }
425
+ },
426
+ verbose: {
427
+ type: "string",
428
+ "enum": [
429
+ "info",
430
+ "verbose",
431
+ "debug"
432
+ ]
433
+ }
434
+ };
435
+ var schema = {
436
+ $schema: $schema,
437
+ type: type,
438
+ additionalProperties: additionalProperties,
439
+ properties: properties
440
+ };
441
+
442
+ // Symbols
443
+ const CONFIG_EXPLORER = Symbol("jujulego:jill:config-explorer");
444
+ const CONFIG_VALIDATOR = Symbol("jujulego:jill:config-validator");
445
+ // Setup
446
+ container.bind(CONFIG_VALIDATOR).toDynamicValue(({ container })=>{
447
+ const ajv = container.get(AJV);
448
+ return ajv.compile(schema);
449
+ }).inSingletonScope();
450
+ container.bind(CONFIG_EXPLORER).toDynamicValue(()=>{
451
+ return cosmiconfig("jill", {
452
+ loaders: {
453
+ ".cjs": (filepath)=>dynamicImport(filepath).then((mod)=>mod.default),
454
+ ".js": (filepath)=>dynamicImport(filepath).then((mod)=>mod.default),
455
+ ".json": defaultLoaders[".json"],
456
+ ".yaml": defaultLoaders[".yaml"],
457
+ ".yml": defaultLoaders[".yml"],
458
+ noExt: defaultLoaders.noExt
459
+ }
460
+ });
461
+ }).inSingletonScope();
462
+
463
+ // Symbols
464
+ const CONFIG = Symbol("jujulego:jill:config");
465
+ // Loader
466
+ async function configLoader() {
467
+ const logger = container.get(Logger).child(withLabel("config"));
468
+ const options = container.get(CONFIG_OPTIONS);
469
+ const explorer = container.get(CONFIG_EXPLORER);
470
+ const validator = container.get(CONFIG_VALIDATOR);
471
+ // Load file
472
+ const loaded = await explorer.search();
473
+ const config = loaded?.config ?? {};
474
+ // Apply options from cli
475
+ if (options.jobs) config.jobs = options.jobs;
476
+ if (options.verbose) config.verbose = options.verbose;
477
+ if (options.hooks !== undefined) config.hooks = options.hooks;
478
+ // Apply defaults
479
+ config.jobs ??= os.cpus().length - 1;
480
+ config.hooks ??= true;
481
+ // Validate
482
+ if (!validator(config)) {
483
+ const ajv = container.get(AJV);
484
+ const errors = ajv.errorsText(validator.errors, {
485
+ separator: "\n- ",
486
+ dataVar: "config"
487
+ });
488
+ logger.error(`Errors in config file:\n- ${errors}`);
489
+ throw new ExitException(1);
490
+ }
491
+ // Apply on logger
492
+ if (config.verbose) {
493
+ container.get(LogGateway).level = LogLevel[config.verbose];
494
+ }
495
+ if (loaded) {
496
+ // Resolve paths relative to config file
497
+ const base = path.dirname(loaded.filepath);
498
+ config.plugins = config.plugins?.map((plugin)=>path.resolve(base, plugin));
499
+ logger.verbose(`Loaded ${loaded.filepath} config file`);
500
+ }
501
+ logger.debug`Loaded config:\n#!json:${config}`;
502
+ return config;
503
+ }
504
+ container.bind(CONFIG).toDynamicValue(async ()=>await workerCache("jujulego:jill:config", configLoader)).inSingletonScope();
505
+
506
+ function _ts_decorate$1(decorators, target, key, desc) {
507
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
508
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
509
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
510
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
511
+ }
512
+ function _ts_param(paramIndex, decorator) {
513
+ return function(target, key) {
514
+ decorator(target, key, paramIndex);
515
+ };
516
+ }
517
+ let PluginLoaderService = class PluginLoaderService {
518
+ // Constructor
519
+ constructor(_config, logger){
520
+ this._config = _config;
521
+ this._logger = logger.child(withLabel("plugin"));
522
+ }
523
+ // Methods
524
+ async _importPlugin(filepath) {
525
+ this._logger.verbose`Loading plugin ${filepath}`;
526
+ // Load plugin
527
+ let plugin = await dynamicImport(filepath);
528
+ while(plugin && typeof plugin === "object" && "default" in plugin){
529
+ plugin = plugin.default;
530
+ }
531
+ if (!plugin) {
532
+ throw new Error(`Invalid plugin ${filepath}: no plugin class found`);
533
+ }
534
+ // Load module from plugin
535
+ const module = getModule(plugin);
536
+ if (!module) {
537
+ throw new Error(`Invalid plugin ${filepath}: invalid plugin class`);
538
+ }
539
+ return module;
540
+ }
541
+ async loadPlugins(ctn = container) {
542
+ if (!this._config.plugins) return;
543
+ for (const path of this._config.plugins){
544
+ const plugin = await this._importPlugin(path);
545
+ ctn.load(plugin);
546
+ }
547
+ }
548
+ };
549
+ PluginLoaderService = _ts_decorate$1([
550
+ Service(),
551
+ _ts_param(0, inject(CONFIG)),
552
+ _ts_param(1, inject(Logger))
553
+ ], PluginLoaderService);
554
+
555
+ // Utils
556
+ function linesFrom(task, stream) {
557
+ const inner = source$();
558
+ if (task.completed) {
559
+ return inner;
560
+ }
561
+ // Listen to stream
562
+ let current = "";
563
+ const stop = task.on(`stream.${stream}`, (chunk)=>{
564
+ const data = current + chunk.data.toString("utf-8");
565
+ const lines = data.split(/\r?\n/);
566
+ current = lines.pop() ?? "";
567
+ for (const line of lines){
568
+ inner.next(line);
569
+ }
570
+ });
571
+ // Listen to end of task
572
+ once$(task, "completed", ()=>{
573
+ stop();
574
+ if (current) {
575
+ inner.next(current);
576
+ }
577
+ });
578
+ return inner;
579
+ }
580
+
581
+ // Utils
582
+ function isCommandCtx(ctx) {
583
+ return "workspace" in ctx && "command" in ctx;
584
+ }
585
+ // Class
586
+ class CommandTask extends SpawnTask {
587
+ // Constructor
588
+ constructor(workspace, command, args, opts = {}){
589
+ let cmd = command;
590
+ if (opts.superCommand) {
591
+ if (typeof opts.superCommand === "string") {
592
+ opts.superCommand = [
593
+ opts.superCommand
594
+ ];
595
+ }
596
+ if (opts.superCommand.length > 0) {
597
+ cmd = opts.superCommand[0];
598
+ args = [
599
+ ...opts.superCommand.slice(1),
600
+ command,
601
+ ...args
602
+ ];
603
+ }
604
+ }
605
+ super(cmd, args, {
606
+ workspace,
607
+ command
608
+ }, {
609
+ ...opts,
610
+ cwd: workspace.cwd,
611
+ env: {
612
+ FORCE_COLOR: "1",
613
+ ...opts.env
614
+ }
615
+ });
616
+ this.workspace = workspace;
617
+ this._logStreams();
618
+ }
619
+ // Methods
620
+ _logStreams() {
621
+ // TODO: clean up this subscriptions
622
+ linesFrom(this, "stdout").subscribe((line)=>this._logger.info(line));
623
+ linesFrom(this, "stderr").subscribe((line)=>this._logger.info(line));
624
+ }
625
+ }
626
+
627
+ // Utils
628
+ function capitalize(txt) {
629
+ return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase();
630
+ }
631
+ function splitCommandLine(line) {
632
+ line = line.trim();
633
+ const parts = [];
634
+ let current_cote = "";
635
+ let last = 0;
636
+ for(let i = 1; i < line.length; ++i){
637
+ const c = line[i];
638
+ if (current_cote) {
639
+ if (c === current_cote) {
640
+ current_cote = "";
641
+ }
642
+ } else {
643
+ if ([
644
+ '"',
645
+ "'"
646
+ ].includes(c)) {
647
+ current_cote = c;
648
+ } else if (c === " ") {
649
+ parts.push(line.slice(last, i));
650
+ last = i + 1;
651
+ }
652
+ }
653
+ }
654
+ parts.push(line.slice(last));
655
+ return parts;
656
+ }
657
+
658
+ // Utils
659
+ function isScriptCtx(ctx) {
660
+ return "workspace" in ctx && "script" in ctx;
661
+ }
662
+ // Class
663
+ class ScriptTask extends GroupTask {
664
+ // Constructor
665
+ constructor(workspace, script, args, opts){
666
+ super(script, {
667
+ workspace,
668
+ script
669
+ }, opts);
670
+ this.workspace = workspace;
671
+ this.script = script;
672
+ this.args = args;
673
+ this._preHookTasks = null;
674
+ this._postHookTasks = null;
675
+ this._scriptTasks = null;
676
+ this._runHooks = opts?.runHooks ?? true;
677
+ }
678
+ // Methods
679
+ async _runScript(script, args) {
680
+ const line = this.workspace.getScript(script);
681
+ if (!line) {
682
+ return null;
683
+ }
684
+ // Create command task for script
685
+ const [command, ...commandArgs] = splitCommandLine(line);
686
+ if (command === "jill") {
687
+ this._logger.debug(`Interpreting ${line}`);
688
+ const argv = commandArgs.map((arg)=>arg.replace(/^["'](.+)["']$/, "$1"));
689
+ const { JillApplication } = await import('./jill.application-NaKSF97o.js').then(function (n) { return n.j; });
690
+ const app = container.get(JillApplication);
691
+ const tasks = await app.tasksOf(argv, {
692
+ project: this.project,
693
+ workspace: this.workspace
694
+ });
695
+ if (tasks.length) {
696
+ const set = new TaskSet();
697
+ for (const tsk of tasks){
698
+ set.add(tsk);
699
+ }
700
+ return set;
701
+ }
702
+ }
703
+ const pm = await this.workspace.project.packageManager();
704
+ const set = new TaskSet();
705
+ set.add(new CommandTask(this.workspace, command, [
706
+ ...commandArgs,
707
+ ...args
708
+ ], {
709
+ logger: this._logger,
710
+ superCommand: pm === "yarn" ? [
711
+ "yarn",
712
+ "exec"
713
+ ] : undefined
714
+ }));
715
+ return set;
716
+ }
717
+ async prepare() {
718
+ // Prepare script run
719
+ this._scriptTasks = await this._runScript(this.script, this.args);
720
+ if (!this._scriptTasks) {
721
+ throw new Error(`No script ${this.script} in ${this.workspace.name}`);
722
+ }
723
+ // Prepare hooks run
724
+ if (this._runHooks) {
725
+ this._preHookTasks = await this._runScript(`pre${this.script}`, []);
726
+ this._postHookTasks = await this._runScript(`post${this.script}`, []);
727
+ }
728
+ // Add tasks to group
729
+ if (this._preHookTasks) {
730
+ this._logger.verbose(`Found pre-hook script "pre${this.script}"`);
731
+ for (const tsk of this._preHookTasks){
732
+ this.add(tsk);
733
+ }
734
+ }
735
+ for (const tsk of this._scriptTasks){
736
+ this.add(tsk);
737
+ }
738
+ if (this._postHookTasks) {
739
+ this._logger.verbose(`Found post-hook script "post${this.script}"`);
740
+ for (const tsk of this._postHookTasks){
741
+ this.add(tsk);
742
+ }
743
+ }
744
+ }
745
+ async *_orchestrate() {
746
+ if (!this._scriptTasks) {
747
+ throw new Error("ScriptTask needs to be prepared. Call prepare before starting it");
748
+ }
749
+ // Run pre-hook
750
+ if (this._preHookTasks) {
751
+ yield* this._preHookTasks;
752
+ if (await this._hasFailed(this._preHookTasks)) {
753
+ return this.setStatus("failed");
754
+ }
755
+ }
756
+ // Run script
757
+ yield* this._scriptTasks;
758
+ if (await this._hasFailed(this._scriptTasks)) {
759
+ return this.setStatus("failed");
760
+ }
761
+ // Run post-hook
762
+ if (this._postHookTasks) {
763
+ yield* this._postHookTasks;
764
+ if (await this._hasFailed(this._postHookTasks)) {
765
+ return this.setStatus("failed");
766
+ }
767
+ }
768
+ this.setStatus("done");
769
+ }
770
+ async _hasFailed(set) {
771
+ const results = await waitFor$(set, "finished");
772
+ return results.failed > 0;
773
+ }
774
+ _stop() {
775
+ if (!this._scriptTasks) return;
776
+ for (const tsk of this._scriptTasks){
777
+ tsk.stop();
778
+ }
779
+ }
780
+ complexity(cache = new Map()) {
781
+ let complexity = super.complexity(cache);
782
+ if (this._scriptTasks) {
783
+ complexity += this._scriptTasks.tasks.reduce((cpl, tsk)=>cpl + tsk.complexity(cache), 0);
784
+ }
785
+ cache.set(this.id, complexity);
786
+ return complexity;
787
+ }
788
+ // Properties
789
+ get project() {
790
+ return this.workspace.project;
791
+ }
792
+ }
793
+
794
+ // Symbols
795
+ const TASK_MANAGER = Symbol("jujulego:jill:TaskManager");
796
+ // Service
797
+ container.bind(TASK_MANAGER).toDynamicValue(({ container })=>{
798
+ const config = container.get(CONFIG);
799
+ const logger = container.get(Logger);
800
+ return new TaskManager({
801
+ jobs: config.jobs,
802
+ logger
803
+ });
804
+ }).inSingletonScope();
805
+
806
+ // Component
807
+ function List({ items, headers }) {
808
+ if (items.length === 0) {
809
+ return null;
810
+ }
811
+ return /*#__PURE__*/ jsx(Box, {
812
+ children: Object.keys(items[0]).map((key)=>/*#__PURE__*/ jsxs(Box, {
813
+ flexDirection: "column",
814
+ marginRight: 2,
815
+ children: [
816
+ headers && /*#__PURE__*/ jsx(Text, {
817
+ bold: true,
818
+ children: capitalize(key)
819
+ }),
820
+ items.map((item, idx)=>/*#__PURE__*/ jsx(Text, {
821
+ children: item[key] || " "
822
+ }, idx))
823
+ ]
824
+ }, key))
825
+ });
826
+ }
827
+
828
+ // Components
829
+ function TaskName({ task }) {
830
+ if (isScriptCtx(task.context)) {
831
+ return /*#__PURE__*/ jsxs(Text, {
832
+ children: [
833
+ "Run ",
834
+ /*#__PURE__*/ jsx(Text, {
835
+ bold: true,
836
+ children: task.context.script
837
+ }),
838
+ " in ",
839
+ task.context.workspace.name
840
+ ]
841
+ });
842
+ } else {
843
+ return /*#__PURE__*/ jsx(Text, {
844
+ children: task.name
845
+ });
846
+ }
847
+ }
848
+
849
+ // Components
850
+ function TaskSpinner({ task }) {
851
+ // State
852
+ const [status, setStatus] = useState(task.status);
853
+ const [time, setTime] = useState(0);
854
+ // Effects
855
+ useLayoutEffect(()=>{
856
+ return task.on("status", (event)=>{
857
+ setStatus(event.status);
858
+ });
859
+ }, [
860
+ task
861
+ ]);
862
+ useLayoutEffect(()=>{
863
+ return task.on("completed", ({ duration })=>{
864
+ setTime(duration);
865
+ });
866
+ }, [
867
+ task
868
+ ]);
869
+ // Render
870
+ const isScriptChild = isCommandCtx(task.context) && task.group && isScriptCtx(task.group.context);
871
+ switch(status){
872
+ case "blocked":
873
+ case "ready":
874
+ case "starting":
875
+ return /*#__PURE__*/ jsxs(Box, {
876
+ children: [
877
+ /*#__PURE__*/ jsx(Text, {
878
+ color: "grey",
879
+ children: "\xb7"
880
+ }),
881
+ /*#__PURE__*/ jsx(Box, {
882
+ paddingLeft: 1,
883
+ children: /*#__PURE__*/ jsx(Text, {
884
+ color: "grey",
885
+ wrap: "truncate",
886
+ children: /*#__PURE__*/ jsx(TaskName, {
887
+ task: task
888
+ })
889
+ })
890
+ })
891
+ ]
892
+ });
893
+ case "running":
894
+ return /*#__PURE__*/ jsxs(Box, {
895
+ children: [
896
+ /*#__PURE__*/ jsx(Text, {
897
+ color: isScriptChild ? "dim" : undefined,
898
+ children: /*#__PURE__*/ jsx(Spinner, {})
899
+ }),
900
+ /*#__PURE__*/ jsx(Box, {
901
+ paddingLeft: 1,
902
+ children: /*#__PURE__*/ jsx(Text, {
903
+ color: isScriptChild ? "dim" : undefined,
904
+ wrap: "truncate",
905
+ children: /*#__PURE__*/ jsx(TaskName, {
906
+ task: task
907
+ })
908
+ })
909
+ })
910
+ ]
911
+ });
912
+ case "done":
913
+ return /*#__PURE__*/ jsxs(Box, {
914
+ children: [
915
+ /*#__PURE__*/ jsx(Text, {
916
+ color: "green",
917
+ children: symbols.success
918
+ }),
919
+ /*#__PURE__*/ jsx(Box, {
920
+ paddingLeft: 1,
921
+ children: /*#__PURE__*/ jsx(Text, {
922
+ color: isScriptChild ? "dim" : undefined,
923
+ wrap: "truncate",
924
+ children: /*#__PURE__*/ jsx(TaskName, {
925
+ task: task
926
+ })
927
+ })
928
+ }),
929
+ /*#__PURE__*/ jsx(Box, {
930
+ paddingLeft: 1,
931
+ flexShrink: 0,
932
+ children: /*#__PURE__*/ jsxs(Text, {
933
+ color: isScriptChild ? "grey" : "dim",
934
+ children: [
935
+ "(took ",
936
+ ms(time),
937
+ ")"
938
+ ]
939
+ })
940
+ })
941
+ ]
942
+ });
943
+ case "failed":
944
+ return /*#__PURE__*/ jsxs(Box, {
945
+ children: [
946
+ /*#__PURE__*/ jsx(Text, {
947
+ color: "red",
948
+ children: symbols.error
949
+ }),
950
+ /*#__PURE__*/ jsx(Box, {
951
+ paddingLeft: 1,
952
+ children: /*#__PURE__*/ jsx(Text, {
953
+ color: isScriptChild ? "dim" : undefined,
954
+ wrap: "truncate",
955
+ children: /*#__PURE__*/ jsx(TaskName, {
956
+ task: task
957
+ })
958
+ })
959
+ }),
960
+ /*#__PURE__*/ jsx(Box, {
961
+ paddingLeft: 1,
962
+ flexShrink: 0,
963
+ children: /*#__PURE__*/ jsxs(Text, {
964
+ color: isScriptChild ? "grey" : "dim",
965
+ children: [
966
+ "(took ",
967
+ ms(time),
968
+ ")"
969
+ ]
970
+ })
971
+ })
972
+ ]
973
+ });
974
+ }
975
+ }
976
+
977
+ // Components
978
+ function GroupTaskSpinner({ group }) {
979
+ // State
980
+ const [verbose, setVerbose] = useState(false);
981
+ const [status, setStatus] = useState(group.status);
982
+ const [tasks, setTasks] = useState([
983
+ ...group.tasks
984
+ ]);
985
+ const [canReduce, setCanReduce] = useState(true);
986
+ // Memo
987
+ const forceExtended = useMemo(()=>verbose || tasks.some((tsk)=>!isCommandCtx(tsk.context)), [
988
+ verbose,
989
+ tasks
990
+ ]);
991
+ const isReduced = useMemo(()=>!forceExtended && canReduce, [
992
+ forceExtended,
993
+ canReduce
994
+ ]);
995
+ // Effects
996
+ useLayoutEffect(()=>{
997
+ const config = container.get(CONFIG);
998
+ if (config.verbose) {
999
+ setVerbose([
1000
+ "verbose",
1001
+ "debug"
1002
+ ].includes(config.verbose));
1003
+ }
1004
+ }, []);
1005
+ useLayoutEffect(()=>{
1006
+ return group.on("status", (event)=>{
1007
+ setStatus(event.status);
1008
+ });
1009
+ }, [
1010
+ group
1011
+ ]);
1012
+ useLayoutEffect(()=>{
1013
+ let dirty = false;
1014
+ return group.on("task.added", ()=>{
1015
+ if (!dirty) {
1016
+ dirty = true;
1017
+ queueMicrotask(()=>{
1018
+ setTasks([
1019
+ ...group.tasks
1020
+ ]);
1021
+ dirty = false;
1022
+ });
1023
+ }
1024
+ });
1025
+ }, [
1026
+ group
1027
+ ]);
1028
+ useLayoutEffect(()=>{
1029
+ if (status === "running") {
1030
+ setCanReduce(false);
1031
+ } else if (status === "done") {
1032
+ setCanReduce(true);
1033
+ }
1034
+ }, [
1035
+ status
1036
+ ]);
1037
+ // Render
1038
+ return /*#__PURE__*/ jsxs(Fragment, {
1039
+ children: [
1040
+ /*#__PURE__*/ jsx(TaskSpinner, {
1041
+ task: group
1042
+ }),
1043
+ isReduced || /*#__PURE__*/ jsx(Box, {
1044
+ flexDirection: "column",
1045
+ marginLeft: 2,
1046
+ children: tasks.map((task)=>/*#__PURE__*/ jsx(Fragment$1, {
1047
+ children: task instanceof GroupTask ? /*#__PURE__*/ jsx(GroupTaskSpinner, {
1048
+ group: task
1049
+ }) : /*#__PURE__*/ jsx(TaskSpinner, {
1050
+ task: task
1051
+ })
1052
+ }, task.id))
1053
+ })
1054
+ ]
1055
+ });
1056
+ }
1057
+
1058
+ // Utils
1059
+ function taskPredicate(task) {
1060
+ if (task.group) {
1061
+ return false;
1062
+ }
1063
+ if ("hidden" in task.context && task.context.hidden) {
1064
+ return false;
1065
+ }
1066
+ return true;
1067
+ }
1068
+ // Components
1069
+ function TaskManagerSpinner({ manager }) {
1070
+ const [tasks, setTasks] = useState(manager.tasks.filter(taskPredicate));
1071
+ useLayoutEffect(()=>{
1072
+ let dirty = false;
1073
+ return manager.on("added", ()=>{
1074
+ if (!dirty) {
1075
+ dirty = true;
1076
+ queueMicrotask(()=>{
1077
+ setTasks(manager.tasks.filter(taskPredicate));
1078
+ dirty = false;
1079
+ });
1080
+ }
1081
+ });
1082
+ }, [
1083
+ manager
1084
+ ]);
1085
+ return /*#__PURE__*/ jsx(Fragment, {
1086
+ children: tasks.map((task)=>task instanceof GroupTask ? /*#__PURE__*/ jsx(GroupTaskSpinner, {
1087
+ group: task
1088
+ }, task.id) : /*#__PURE__*/ jsx(TaskSpinner, {
1089
+ task: task
1090
+ }, task.id))
1091
+ });
1092
+ }
1093
+
1094
+ // Utils
1095
+ function printJson(data, stream = process.stdout) {
1096
+ if (stream.isTTY) {
1097
+ stream.write(JSON.stringify(data, null, 2));
1098
+ } else {
1099
+ stream.write(JSON.stringify(data));
1100
+ }
1101
+ }
1102
+
1103
+ // Component
1104
+ function StaticLogs() {
1105
+ // State
1106
+ const { write } = useStderr();
1107
+ // Effect
1108
+ useLayoutEffect(()=>{
1109
+ const gateway = container.get(LogGateway);
1110
+ // Remove Console transport
1111
+ const listeners = gateway.listeners;
1112
+ gateway.clear();
1113
+ // Add custom transport
1114
+ const off = gateway.subscribe((log)=>{
1115
+ write(jillLogFormat(log) + "\n");
1116
+ });
1117
+ return ()=>{
1118
+ off();
1119
+ // Restore previous listeners
1120
+ for (const lst of listeners){
1121
+ gateway.subscribe(lst);
1122
+ }
1123
+ };
1124
+ }, [
1125
+ write
1126
+ ]);
1127
+ return null;
1128
+ }
1129
+
1130
+ // Component
1131
+ function Layout({ children }) {
1132
+ return /*#__PURE__*/ jsxs(Fragment, {
1133
+ children: [
1134
+ /*#__PURE__*/ jsx(StaticLogs, {}),
1135
+ children
1136
+ ]
1137
+ });
1138
+ }
1139
+
1140
+ // Constants
1141
+ const INK_APP = Symbol.for("jujulego:jill:ink-app");
1142
+ // Setup
1143
+ container.bind(INK_APP).toDynamicValue(()=>{
1144
+ if (!wt.isMainThread) {
1145
+ throw new Error("Ink should only be used in main thread");
1146
+ }
1147
+ return render(/*#__PURE__*/ jsx(Layout, {}));
1148
+ }).inSingletonScope();
1149
+
1150
+ function _ts_decorate(decorators, target, key, desc) {
1151
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1152
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1153
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1154
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1155
+ }
1156
+ let InkCommand = class InkCommand {
1157
+ builder(parser) {
1158
+ return parser;
1159
+ }
1160
+ async handler(args) {
1161
+ for await (const children of this.render(args)){
1162
+ this.app.rerender(/*#__PURE__*/ jsx(Layout, {
1163
+ children: children
1164
+ }));
1165
+ }
1166
+ await this.app.waitUntilExit();
1167
+ }
1168
+ };
1169
+ _ts_decorate([
1170
+ lazyInject(INK_APP)
1171
+ ], InkCommand.prototype, "app", void 0);
1172
+ InkCommand = _ts_decorate([
1173
+ injectable()
1174
+ ], InkCommand);
1175
+
1176
+ export { capitalize as A, splitCommandLine as B, CONFIG as C, workerCache as D, ExitException as E, ContextService as F, GroupTaskSpinner as G, applyConfigOptions as H, InkCommand as I, Layout as L, Middleware as M, PluginLoaderService as P, Service as S, TaskManagerSpinner as T, List as a, TaskName as b, TaskSpinner as c, COMMAND as d, COMMAND_MODULE as e, buildCommandModule as f, getCommandOpts as g, Command as h, applyMiddlewares as i, getRegistry as j, getModule as k, setModule as l, CURRENT as m, container as n, lazyInject as o, lazyInjectNamed as p, isCommandCtx as q, CommandTask as r, setRegistry as s, isScriptCtx as t, ScriptTask as u, TASK_MANAGER as v, linesFrom as w, dynamicImport as x, fixDefaultExport as y, printJson as z };
1177
+ //# sourceMappingURL=ink-command-SIxVoU_e.js.map