@spica/cli 0.18.41 → 0.18.44
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 +413 -441
- package/package.json +1 -37
package/index.js
CHANGED
|
@@ -90,7 +90,7 @@ $\xA0spica context switch <name> to switch context.`
|
|
|
90
90
|
return searchResult.config;
|
|
91
91
|
}
|
|
92
92
|
config2.get = get;
|
|
93
|
-
async function
|
|
93
|
+
async function path15() {
|
|
94
94
|
let config3 = await explorer.search(process.cwd());
|
|
95
95
|
if (!config3) {
|
|
96
96
|
config3 = await explorer.search(os.homedir());
|
|
@@ -100,9 +100,9 @@ $\xA0spica context switch <name> to switch context.`
|
|
|
100
100
|
}
|
|
101
101
|
return void 0;
|
|
102
102
|
}
|
|
103
|
-
config2.path =
|
|
103
|
+
config2.path = path15;
|
|
104
104
|
async function set(config3) {
|
|
105
|
-
let configPath = await
|
|
105
|
+
let configPath = await path15();
|
|
106
106
|
if (!configPath) {
|
|
107
107
|
configPath = _path.join(os.homedir(), ".spicarc");
|
|
108
108
|
}
|
|
@@ -3236,8 +3236,8 @@ function isNotFoundException(e) {
|
|
|
3236
3236
|
const code = 404;
|
|
3237
3237
|
return e.status == code || e.statusCode == code || e.data && e.data.statusCode == code;
|
|
3238
3238
|
}
|
|
3239
|
-
async function getFilteredResources(
|
|
3240
|
-
const res = await service.get(
|
|
3239
|
+
async function getFilteredResources(path15, service, ids, paginate) {
|
|
3240
|
+
const res = await service.get(path15);
|
|
3241
3241
|
const resources = paginate ? res.data : res;
|
|
3242
3242
|
return ids ? resources.filter((resource) => ids.includes(resource._id)) : resources;
|
|
3243
3243
|
}
|
|
@@ -3429,25 +3429,116 @@ function upgrade_default(program2) {
|
|
|
3429
3429
|
}
|
|
3430
3430
|
|
|
3431
3431
|
// apps/cli/src/commands/sync/plan.ts
|
|
3432
|
-
import
|
|
3432
|
+
import path11 from "path";
|
|
3433
|
+
|
|
3434
|
+
// packages/sync/dist/src/types.js
|
|
3435
|
+
var ChangeKind;
|
|
3436
|
+
(function(ChangeKind2) {
|
|
3437
|
+
ChangeKind2["Create"] = "create";
|
|
3438
|
+
ChangeKind2["Update"] = "update";
|
|
3439
|
+
ChangeKind2["Delete"] = "delete";
|
|
3440
|
+
})(ChangeKind || (ChangeKind = {}));
|
|
3441
|
+
|
|
3442
|
+
// packages/sync/dist/src/reporter.js
|
|
3443
|
+
var noopProgress = {
|
|
3444
|
+
update() {
|
|
3445
|
+
},
|
|
3446
|
+
succeed() {
|
|
3447
|
+
},
|
|
3448
|
+
fail() {
|
|
3449
|
+
}
|
|
3450
|
+
};
|
|
3451
|
+
var silentReporter = {
|
|
3452
|
+
task: (_label, op) => op(),
|
|
3453
|
+
progress: () => noopProgress,
|
|
3454
|
+
warn() {
|
|
3455
|
+
}
|
|
3456
|
+
};
|
|
3457
|
+
|
|
3458
|
+
// packages/sync/dist/src/fs-utils.js
|
|
3459
|
+
import fs9 from "fs";
|
|
3460
|
+
import path9 from "path";
|
|
3461
|
+
import yaml from "yaml";
|
|
3462
|
+
function ensureDir(dir) {
|
|
3463
|
+
fs9.mkdirSync(dir, { recursive: true });
|
|
3464
|
+
}
|
|
3465
|
+
function listFolders(dir) {
|
|
3466
|
+
if (!fs9.existsSync(dir))
|
|
3467
|
+
return [];
|
|
3468
|
+
return fs9.readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
|
|
3469
|
+
}
|
|
3470
|
+
function readYaml(filePath) {
|
|
3471
|
+
if (!fs9.existsSync(filePath))
|
|
3472
|
+
return null;
|
|
3473
|
+
return yaml.parse(fs9.readFileSync(filePath, "utf-8"));
|
|
3474
|
+
}
|
|
3475
|
+
function writeYaml(filePath, data) {
|
|
3476
|
+
ensureDir(path9.dirname(filePath));
|
|
3477
|
+
fs9.writeFileSync(filePath, yaml.stringify(data), "utf-8");
|
|
3478
|
+
}
|
|
3479
|
+
function readText(filePath) {
|
|
3480
|
+
if (!fs9.existsSync(filePath))
|
|
3481
|
+
return null;
|
|
3482
|
+
return fs9.readFileSync(filePath, "utf-8");
|
|
3483
|
+
}
|
|
3484
|
+
function writeText(filePath, content) {
|
|
3485
|
+
ensureDir(path9.dirname(filePath));
|
|
3486
|
+
fs9.writeFileSync(filePath, content, "utf-8");
|
|
3487
|
+
}
|
|
3488
|
+
function removeDir(dir) {
|
|
3489
|
+
if (fs9.existsSync(dir)) {
|
|
3490
|
+
fs9.rmSync(dir, { recursive: true, force: true });
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
3493
|
+
function omit(obj, fields) {
|
|
3494
|
+
const clone = structuredClone(obj);
|
|
3495
|
+
for (const f of fields)
|
|
3496
|
+
delete clone[f];
|
|
3497
|
+
return clone;
|
|
3498
|
+
}
|
|
3499
|
+
function sanitizeSlug(slug) {
|
|
3500
|
+
return path9.basename(path9.normalize(slug));
|
|
3501
|
+
}
|
|
3502
|
+
function unwrapList(res) {
|
|
3503
|
+
if (Array.isArray(res))
|
|
3504
|
+
return res;
|
|
3505
|
+
if (res && typeof res === "object" && Array.isArray(res.data)) {
|
|
3506
|
+
return res.data;
|
|
3507
|
+
}
|
|
3508
|
+
const body = res;
|
|
3509
|
+
const got = typeof body === "string" ? `${body.slice(0, 80).replace(/\s+/g, " ")}\u2026` : typeof body;
|
|
3510
|
+
throw new Error(`Expected a list response but got: ${got}. Check that your context URL points at the Spica API (e.g. ".../api"), not the panel.`);
|
|
3511
|
+
}
|
|
3512
|
+
async function readLocalSchemas(rootDir, moduleName) {
|
|
3513
|
+
const dir = path9.join(rootDir, moduleName);
|
|
3514
|
+
const slugs = listFolders(dir);
|
|
3515
|
+
const results = [];
|
|
3516
|
+
for (const slug of slugs) {
|
|
3517
|
+
const data = readYaml(path9.join(dir, slug, "schema.yaml"));
|
|
3518
|
+
if (data)
|
|
3519
|
+
results.push({ slug, data });
|
|
3520
|
+
}
|
|
3521
|
+
return results;
|
|
3522
|
+
}
|
|
3523
|
+
function writeLocalSchema(rootDir, moduleName, remote) {
|
|
3524
|
+
const dir = path9.join(rootDir, moduleName, remote.slug);
|
|
3525
|
+
writeYaml(path9.join(dir, "schema.yaml"), remote.data);
|
|
3526
|
+
}
|
|
3527
|
+
function deleteLocalSchema(rootDir, moduleName, slug) {
|
|
3528
|
+
removeDir(path9.join(rootDir, moduleName, slug));
|
|
3529
|
+
}
|
|
3433
3530
|
|
|
3434
|
-
//
|
|
3531
|
+
// packages/sync/dist/src/planner.js
|
|
3435
3532
|
import { bold as bold4, cyan, green as green4, red as red4, yellow as yellow2 } from "colorette";
|
|
3436
3533
|
import isEqual2 from "lodash/isEqual.js";
|
|
3437
3534
|
import { createTwoFilesPatch } from "diff";
|
|
3438
|
-
import
|
|
3439
|
-
async function buildPlan(modules, http2, rootDir, detailed = false, detectRenames = true) {
|
|
3535
|
+
import yaml2 from "yaml";
|
|
3536
|
+
async function buildPlan(modules, http2, rootDir, detailed = false, detectRenames = true, reporter = silentReporter) {
|
|
3440
3537
|
const modulePlans = [];
|
|
3441
3538
|
for (const mod of modules) {
|
|
3442
3539
|
const [locals, remotes] = await Promise.all([
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
op: () => mod.readLocal(rootDir)
|
|
3446
|
-
}),
|
|
3447
|
-
spin({
|
|
3448
|
-
text: `Fetching remote ${mod.displayName}`,
|
|
3449
|
-
op: () => mod.readRemote(http2)
|
|
3450
|
-
})
|
|
3540
|
+
reporter.task(`Reading local ${mod.displayName} files`, () => mod.readLocal(rootDir)),
|
|
3541
|
+
reporter.task(`Fetching remote ${mod.displayName}`, () => mod.readRemote(http2))
|
|
3451
3542
|
]);
|
|
3452
3543
|
const localBySlug = new Map(locals.map((l) => [l.slug, l]));
|
|
3453
3544
|
const remoteBySlug = new Map(remotes.map((r) => [r.slug, r]));
|
|
@@ -3458,7 +3549,7 @@ async function buildPlan(modules, http2, rootDir, detailed = false, detectRename
|
|
|
3458
3549
|
const remote = remoteBySlug.get(local.slug);
|
|
3459
3550
|
if (!remote) {
|
|
3460
3551
|
creates.push({
|
|
3461
|
-
kind:
|
|
3552
|
+
kind: ChangeKind.Create,
|
|
3462
3553
|
slug: local.slug,
|
|
3463
3554
|
summary: mod.summaryLine(local),
|
|
3464
3555
|
changedFields: [],
|
|
@@ -3470,7 +3561,7 @@ async function buildPlan(modules, http2, rootDir, detailed = false, detectRename
|
|
|
3470
3561
|
const changedFields = mod.diffFields(local.data, remote.data);
|
|
3471
3562
|
if (changedFields.length > 0) {
|
|
3472
3563
|
updates.push({
|
|
3473
|
-
kind:
|
|
3564
|
+
kind: ChangeKind.Update,
|
|
3474
3565
|
slug: local.slug,
|
|
3475
3566
|
summary: mod.summaryLine(local),
|
|
3476
3567
|
changedFields,
|
|
@@ -3484,7 +3575,7 @@ async function buildPlan(modules, http2, rootDir, detailed = false, detectRename
|
|
|
3484
3575
|
for (const remote of remotes) {
|
|
3485
3576
|
if (!localBySlug.has(remote.slug)) {
|
|
3486
3577
|
deletes.push({
|
|
3487
|
-
kind:
|
|
3578
|
+
kind: ChangeKind.Delete,
|
|
3488
3579
|
slug: remote.slug,
|
|
3489
3580
|
summary: mod.summaryLine(remote),
|
|
3490
3581
|
changedFields: [],
|
|
@@ -3505,7 +3596,7 @@ async function buildPlan(modules, http2, rootDir, detailed = false, detectRename
|
|
|
3505
3596
|
promotedRemoteIds.add(matchedDelete.remote.id);
|
|
3506
3597
|
const changedFields = mod.diffFields(create2.local.data, matchedDelete.remote.data);
|
|
3507
3598
|
updates.push({
|
|
3508
|
-
kind:
|
|
3599
|
+
kind: ChangeKind.Update,
|
|
3509
3600
|
slug: create2.slug,
|
|
3510
3601
|
summary: mod.summaryLine(create2.local),
|
|
3511
3602
|
changedFields,
|
|
@@ -3518,11 +3609,7 @@ async function buildPlan(modules, http2, rootDir, detailed = false, detectRename
|
|
|
3518
3609
|
}
|
|
3519
3610
|
}
|
|
3520
3611
|
creates.splice(0, creates.length, ...remainingCreates);
|
|
3521
|
-
deletes.splice(
|
|
3522
|
-
0,
|
|
3523
|
-
deletes.length,
|
|
3524
|
-
...deletes.filter((e) => !promotedRemoteIds.has(e.remote.id))
|
|
3525
|
-
);
|
|
3612
|
+
deletes.splice(0, deletes.length, ...deletes.filter((e) => !promotedRemoteIds.has(e.remote.id)));
|
|
3526
3613
|
}
|
|
3527
3614
|
modulePlans.push({ module: mod, creates, updates, deletes });
|
|
3528
3615
|
}
|
|
@@ -3554,22 +3641,20 @@ function renderPlan(plan2, opts = {}) {
|
|
|
3554
3641
|
}
|
|
3555
3642
|
for (const mp of plan2.modules) {
|
|
3556
3643
|
const { module: mod, creates, updates, deletes } = mp;
|
|
3557
|
-
if (!creates.length && !updates.length && !deletes.length)
|
|
3644
|
+
if (!creates.length && !updates.length && !deletes.length)
|
|
3645
|
+
continue;
|
|
3558
3646
|
const staleCount = opts.clean ? creates.length : 0;
|
|
3559
|
-
console.log(
|
|
3560
|
-
|
|
3561
|
-
${bold4(cyan(`\u2500\u2500 ${mod.displayName.toUpperCase()} \u2500\u2500`))} ${summaryBadges(deletes.length, updates.length, staleCount)}`
|
|
3562
|
-
);
|
|
3647
|
+
console.log(`
|
|
3648
|
+
${bold4(cyan(`\u2500\u2500 ${mod.displayName.toUpperCase()} \u2500\u2500`))} ${summaryBadges(deletes.length, updates.length, staleCount)}`);
|
|
3563
3649
|
for (const e of deletes) {
|
|
3564
3650
|
console.log(` ${green4("+")} ${bold4(e.slug)} ${dim(e.summary)}`);
|
|
3565
3651
|
}
|
|
3566
3652
|
for (const e of updates) {
|
|
3567
|
-
console.log(
|
|
3568
|
-
` ${yellow2("~")} ${bold4(e.slug)} ${dim(e.summary)} ${yellow2(`[${e.changedFields.join(", ")}]`)}`
|
|
3569
|
-
);
|
|
3653
|
+
console.log(` ${yellow2("~")} ${bold4(e.slug)} ${dim(e.summary)} ${yellow2(`[${e.changedFields.join(", ")}]`)}`);
|
|
3570
3654
|
if (opts.detailed) {
|
|
3571
3655
|
for (const [section, diff] of Object.entries(e.diffs)) {
|
|
3572
|
-
if (!diff)
|
|
3656
|
+
if (!diff)
|
|
3657
|
+
continue;
|
|
3573
3658
|
console.log(` ${bold4(section)}:`);
|
|
3574
3659
|
renderUnifiedDiff(diff);
|
|
3575
3660
|
}
|
|
@@ -3581,10 +3666,8 @@ ${bold4(cyan(`\u2500\u2500 ${mod.displayName.toUpperCase()} \u2500\u2500`))} ${
|
|
|
3581
3666
|
}
|
|
3582
3667
|
}
|
|
3583
3668
|
}
|
|
3584
|
-
console.log(
|
|
3585
|
-
|
|
3586
|
-
${bold4("Plan:")} ${green4(`+${toWrite} to write`)} ${yellow2(`~${toUpdate} to update`)} ${red4(`-${toDelete} to delete`)}`
|
|
3587
|
-
);
|
|
3669
|
+
console.log(`
|
|
3670
|
+
${bold4("Plan:")} ${green4(`+${toWrite} to write`)} ${yellow2(`~${toUpdate} to update`)} ${red4(`-${toDelete} to delete`)}`);
|
|
3588
3671
|
if (!opts.detailed && toUpdate > 0) {
|
|
3589
3672
|
console.log(` ${dim("Run with --detailed to see full diffs.")}`);
|
|
3590
3673
|
}
|
|
@@ -3600,21 +3683,19 @@ ${bold4("Plan:")} ${green4(`+${toWrite} to write`)} ${yellow2(`~${toUpdate} t
|
|
|
3600
3683
|
}
|
|
3601
3684
|
for (const mp of plan2.modules) {
|
|
3602
3685
|
const { module: mod, creates, updates, deletes } = mp;
|
|
3603
|
-
if (!creates.length && !updates.length && !deletes.length)
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
${bold4(cyan(`\u2500\u2500 ${mod.displayName.toUpperCase()} \u2500\u2500`))} ${summaryBadges(creates.length, updates.length, deletes.length)}`
|
|
3607
|
-
);
|
|
3686
|
+
if (!creates.length && !updates.length && !deletes.length)
|
|
3687
|
+
continue;
|
|
3688
|
+
console.log(`
|
|
3689
|
+
${bold4(cyan(`\u2500\u2500 ${mod.displayName.toUpperCase()} \u2500\u2500`))} ${summaryBadges(creates.length, updates.length, deletes.length)}`);
|
|
3608
3690
|
for (const e of creates) {
|
|
3609
3691
|
console.log(` ${green4("+")} ${bold4(e.slug)} ${dim(e.summary)}`);
|
|
3610
3692
|
}
|
|
3611
3693
|
for (const e of updates) {
|
|
3612
|
-
console.log(
|
|
3613
|
-
` ${yellow2("~")} ${bold4(e.slug)} ${dim(e.summary)} ${yellow2(`[${e.changedFields.join(", ")}]`)}`
|
|
3614
|
-
);
|
|
3694
|
+
console.log(` ${yellow2("~")} ${bold4(e.slug)} ${dim(e.summary)} ${yellow2(`[${e.changedFields.join(", ")}]`)}`);
|
|
3615
3695
|
if (opts.detailed) {
|
|
3616
3696
|
for (const [section, diff] of Object.entries(e.diffs)) {
|
|
3617
|
-
if (!diff)
|
|
3697
|
+
if (!diff)
|
|
3698
|
+
continue;
|
|
3618
3699
|
console.log(` ${bold4(section)}:`);
|
|
3619
3700
|
renderUnifiedDiff(diff);
|
|
3620
3701
|
}
|
|
@@ -3624,19 +3705,20 @@ ${bold4(cyan(`\u2500\u2500 ${mod.displayName.toUpperCase()} \u2500\u2500`))} ${
|
|
|
3624
3705
|
console.log(` ${red4("-")} ${bold4(e.slug)} ${dim(e.summary)}`);
|
|
3625
3706
|
}
|
|
3626
3707
|
}
|
|
3627
|
-
console.log(
|
|
3628
|
-
|
|
3629
|
-
${bold4("Plan:")} ${green4(`+${totalCreates} to create`)} ${yellow2(`~${totalUpdates} to update`)} ${red4(`-${totalDeletes} to delete`)}`
|
|
3630
|
-
);
|
|
3708
|
+
console.log(`
|
|
3709
|
+
${bold4("Plan:")} ${green4(`+${totalCreates} to create`)} ${yellow2(`~${totalUpdates} to update`)} ${red4(`-${totalDeletes} to delete`)}`);
|
|
3631
3710
|
if (!opts.detailed && totalUpdates > 0) {
|
|
3632
3711
|
console.log(` ${dim("Run with --detailed to see full diffs.")}`);
|
|
3633
3712
|
}
|
|
3634
3713
|
}
|
|
3635
3714
|
function summaryBadges(c, u, d) {
|
|
3636
3715
|
const parts = [];
|
|
3637
|
-
if (c)
|
|
3638
|
-
|
|
3639
|
-
if (
|
|
3716
|
+
if (c)
|
|
3717
|
+
parts.push(green4(`+${c}`));
|
|
3718
|
+
if (u)
|
|
3719
|
+
parts.push(yellow2(`~${u}`));
|
|
3720
|
+
if (d)
|
|
3721
|
+
parts.push(red4(`-${d}`));
|
|
3640
3722
|
return parts.join(" ");
|
|
3641
3723
|
}
|
|
3642
3724
|
function dim(s) {
|
|
@@ -3655,85 +3737,64 @@ function renderUnifiedDiff(diff) {
|
|
|
3655
3737
|
}
|
|
3656
3738
|
async function applyPlan(plan2, http2, opts = {}) {
|
|
3657
3739
|
const concurrency = opts.concurrency ?? 10;
|
|
3740
|
+
const reporter = opts.reporter ?? silentReporter;
|
|
3658
3741
|
const errors = [];
|
|
3659
3742
|
const handleErr = (label, e) => {
|
|
3660
3743
|
const msg = formatError(e);
|
|
3661
3744
|
const full = `${label}: ${msg}`;
|
|
3662
|
-
if (opts.abortOnError)
|
|
3663
|
-
|
|
3745
|
+
if (opts.abortOnError)
|
|
3746
|
+
throw new Error(full);
|
|
3747
|
+
reporter.warn(full);
|
|
3664
3748
|
errors.push(full);
|
|
3665
3749
|
};
|
|
3666
3750
|
for (const mp of plan2.modules) {
|
|
3667
3751
|
const { module: mod, creates, updates, deletes } = mp;
|
|
3668
3752
|
if (deletes.length) {
|
|
3669
|
-
await runConcurrently(
|
|
3670
|
-
deletes.map((e) => ({ slug: e.slug, run: () => mod.delete(http2, e.remote.id) })),
|
|
3671
|
-
concurrency,
|
|
3672
|
-
`Deleting ${mod.displayName}`,
|
|
3673
|
-
handleErr
|
|
3674
|
-
);
|
|
3753
|
+
await runConcurrently(deletes.map((e) => ({ slug: e.slug, run: () => mod.delete(http2, e.remote.id) })), concurrency, `Deleting ${mod.displayName}`, handleErr, reporter);
|
|
3675
3754
|
}
|
|
3676
3755
|
if (updates.length) {
|
|
3677
|
-
await runConcurrently(
|
|
3678
|
-
updates.map((e) => ({ slug: e.slug, run: () => mod.update(http2, e.local, e.remote.id) })),
|
|
3679
|
-
concurrency,
|
|
3680
|
-
`Updating ${mod.displayName}`,
|
|
3681
|
-
handleErr
|
|
3682
|
-
);
|
|
3756
|
+
await runConcurrently(updates.map((e) => ({ slug: e.slug, run: () => mod.update(http2, e.local, e.remote.id) })), concurrency, `Updating ${mod.displayName}`, handleErr, reporter);
|
|
3683
3757
|
}
|
|
3684
3758
|
if (creates.length) {
|
|
3685
|
-
await runConcurrently(
|
|
3686
|
-
creates.map((e) => ({ slug: e.slug, run: () => mod.create(http2, e.local) })),
|
|
3687
|
-
concurrency,
|
|
3688
|
-
`Creating ${mod.displayName}`,
|
|
3689
|
-
handleErr
|
|
3690
|
-
);
|
|
3759
|
+
await runConcurrently(creates.map((e) => ({ slug: e.slug, run: () => mod.create(http2, e.local) })), concurrency, `Creating ${mod.displayName}`, handleErr, reporter);
|
|
3691
3760
|
}
|
|
3692
3761
|
}
|
|
3693
3762
|
return { errors };
|
|
3694
3763
|
}
|
|
3695
3764
|
async function fetchToDisk(plan2, http2, rootDir, opts = {}) {
|
|
3696
3765
|
const concurrency = opts.concurrency ?? 10;
|
|
3766
|
+
const reporter = opts.reporter ?? silentReporter;
|
|
3697
3767
|
const errors = [];
|
|
3698
3768
|
let written = 0;
|
|
3699
3769
|
let deleted = 0;
|
|
3700
3770
|
const handleErr = (label, e) => {
|
|
3701
3771
|
const msg = formatError(e);
|
|
3702
3772
|
const full = `${label}: ${msg}`;
|
|
3703
|
-
if (opts.abortOnError)
|
|
3704
|
-
|
|
3773
|
+
if (opts.abortOnError)
|
|
3774
|
+
throw new Error(full);
|
|
3775
|
+
reporter.warn(full);
|
|
3705
3776
|
errors.push(full);
|
|
3706
3777
|
};
|
|
3707
3778
|
for (const mp of plan2.modules) {
|
|
3708
3779
|
const { module: mod, updates, deletes, creates } = mp;
|
|
3709
3780
|
const toWrite = [...deletes, ...updates];
|
|
3710
3781
|
if (toWrite.length) {
|
|
3711
|
-
await runConcurrently(
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
})),
|
|
3719
|
-
concurrency,
|
|
3720
|
-
`Writing ${mod.displayName}`,
|
|
3721
|
-
handleErr
|
|
3722
|
-
);
|
|
3782
|
+
await runConcurrently(toWrite.map((e) => ({
|
|
3783
|
+
slug: e.slug,
|
|
3784
|
+
run: async () => {
|
|
3785
|
+
await mod.writeLocal(rootDir, e.remote);
|
|
3786
|
+
written++;
|
|
3787
|
+
}
|
|
3788
|
+
})), concurrency, `Writing ${mod.displayName}`, handleErr, reporter);
|
|
3723
3789
|
}
|
|
3724
3790
|
if (opts.clean && creates.length) {
|
|
3725
|
-
await runConcurrently(
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
})),
|
|
3733
|
-
concurrency,
|
|
3734
|
-
`Deleting ${mod.displayName}`,
|
|
3735
|
-
handleErr
|
|
3736
|
-
);
|
|
3791
|
+
await runConcurrently(creates.map((e) => ({
|
|
3792
|
+
slug: e.slug,
|
|
3793
|
+
run: async () => {
|
|
3794
|
+
await mod.deleteLocal(rootDir, e.slug);
|
|
3795
|
+
deleted++;
|
|
3796
|
+
}
|
|
3797
|
+
})), concurrency, `Deleting ${mod.displayName}`, handleErr, reporter);
|
|
3737
3798
|
}
|
|
3738
3799
|
}
|
|
3739
3800
|
return { written, deleted, errors };
|
|
@@ -3742,41 +3803,47 @@ function diffObjectFields(local, remote, ignoredFields) {
|
|
|
3742
3803
|
const allKeys = /* @__PURE__ */ new Set([...Object.keys(local), ...Object.keys(remote)]);
|
|
3743
3804
|
const changed = [];
|
|
3744
3805
|
for (const key of allKeys) {
|
|
3745
|
-
if (ignoredFields.includes(key))
|
|
3806
|
+
if (ignoredFields.includes(key))
|
|
3807
|
+
continue;
|
|
3746
3808
|
if (!isEqual2(local[key], remote[key])) {
|
|
3747
3809
|
changed.push(key);
|
|
3748
3810
|
}
|
|
3749
3811
|
}
|
|
3750
3812
|
return changed;
|
|
3751
3813
|
}
|
|
3814
|
+
function diffSchemaFields(local, remote, ignoredFields) {
|
|
3815
|
+
return diffObjectFields(local, remote, ignoredFields);
|
|
3816
|
+
}
|
|
3752
3817
|
function buildUnifiedDiff(oldContent, newContent, filename) {
|
|
3753
3818
|
return createTwoFilesPatch(`remote/${filename}`, `local/${filename}`, oldContent, newContent);
|
|
3754
3819
|
}
|
|
3755
|
-
|
|
3756
|
-
|
|
3820
|
+
function renderSchemaDetail(localData, remoteData, ignoredFields) {
|
|
3821
|
+
const localYaml = yaml2.stringify(omit(localData, ignoredFields));
|
|
3822
|
+
const remoteYaml = yaml2.stringify(omit(remoteData, ignoredFields));
|
|
3823
|
+
return { schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml") };
|
|
3824
|
+
}
|
|
3825
|
+
async function runConcurrently(tasks, concurrency, label, onError, reporter) {
|
|
3826
|
+
if (!tasks.length)
|
|
3827
|
+
return;
|
|
3757
3828
|
const total = tasks.length;
|
|
3758
3829
|
let done = 0;
|
|
3759
|
-
const
|
|
3830
|
+
const progress = reporter.progress(label, total);
|
|
3760
3831
|
try {
|
|
3761
3832
|
const limit = Math.max(1, Math.min(total, concurrency));
|
|
3762
3833
|
let batch = [];
|
|
3763
3834
|
for (let i = 0; i < tasks.length; i++) {
|
|
3764
3835
|
batch.push(tasks[i]);
|
|
3765
3836
|
if (batch.length === limit || i === tasks.length - 1) {
|
|
3766
|
-
await Promise.all(
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
spinner.text = `${label}: ${task.slug} (${done}/${total})`;
|
|
3771
|
-
})
|
|
3772
|
-
)
|
|
3773
|
-
);
|
|
3837
|
+
await Promise.all(batch.map((task) => task.run().catch((e) => onError(`${label}: ${task.slug}`, e)).finally(() => {
|
|
3838
|
+
done++;
|
|
3839
|
+
progress.update(task.slug, done, total);
|
|
3840
|
+
})));
|
|
3774
3841
|
batch = [];
|
|
3775
3842
|
}
|
|
3776
3843
|
}
|
|
3777
|
-
|
|
3844
|
+
progress.succeed();
|
|
3778
3845
|
} catch (e) {
|
|
3779
|
-
|
|
3846
|
+
progress.fail();
|
|
3780
3847
|
throw e;
|
|
3781
3848
|
}
|
|
3782
3849
|
}
|
|
@@ -3788,71 +3855,15 @@ function formatError(e) {
|
|
|
3788
3855
|
return String(e);
|
|
3789
3856
|
}
|
|
3790
3857
|
|
|
3791
|
-
//
|
|
3792
|
-
import path10 from "path";
|
|
3793
|
-
import yaml2 from "yaml";
|
|
3794
|
-
|
|
3795
|
-
// apps/cli/src/commands/sync/fs-utils.ts
|
|
3796
|
-
import fs9 from "fs";
|
|
3797
|
-
import path9 from "path";
|
|
3798
|
-
import yaml from "yaml";
|
|
3799
|
-
function ensureDir(dir) {
|
|
3800
|
-
fs9.mkdirSync(dir, { recursive: true });
|
|
3801
|
-
}
|
|
3802
|
-
function listFolders(dir) {
|
|
3803
|
-
if (!fs9.existsSync(dir)) return [];
|
|
3804
|
-
return fs9.readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
|
|
3805
|
-
}
|
|
3806
|
-
function readYaml(filePath) {
|
|
3807
|
-
if (!fs9.existsSync(filePath)) return null;
|
|
3808
|
-
return yaml.parse(fs9.readFileSync(filePath, "utf-8"));
|
|
3809
|
-
}
|
|
3810
|
-
function writeYaml(filePath, data) {
|
|
3811
|
-
ensureDir(path9.dirname(filePath));
|
|
3812
|
-
fs9.writeFileSync(filePath, yaml.stringify(data), "utf-8");
|
|
3813
|
-
}
|
|
3814
|
-
function readText(filePath) {
|
|
3815
|
-
if (!fs9.existsSync(filePath)) return null;
|
|
3816
|
-
return fs9.readFileSync(filePath, "utf-8");
|
|
3817
|
-
}
|
|
3818
|
-
function writeText(filePath, content) {
|
|
3819
|
-
ensureDir(path9.dirname(filePath));
|
|
3820
|
-
fs9.writeFileSync(filePath, content, "utf-8");
|
|
3821
|
-
}
|
|
3822
|
-
function removeDir(dir) {
|
|
3823
|
-
if (fs9.existsSync(dir)) {
|
|
3824
|
-
fs9.rmSync(dir, { recursive: true, force: true });
|
|
3825
|
-
}
|
|
3826
|
-
}
|
|
3827
|
-
function omit(obj, fields) {
|
|
3828
|
-
const clone = structuredClone(obj);
|
|
3829
|
-
for (const f of fields) delete clone[f];
|
|
3830
|
-
return clone;
|
|
3831
|
-
}
|
|
3832
|
-
function sanitizeSlug(slug) {
|
|
3833
|
-
return path9.basename(path9.normalize(slug));
|
|
3834
|
-
}
|
|
3835
|
-
function unwrapList(res) {
|
|
3836
|
-
if (Array.isArray(res)) return res;
|
|
3837
|
-
return res.data ?? [];
|
|
3838
|
-
}
|
|
3839
|
-
|
|
3840
|
-
// apps/cli/src/commands/sync/modules/bucket.ts
|
|
3858
|
+
// packages/sync/dist/src/modules/bucket.js
|
|
3841
3859
|
var IGNORED_FIELDS = ["_id"];
|
|
3842
3860
|
var bucketModule = {
|
|
3843
3861
|
name: "bucket",
|
|
3844
3862
|
displayName: "Bucket",
|
|
3845
3863
|
identityField: "title",
|
|
3846
3864
|
ignoredFields: IGNORED_FIELDS,
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
const slugs = listFolders(dir);
|
|
3850
|
-
const results = [];
|
|
3851
|
-
for (const slug of slugs) {
|
|
3852
|
-
const data = readYaml(path10.join(dir, slug, "schema.yaml"));
|
|
3853
|
-
if (data) results.push({ slug, data });
|
|
3854
|
-
}
|
|
3855
|
-
return results;
|
|
3865
|
+
readLocal(rootDir) {
|
|
3866
|
+
return readLocalSchemas(rootDir, "bucket");
|
|
3856
3867
|
},
|
|
3857
3868
|
async readRemote(http2) {
|
|
3858
3869
|
const res = await http2.get("bucket");
|
|
@@ -3873,25 +3884,16 @@ var bucketModule = {
|
|
|
3873
3884
|
await http2.delete(`bucket/${remoteId}`);
|
|
3874
3885
|
},
|
|
3875
3886
|
async writeLocal(rootDir, remote) {
|
|
3876
|
-
|
|
3877
|
-
writeYaml(path10.join(dir, "schema.yaml"), remote.data);
|
|
3887
|
+
writeLocalSchema(rootDir, "bucket", remote);
|
|
3878
3888
|
},
|
|
3879
3889
|
async deleteLocal(rootDir, slug) {
|
|
3880
|
-
|
|
3890
|
+
deleteLocalSchema(rootDir, "bucket", slug);
|
|
3881
3891
|
},
|
|
3882
3892
|
diffFields(local, remote) {
|
|
3883
|
-
return
|
|
3884
|
-
local,
|
|
3885
|
-
remote,
|
|
3886
|
-
IGNORED_FIELDS
|
|
3887
|
-
);
|
|
3893
|
+
return diffSchemaFields(local, remote, IGNORED_FIELDS);
|
|
3888
3894
|
},
|
|
3889
3895
|
renderDetail(local, remote) {
|
|
3890
|
-
|
|
3891
|
-
const remoteYaml = yaml2.stringify(omit(remote.data, IGNORED_FIELDS));
|
|
3892
|
-
return {
|
|
3893
|
-
schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml")
|
|
3894
|
-
};
|
|
3896
|
+
return renderSchemaDetail(local.data, remote.data, IGNORED_FIELDS);
|
|
3895
3897
|
},
|
|
3896
3898
|
summaryLine(resource) {
|
|
3897
3899
|
const propCount = Object.keys(resource.data.properties ?? {}).length;
|
|
@@ -3902,24 +3904,15 @@ var bucketModule = {
|
|
|
3902
3904
|
}
|
|
3903
3905
|
};
|
|
3904
3906
|
|
|
3905
|
-
//
|
|
3906
|
-
import path11 from "path";
|
|
3907
|
-
import yaml3 from "yaml";
|
|
3907
|
+
// packages/sync/dist/src/modules/env-var.js
|
|
3908
3908
|
var IGNORED_FIELDS2 = ["_id"];
|
|
3909
3909
|
var envVarModule = {
|
|
3910
3910
|
name: "env-var",
|
|
3911
3911
|
displayName: "Environment Variable",
|
|
3912
3912
|
identityField: "key",
|
|
3913
3913
|
ignoredFields: IGNORED_FIELDS2,
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
const slugs = listFolders(dir);
|
|
3917
|
-
const results = [];
|
|
3918
|
-
for (const slug of slugs) {
|
|
3919
|
-
const data = readYaml(path11.join(dir, slug, "schema.yaml"));
|
|
3920
|
-
if (data) results.push({ slug, data });
|
|
3921
|
-
}
|
|
3922
|
-
return results;
|
|
3914
|
+
readLocal(rootDir) {
|
|
3915
|
+
return readLocalSchemas(rootDir, "env-var");
|
|
3923
3916
|
},
|
|
3924
3917
|
async readRemote(http2) {
|
|
3925
3918
|
const res = await http2.get("env-var");
|
|
@@ -3940,25 +3933,16 @@ var envVarModule = {
|
|
|
3940
3933
|
await http2.delete(`env-var/${remoteId}`);
|
|
3941
3934
|
},
|
|
3942
3935
|
async writeLocal(rootDir, remote) {
|
|
3943
|
-
|
|
3944
|
-
writeYaml(path11.join(dir, "schema.yaml"), remote.data);
|
|
3936
|
+
writeLocalSchema(rootDir, "env-var", remote);
|
|
3945
3937
|
},
|
|
3946
3938
|
async deleteLocal(rootDir, slug) {
|
|
3947
|
-
|
|
3939
|
+
deleteLocalSchema(rootDir, "env-var", slug);
|
|
3948
3940
|
},
|
|
3949
3941
|
diffFields(local, remote) {
|
|
3950
|
-
return
|
|
3951
|
-
local,
|
|
3952
|
-
remote,
|
|
3953
|
-
IGNORED_FIELDS2
|
|
3954
|
-
);
|
|
3942
|
+
return diffSchemaFields(local, remote, IGNORED_FIELDS2);
|
|
3955
3943
|
},
|
|
3956
3944
|
renderDetail(local, remote) {
|
|
3957
|
-
|
|
3958
|
-
const remoteYaml = yaml3.stringify(omit(remote.data, IGNORED_FIELDS2));
|
|
3959
|
-
return {
|
|
3960
|
-
schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml")
|
|
3961
|
-
};
|
|
3945
|
+
return renderSchemaDetail(local.data, remote.data, IGNORED_FIELDS2);
|
|
3962
3946
|
},
|
|
3963
3947
|
summaryLine(resource) {
|
|
3964
3948
|
return `key: ${resource.data.key}`;
|
|
@@ -3968,9 +3952,9 @@ var envVarModule = {
|
|
|
3968
3952
|
}
|
|
3969
3953
|
};
|
|
3970
3954
|
|
|
3971
|
-
//
|
|
3972
|
-
import
|
|
3973
|
-
import
|
|
3955
|
+
// packages/sync/dist/src/modules/function.js
|
|
3956
|
+
import path10 from "path";
|
|
3957
|
+
import yaml3 from "yaml";
|
|
3974
3958
|
import isEqual3 from "lodash/isEqual.js";
|
|
3975
3959
|
import { bold as bold5, cyan as cyan2, green as green5, yellow as yellow3 } from "colorette";
|
|
3976
3960
|
var SCHEMA_IGNORED = ["_id", "env_vars", "secrets"];
|
|
@@ -3994,7 +3978,8 @@ function normalizeSchema(schema) {
|
|
|
3994
3978
|
return normalized;
|
|
3995
3979
|
}
|
|
3996
3980
|
function isNotFoundError(error) {
|
|
3997
|
-
if (!error || typeof error !== "object")
|
|
3981
|
+
if (!error || typeof error !== "object")
|
|
3982
|
+
return false;
|
|
3998
3983
|
const c = error;
|
|
3999
3984
|
return c.status === 404 || c.statusCode === 404 || c.response?.status === 404 || c.response?.statusCode === 404;
|
|
4000
3985
|
}
|
|
@@ -4004,14 +3989,15 @@ var functionModule = {
|
|
|
4004
3989
|
identityField: "name",
|
|
4005
3990
|
ignoredFields: SCHEMA_IGNORED,
|
|
4006
3991
|
readSchema(rootDir, slug) {
|
|
4007
|
-
return readYaml(
|
|
3992
|
+
return readYaml(path10.join(rootDir, "function", slug, "schema.yaml"));
|
|
4008
3993
|
},
|
|
4009
3994
|
readIndex(rootDir, slug, language) {
|
|
4010
|
-
return readText(
|
|
3995
|
+
return readText(path10.join(rootDir, "function", slug, indexFilename(language))) ?? "";
|
|
4011
3996
|
},
|
|
4012
3997
|
readDependencies(rootDir, slug) {
|
|
4013
|
-
const pkgText = readText(
|
|
4014
|
-
if (!pkgText)
|
|
3998
|
+
const pkgText = readText(path10.join(rootDir, "function", slug, "package.json"));
|
|
3999
|
+
if (!pkgText)
|
|
4000
|
+
return {};
|
|
4015
4001
|
try {
|
|
4016
4002
|
return JSON.parse(pkgText).dependencies ?? {};
|
|
4017
4003
|
} catch {
|
|
@@ -4019,12 +4005,13 @@ var functionModule = {
|
|
|
4019
4005
|
}
|
|
4020
4006
|
},
|
|
4021
4007
|
async readLocal(rootDir) {
|
|
4022
|
-
const dir =
|
|
4008
|
+
const dir = path10.join(rootDir, "function");
|
|
4023
4009
|
const slugs = listFolders(dir);
|
|
4024
4010
|
const results = [];
|
|
4025
4011
|
for (const slug of slugs) {
|
|
4026
4012
|
const schema = this.readSchema(rootDir, slug);
|
|
4027
|
-
if (!schema)
|
|
4013
|
+
if (!schema)
|
|
4014
|
+
continue;
|
|
4028
4015
|
const index = this.readIndex(rootDir, slug, schema.language);
|
|
4029
4016
|
const dependencies = this.readDependencies(rootDir, slug);
|
|
4030
4017
|
results.push({ slug, data: { schema, index, dependencies } });
|
|
@@ -4032,43 +4019,41 @@ var functionModule = {
|
|
|
4032
4019
|
return results;
|
|
4033
4020
|
},
|
|
4034
4021
|
async readRemote(http2) {
|
|
4035
|
-
const
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4022
|
+
const res = await http2.get("function");
|
|
4023
|
+
const fns = unwrapList(res);
|
|
4024
|
+
return Promise.all(fns.map(async (fn) => {
|
|
4025
|
+
const id = fn._id;
|
|
4026
|
+
const [indexRes, depsRes] = await Promise.all([
|
|
4027
|
+
http2.get(`function/${id}/index`).catch(() => ({ index: "" })),
|
|
4028
|
+
http2.get(`function/${id}/dependencies`).catch(() => [])
|
|
4029
|
+
]);
|
|
4030
|
+
const dependencies = {};
|
|
4031
|
+
for (const dep of depsRes) {
|
|
4032
|
+
dependencies[dep.name] = dep.version;
|
|
4033
|
+
}
|
|
4034
|
+
return {
|
|
4035
|
+
slug: sanitizeSlug(fn.name),
|
|
4036
|
+
id,
|
|
4037
|
+
data: {
|
|
4038
|
+
schema: normalizeSchema(fn),
|
|
4039
|
+
index: indexRes.index ?? "",
|
|
4040
|
+
dependencies
|
|
4046
4041
|
}
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
id,
|
|
4050
|
-
data: {
|
|
4051
|
-
schema: normalizeSchema(fn),
|
|
4052
|
-
index: indexRes.index ?? "",
|
|
4053
|
-
dependencies
|
|
4054
|
-
}
|
|
4055
|
-
};
|
|
4056
|
-
})
|
|
4057
|
-
);
|
|
4042
|
+
};
|
|
4043
|
+
}));
|
|
4058
4044
|
},
|
|
4059
4045
|
async create(http2, local) {
|
|
4060
4046
|
const id = await this.createSchema(http2, local.data.schema);
|
|
4061
|
-
if (!id)
|
|
4047
|
+
if (!id)
|
|
4048
|
+
return;
|
|
4062
4049
|
await this.uploadIndex(http2, id, local.data.index);
|
|
4063
4050
|
await this.uploadDependencies(http2, id, local.data.dependencies);
|
|
4064
4051
|
},
|
|
4065
4052
|
async createSchema(http2, schema) {
|
|
4066
|
-
const created = await http2.post(
|
|
4067
|
-
"function",
|
|
4068
|
-
omit(schema, ["env_vars", "secrets"])
|
|
4069
|
-
);
|
|
4053
|
+
const created = await http2.post("function", omit(schema, ["env_vars", "secrets"]));
|
|
4070
4054
|
const id = created._id ?? schema._id;
|
|
4071
|
-
if (!id)
|
|
4055
|
+
if (!id)
|
|
4056
|
+
return void 0;
|
|
4072
4057
|
const envVarIds = schema.env_vars ?? [];
|
|
4073
4058
|
await Promise.all(envVarIds.map((evId) => http2.put(`function/${id}/env-var/${evId}`, {})));
|
|
4074
4059
|
const secretIds = schema.secrets ?? [];
|
|
@@ -4103,14 +4088,11 @@ var functionModule = {
|
|
|
4103
4088
|
const currentDeps = await http2.get(`function/${remoteId}/dependencies`).catch(() => []);
|
|
4104
4089
|
const currentByName = new Map(currentDeps.map((d) => [d.name, d.version]));
|
|
4105
4090
|
const toDelete = currentDeps.filter((d) => !(d.name in localDeps));
|
|
4106
|
-
await Promise.all(
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
})
|
|
4112
|
-
)
|
|
4113
|
-
);
|
|
4091
|
+
await Promise.all(toDelete.map((d) => http2.delete(`function/${remoteId}/dependencies/${d.name}`).catch((error) => {
|
|
4092
|
+
if (isNotFoundError(error))
|
|
4093
|
+
return;
|
|
4094
|
+
throw error;
|
|
4095
|
+
})));
|
|
4114
4096
|
const toAdd = Object.entries(localDeps).filter(([n, v]) => {
|
|
4115
4097
|
const cur = currentByName.get(n);
|
|
4116
4098
|
return !cur || cur !== v;
|
|
@@ -4123,56 +4105,53 @@ var functionModule = {
|
|
|
4123
4105
|
await http2.delete(`function/${remoteId}`);
|
|
4124
4106
|
},
|
|
4125
4107
|
async writeLocal(rootDir, remote) {
|
|
4126
|
-
const fnDir =
|
|
4108
|
+
const fnDir = path10.join(rootDir, "function", remote.slug);
|
|
4127
4109
|
ensureDir(fnDir);
|
|
4128
|
-
writeYaml(
|
|
4110
|
+
writeYaml(path10.join(fnDir, "schema.yaml"), remote.data.schema);
|
|
4129
4111
|
const indexFile = indexFilename(remote.data.schema.language);
|
|
4130
|
-
writeText(
|
|
4112
|
+
writeText(path10.join(fnDir, indexFile), remote.data.index);
|
|
4131
4113
|
const pkg = {
|
|
4132
4114
|
name: remote.slug,
|
|
4133
4115
|
version: "0.0.1",
|
|
4134
4116
|
dependencies: remote.data.dependencies
|
|
4135
4117
|
};
|
|
4136
|
-
writeText(
|
|
4118
|
+
writeText(path10.join(fnDir, "package.json"), JSON.stringify(pkg, null, 2));
|
|
4137
4119
|
},
|
|
4138
4120
|
async deleteLocal(rootDir, slug) {
|
|
4139
|
-
removeDir(
|
|
4121
|
+
removeDir(path10.join(rootDir, "function", slug));
|
|
4140
4122
|
},
|
|
4141
4123
|
diffFields(local, remote) {
|
|
4142
4124
|
const changed = [];
|
|
4143
|
-
const schemaChanged = diffObjectFields(
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
if (
|
|
4149
|
-
|
|
4150
|
-
if (!isEqual3(local.dependencies, remote.dependencies)) changed.push("dependencies");
|
|
4125
|
+
const schemaChanged = diffObjectFields(local.schema, remote.schema, SCHEMA_IGNORED);
|
|
4126
|
+
if (schemaChanged.length)
|
|
4127
|
+
changed.push("schema");
|
|
4128
|
+
if (local.index !== remote.index)
|
|
4129
|
+
changed.push("index");
|
|
4130
|
+
if (!isEqual3(local.dependencies, remote.dependencies))
|
|
4131
|
+
changed.push("dependencies");
|
|
4151
4132
|
const localEnvVars = local.schema.env_vars ?? [];
|
|
4152
4133
|
const remoteEnvVars = remote.schema.env_vars ?? [];
|
|
4153
|
-
if (!isEqual3(new Set(localEnvVars), new Set(remoteEnvVars)))
|
|
4134
|
+
if (!isEqual3(new Set(localEnvVars), new Set(remoteEnvVars)))
|
|
4135
|
+
changed.push("env_vars");
|
|
4154
4136
|
const localSecrets = local.schema.secrets ?? [];
|
|
4155
4137
|
const remoteSecrets = remote.schema.secrets ?? [];
|
|
4156
|
-
if (!isEqual3(new Set(localSecrets), new Set(remoteSecrets)))
|
|
4138
|
+
if (!isEqual3(new Set(localSecrets), new Set(remoteSecrets)))
|
|
4139
|
+
changed.push("secrets");
|
|
4157
4140
|
return changed;
|
|
4158
4141
|
},
|
|
4159
4142
|
renderDetail(local, remote) {
|
|
4160
4143
|
const result = {};
|
|
4161
|
-
const localSchemaYaml =
|
|
4162
|
-
const remoteSchemaYaml =
|
|
4144
|
+
const localSchemaYaml = yaml3.stringify(omit(local.data.schema, SCHEMA_IGNORED));
|
|
4145
|
+
const remoteSchemaYaml = yaml3.stringify(omit(remote.data.schema, SCHEMA_IGNORED));
|
|
4163
4146
|
if (localSchemaYaml !== remoteSchemaYaml) {
|
|
4164
4147
|
result["schema"] = buildUnifiedDiff(remoteSchemaYaml, localSchemaYaml, "schema.yaml");
|
|
4165
4148
|
}
|
|
4166
4149
|
if (local.data.index !== remote.data.index) {
|
|
4167
|
-
result["index"] = buildUnifiedDiff(
|
|
4168
|
-
remote.data.index,
|
|
4169
|
-
local.data.index,
|
|
4170
|
-
indexFilename(local.data.schema.language)
|
|
4171
|
-
);
|
|
4150
|
+
result["index"] = buildUnifiedDiff(remote.data.index, local.data.index, indexFilename(local.data.schema.language));
|
|
4172
4151
|
}
|
|
4173
4152
|
const sortKeys = (obj) => Object.fromEntries(Object.entries(obj).sort(([a], [b]) => a.localeCompare(b)));
|
|
4174
|
-
const localDepsYaml =
|
|
4175
|
-
const remoteDepsYaml =
|
|
4153
|
+
const localDepsYaml = yaml3.stringify(sortKeys(local.data.dependencies));
|
|
4154
|
+
const remoteDepsYaml = yaml3.stringify(sortKeys(remote.data.dependencies));
|
|
4176
4155
|
if (localDepsYaml !== remoteDepsYaml) {
|
|
4177
4156
|
result["dependencies"] = buildUnifiedDiff(remoteDepsYaml, localDepsYaml, "package.json");
|
|
4178
4157
|
}
|
|
@@ -4188,39 +4167,28 @@ var functionModule = {
|
|
|
4188
4167
|
watchedFiles: ["schema.yaml", "index.ts", "index.mjs", "index.js", "package.json"],
|
|
4189
4168
|
async devHandleEvent(ctx) {
|
|
4190
4169
|
const self = this;
|
|
4191
|
-
const {
|
|
4192
|
-
type,
|
|
4193
|
-
slug,
|
|
4194
|
-
file,
|
|
4195
|
-
http: http2,
|
|
4196
|
-
rootDir,
|
|
4197
|
-
warn,
|
|
4198
|
-
spin: spin2,
|
|
4199
|
-
refreshState,
|
|
4200
|
-
getRemoteId,
|
|
4201
|
-
setRemoteId,
|
|
4202
|
-
clearRemoteId,
|
|
4203
|
-
schedulePendingDelete,
|
|
4204
|
-
consumePendingDeleteByRemoteId
|
|
4205
|
-
} = ctx;
|
|
4170
|
+
const { type, slug, file, http: http2, rootDir, warn, spin: spin2, refreshState, getRemoteId, setRemoteId, clearRemoteId, schedulePendingDelete, consumePendingDeleteByRemoteId } = ctx;
|
|
4206
4171
|
const isIndexFile = (f) => f === "index.ts" || f === "index.mjs" || f === "index.js";
|
|
4207
4172
|
const isPackageFile = (f) => f === "package.json";
|
|
4208
4173
|
function fnLabel(symbol, forSlug, sub) {
|
|
4209
4174
|
const parts = [symbol, bold5(self.displayName), bold5(forSlug)];
|
|
4210
|
-
if (sub)
|
|
4175
|
+
if (sub)
|
|
4176
|
+
parts.push(yellow3(`[${sub}]`));
|
|
4211
4177
|
return parts.join(" ");
|
|
4212
4178
|
}
|
|
4213
4179
|
async function waitForRemoteId() {
|
|
4214
4180
|
for (let attempt = 0; attempt < 10; attempt++) {
|
|
4215
4181
|
const id = getRemoteId(slug);
|
|
4216
|
-
if (id)
|
|
4182
|
+
if (id)
|
|
4183
|
+
return id;
|
|
4217
4184
|
await ctx.sleep(200);
|
|
4218
4185
|
}
|
|
4219
4186
|
return getRemoteId(slug);
|
|
4220
4187
|
}
|
|
4221
4188
|
async function onSchemaAdd() {
|
|
4222
4189
|
const schema = self.readSchema(rootDir, slug);
|
|
4223
|
-
if (!schema)
|
|
4190
|
+
if (!schema)
|
|
4191
|
+
return;
|
|
4224
4192
|
const newLocalId = schema._id;
|
|
4225
4193
|
if (newLocalId) {
|
|
4226
4194
|
const pending = consumePendingDeleteByRemoteId(newLocalId);
|
|
@@ -4238,7 +4206,8 @@ var functionModule = {
|
|
|
4238
4206
|
} else {
|
|
4239
4207
|
await spin2(fnLabel(green5("+"), slug), "created", async () => {
|
|
4240
4208
|
const newRemoteId = await self.createSchema(http2, schema);
|
|
4241
|
-
if (!newRemoteId)
|
|
4209
|
+
if (!newRemoteId)
|
|
4210
|
+
return;
|
|
4242
4211
|
await refreshState();
|
|
4243
4212
|
});
|
|
4244
4213
|
}
|
|
@@ -4251,11 +4220,7 @@ var functionModule = {
|
|
|
4251
4220
|
if (id) {
|
|
4252
4221
|
const schema = self.readSchema(rootDir, slug);
|
|
4253
4222
|
if (schema) {
|
|
4254
|
-
await spin2(
|
|
4255
|
-
fnLabel(yellow3("~"), slug, "schema"),
|
|
4256
|
-
"updated",
|
|
4257
|
-
() => self.updateSchema(http2, id, schema)
|
|
4258
|
-
);
|
|
4223
|
+
await spin2(fnLabel(yellow3("~"), slug, "schema"), "updated", () => self.updateSchema(http2, id, schema));
|
|
4259
4224
|
}
|
|
4260
4225
|
}
|
|
4261
4226
|
} else if (type !== "unlink" && isIndexFile(file)) {
|
|
@@ -4273,34 +4238,26 @@ var functionModule = {
|
|
|
4273
4238
|
}
|
|
4274
4239
|
} else if (type === "unlink" && isSchema) {
|
|
4275
4240
|
const id = getRemoteId(slug);
|
|
4276
|
-
if (id)
|
|
4241
|
+
if (id)
|
|
4242
|
+
schedulePendingDelete(slug, id);
|
|
4277
4243
|
} else if (type === "unlink" && (isIndexFile(file) || isPackageFile(file))) {
|
|
4278
4244
|
}
|
|
4279
4245
|
}
|
|
4280
4246
|
};
|
|
4281
4247
|
|
|
4282
|
-
//
|
|
4283
|
-
import path13 from "path";
|
|
4284
|
-
import yaml5 from "yaml";
|
|
4248
|
+
// packages/sync/dist/src/modules/policy.js
|
|
4285
4249
|
var IGNORED_FIELDS3 = ["_id", "system"];
|
|
4286
4250
|
var policyModule = {
|
|
4287
4251
|
name: "policy",
|
|
4288
4252
|
displayName: "Policy",
|
|
4289
4253
|
identityField: "name",
|
|
4290
4254
|
ignoredFields: IGNORED_FIELDS3,
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
const slugs = listFolders(dir);
|
|
4294
|
-
const results = [];
|
|
4295
|
-
for (const slug of slugs) {
|
|
4296
|
-
const data = readYaml(path13.join(dir, slug, "schema.yaml"));
|
|
4297
|
-
if (data) results.push({ slug, data });
|
|
4298
|
-
}
|
|
4299
|
-
return results;
|
|
4255
|
+
readLocal(rootDir) {
|
|
4256
|
+
return readLocalSchemas(rootDir, "policy");
|
|
4300
4257
|
},
|
|
4301
4258
|
async readRemote(http2) {
|
|
4302
4259
|
const res = await http2.get("passport/policy");
|
|
4303
|
-
const items =
|
|
4260
|
+
const items = unwrapList(res);
|
|
4304
4261
|
return items.filter((p) => !p.system).map((p) => ({
|
|
4305
4262
|
slug: sanitizeSlug(p.name),
|
|
4306
4263
|
id: p._id,
|
|
@@ -4319,25 +4276,16 @@ var policyModule = {
|
|
|
4319
4276
|
await http2.delete(`passport/policy/${remoteId}`);
|
|
4320
4277
|
},
|
|
4321
4278
|
async writeLocal(rootDir, remote) {
|
|
4322
|
-
|
|
4323
|
-
writeYaml(path13.join(dir, "schema.yaml"), remote.data);
|
|
4279
|
+
writeLocalSchema(rootDir, "policy", remote);
|
|
4324
4280
|
},
|
|
4325
4281
|
async deleteLocal(rootDir, slug) {
|
|
4326
|
-
|
|
4282
|
+
deleteLocalSchema(rootDir, "policy", slug);
|
|
4327
4283
|
},
|
|
4328
4284
|
diffFields(local, remote) {
|
|
4329
|
-
return
|
|
4330
|
-
local,
|
|
4331
|
-
remote,
|
|
4332
|
-
IGNORED_FIELDS3
|
|
4333
|
-
);
|
|
4285
|
+
return diffSchemaFields(local, remote, IGNORED_FIELDS3);
|
|
4334
4286
|
},
|
|
4335
4287
|
renderDetail(local, remote) {
|
|
4336
|
-
|
|
4337
|
-
const remoteYaml = yaml5.stringify(omit(remote.data, IGNORED_FIELDS3));
|
|
4338
|
-
return {
|
|
4339
|
-
schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml")
|
|
4340
|
-
};
|
|
4288
|
+
return renderSchemaDetail(local.data, remote.data, IGNORED_FIELDS3);
|
|
4341
4289
|
},
|
|
4342
4290
|
summaryLine(resource) {
|
|
4343
4291
|
const stmts = resource.data.statement?.length ?? 0;
|
|
@@ -4348,24 +4296,15 @@ var policyModule = {
|
|
|
4348
4296
|
}
|
|
4349
4297
|
};
|
|
4350
4298
|
|
|
4351
|
-
//
|
|
4352
|
-
import path14 from "path";
|
|
4353
|
-
import yaml6 from "yaml";
|
|
4299
|
+
// packages/sync/dist/src/modules/secret.js
|
|
4354
4300
|
var IGNORED_FIELDS4 = ["_id", "value"];
|
|
4355
4301
|
var secretModule = {
|
|
4356
4302
|
name: "secret",
|
|
4357
4303
|
displayName: "Secret",
|
|
4358
4304
|
identityField: "key",
|
|
4359
4305
|
ignoredFields: IGNORED_FIELDS4,
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
const slugs = listFolders(dir);
|
|
4363
|
-
const results = [];
|
|
4364
|
-
for (const slug of slugs) {
|
|
4365
|
-
const data = readYaml(path14.join(dir, slug, "schema.yaml"));
|
|
4366
|
-
if (data) results.push({ slug, data });
|
|
4367
|
-
}
|
|
4368
|
-
return results;
|
|
4306
|
+
readLocal(rootDir) {
|
|
4307
|
+
return readLocalSchemas(rootDir, "secret");
|
|
4369
4308
|
},
|
|
4370
4309
|
async readRemote(http2) {
|
|
4371
4310
|
const res = await http2.get("secret");
|
|
@@ -4386,25 +4325,16 @@ var secretModule = {
|
|
|
4386
4325
|
await http2.delete(`secret/${remoteId}`);
|
|
4387
4326
|
},
|
|
4388
4327
|
async writeLocal(rootDir, remote) {
|
|
4389
|
-
|
|
4390
|
-
writeYaml(path14.join(dir, "schema.yaml"), remote.data);
|
|
4328
|
+
writeLocalSchema(rootDir, "secret", remote);
|
|
4391
4329
|
},
|
|
4392
4330
|
async deleteLocal(rootDir, slug) {
|
|
4393
|
-
|
|
4331
|
+
deleteLocalSchema(rootDir, "secret", slug);
|
|
4394
4332
|
},
|
|
4395
4333
|
diffFields(local, remote) {
|
|
4396
|
-
return
|
|
4397
|
-
local,
|
|
4398
|
-
remote,
|
|
4399
|
-
IGNORED_FIELDS4
|
|
4400
|
-
);
|
|
4334
|
+
return diffSchemaFields(local, remote, IGNORED_FIELDS4);
|
|
4401
4335
|
},
|
|
4402
4336
|
renderDetail(local, remote) {
|
|
4403
|
-
|
|
4404
|
-
const remoteYaml = yaml6.stringify(omit(remote.data, IGNORED_FIELDS4));
|
|
4405
|
-
return {
|
|
4406
|
-
schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml")
|
|
4407
|
-
};
|
|
4337
|
+
return renderSchemaDetail(local.data, remote.data, IGNORED_FIELDS4);
|
|
4408
4338
|
},
|
|
4409
4339
|
summaryLine(resource) {
|
|
4410
4340
|
return `key: ${resource.data.key}`;
|
|
@@ -4414,7 +4344,7 @@ var secretModule = {
|
|
|
4414
4344
|
}
|
|
4415
4345
|
};
|
|
4416
4346
|
|
|
4417
|
-
//
|
|
4347
|
+
// packages/sync/dist/src/modules/index.js
|
|
4418
4348
|
var ALL_MODULES = [
|
|
4419
4349
|
policyModule,
|
|
4420
4350
|
bucketModule,
|
|
@@ -4424,24 +4354,44 @@ var ALL_MODULES = [
|
|
|
4424
4354
|
];
|
|
4425
4355
|
var MODULE_NAMES = ALL_MODULES.map((m) => m.name);
|
|
4426
4356
|
function resolveModules(names) {
|
|
4427
|
-
if (!names || names.length === 0)
|
|
4357
|
+
if (!names || names.length === 0)
|
|
4358
|
+
return ALL_MODULES;
|
|
4428
4359
|
return [...new Set(names)].map((name) => {
|
|
4429
4360
|
const mod = ALL_MODULES.find((m) => m.name === name);
|
|
4430
|
-
if (!mod)
|
|
4361
|
+
if (!mod)
|
|
4362
|
+
throw new Error(`Unknown module: "${name}". Available: ${MODULE_NAMES.join(", ")}`);
|
|
4431
4363
|
return mod;
|
|
4432
4364
|
});
|
|
4433
4365
|
}
|
|
4434
4366
|
|
|
4367
|
+
// apps/cli/src/commands/sync/reporter.ts
|
|
4368
|
+
import ora2 from "ora";
|
|
4369
|
+
import { bold as bold6, yellow as yellow4 } from "colorette";
|
|
4370
|
+
var cliReporter = {
|
|
4371
|
+
task: (label, op) => spin({ text: label, op }),
|
|
4372
|
+
progress: (label, total) => {
|
|
4373
|
+
const spinner = ora2({ text: `${label} (0/${total})`, color: "yellow" }).start();
|
|
4374
|
+
return {
|
|
4375
|
+
update: (slug, done, t) => {
|
|
4376
|
+
spinner.text = `${label}: ${slug} (${done}/${t})`;
|
|
4377
|
+
},
|
|
4378
|
+
succeed: () => spinner.succeed(),
|
|
4379
|
+
fail: () => spinner.fail()
|
|
4380
|
+
};
|
|
4381
|
+
},
|
|
4382
|
+
warn: (message) => console.warn(bold6(yellow4(` \u26A0 ${message}`)))
|
|
4383
|
+
};
|
|
4384
|
+
|
|
4435
4385
|
// apps/cli/src/commands/sync/plan.ts
|
|
4436
4386
|
async function plan({ args, options }) {
|
|
4437
4387
|
try {
|
|
4438
|
-
const rootDir =
|
|
4388
|
+
const rootDir = path11.resolve(args.dir ?? process.cwd());
|
|
4439
4389
|
const detailed = !!options.detailed;
|
|
4440
4390
|
const json = !!options.json;
|
|
4441
4391
|
const moduleFilter = options.module ? (Array.isArray(options.module) ? options.module : [options.module]).map(String) : void 0;
|
|
4442
4392
|
const modules = resolveModules(moduleFilter);
|
|
4443
4393
|
const http2 = await httpService.createFromCurrentCtx();
|
|
4444
|
-
const p = await buildPlan(modules, http2, rootDir, detailed);
|
|
4394
|
+
const p = await buildPlan(modules, http2, rootDir, detailed, true, cliReporter);
|
|
4445
4395
|
renderPlan(p, { detailed, json });
|
|
4446
4396
|
process.exitCode = 0;
|
|
4447
4397
|
} catch (err) {
|
|
@@ -4468,9 +4418,9 @@ function plan_default(program2) {
|
|
|
4468
4418
|
}
|
|
4469
4419
|
|
|
4470
4420
|
// apps/cli/src/commands/sync/apply.ts
|
|
4471
|
-
import
|
|
4421
|
+
import path12 from "path";
|
|
4472
4422
|
import caporalCore4 from "@caporal/core";
|
|
4473
|
-
import { bold as
|
|
4423
|
+
import { bold as bold7, green as green6, red as red5, yellow as yellow5 } from "colorette";
|
|
4474
4424
|
|
|
4475
4425
|
// apps/cli/src/commands/sync/prompt.ts
|
|
4476
4426
|
import readline from "readline";
|
|
@@ -4487,7 +4437,7 @@ async function confirm(question) {
|
|
|
4487
4437
|
// apps/cli/src/commands/sync/apply.ts
|
|
4488
4438
|
var { CaporalValidator: CaporalValidator4 } = caporalCore4;
|
|
4489
4439
|
async function apply2({ args, options }) {
|
|
4490
|
-
const rootDir =
|
|
4440
|
+
const rootDir = path12.resolve(args.dir ?? process.cwd());
|
|
4491
4441
|
const autoApprove = !!options.autoApprove;
|
|
4492
4442
|
const concurrency = options.concurrency ?? 10;
|
|
4493
4443
|
const abortOnError = !!options.abortOnError;
|
|
@@ -4495,8 +4445,8 @@ async function apply2({ args, options }) {
|
|
|
4495
4445
|
const moduleFilter = options.module ? (Array.isArray(options.module) ? options.module : [options.module]).map(String) : void 0;
|
|
4496
4446
|
const modules = resolveModules(moduleFilter);
|
|
4497
4447
|
const http2 = await httpService.createFromCurrentCtx();
|
|
4498
|
-
console.log(
|
|
4499
|
-
const p = await buildPlan(modules, http2, rootDir, detailed);
|
|
4448
|
+
console.log(bold7("\nBuilding plan\u2026"));
|
|
4449
|
+
const p = await buildPlan(modules, http2, rootDir, detailed, true, cliReporter);
|
|
4500
4450
|
const totalChanges = p.modules.reduce(
|
|
4501
4451
|
(n, m) => n + m.creates.length + m.updates.length + m.deletes.length,
|
|
4502
4452
|
0
|
|
@@ -4509,33 +4459,33 @@ async function apply2({ args, options }) {
|
|
|
4509
4459
|
if (!autoApprove) {
|
|
4510
4460
|
const ok = await confirm("\nApply these changes?");
|
|
4511
4461
|
if (!ok) {
|
|
4512
|
-
console.log(
|
|
4462
|
+
console.log(bold7(yellow5("Aborted. No changes were applied.")));
|
|
4513
4463
|
process.exitCode = 1;
|
|
4514
4464
|
return;
|
|
4515
4465
|
}
|
|
4516
4466
|
}
|
|
4517
|
-
console.log(
|
|
4467
|
+
console.log(bold7("\nApplying changes\u2026"));
|
|
4518
4468
|
try {
|
|
4519
|
-
const { errors } = await applyPlan(p, http2, { concurrency, abortOnError });
|
|
4469
|
+
const { errors } = await applyPlan(p, http2, { concurrency, abortOnError, reporter: cliReporter });
|
|
4520
4470
|
if (errors.length) {
|
|
4521
|
-
console.log(
|
|
4471
|
+
console.log(bold7(yellow5(`
|
|
4522
4472
|
\u26A0 Completed with ${errors.length} error(s):`)));
|
|
4523
|
-
for (const e of errors) console.log(` ${
|
|
4473
|
+
for (const e of errors) console.log(` ${red5("\u2717")} ${e}`);
|
|
4524
4474
|
process.exitCode = 1;
|
|
4525
4475
|
} else {
|
|
4526
4476
|
const c = p.modules.reduce((n, m) => n + m.creates.length, 0);
|
|
4527
4477
|
const u = p.modules.reduce((n, m) => n + m.updates.length, 0);
|
|
4528
4478
|
const d = p.modules.reduce((n, m) => n + m.deletes.length, 0);
|
|
4529
4479
|
console.log(
|
|
4530
|
-
|
|
4531
|
-
\u2713 Apply completed.`)) + ` ${green6(`+${c} created`)} ${
|
|
4480
|
+
bold7(green6(`
|
|
4481
|
+
\u2713 Apply completed.`)) + ` ${green6(`+${c} created`)} ${yellow5(`~${u} updated`)} ${red5(`-${d} deleted`)}`
|
|
4532
4482
|
);
|
|
4533
4483
|
process.exitCode = 0;
|
|
4534
4484
|
}
|
|
4535
4485
|
} catch (err) {
|
|
4536
4486
|
const msg = err instanceof Error ? err.message : String(err);
|
|
4537
4487
|
console.log(`
|
|
4538
|
-
${
|
|
4488
|
+
${bold7(red5(`\u2717 ${msg}`))}`);
|
|
4539
4489
|
process.exitCode = 1;
|
|
4540
4490
|
}
|
|
4541
4491
|
}
|
|
@@ -4558,12 +4508,12 @@ function apply_default2(program2) {
|
|
|
4558
4508
|
}
|
|
4559
4509
|
|
|
4560
4510
|
// apps/cli/src/commands/sync/fetch.ts
|
|
4561
|
-
import
|
|
4511
|
+
import path13 from "path";
|
|
4562
4512
|
import caporalCore5 from "@caporal/core";
|
|
4563
|
-
import { bold as
|
|
4513
|
+
import { bold as bold8, green as green7, red as red6, yellow as yellow6 } from "colorette";
|
|
4564
4514
|
var { CaporalValidator: CaporalValidator5 } = caporalCore5;
|
|
4565
4515
|
async function fetch_({ args, options }) {
|
|
4566
|
-
const rootDir =
|
|
4516
|
+
const rootDir = path13.resolve(args.dir ?? process.cwd());
|
|
4567
4517
|
const autoApprove = !!options.autoApprove;
|
|
4568
4518
|
const concurrency = options.concurrency ?? 10;
|
|
4569
4519
|
const abortOnError = !!options.abortOnError;
|
|
@@ -4572,8 +4522,8 @@ async function fetch_({ args, options }) {
|
|
|
4572
4522
|
const moduleFilter = options.module ? (Array.isArray(options.module) ? options.module : [options.module]).map(String) : void 0;
|
|
4573
4523
|
const modules = resolveModules(moduleFilter);
|
|
4574
4524
|
const http2 = await httpService.createFromCurrentCtx();
|
|
4575
|
-
console.log(
|
|
4576
|
-
const p = await buildPlan(modules, http2, rootDir, detailed, false);
|
|
4525
|
+
console.log(bold8("\nBuilding plan\u2026"));
|
|
4526
|
+
const p = await buildPlan(modules, http2, rootDir, detailed, false, cliReporter);
|
|
4577
4527
|
const toWrite = p.modules.reduce((n, m) => n + m.deletes.length + m.updates.length, 0);
|
|
4578
4528
|
const toDelete = p.modules.reduce((n, m) => n + m.creates.length, 0);
|
|
4579
4529
|
const effectiveChanges = toWrite + (clean ? toDelete : 0);
|
|
@@ -4585,34 +4535,35 @@ async function fetch_({ args, options }) {
|
|
|
4585
4535
|
if (!autoApprove) {
|
|
4586
4536
|
const ok = await confirm("\nApply these changes?");
|
|
4587
4537
|
if (!ok) {
|
|
4588
|
-
console.log(
|
|
4538
|
+
console.log(bold8(yellow6("Aborted. No changes were applied.")));
|
|
4589
4539
|
process.exitCode = 1;
|
|
4590
4540
|
return;
|
|
4591
4541
|
}
|
|
4592
4542
|
}
|
|
4593
|
-
console.log(
|
|
4543
|
+
console.log(bold8("\nWriting files\u2026"));
|
|
4594
4544
|
try {
|
|
4595
4545
|
const { written, deleted, errors } = await fetchToDisk(p, http2, rootDir, {
|
|
4596
4546
|
concurrency,
|
|
4597
4547
|
abortOnError,
|
|
4598
|
-
clean
|
|
4548
|
+
clean,
|
|
4549
|
+
reporter: cliReporter
|
|
4599
4550
|
});
|
|
4600
4551
|
if (errors.length) {
|
|
4601
|
-
console.log(
|
|
4552
|
+
console.log(bold8(yellow6(`
|
|
4602
4553
|
\u26A0 Completed with ${errors.length} error(s):`)));
|
|
4603
|
-
for (const e of errors) console.log(` ${
|
|
4554
|
+
for (const e of errors) console.log(` ${red6("\u2717")} ${e}`);
|
|
4604
4555
|
process.exitCode = 1;
|
|
4605
4556
|
} else {
|
|
4606
4557
|
console.log(
|
|
4607
|
-
|
|
4608
|
-
\u2713 Fetch complete.`)) + ` ${green7(`+${written} written`)}` + (deleted ? ` ${
|
|
4558
|
+
bold8(green7(`
|
|
4559
|
+
\u2713 Fetch complete.`)) + ` ${green7(`+${written} written`)}` + (deleted ? ` ${red6(`-${deleted} deleted`)}` : "")
|
|
4609
4560
|
);
|
|
4610
4561
|
process.exitCode = 0;
|
|
4611
4562
|
}
|
|
4612
4563
|
} catch (err) {
|
|
4613
4564
|
const msg = err instanceof Error ? err.message : String(err);
|
|
4614
4565
|
console.log(`
|
|
4615
|
-
${
|
|
4566
|
+
${bold8(red6(`\u2717 ${msg}`))}`);
|
|
4616
4567
|
process.exitCode = 1;
|
|
4617
4568
|
}
|
|
4618
4569
|
}
|
|
@@ -4634,9 +4585,9 @@ function fetch_default(program2) {
|
|
|
4634
4585
|
}
|
|
4635
4586
|
|
|
4636
4587
|
// apps/cli/src/commands/sync/dev.ts
|
|
4637
|
-
import
|
|
4588
|
+
import path14 from "path";
|
|
4638
4589
|
import chokidar from "chokidar";
|
|
4639
|
-
import { bold as
|
|
4590
|
+
import { bold as bold9, cyan as cyan3, green as green8, red as red7, yellow as yellow7 } from "colorette";
|
|
4640
4591
|
import ora3 from "ora";
|
|
4641
4592
|
function createEmptyState() {
|
|
4642
4593
|
return { remoteIds: /* @__PURE__ */ new Map() };
|
|
@@ -4667,8 +4618,8 @@ function buildWatchIgnored(modules, rootDir) {
|
|
|
4667
4618
|
modules.map((m) => [m.name, new Set(m.watchedFiles ?? ["schema.yaml"])])
|
|
4668
4619
|
);
|
|
4669
4620
|
return (filePath, stats) => {
|
|
4670
|
-
const rel =
|
|
4671
|
-
const parts = rel.split(
|
|
4621
|
+
const rel = path14.relative(rootDir, filePath);
|
|
4622
|
+
const parts = rel.split(path14.sep);
|
|
4672
4623
|
if (stats?.isDirectory()) {
|
|
4673
4624
|
return parts.length >= 3;
|
|
4674
4625
|
}
|
|
@@ -4681,7 +4632,7 @@ function buildWatchIgnored(modules, rootDir) {
|
|
|
4681
4632
|
};
|
|
4682
4633
|
}
|
|
4683
4634
|
function readLocalSchema(mod, rootDir, slug) {
|
|
4684
|
-
const schemaPath =
|
|
4635
|
+
const schemaPath = path14.join(rootDir, mod.name, slug, "schema.yaml");
|
|
4685
4636
|
const data = readYaml(schemaPath);
|
|
4686
4637
|
if (!data) return null;
|
|
4687
4638
|
return { slug, data };
|
|
@@ -4694,11 +4645,11 @@ function createDevDispatcher(opts) {
|
|
|
4694
4645
|
const pendingDeletes = /* @__PURE__ */ new Map();
|
|
4695
4646
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
4696
4647
|
function parseFilePath(absolutePath) {
|
|
4697
|
-
const rel =
|
|
4698
|
-
const parts = rel.split(
|
|
4648
|
+
const rel = path14.relative(rootDir, absolutePath);
|
|
4649
|
+
const parts = rel.split(path14.sep);
|
|
4699
4650
|
if (parts.length < 3) return null;
|
|
4700
4651
|
const [moduleName, slug, ...rest] = parts;
|
|
4701
|
-
return { moduleName, slug, file: rest.join(
|
|
4652
|
+
return { moduleName, slug, file: rest.join(path14.sep) };
|
|
4702
4653
|
}
|
|
4703
4654
|
function getRemoteId(moduleName, slug) {
|
|
4704
4655
|
return state.remoteIds.get(moduleName)?.get(slug);
|
|
@@ -4738,20 +4689,24 @@ function createDevDispatcher(opts) {
|
|
|
4738
4689
|
pendingDeletes.delete(promotedKey);
|
|
4739
4690
|
clearRemoteId(mod.name, pending.slug);
|
|
4740
4691
|
setRemoteId(mod.name, slug, pending.remoteId);
|
|
4741
|
-
const renameLabel = `${
|
|
4742
|
-
await spinOp(
|
|
4692
|
+
const renameLabel = `${yellow7("~")} ${bold9(mod.displayName)} ${bold9(pending.slug)} ${cyan3("\u2192")} ${bold9(slug)}`;
|
|
4693
|
+
await spinOp(
|
|
4694
|
+
renameLabel,
|
|
4695
|
+
"updated (rename)",
|
|
4696
|
+
() => mod.update(http2, local, pending.remoteId)
|
|
4697
|
+
);
|
|
4743
4698
|
return;
|
|
4744
4699
|
}
|
|
4745
4700
|
const remoteId = getRemoteId(mod.name, slug);
|
|
4746
4701
|
if (remoteId) {
|
|
4747
4702
|
await spinOp(
|
|
4748
|
-
`${
|
|
4703
|
+
`${yellow7("~")} ${bold9(mod.displayName)} ${bold9(slug)}`,
|
|
4749
4704
|
"updated",
|
|
4750
4705
|
() => mod.update(http2, local, remoteId)
|
|
4751
4706
|
);
|
|
4752
4707
|
} else {
|
|
4753
4708
|
await spinOp(
|
|
4754
|
-
`${green8("+")} ${
|
|
4709
|
+
`${green8("+")} ${bold9(mod.displayName)} ${bold9(slug)}`,
|
|
4755
4710
|
"created",
|
|
4756
4711
|
async () => {
|
|
4757
4712
|
await mod.create(http2, local);
|
|
@@ -4766,7 +4721,7 @@ function createDevDispatcher(opts) {
|
|
|
4766
4721
|
const remoteId = getRemoteId(mod.name, slug);
|
|
4767
4722
|
if (!remoteId) return handleSchemaAdd(mod, slug);
|
|
4768
4723
|
await spinOp(
|
|
4769
|
-
`${
|
|
4724
|
+
`${yellow7("~")} ${bold9(mod.displayName)} ${bold9(slug)}`,
|
|
4770
4725
|
"updated",
|
|
4771
4726
|
() => mod.update(http2, local, remoteId)
|
|
4772
4727
|
);
|
|
@@ -4776,14 +4731,16 @@ function createDevDispatcher(opts) {
|
|
|
4776
4731
|
if (!remoteId) return;
|
|
4777
4732
|
const pendingKey = `${mod.name}:${slug}`;
|
|
4778
4733
|
if (pendingDeletes.has(pendingKey)) return;
|
|
4779
|
-
const timer = setTimeout(
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
|
|
4783
|
-
|
|
4784
|
-
|
|
4785
|
-
|
|
4786
|
-
|
|
4734
|
+
const timer = setTimeout(() => {
|
|
4735
|
+
Promise.resolve().then(async () => {
|
|
4736
|
+
pendingDeletes.delete(pendingKey);
|
|
4737
|
+
clearRemoteId(mod.name, slug);
|
|
4738
|
+
await spinOp(
|
|
4739
|
+
`${red7("-")} ${bold9(mod.displayName)} ${bold9(slug)}`,
|
|
4740
|
+
"deleted",
|
|
4741
|
+
() => mod.delete(http2, remoteId)
|
|
4742
|
+
);
|
|
4743
|
+
}).catch((err) => warn(`Delete failed for ${slug}: ${formatError2(err)}`));
|
|
4787
4744
|
}, renameWindowMs);
|
|
4788
4745
|
pendingDeletes.set(pendingKey, { timer, moduleName: mod.name, slug, remoteId });
|
|
4789
4746
|
}
|
|
@@ -4800,14 +4757,16 @@ function createDevDispatcher(opts) {
|
|
|
4800
4757
|
schedulePendingDelete(s, remoteId) {
|
|
4801
4758
|
const pendingKey = `${mod.name}:${s}`;
|
|
4802
4759
|
if (pendingDeletes.has(pendingKey)) return;
|
|
4803
|
-
const timer = setTimeout(
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4760
|
+
const timer = setTimeout(() => {
|
|
4761
|
+
Promise.resolve().then(async () => {
|
|
4762
|
+
pendingDeletes.delete(pendingKey);
|
|
4763
|
+
clearRemoteId(mod.name, s);
|
|
4764
|
+
await spinOp(
|
|
4765
|
+
`${red7("-")} ${bold9(mod.displayName)} ${bold9(s)}`,
|
|
4766
|
+
"deleted",
|
|
4767
|
+
() => mod.delete(http2, remoteId)
|
|
4768
|
+
);
|
|
4769
|
+
}).catch((err) => warn(`Delete failed for ${s}: ${formatError2(err)}`));
|
|
4811
4770
|
}, renameWindowMs);
|
|
4812
4771
|
pendingDeletes.set(pendingKey, { timer, moduleName: mod.name, slug: s, remoteId });
|
|
4813
4772
|
},
|
|
@@ -4861,7 +4820,7 @@ function formatError2(e) {
|
|
|
4861
4820
|
return String(e);
|
|
4862
4821
|
}
|
|
4863
4822
|
async function dev({ args, options }) {
|
|
4864
|
-
const rootDir =
|
|
4823
|
+
const rootDir = path14.resolve(args.dir ?? process.cwd());
|
|
4865
4824
|
const moduleFilter = options.module ? (Array.isArray(options.module) ? options.module : [options.module]).map(String) : void 0;
|
|
4866
4825
|
const modules = resolveModules(moduleFilter);
|
|
4867
4826
|
let ctx;
|
|
@@ -4869,27 +4828,27 @@ async function dev({ args, options }) {
|
|
|
4869
4828
|
ctx = await context.getCurrent();
|
|
4870
4829
|
} catch (err) {
|
|
4871
4830
|
console.log(`
|
|
4872
|
-
${
|
|
4831
|
+
${bold9(red7(`\u2717 ${formatError2(err)}`))}`);
|
|
4873
4832
|
process.exitCode = 1;
|
|
4874
4833
|
return;
|
|
4875
4834
|
}
|
|
4876
4835
|
if (ctx.url.startsWith("https://")) {
|
|
4877
4836
|
console.log(
|
|
4878
|
-
|
|
4879
|
-
Tip: Start a local instance with ${
|
|
4837
|
+
bold9(yellow7("\n\u26A0 Warning: The current context is using HTTPS.\n")) + ` ${bold9("sync dev")} is designed for development. Using it against a remote instance may have unintended side-effects.
|
|
4838
|
+
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")}.`
|
|
4880
4839
|
);
|
|
4881
4840
|
}
|
|
4882
4841
|
const http2 = httpService.create({
|
|
4883
4842
|
baseUrl: ctx.url,
|
|
4884
4843
|
authorization: ctx.authorization
|
|
4885
4844
|
});
|
|
4886
|
-
console.log(
|
|
4845
|
+
console.log(bold9("\nBuilding plan\u2026"));
|
|
4887
4846
|
let plan2;
|
|
4888
4847
|
try {
|
|
4889
|
-
plan2 = await buildPlan(modules, http2, rootDir, false);
|
|
4848
|
+
plan2 = await buildPlan(modules, http2, rootDir, false, true, cliReporter);
|
|
4890
4849
|
} catch (err) {
|
|
4891
4850
|
console.log(`
|
|
4892
|
-
${
|
|
4851
|
+
${bold9(red7(`\u2717 Failed to build plan: ${formatError2(err)}`))}`);
|
|
4893
4852
|
process.exitCode = 1;
|
|
4894
4853
|
return;
|
|
4895
4854
|
}
|
|
@@ -4901,22 +4860,26 @@ ${bold8(red8(`\u2717 Failed to build plan: ${formatError2(err)}`))}`);
|
|
|
4901
4860
|
if (totalChanges > 0) {
|
|
4902
4861
|
const ok = await confirm("\nApply these changes?");
|
|
4903
4862
|
if (!ok) {
|
|
4904
|
-
console.log(
|
|
4863
|
+
console.log(bold9(yellow7("Aborted. No changes were applied.")));
|
|
4905
4864
|
process.exitCode = 1;
|
|
4906
4865
|
return;
|
|
4907
4866
|
}
|
|
4908
|
-
console.log(
|
|
4867
|
+
console.log(bold9("\nApplying changes\u2026"));
|
|
4909
4868
|
try {
|
|
4910
|
-
const { errors } = await applyPlan(plan2, http2, {
|
|
4869
|
+
const { errors } = await applyPlan(plan2, http2, {
|
|
4870
|
+
concurrency: 5,
|
|
4871
|
+
abortOnError: true,
|
|
4872
|
+
reporter: cliReporter
|
|
4873
|
+
});
|
|
4911
4874
|
if (errors.length) {
|
|
4912
|
-
console.log(
|
|
4875
|
+
console.log(bold9(red7(`
|
|
4913
4876
|
\u2717 Apply failed with ${errors.length} error(s).`)));
|
|
4914
4877
|
process.exitCode = 1;
|
|
4915
4878
|
return;
|
|
4916
4879
|
}
|
|
4917
4880
|
} catch (err) {
|
|
4918
4881
|
console.log(`
|
|
4919
|
-
${
|
|
4882
|
+
${bold9(red7(`\u2717 ${formatError2(err)}`))}`);
|
|
4920
4883
|
process.exitCode = 1;
|
|
4921
4884
|
return;
|
|
4922
4885
|
}
|
|
@@ -4924,19 +4887,19 @@ ${bold8(red8(`\u2717 ${formatError2(err)}`))}`);
|
|
|
4924
4887
|
const u = plan2.modules.reduce((n, m) => n + m.updates.length, 0);
|
|
4925
4888
|
const d = plan2.modules.reduce((n, m) => n + m.deletes.length, 0);
|
|
4926
4889
|
console.log(
|
|
4927
|
-
|
|
4928
|
-
\u2713 Initial sync completed.`)) + ` ${green8(`+${c} created`)} ${
|
|
4890
|
+
bold9(green8(`
|
|
4891
|
+
\u2713 Initial sync completed.`)) + ` ${green8(`+${c} created`)} ${yellow7(`~${u} updated`)} ${red7(`-${d} deleted`)}`
|
|
4929
4892
|
);
|
|
4930
4893
|
} else {
|
|
4931
|
-
console.log(
|
|
4894
|
+
console.log(bold9(green8("\n\u2713 Already in sync. Starting watcher\u2026")));
|
|
4932
4895
|
}
|
|
4933
4896
|
const state = createEmptyState();
|
|
4934
4897
|
await Promise.all(modules.map((mod) => refreshStateForModule(mod, http2, state, rootDir)));
|
|
4935
|
-
const watchDirs = modules.map((mod) =>
|
|
4898
|
+
const watchDirs = modules.map((mod) => path14.join(rootDir, mod.name));
|
|
4936
4899
|
const dispatcher = createDevDispatcher({ modules, http: http2, rootDir, state });
|
|
4937
4900
|
console.log(
|
|
4938
|
-
|
|
4939
|
-
${cyan3("\u{1F441}")} Watching for changes in ${
|
|
4901
|
+
bold9(`
|
|
4902
|
+
${cyan3("\u{1F441}")} Watching for changes in ${bold9(rootDir)}\u2026`) + ` ${bold9(yellow7("(Ctrl-C to stop)"))}`
|
|
4940
4903
|
);
|
|
4941
4904
|
const watcher = chokidar.watch(watchDirs, {
|
|
4942
4905
|
ignoreInitial: true,
|
|
@@ -4944,16 +4907,25 @@ ${cyan3("\u{1F441}")} Watching for changes in ${bold8(rootDir)}\u2026`) + ` ${
|
|
|
4944
4907
|
ignored: buildWatchIgnored(modules, rootDir),
|
|
4945
4908
|
awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 50 }
|
|
4946
4909
|
});
|
|
4947
|
-
watcher.on(
|
|
4948
|
-
|
|
4949
|
-
|
|
4910
|
+
watcher.on(
|
|
4911
|
+
"add",
|
|
4912
|
+
(p) => dispatcher.handleEvent("add", p).catch((err) => console.warn(`[sync/dev] add: ${formatError2(err)}`))
|
|
4913
|
+
);
|
|
4914
|
+
watcher.on(
|
|
4915
|
+
"change",
|
|
4916
|
+
(p) => dispatcher.handleEvent("change", p).catch((err) => console.warn(`[sync/dev] change: ${formatError2(err)}`))
|
|
4917
|
+
);
|
|
4918
|
+
watcher.on(
|
|
4919
|
+
"unlink",
|
|
4920
|
+
(p) => dispatcher.handleEvent("unlink", p).catch((err) => console.warn(`[sync/dev] unlink: ${formatError2(err)}`))
|
|
4921
|
+
);
|
|
4950
4922
|
watcher.on(
|
|
4951
4923
|
"error",
|
|
4952
|
-
(err) => console.warn(
|
|
4924
|
+
(err) => console.warn(bold9(yellow7(`
|
|
4953
4925
|
\u26A0 Watcher error: ${formatError2(err)}`)))
|
|
4954
4926
|
);
|
|
4955
4927
|
const shutdown = () => {
|
|
4956
|
-
console.log(
|
|
4928
|
+
console.log(bold9("\nStopping watcher\u2026"));
|
|
4957
4929
|
dispatcher.dispose();
|
|
4958
4930
|
watcher.close().then(() => process.exit(0));
|
|
4959
4931
|
};
|