@mnci/cli 4.17.5 → 4.18.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.
package/dist/cli.js CHANGED
@@ -2971,13 +2971,47 @@ function isNxGoPluginRegistered(workspaceRoot) {
2971
2971
  }
2972
2972
  return (readJson(nxJsonPath).plugins ?? []).some((entry) => (typeof entry === "string" ? entry : entry.plugin) === NX_GO_PLUGIN);
2973
2973
  }
2974
+ function modulePrefixFromRemoteUrl(url) {
2975
+ const trimmed = url.trim();
2976
+ const scpLike = /^[^@/]+@([^:]+):(.+)$/.exec(trimmed);
2977
+ const withScheme = /^[a-z][\w+.-]*:\/\/(?:[^@/]+@)?(.+)$/i.exec(trimmed);
2978
+ let path;
2979
+ if (scpLike) {
2980
+ path = `${scpLike[1]}/${scpLike[2]}`;
2981
+ } else if (withScheme) {
2982
+ path = withScheme[1];
2983
+ }
2984
+ if (path === void 0) {
2985
+ return void 0;
2986
+ }
2987
+ const cleaned = path.replace(/\.git$/, "").replace(/\/+$/, "");
2988
+ return cleaned === "" ? void 0 : cleaned;
2989
+ }
2990
+ function goModulePrefix(workspaceRoot) {
2991
+ const result = runCapture("git", ["remote", "get-url", "origin"], workspaceRoot);
2992
+ return result.status === 0 ? modulePrefixFromRemoteUrl(result.stdout) : void 0;
2993
+ }
2974
2994
  function registerNxGoPlugin(workspaceRoot) {
2975
2995
  const nxJsonPath = (0, import_node_path7.join)(workspaceRoot, "nx.json");
2976
- if (!fileExists(nxJsonPath) || isNxGoPluginRegistered(workspaceRoot)) {
2996
+ if (!fileExists(nxJsonPath)) {
2977
2997
  return false;
2978
2998
  }
2999
+ const modulePrefix = goModulePrefix(workspaceRoot);
3000
+ const entry = modulePrefix === void 0 ? NX_GO_PLUGIN : { plugin: NX_GO_PLUGIN, options: { modulePrefix } };
2979
3001
  const nxJson = readJson(nxJsonPath);
2980
- writeFileEnsured(nxJsonPath, toJson({ ...nxJson, plugins: [...nxJson.plugins ?? [], NX_GO_PLUGIN] }));
3002
+ const plugins = [...nxJson.plugins ?? []];
3003
+ const index = plugins.findIndex((plugin) => (typeof plugin === "string" ? plugin : plugin.plugin) === NX_GO_PLUGIN);
3004
+ if (index === -1) {
3005
+ plugins.push(entry);
3006
+ } else {
3007
+ const current = plugins[index];
3008
+ const currentPrefix = typeof current === "string" ? void 0 : current.options?.modulePrefix;
3009
+ if (modulePrefix === void 0 || currentPrefix === modulePrefix) {
3010
+ return false;
3011
+ }
3012
+ plugins[index] = entry;
3013
+ }
3014
+ writeFileEnsured(nxJsonPath, toJson({ ...nxJson, plugins }));
2981
3015
  return true;
2982
3016
  }
2983
3017
 
@@ -3322,21 +3356,36 @@ function ensureNxGoPlugin(workspaceRoot) {
3322
3356
  }
3323
3357
  }
3324
3358
  function ensureGoModule(workspaceRoot) {
3325
- if (fileExists((0, import_node_path10.join)(workspaceRoot, "go.mod"))) {
3359
+ if (fileExists((0, import_node_path10.join)(workspaceRoot, "go.work")) || fileExists((0, import_node_path10.join)(workspaceRoot, "go.mod"))) {
3326
3360
  return;
3327
3361
  }
3328
- logger.step("Bootstrapping the workspace Go module (single root go.mod)");
3362
+ logger.step("Bootstrapping the Go workspace (go.work, one module per project)");
3329
3363
  runNx(["g", "@nx-go/nx-go:init", "--no-interactive"], workspaceRoot);
3330
- runNx(["g", "@nx-go/nx-go:convert-to-one-mod", "--no-interactive"], workspaceRoot);
3331
3364
  }
3332
- function goModulePath(workspaceRoot) {
3365
+ function readGoModModule(goModPath) {
3333
3366
  try {
3334
- const contents = (0, import_node_fs9.readFileSync)((0, import_node_path10.join)(workspaceRoot, "go.mod"), "utf8");
3335
- return /^module\s+(\S+)/m.exec(contents)?.[1];
3367
+ return /^module\s+(\S+)/m.exec((0, import_node_fs9.readFileSync)(goModPath, "utf8"))?.[1];
3336
3368
  } catch {
3337
3369
  return void 0;
3338
3370
  }
3339
3371
  }
3372
+ function goModulePathFor(workspaceRoot, projectDir) {
3373
+ const projectModule = readGoModModule((0, import_node_path10.join)(workspaceRoot, projectDir, "go.mod"));
3374
+ if (projectModule !== void 0) {
3375
+ return projectModule;
3376
+ }
3377
+ const rootModule = readGoModModule((0, import_node_path10.join)(workspaceRoot, "go.mod"));
3378
+ return rootModule === void 0 ? void 0 : `${rootModule}/${projectDir}`;
3379
+ }
3380
+ function setGoModulePath(workspaceRoot, projectDir) {
3381
+ const prefix = goModulePrefix(workspaceRoot);
3382
+ const goModPath = (0, import_node_path10.join)(workspaceRoot, projectDir, "go.mod");
3383
+ if (prefix === void 0 || !fileExists(goModPath)) {
3384
+ return;
3385
+ }
3386
+ const rewritten = (0, import_node_fs9.readFileSync)(goModPath, "utf8").replace(/^module\s+\S+/m, () => `module ${prefix}/${projectDir}`);
3387
+ writeFileEnsured(goModPath, rewritten);
3388
+ }
3340
3389
  function goTestTarget() {
3341
3390
  return { executor: "@nx-go/nx-go:test" };
3342
3391
  }
@@ -3533,6 +3582,7 @@ function addGoApp(workspaceRoot, name, options = {}) {
3533
3582
  ],
3534
3583
  workspaceRoot
3535
3584
  );
3585
+ setGoModulePath(workspaceRoot, `apps/${name}`);
3536
3586
  addProjectJsonTargets((0, import_node_path10.join)(workspaceRoot, "apps", name, "project.json"), {
3537
3587
  build: goBuildTarget(name),
3538
3588
  test: goTestTarget(),
@@ -3571,6 +3621,7 @@ function addGoFunctionApp(workspaceRoot, name) {
3571
3621
  ],
3572
3622
  workspaceRoot
3573
3623
  );
3624
+ setGoModulePath(workspaceRoot, `apps/${name}`);
3574
3625
  addProjectJsonTargets((0, import_node_path10.join)(workspaceRoot, "apps", name, "project.json"), {
3575
3626
  "build": goBuildTarget(name),
3576
3627
  "test": goTestTarget(),
@@ -3594,15 +3645,16 @@ function addGoLib(workspaceRoot, name) {
3594
3645
  ],
3595
3646
  workspaceRoot
3596
3647
  );
3648
+ setGoModulePath(workspaceRoot, `packages/${name}`);
3597
3649
  addProjectJsonTargets((0, import_node_path10.join)(workspaceRoot, "packages", name, "project.json"), {
3598
3650
  test: goTestTarget(),
3599
3651
  lint: goLintTarget()
3600
3652
  });
3601
3653
  const slice = reshapeGoLibraryScaffold((0, import_node_path10.join)(workspaceRoot, "packages", name), name);
3602
3654
  registerProjectCommands(workspaceRoot, name, { build: false });
3603
- const module2 = goModulePath(workspaceRoot);
3655
+ const module2 = goModulePathFor(workspaceRoot, `packages/${name}`);
3604
3656
  if (module2) {
3605
- logger.step(`Import its starter slice as ${module2}/packages/${name}/${slice}`);
3657
+ logger.step(`Import its starter slice as ${module2}/${slice}`);
3606
3658
  }
3607
3659
  }
3608
3660
  function addGoInternalLib(workspaceRoot, name) {
@@ -3618,15 +3670,16 @@ function addGoInternalLib(workspaceRoot, name) {
3618
3670
  ],
3619
3671
  workspaceRoot
3620
3672
  );
3673
+ setGoModulePath(workspaceRoot, `libs/${name}`);
3621
3674
  addProjectJsonTargets((0, import_node_path10.join)(workspaceRoot, "libs", name, "project.json"), {
3622
3675
  test: goTestTarget(),
3623
3676
  lint: goLintTarget()
3624
3677
  });
3625
3678
  const slice = reshapeGoLibraryScaffold((0, import_node_path10.join)(workspaceRoot, "libs", name), name);
3626
3679
  registerProjectCommands(workspaceRoot, name, { build: false });
3627
- const module2 = goModulePath(workspaceRoot);
3680
+ const module2 = goModulePathFor(workspaceRoot, `libs/${name}`);
3628
3681
  if (module2) {
3629
- logger.step(`Import its starter slice as ${module2}/libs/${name}/${slice}`);
3682
+ logger.step(`Import its starter slice as ${module2}/${slice}`);
3630
3683
  }
3631
3684
  }
3632
3685
 
@@ -6459,6 +6512,23 @@ function checkGoPluginRegistered(workspaceRoot) {
6459
6512
  remedy: "run `mnci upgrade`"
6460
6513
  };
6461
6514
  }
6515
+ function checkGoWorkInSync(workspaceRoot) {
6516
+ const goWorkPath = (0, import_node_path24.join)(workspaceRoot, "go.work");
6517
+ if (!fileExists(goWorkPath)) {
6518
+ return void 0;
6519
+ }
6520
+ const contents = (0, import_node_fs17.existsSync)(goWorkPath) ? (0, import_node_fs17.readFileSync)(goWorkPath, "utf8") : "";
6521
+ const blockEntries = Array.from(contents.matchAll(/(?:^|\n)\s*use\s+\(([^)]*)\)/g), (match) => match[1].split("\n")).flat();
6522
+ const singleEntries = Array.from(contents.matchAll(/(?:^|\n)\s*use\s+(\.\S+)/g), (match) => match[1]);
6523
+ const uses = [...blockEntries, ...singleEntries].map((entry) => entry.trim()).filter(Boolean);
6524
+ const stale = uses.filter((entry) => !(0, import_node_fs17.existsSync)((0, import_node_path24.join)(workspaceRoot, entry, "go.mod")));
6525
+ return {
6526
+ check: "every go.work `use` entry points to a module that still exists",
6527
+ ok: stale.length === 0,
6528
+ detail: `go.work lists ${stale.join(", ")} but there is no go.mod there \u2014 \`go list -m\` fails, which breaks the whole Nx project graph`,
6529
+ remedy: "drop the stale `use` line(s) from go.work (mnci owns the file; a removed Go project should have its entry removed)"
6530
+ };
6531
+ }
6462
6532
  function checkNativeApps(workspaceRoot) {
6463
6533
  if (!hasNativeGoApp(workspaceRoot)) {
6464
6534
  return [];
@@ -6584,6 +6654,7 @@ function collectFindings(workspaceRoot) {
6584
6654
  ...checkTargetFilesExist(workspaceRoot),
6585
6655
  checkSync(workspaceRoot),
6586
6656
  checkGoPluginRegistered(workspaceRoot),
6657
+ checkGoWorkInSync(workspaceRoot),
6587
6658
  ...checkNativeApps(workspaceRoot)
6588
6659
  ].filter((finding) => finding !== void 0);
6589
6660
  }