@chrisluyi/daas-cli 1.5.3 → 1.6.0

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.
Files changed (2) hide show
  1. package/dist/index.js +106 -26
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -2445,7 +2445,7 @@ function stripScope(name) {
2445
2445
  return name.replace(/^@[^/]+\//, "").replace(/-/g, "_");
2446
2446
  }
2447
2447
  // package.json
2448
- var version = "1.5.3";
2448
+ var version = "1.6.0";
2449
2449
 
2450
2450
  // src/version.ts
2451
2451
  function getCliVersion() {
@@ -3605,7 +3605,6 @@ function getPackageName(cwd) {
3605
3605
  import { spawnSync } from "child_process";
3606
3606
  import { existsSync as existsSync8, readdirSync as readdirSync2, readFileSync as readFileSync7, writeFileSync as writeFileSync5 } from "fs";
3607
3607
  import { relative as relative2, resolve as resolve9 } from "path";
3608
-
3609
3608
  class RegionError extends Error {
3610
3609
  constructor(message) {
3611
3610
  super(message);
@@ -3616,6 +3615,17 @@ var DEFAULT_ALIASES = {
3616
3615
  "@region-config": "./src/config/regions/{region}/config",
3617
3616
  "@regional": "./src/config/regions/{region}/index"
3618
3617
  };
3618
+ var DEFAULT_TARGET_ALIASES = {
3619
+ ...DEFAULT_ALIASES,
3620
+ "@platform-config": "./src/config/platforms/{platform}",
3621
+ "@product-config": "./src/config/products/{product}"
3622
+ };
3623
+ function parseSelector(selector) {
3624
+ if (selector.split("-").length >= 4) {
3625
+ return { selector, ...parseTarget(selector) };
3626
+ }
3627
+ return { selector, region: selector };
3628
+ }
3619
3629
  function readRegionalConfig(cwd = process.cwd()) {
3620
3630
  const pkgPath = resolve9(cwd, "package.json");
3621
3631
  if (!existsSync8(pkgPath)) {
@@ -3626,6 +3636,8 @@ function readRegionalConfig(cwd = process.cwd()) {
3626
3636
  return {
3627
3637
  ...r4,
3628
3638
  regionsDir: r4.regionsDir ?? "src/config/regions",
3639
+ platformsDir: r4.platformsDir ?? "src/config/platforms",
3640
+ productsDir: r4.productsDir ?? "src/config/products",
3629
3641
  tsconfig: r4.tsconfig ?? "tsconfig.daas.json",
3630
3642
  extends: r4.extends ?? "./tsconfig.json",
3631
3643
  bunPaths: r4.bunPaths ?? false
@@ -3644,31 +3656,91 @@ function discoverRegions(cwd, cfg) {
3644
3656
  }
3645
3657
  return regions;
3646
3658
  }
3647
- function renderRegionTsconfig(cwd, cfg, region) {
3659
+ function discoverDimension(cwd, dir, explicit) {
3660
+ const abs = resolve9(cwd, dir);
3661
+ if (!existsSync8(abs))
3662
+ return null;
3663
+ const entries = readdirSync2(abs, { withFileTypes: true }).filter((e3) => e3.isDirectory() || e3.name.endsWith(".ts")).map((e3) => ({
3664
+ name: e3.isDirectory() ? e3.name : e3.name.replace(/\.ts$/, ""),
3665
+ rel: `${dir}/${e3.name}`
3666
+ })).sort((a4, b4) => a4.name.localeCompare(b4.name));
3667
+ if (explicit && explicit.length > 0) {
3668
+ return entries.filter((e3) => explicit.includes(e3.name));
3669
+ }
3670
+ return entries;
3671
+ }
3672
+ function renderRegionTsconfig(cwd, cfg, selectorArg) {
3673
+ const sel = parseSelector(selectorArg);
3648
3674
  const regions = discoverRegions(cwd, cfg);
3649
- if (!regions.includes(region)) {
3650
- throw new RegionError(`Unknown region "${region}". Known: ${regions.join(", ")}`);
3651
- }
3652
- const aliases = cfg.aliases ?? DEFAULT_ALIASES;
3653
- const paths = Object.fromEntries(Object.entries(aliases).map(([k3, v3]) => [
3654
- k3,
3655
- [v3.replaceAll("{region}", region)]
3656
- ]));
3675
+ if (!regions.includes(sel.region)) {
3676
+ throw new RegionError(`Unknown region "${sel.region}". Known: ${regions.join(", ")}`);
3677
+ }
3678
+ const aliases = cfg.aliases ?? (sel.platform ? DEFAULT_TARGET_ALIASES : DEFAULT_ALIASES);
3679
+ const substitutions = {
3680
+ "{region}": sel.region,
3681
+ "{platform}": sel.platform,
3682
+ "{product}": sel.product,
3683
+ "{environment}": sel.environment
3684
+ };
3685
+ const paths = Object.fromEntries(Object.entries(aliases).map(([k3, v3]) => {
3686
+ let out = v3;
3687
+ for (const [ph, value] of Object.entries(substitutions)) {
3688
+ if (!out.includes(ph))
3689
+ continue;
3690
+ if (value === undefined) {
3691
+ throw new RegionError(`Alias "${k3}" uses ${ph} \u2014 pass a full target ({region}-{product}-{platform}-{environment}), not a bare region`);
3692
+ }
3693
+ out = out.replaceAll(ph, value);
3694
+ }
3695
+ return [k3, [out]];
3696
+ }));
3657
3697
  const tsconfigDir = resolve9(cwd, cfg.tsconfig, "..");
3658
- const regionsDirAbs = resolve9(cwd, cfg.regionsDir);
3659
- const others = regions.filter((r4) => r4 !== region).map((r4) => relative2(tsconfigDir, resolve9(regionsDirAbs, r4)));
3660
- const exclude = [...others, ...cfg.multiRegionFiles ?? []];
3698
+ const toRel = (p) => relative2(tsconfigDir, resolve9(cwd, p));
3699
+ const exclude = regions.filter((r4) => r4 !== sel.region).map((r4) => toRel(`${cfg.regionsDir}/${r4}`));
3700
+ const resolveDimDir = (template) => {
3701
+ let out = template;
3702
+ for (const [ph, value] of Object.entries(substitutions)) {
3703
+ if (!out.includes(ph))
3704
+ continue;
3705
+ if (value === undefined)
3706
+ return null;
3707
+ out = out.replaceAll(ph, value);
3708
+ }
3709
+ return out;
3710
+ };
3711
+ for (const [dim, dirTemplate, explicit] of [
3712
+ ["platform", cfg.platformsDir, cfg.platforms],
3713
+ ["product", cfg.productsDir, cfg.products]
3714
+ ]) {
3715
+ const selected = sel[dim];
3716
+ if (!selected)
3717
+ continue;
3718
+ const dir = resolveDimDir(dirTemplate);
3719
+ if (!dir)
3720
+ continue;
3721
+ const entries = discoverDimension(cwd, dir, explicit);
3722
+ if (!entries)
3723
+ continue;
3724
+ if (!entries.some((e3) => e3.name === selected)) {
3725
+ throw new RegionError(`Unknown ${dim} "${selected}". Known: ${entries.map((e3) => e3.name).join(", ")}`);
3726
+ }
3727
+ for (const e3 of entries) {
3728
+ if (e3.name !== selected)
3729
+ exclude.push(toRel(e3.rel));
3730
+ }
3731
+ }
3732
+ exclude.push(...cfg.multiRegionFiles ?? []);
3661
3733
  const compilerOptions = {
3662
3734
  ...cfg.bunPaths ? { baseUrl: ".", ignoreDeprecations: "6.0" } : {},
3663
3735
  paths
3664
3736
  };
3665
3737
  const body = JSON.stringify({ extends: cfg.extends, compilerOptions, exclude }, null, 2);
3666
- return `// GENERATED by \`daas region ${region}\` \u2014 do not edit; regenerate or \`daas region --reset\`.
3738
+ return `// GENERATED by \`daas region ${sel.selector}\` \u2014 do not edit; regenerate or \`daas region --reset\`.
3667
3739
  ${body}
3668
3740
  `;
3669
3741
  }
3670
- function writeRegionTsconfig(cwd, cfg, region, applyTo) {
3671
- const content = renderRegionTsconfig(cwd, cfg, region);
3742
+ function writeRegionTsconfig(cwd, cfg, selector, applyTo) {
3743
+ const content = renderRegionTsconfig(cwd, cfg, selector);
3672
3744
  const out = resolve9(cwd, cfg.tsconfig);
3673
3745
  writeFileSync5(out, content);
3674
3746
  if (applyTo) {
@@ -3679,7 +3751,7 @@ function writeRegionTsconfig(cwd, cfg, region, applyTo) {
3679
3751
  }
3680
3752
  function resetTsconfig(cwd, path) {
3681
3753
  const rel = relative2(cwd, resolve9(cwd, path));
3682
- const show = spawnSync("git", ["show", `HEAD:${rel}`], {
3754
+ const show = spawnSync("git", ["show", `HEAD:./${rel}`], {
3683
3755
  cwd,
3684
3756
  encoding: "utf8"
3685
3757
  });
@@ -3695,11 +3767,12 @@ function runRegionCheck(cwd, cfg, runner = (p) => {
3695
3767
  });
3696
3768
  return { ok: r4.status === 0, output: `${r4.stdout ?? ""}${r4.stderr ?? ""}` };
3697
3769
  }) {
3770
+ const selectors = cfg.targets && cfg.targets.length > 0 ? cfg.targets : discoverRegions(cwd, cfg);
3698
3771
  const results = [];
3699
- for (const region of discoverRegions(cwd, cfg)) {
3700
- writeRegionTsconfig(cwd, cfg, region);
3772
+ for (const selector of selectors) {
3773
+ writeRegionTsconfig(cwd, cfg, selector);
3701
3774
  const { ok, output } = runner(resolve9(cwd, cfg.tsconfig));
3702
- results.push({ region, ok, output });
3775
+ results.push({ region: selector, ok, output });
3703
3776
  }
3704
3777
  return results;
3705
3778
  }
@@ -3713,14 +3786,18 @@ var regionCommand = defineCommand({
3713
3786
  args: {
3714
3787
  region: {
3715
3788
  type: "positional",
3716
- description: "Region to select (see --list)",
3789
+ description: "Region, or full target string {region}-{product}-{platform}-{environment} e.g. sg-digibank-shared-dev (see --list)",
3717
3790
  required: false
3718
3791
  },
3719
- list: { type: "boolean", default: false, description: "List regions" },
3792
+ list: {
3793
+ type: "boolean",
3794
+ default: false,
3795
+ description: "List regions (and configured targets)"
3796
+ },
3720
3797
  check: {
3721
3798
  type: "boolean",
3722
3799
  default: false,
3723
- description: "Matrix gate: generate + tsc --noEmit for EVERY region; fails if any region's program fails"
3800
+ description: "Matrix gate: generate + tsc --noEmit per daas.regional.targets entry (else per region); fails if any program fails"
3724
3801
  },
3725
3802
  apply: {
3726
3803
  type: "string",
@@ -3751,9 +3828,12 @@ var regionCommand = defineCommand({
3751
3828
  if (args.list) {
3752
3829
  const regions = discoverRegions(cwd, cfg);
3753
3830
  if (args.json)
3754
- writeJsonSuccess("region", { regions });
3755
- else
3831
+ writeJsonSuccess("region", { regions, targets: cfg.targets ?? [] });
3832
+ else {
3756
3833
  consola.info(`Regions: ${regions.join(", ")}`);
3834
+ if (cfg.targets && cfg.targets.length > 0)
3835
+ consola.info(`Targets: ${cfg.targets.join(", ")}`);
3836
+ }
3757
3837
  return;
3758
3838
  }
3759
3839
  if (args.check) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrisluyi/daas-cli",
3
- "version": "1.5.3",
3
+ "version": "1.6.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "daas": "./dist/index.js"
@@ -22,8 +22,8 @@
22
22
  "dependencies": {
23
23
  "@clack/prompts": "^0.7.0",
24
24
  "@chrisluyi/daas-logger": "1.1.1",
25
- "@chrisluyi/daas-rsbuild-config": "1.5.3",
26
- "@chrisluyi/daas-template": "1.5.3",
25
+ "@chrisluyi/daas-rsbuild-config": "1.6.0",
26
+ "@chrisluyi/daas-template": "1.6.0",
27
27
  "@rsbuild/core": "*",
28
28
  "citty": "^0.1.6",
29
29
  "consola": "^3.2.3",