@spica/cli 0.20.7 → 0.20.8
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/index.js +126 -47
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// apps/cli/index.ts
|
|
2
|
-
import
|
|
2
|
+
import caporalCore8 from "@caporal/core";
|
|
3
3
|
|
|
4
4
|
// apps/cli/src/console.ts
|
|
5
5
|
import { blue, bold, red, yellow } from "colorette";
|
|
@@ -3443,6 +3443,83 @@ var ChangeKind;
|
|
|
3443
3443
|
})(ChangeKind || (ChangeKind = {}));
|
|
3444
3444
|
var DEFAULT_CONCURRENCY = 10;
|
|
3445
3445
|
|
|
3446
|
+
// packages/sync/dist/src/concurrency.js
|
|
3447
|
+
var REQUEST_METHODS = ["get", "delete", "head", "post", "patch", "put"];
|
|
3448
|
+
var limitedClients = /* @__PURE__ */ new WeakMap();
|
|
3449
|
+
function limitConcurrency(http2, limit = DEFAULT_CONCURRENCY, signal) {
|
|
3450
|
+
const existing = limitedClients.get(http2);
|
|
3451
|
+
if (existing && !signal)
|
|
3452
|
+
return http2;
|
|
3453
|
+
const { semaphore, target } = existing ?? {
|
|
3454
|
+
semaphore: new Semaphore(normalizeLimit(limit)),
|
|
3455
|
+
target: http2
|
|
3456
|
+
};
|
|
3457
|
+
const limited = { ...target };
|
|
3458
|
+
for (const method of REQUEST_METHODS) {
|
|
3459
|
+
limited[method] = (...args) => semaphore.run(() => {
|
|
3460
|
+
if (signal?.aborted)
|
|
3461
|
+
throw signal.reason;
|
|
3462
|
+
return target[method](...args);
|
|
3463
|
+
});
|
|
3464
|
+
}
|
|
3465
|
+
limitedClients.set(limited, { semaphore, target });
|
|
3466
|
+
return limited;
|
|
3467
|
+
}
|
|
3468
|
+
async function mapWithConcurrency(items, limit, fn) {
|
|
3469
|
+
const results = new Array(items.length);
|
|
3470
|
+
let next = 0;
|
|
3471
|
+
let failure;
|
|
3472
|
+
const worker = async () => {
|
|
3473
|
+
while (!failure && next < items.length) {
|
|
3474
|
+
const index = next++;
|
|
3475
|
+
try {
|
|
3476
|
+
results[index] = await fn(items[index], index);
|
|
3477
|
+
} catch (error) {
|
|
3478
|
+
if (!failure)
|
|
3479
|
+
failure = { error };
|
|
3480
|
+
}
|
|
3481
|
+
}
|
|
3482
|
+
};
|
|
3483
|
+
const workerCount = Math.min(normalizeLimit(limit), items.length);
|
|
3484
|
+
await Promise.all(Array.from({ length: workerCount }, worker));
|
|
3485
|
+
if (failure)
|
|
3486
|
+
throw failure.error;
|
|
3487
|
+
return results;
|
|
3488
|
+
}
|
|
3489
|
+
function normalizeLimit(limit) {
|
|
3490
|
+
return Number.isFinite(limit) ? Math.max(1, Math.floor(limit)) : DEFAULT_CONCURRENCY;
|
|
3491
|
+
}
|
|
3492
|
+
var Semaphore = class {
|
|
3493
|
+
constructor(limit) {
|
|
3494
|
+
this.limit = limit;
|
|
3495
|
+
this.active = 0;
|
|
3496
|
+
this.waiting = [];
|
|
3497
|
+
}
|
|
3498
|
+
async run(task) {
|
|
3499
|
+
await this.acquire();
|
|
3500
|
+
try {
|
|
3501
|
+
return await task();
|
|
3502
|
+
} finally {
|
|
3503
|
+
this.release();
|
|
3504
|
+
}
|
|
3505
|
+
}
|
|
3506
|
+
acquire() {
|
|
3507
|
+
if (this.active < this.limit) {
|
|
3508
|
+
this.active++;
|
|
3509
|
+
return Promise.resolve();
|
|
3510
|
+
}
|
|
3511
|
+
return new Promise((resolve) => this.waiting.push(resolve));
|
|
3512
|
+
}
|
|
3513
|
+
release() {
|
|
3514
|
+
const next = this.waiting.shift();
|
|
3515
|
+
if (next) {
|
|
3516
|
+
next();
|
|
3517
|
+
} else {
|
|
3518
|
+
this.active--;
|
|
3519
|
+
}
|
|
3520
|
+
}
|
|
3521
|
+
};
|
|
3522
|
+
|
|
3446
3523
|
// packages/sync/dist/src/reporter.js
|
|
3447
3524
|
var noopProgress = {
|
|
3448
3525
|
update() {
|
|
@@ -3547,13 +3624,14 @@ import { bold as bold4, cyan, green as green4, red as red4, yellow as yellow2 }
|
|
|
3547
3624
|
import isEqual2 from "lodash/isEqual.js";
|
|
3548
3625
|
import { createTwoFilesPatch } from "diff";
|
|
3549
3626
|
import yaml2 from "yaml";
|
|
3550
|
-
async function buildPlan(modules,
|
|
3627
|
+
async function buildPlan(modules, unlimitedHttp, rootDir, options = {}) {
|
|
3551
3628
|
const { detailed = false, detectRenames = true, reporter = silentReporter, concurrency = DEFAULT_CONCURRENCY, source } = options;
|
|
3629
|
+
const http2 = limitConcurrency(unlimitedHttp, concurrency);
|
|
3552
3630
|
const modulePlans = [];
|
|
3553
3631
|
for (const mod of modules) {
|
|
3554
3632
|
const [locals, remotes] = await Promise.all([
|
|
3555
3633
|
reporter.task(`Reading local ${mod.displayName} files`, () => mod.readLocal(rootDir, source)),
|
|
3556
|
-
reporter.task(`Fetching remote ${mod.displayName}`, () => mod.readRemote(http2
|
|
3634
|
+
reporter.task(`Fetching remote ${mod.displayName}`, () => mod.readRemote(http2))
|
|
3557
3635
|
]);
|
|
3558
3636
|
const localBySlug = new Map(locals.map((l) => [l.slug, l]));
|
|
3559
3637
|
const remoteBySlug = new Map(remotes.map((r) => [r.slug, r]));
|
|
@@ -3750,29 +3828,30 @@ function renderUnifiedDiff(diff, reversed = false) {
|
|
|
3750
3828
|
}
|
|
3751
3829
|
}
|
|
3752
3830
|
}
|
|
3753
|
-
async function applyPlan(plan2,
|
|
3831
|
+
async function applyPlan(plan2, unlimitedHttp, opts = {}) {
|
|
3754
3832
|
const concurrency = opts.concurrency ?? DEFAULT_CONCURRENCY;
|
|
3755
3833
|
const reporter = opts.reporter ?? silentReporter;
|
|
3756
3834
|
const errors = [];
|
|
3835
|
+
const abortController = new AbortController();
|
|
3836
|
+
const http2 = limitConcurrency(unlimitedHttp, concurrency, abortController.signal);
|
|
3837
|
+
let abortError;
|
|
3757
3838
|
const handleErr = (label, e) => {
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3839
|
+
if (abortError)
|
|
3840
|
+
throw abortError;
|
|
3841
|
+
const full = `${label}: ${formatError(e)}`;
|
|
3842
|
+
if (opts.abortOnError) {
|
|
3843
|
+
abortError = new Error(full);
|
|
3844
|
+
abortController.abort(abortError);
|
|
3845
|
+
throw abortError;
|
|
3846
|
+
}
|
|
3762
3847
|
reporter.warn(full);
|
|
3763
3848
|
errors.push(full);
|
|
3764
3849
|
};
|
|
3765
3850
|
for (const mp of plan2.modules) {
|
|
3766
3851
|
const { module: mod, creates, updates, deletes } = mp;
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
}
|
|
3770
|
-
if (updates.length) {
|
|
3771
|
-
await runConcurrently(updates.map((e) => ({ slug: e.slug, run: () => mod.update(http2, e.local, e.remote.id) })), concurrency, `Updating ${mod.displayName}`, handleErr, reporter);
|
|
3772
|
-
}
|
|
3773
|
-
if (creates.length) {
|
|
3774
|
-
await runConcurrently(creates.map((e) => ({ slug: e.slug, run: () => mod.create(http2, e.local) })), concurrency, `Creating ${mod.displayName}`, handleErr, reporter);
|
|
3775
|
-
}
|
|
3852
|
+
await runAllAtOnce(deletes.map((e) => ({ slug: e.slug, run: () => mod.delete(http2, e.remote.id) })), `Deleting ${mod.displayName}`, handleErr, reporter);
|
|
3853
|
+
await runAllAtOnce(updates.map((e) => ({ slug: e.slug, run: () => mod.update(http2, e.local, e.remote.id) })), `Updating ${mod.displayName}`, handleErr, reporter);
|
|
3854
|
+
await runAllAtOnce(creates.map((e) => ({ slug: e.slug, run: () => mod.create(http2, e.local) })), `Creating ${mod.displayName}`, handleErr, reporter);
|
|
3776
3855
|
}
|
|
3777
3856
|
return { errors };
|
|
3778
3857
|
}
|
|
@@ -3837,6 +3916,9 @@ function renderSchemaDetail(localData, remoteData, ignoredFields) {
|
|
|
3837
3916
|
const remoteYaml = yaml2.stringify(omit(remoteData, ignoredFields));
|
|
3838
3917
|
return { schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml") };
|
|
3839
3918
|
}
|
|
3919
|
+
function runAllAtOnce(tasks, label, onError, reporter) {
|
|
3920
|
+
return runConcurrently(tasks, tasks.length, label, onError, reporter);
|
|
3921
|
+
}
|
|
3840
3922
|
async function runConcurrently(tasks, concurrency, label, onError, reporter) {
|
|
3841
3923
|
if (!tasks.length)
|
|
3842
3924
|
return;
|
|
@@ -3844,18 +3926,10 @@ async function runConcurrently(tasks, concurrency, label, onError, reporter) {
|
|
|
3844
3926
|
let done = 0;
|
|
3845
3927
|
const progress = reporter.progress(label, total);
|
|
3846
3928
|
try {
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
if (batch.length === limit || i === tasks.length - 1) {
|
|
3852
|
-
await Promise.all(batch.map((task) => task.run().catch((e) => onError(`${label}: ${task.slug}`, e)).finally(() => {
|
|
3853
|
-
done++;
|
|
3854
|
-
progress.update(task.slug, done, total);
|
|
3855
|
-
})));
|
|
3856
|
-
batch = [];
|
|
3857
|
-
}
|
|
3858
|
-
}
|
|
3929
|
+
await mapWithConcurrency(tasks, concurrency, (task) => task.run().catch((e) => onError(`${label}: ${task.slug}`, e)).finally(() => {
|
|
3930
|
+
done++;
|
|
3931
|
+
progress.update(task.slug, done, total);
|
|
3932
|
+
}));
|
|
3859
3933
|
progress.succeed();
|
|
3860
3934
|
} catch (e) {
|
|
3861
3935
|
progress.fail();
|
|
@@ -3998,14 +4072,6 @@ function normalizeSchema(schema) {
|
|
|
3998
4072
|
}
|
|
3999
4073
|
return normalized;
|
|
4000
4074
|
}
|
|
4001
|
-
async function mapInBatches(items, size, fn) {
|
|
4002
|
-
const limit = Math.max(1, size);
|
|
4003
|
-
const results = [];
|
|
4004
|
-
for (let i = 0; i < items.length; i += limit) {
|
|
4005
|
-
results.push(...await Promise.all(items.slice(i, i + limit).map(fn)));
|
|
4006
|
-
}
|
|
4007
|
-
return results;
|
|
4008
|
-
}
|
|
4009
4075
|
function isNotFoundError(error) {
|
|
4010
4076
|
if (!error || typeof error !== "object")
|
|
4011
4077
|
return false;
|
|
@@ -4065,10 +4131,10 @@ var functionModule = {
|
|
|
4065
4131
|
}
|
|
4066
4132
|
return results;
|
|
4067
4133
|
},
|
|
4068
|
-
async readRemote(http2
|
|
4134
|
+
async readRemote(http2) {
|
|
4069
4135
|
const res = await http2.get("function");
|
|
4070
4136
|
const fns = unwrapList(res);
|
|
4071
|
-
return
|
|
4137
|
+
return Promise.all(fns.map(async (fn) => {
|
|
4072
4138
|
const id = fn._id;
|
|
4073
4139
|
const [indexRes, depsRes] = await Promise.all([
|
|
4074
4140
|
http2.get(`function/${id}/index`).catch(emptyIfMissing({ index: "" })),
|
|
@@ -4089,7 +4155,7 @@ var functionModule = {
|
|
|
4089
4155
|
dependencies
|
|
4090
4156
|
}
|
|
4091
4157
|
};
|
|
4092
|
-
});
|
|
4158
|
+
}));
|
|
4093
4159
|
},
|
|
4094
4160
|
async create(http2, local) {
|
|
4095
4161
|
const id = await this.createSchema(http2, local.data.schema);
|
|
@@ -4651,8 +4717,10 @@ function fetch_default(program2) {
|
|
|
4651
4717
|
// apps/cli/src/commands/sync/dev.ts
|
|
4652
4718
|
import path14 from "path";
|
|
4653
4719
|
import chokidar from "chokidar";
|
|
4720
|
+
import caporalCore7 from "@caporal/core";
|
|
4654
4721
|
import { bold as bold9, cyan as cyan3, green as green8, red as red7, yellow as yellow7 } from "colorette";
|
|
4655
4722
|
import ora3 from "ora";
|
|
4723
|
+
var { CaporalValidator: CaporalValidator7 } = caporalCore7;
|
|
4656
4724
|
function createEmptyState() {
|
|
4657
4725
|
return { remoteIds: /* @__PURE__ */ new Map() };
|
|
4658
4726
|
}
|
|
@@ -4886,6 +4954,7 @@ function formatError2(e) {
|
|
|
4886
4954
|
async function dev({ args, options }) {
|
|
4887
4955
|
const rootDir = path14.resolve(args.dir ?? process.cwd());
|
|
4888
4956
|
const moduleFilter = options.module ? (Array.isArray(options.module) ? options.module : [options.module]).map(String) : void 0;
|
|
4957
|
+
const concurrency = options.concurrency;
|
|
4889
4958
|
const modules = resolveModules(moduleFilter);
|
|
4890
4959
|
let ctx;
|
|
4891
4960
|
try {
|
|
@@ -4902,14 +4971,17 @@ ${bold9(red7(`\u2717 ${formatError2(err)}`))}`);
|
|
|
4902
4971
|
Tip: Start a local instance with ${bold9("spica project start")} and switch to it with ${bold9("spica context switch <name>")} before running ${bold9("sync dev")}.`
|
|
4903
4972
|
);
|
|
4904
4973
|
}
|
|
4905
|
-
const http2 =
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4974
|
+
const http2 = limitConcurrency(
|
|
4975
|
+
httpService.create({
|
|
4976
|
+
baseUrl: ctx.url,
|
|
4977
|
+
authorization: ctx.authorization
|
|
4978
|
+
}),
|
|
4979
|
+
concurrency
|
|
4980
|
+
);
|
|
4909
4981
|
console.log(bold9("\nBuilding plan\u2026"));
|
|
4910
4982
|
let plan2;
|
|
4911
4983
|
try {
|
|
4912
|
-
plan2 = await buildPlan(modules, http2, rootDir, { reporter: cliReporter });
|
|
4984
|
+
plan2 = await buildPlan(modules, http2, rootDir, { reporter: cliReporter, concurrency });
|
|
4913
4985
|
} catch (err) {
|
|
4914
4986
|
console.log(`
|
|
4915
4987
|
${bold9(red7(`\u2717 Failed to build plan: ${formatError2(err)}`))}`);
|
|
@@ -4931,7 +5003,7 @@ ${bold9(red7(`\u2717 Failed to build plan: ${formatError2(err)}`))}`);
|
|
|
4931
5003
|
console.log(bold9("\nApplying changes\u2026"));
|
|
4932
5004
|
try {
|
|
4933
5005
|
const { errors } = await applyPlan(plan2, http2, {
|
|
4934
|
-
concurrency
|
|
5006
|
+
concurrency,
|
|
4935
5007
|
abortOnError: true,
|
|
4936
5008
|
reporter: cliReporter
|
|
4937
5009
|
});
|
|
@@ -5001,6 +5073,13 @@ function dev_default(program2) {
|
|
|
5001
5073
|
"dev",
|
|
5002
5074
|
"Watch local project files and automatically sync changes to the connected Spica instance."
|
|
5003
5075
|
).argument("[dir]", "Project directory (default: current working directory)").option(
|
|
5076
|
+
"--concurrency <n>",
|
|
5077
|
+
"Maximum parallel API requests.",
|
|
5078
|
+
{
|
|
5079
|
+
default: DEFAULT_CONCURRENCY,
|
|
5080
|
+
validator: CaporalValidator7.NUMBER
|
|
5081
|
+
}
|
|
5082
|
+
).option(
|
|
5004
5083
|
"--module <name>",
|
|
5005
5084
|
`Filter to specific module(s). Available: ${MODULE_NAMES.join(", ")}.`,
|
|
5006
5085
|
{
|
|
@@ -5021,7 +5100,7 @@ function readPackageVersion(baseUrl) {
|
|
|
5021
5100
|
}
|
|
5022
5101
|
|
|
5023
5102
|
// apps/cli/index.ts
|
|
5024
|
-
var { program } =
|
|
5103
|
+
var { program } = caporalCore8;
|
|
5025
5104
|
function run(argv) {
|
|
5026
5105
|
program.version(readPackageVersion(import.meta.url)).name("spica").bin("spica").description("Command line interface for spica.");
|
|
5027
5106
|
apply_default(program);
|