@git.zone/cli 6.16.3 → 6.17.1

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.
@@ -1,6 +1,11 @@
1
1
  import { getCliConfigValue } from "./helpers.smartconfig.js";
2
2
  import { getProcessUserArgv } from "./helpers.climode.js";
3
3
  import { shouldCleanTestArtifactsOnRelease } from "./helpers.tstest.js";
4
+ import {
5
+ detectProjectType,
6
+ projectTypeHasPackageJson,
7
+ type ProjectType,
8
+ } from "./mod_commit/mod.helpers.js";
4
9
  import { parseReleaseVersionFloor } from "./mod_release/helpers.releaseversion.js";
5
10
  import { defaultNpmVerificationWindowMinutes } from "./mod_release/helpers.npmartifact.js";
6
11
 
@@ -193,6 +198,8 @@ export interface IResolvedCommitWorkflow {
193
198
  export interface IResolvedReleaseWorkflow {
194
199
  confirmation: TConfirmationMode;
195
200
  plan: string[];
201
+ /** Version files and dependency installation the project owns. */
202
+ projectType: ProjectType;
196
203
  targets: TReleaseTarget[];
197
204
  runTests: boolean;
198
205
  runBuild: boolean;
@@ -631,20 +638,49 @@ const releaseTargetOrder: TReleaseTarget[] = [
631
638
  "docker",
632
639
  ];
633
640
 
641
+ /**
642
+ * The release build and every release artifact are produced in a disposable
643
+ * checkout of the release commit, so a release that neither builds nor packs
644
+ * anything has nothing to isolate and keeps working only from the release
645
+ * worktree.
646
+ */
647
+ export const releaseNeedsIsolatedCheckout = (
648
+ workflowArg: Pick<IResolvedReleaseWorkflow, "runBuild" | "targets">,
649
+ ): boolean =>
650
+ workflowArg.runBuild ||
651
+ workflowArg.targets.includes("npm") ||
652
+ workflowArg.targets.includes("giteaAssets");
653
+
634
654
  const buildReleasePlan = (options: {
635
655
  mergeRequested: boolean;
636
656
  runTests: boolean;
637
657
  runBuild: boolean;
658
+ projectType: ProjectType;
638
659
  targets: TReleaseTarget[];
639
660
  cleanTestArtifacts: boolean;
640
661
  }): string[] => {
641
662
  const plan: string[] = ["preflight.branch", "preflight.cleanTree"];
663
+ // Only a checkout with a package.json installs from a committed pnpm
664
+ // lockfile, so only such a release proves that install before it creates its
665
+ // commit and tag. A Deno-only project has no pnpm lockfile to prove.
666
+ if (
667
+ releaseNeedsIsolatedCheckout(options) &&
668
+ projectTypeHasPackageJson(options.projectType)
669
+ )
670
+ plan.push("preflight.lockfile");
642
671
  if (options.runTests) plan.push("preflight.test");
643
672
  if (options.mergeRequested) {
644
673
  plan.push("integration.main", "integration.pushMain");
645
674
  }
646
675
  plan.push("core.version", "core.changelog", "core.commit", "core.tag");
647
- if (options.runBuild) plan.push("core.build");
676
+ // tSDocker keys qualification to the project directory, so an active Docker
677
+ // target keeps the release worktree build it reads.
678
+ if (options.runBuild && options.targets.includes("docker"))
679
+ plan.push("core.build");
680
+ if (releaseNeedsIsolatedCheckout(options)) {
681
+ plan.push("core.releaseCheckout");
682
+ if (options.runBuild) plan.push("core.releaseCheckout.build");
683
+ }
648
684
  if (options.targets.includes("npm")) plan.push("core.packNpmArtifact");
649
685
  if (options.targets.includes("giteaAssets"))
650
686
  plan.push("core.freezeGiteaAssets");
@@ -902,6 +938,14 @@ export const resolveReleaseWorkflow = async (
902
938
  const cleanTestArtifacts = await shouldCleanTestArtifactsOnRelease(
903
939
  process.cwd(),
904
940
  );
941
+ // The plan names `preflight.lockfile` when the release runs it, and the
942
+ // release core has to know which version files it owns. The release worktree
943
+ // is clean before anything is created, so the working folder answers both
944
+ // questions the way the release commit does for every tracked manifest; the
945
+ // preflight itself reads the commit. An ignored `package.json` in the working
946
+ // folder of a Deno-only commit is the one case where the plan lists the step
947
+ // and the step then does nothing.
948
+ const projectType = await detectProjectType(process.cwd());
905
949
 
906
950
  return {
907
951
  confirmation,
@@ -909,9 +953,11 @@ export const resolveReleaseWorkflow = async (
909
953
  mergeRequested,
910
954
  runTests,
911
955
  runBuild,
956
+ projectType,
912
957
  targets,
913
958
  cleanTestArtifacts,
914
959
  }),
960
+ projectType,
915
961
  targets,
916
962
  runTests,
917
963
  runBuild,
@@ -58,6 +58,14 @@ export async function detectProjectType(cwdArg = paths.cwd): Promise<ProjectType
58
58
  }
59
59
  }
60
60
 
61
+ /**
62
+ * A pnpm lockfile, `pnpm install` and `pnpm pack` belong to a project's
63
+ * package.json, so every npm-shaped decision asks this one question.
64
+ */
65
+ export const projectTypeHasPackageJson = (
66
+ projectTypeArg: ProjectType,
67
+ ): boolean => projectTypeArg === 'npm' || projectTypeArg === 'both';
68
+
61
69
  /**
62
70
  * Parses a semantic version string and bumps it according to the version type
63
71
  * @param currentVersion Current version string (e.g., "1.2.3")
@@ -1,14 +1,47 @@
1
1
  import { BaseFormatter } from '../classes.baseformatter.js';
2
- import type { IPlannedChange } from '../interfaces.format.js';
2
+ import type { IFormatWarning, IPlannedChange } from '../interfaces.format.js';
3
3
  import * as plugins from '../mod.plugins.js';
4
4
  import * as paths from '../../paths.js';
5
5
  import { logger, logVerbose } from '../../gitzone.logging.js';
6
6
 
7
+ /**
8
+ * `replace` writes the rendered template over the destination. `jsonTopLevel`
9
+ * keeps the top-level keys a project added itself, because an editor
10
+ * configuration is shared between the template and its user.
11
+ */
12
+ type TTemplateMergeStrategy = 'replace' | 'jsonTopLevel';
13
+
14
+ interface ITemplateFile {
15
+ templatePath: string;
16
+ destPath: string;
17
+ merge: TTemplateMergeStrategy;
18
+ }
19
+
20
+ /**
21
+ * The content a file should end up with, plus the reason a requested merge
22
+ * could not run, which makes the resulting whole-file replacement explainable.
23
+ */
24
+ interface ITemplateMergeOutcome {
25
+ content: string;
26
+ replacedReason?: string;
27
+ }
28
+
29
+ interface IJsonObjectParseOutcome {
30
+ object?: Record<string, unknown>;
31
+ reason?: string;
32
+ }
33
+
7
34
  export class TemplatesFormatter extends BaseFormatter {
35
+ private replacementWarnings: IFormatWarning[] = [];
36
+
8
37
  get name(): string {
9
38
  return 'templates';
10
39
  }
11
40
 
41
+ async validate(): Promise<IFormatWarning[]> {
42
+ return this.replacementWarnings;
43
+ }
44
+
12
45
  /**
13
46
  * Render a template directory through smartscaf and return a map of path → content.
14
47
  */
@@ -39,11 +72,20 @@ export class TemplatesFormatter extends BaseFormatter {
39
72
  const changes: IPlannedChange[] = [];
40
73
  const project = this.project;
41
74
  const projectType = project.gitzoneConfig?.data?.projectType;
75
+ this.replacementWarnings = [];
42
76
 
43
77
  // VSCode template - for all projects
44
78
  const vscodeChanges = await this.analyzeTemplate('vscode', [
45
- { templatePath: '.vscode/settings.json', destPath: '.vscode/settings.json' },
46
- { templatePath: '.vscode/launch.json', destPath: '.vscode/launch.json' },
79
+ {
80
+ templatePath: '.vscode/settings.json',
81
+ destPath: '.vscode/settings.json',
82
+ merge: 'jsonTopLevel',
83
+ },
84
+ {
85
+ templatePath: '.vscode/launch.json',
86
+ destPath: '.vscode/launch.json',
87
+ merge: 'jsonTopLevel',
88
+ },
47
89
  ]);
48
90
  changes.push(...vscodeChanges);
49
91
 
@@ -55,8 +97,8 @@ export class TemplatesFormatter extends BaseFormatter {
55
97
  || project.gitzoneConfig?.data?.npmciOptions?.npmAccessLevel;
56
98
  const ciTemplate = accessLevel === 'public' ? 'ci_default' : 'ci_default_private';
57
99
  const ciChanges = await this.analyzeTemplate(ciTemplate, [
58
- { templatePath: '.gitea/workflows/default_nottags.yaml', destPath: '.gitea/workflows/default_nottags.yaml' },
59
- { templatePath: '.gitea/workflows/default_tags.yaml', destPath: '.gitea/workflows/default_tags.yaml' },
100
+ { templatePath: '.gitea/workflows/default_nottags.yaml', destPath: '.gitea/workflows/default_nottags.yaml', merge: 'replace' },
101
+ { templatePath: '.gitea/workflows/default_tags.yaml', destPath: '.gitea/workflows/default_tags.yaml', merge: 'replace' },
60
102
  ]);
61
103
  changes.push(...ciChanges);
62
104
  break;
@@ -64,20 +106,20 @@ export class TemplatesFormatter extends BaseFormatter {
64
106
  case 'service':
65
107
  case 'website':
66
108
  const dockerCiChanges = await this.analyzeTemplate('ci_docker', [
67
- { templatePath: '.gitea/workflows/docker_nottags.yaml', destPath: '.gitea/workflows/docker_nottags.yaml' },
68
- { templatePath: '.gitea/workflows/docker_tags.yaml', destPath: '.gitea/workflows/docker_tags.yaml' },
109
+ { templatePath: '.gitea/workflows/docker_nottags.yaml', destPath: '.gitea/workflows/docker_nottags.yaml', merge: 'replace' },
110
+ { templatePath: '.gitea/workflows/docker_tags.yaml', destPath: '.gitea/workflows/docker_tags.yaml', merge: 'replace' },
69
111
  ]);
70
112
  changes.push(...dockerCiChanges);
71
113
 
72
114
  const dockerfileChanges = await this.analyzeTemplate('dockerfile_service', [
73
- { templatePath: 'Dockerfile', destPath: 'Dockerfile' },
74
- { templatePath: 'dockerignore', destPath: '.dockerignore' },
115
+ { templatePath: 'Dockerfile', destPath: 'Dockerfile', merge: 'replace' },
116
+ { templatePath: 'dockerignore', destPath: '.dockerignore', merge: 'replace' },
75
117
  ]);
76
118
  changes.push(...dockerfileChanges);
77
119
 
78
120
  const cliChanges = await this.analyzeTemplate('cli', [
79
- { templatePath: 'cli.js', destPath: 'cli.js' },
80
- { templatePath: 'cli.ts.js', destPath: 'cli.ts.js' },
121
+ { templatePath: 'cli.js', destPath: 'cli.js', merge: 'replace' },
122
+ { templatePath: 'cli.ts.js', destPath: 'cli.ts.js', merge: 'replace' },
81
123
  ]);
82
124
  changes.push(...cliChanges);
83
125
  break;
@@ -86,13 +128,13 @@ export class TemplatesFormatter extends BaseFormatter {
86
128
  // Update templates based on projectType
87
129
  if (projectType === 'website') {
88
130
  const websiteChanges = await this.analyzeTemplate('website_update', [
89
- { templatePath: 'html/index.html', destPath: 'html/index.html' },
131
+ { templatePath: 'html/index.html', destPath: 'html/index.html', merge: 'replace' },
90
132
  ]);
91
133
  changes.push(...websiteChanges);
92
134
  } else if (projectType === 'wcc') {
93
135
  const wccChanges = await this.analyzeTemplate('wcc_update', [
94
- { templatePath: 'html/index.html', destPath: 'html/index.html' },
95
- { templatePath: 'html/index.ts', destPath: 'html/index.ts' },
136
+ { templatePath: 'html/index.html', destPath: 'html/index.html', merge: 'replace' },
137
+ { templatePath: 'html/index.ts', destPath: 'html/index.ts', merge: 'replace' },
96
138
  ]);
97
139
  changes.push(...wccChanges);
98
140
  }
@@ -100,9 +142,78 @@ export class TemplatesFormatter extends BaseFormatter {
100
142
  return changes;
101
143
  }
102
144
 
145
+ /**
146
+ * Parse a JSON object a merge needs. A file that is not a JSON object cannot
147
+ * be merged into, and guessing its shape would lose data: the caller is told
148
+ * why and falls back to replacing it.
149
+ */
150
+ private parseJsonObjectForMerge(
151
+ contentArg: string,
152
+ sourceDescriptionArg: string,
153
+ ): IJsonObjectParseOutcome {
154
+ let parsed: unknown;
155
+ try {
156
+ parsed = JSON.parse(contentArg);
157
+ } catch (error) {
158
+ const errorMessage = error instanceof Error ? error.message : String(error);
159
+ return {
160
+ reason: `${sourceDescriptionArg} is not valid JSON (${errorMessage})`,
161
+ };
162
+ }
163
+ if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
164
+ return { reason: `${sourceDescriptionArg} is not a JSON object` };
165
+ }
166
+ return { object: parsed as Record<string, unknown> };
167
+ }
168
+
169
+ /**
170
+ * Merge the rendered template into the destination's own top-level keys. The
171
+ * template owns every key it declares; a key only the project declares — an
172
+ * editor setting the template never knew about — survives the update.
173
+ * A template-owned key is taken as a whole, so an array the template declares
174
+ * replaces the project's array of the same name.
175
+ */
176
+ private mergeJsonTopLevel(
177
+ currentContentArg: string,
178
+ renderedContentArg: string,
179
+ ): ITemplateMergeOutcome {
180
+ const rendered = this.parseJsonObjectForMerge(
181
+ renderedContentArg,
182
+ 'the rendered template',
183
+ );
184
+ const current = this.parseJsonObjectForMerge(
185
+ currentContentArg,
186
+ 'the current file',
187
+ );
188
+ const renderedObject = rendered.object;
189
+ const currentObject = current.object;
190
+ if (!renderedObject || !currentObject) {
191
+ return {
192
+ content: renderedContentArg,
193
+ replacedReason: rendered.reason ?? current.reason,
194
+ };
195
+ }
196
+
197
+ // Nothing of the project's own is at stake, so the template's own text —
198
+ // its indentation and inline arrays included — stays the settled state.
199
+ const foreignKeys = Object.keys(currentObject).filter(
200
+ (keyArg) => !(keyArg in renderedObject),
201
+ );
202
+ if (foreignKeys.length === 0) {
203
+ return { content: renderedContentArg };
204
+ }
205
+
206
+ const merged: Record<string, unknown> = {
207
+ ...currentObject,
208
+ ...renderedObject,
209
+ };
210
+ const trailingNewline = renderedContentArg.endsWith('\n') ? '\n' : '';
211
+ return { content: `${JSON.stringify(merged, null, 2)}${trailingNewline}` };
212
+ }
213
+
103
214
  private async analyzeTemplate(
104
215
  templateName: string,
105
- files: Array<{ templatePath: string; destPath: string }>,
216
+ files: ITemplateFile[],
106
217
  ): Promise<IPlannedChange[]> {
107
218
  const changes: IPlannedChange[] = [];
108
219
  const templateDir = plugins.path.join(paths.templatesDir, templateName);
@@ -141,13 +252,32 @@ export class TemplatesFormatter extends BaseFormatter {
141
252
  .read()) as string;
142
253
  }
143
254
 
144
- if (processedContent !== currentContent) {
255
+ const outcome: ITemplateMergeOutcome =
256
+ destExists && file.merge === 'jsonTopLevel'
257
+ ? this.mergeJsonTopLevel(currentContent, processedContent)
258
+ : { content: processedContent };
259
+
260
+ if (outcome.content !== currentContent) {
261
+ // A replacement the project did not ask for must state its reason
262
+ // where the decision is made: in the plan the user confirms.
263
+ if (outcome.replacedReason) {
264
+ const replacementMessage = `${file.destPath} is replaced by the ${templateName} template instead of merged, because ${outcome.replacedReason}. Top-level keys only this project declares are lost.`;
265
+ this.replacementWarnings.push({
266
+ level: 'warning',
267
+ message: replacementMessage,
268
+ module: this.name,
269
+ });
270
+ logVerbose(replacementMessage);
271
+ }
272
+
145
273
  changes.push({
146
274
  type: destExists ? 'modify' : 'create',
147
275
  path: file.destPath,
148
276
  module: this.name,
149
- description: `Apply template ${templateName}/${file.templatePath}`,
150
- content: processedContent,
277
+ description: outcome.replacedReason
278
+ ? `Replace ${file.destPath}: ${outcome.replacedReason}`
279
+ : `Apply template ${templateName}/${file.templatePath}`,
280
+ content: outcome.content,
151
281
  });
152
282
  }
153
283
  }