Skip to content

Commit 1e5b6ff

Browse files
authored
minor fixes (#66)
* minor fixes * trim the prefix
1 parent 0ed8927 commit 1e5b6ff

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed

src/hooks/patch.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function patchCsproj(file: string, version: SemVer) {
7373
}
7474

7575
async function patchPomXml(file: string, version: SemVer) {
76-
const { def } = semverFormats(version);
76+
const { original } = semverFormats(version);
7777
const contents = await Deno.readTextFile(file);
7878
const document = xml.parse(contents, {
7979
captureSpacesBetweenElements: true,
@@ -94,7 +94,7 @@ async function patchPomXml(file: string, version: SemVer) {
9494
} else if (el.type === "element" && el.name === "version") {
9595
const value = el.elements[0];
9696
if (value.type === "text") {
97-
value.text = def;
97+
value.text = original;
9898
isVersionSet = true;
9999
break;
100100
}
@@ -116,24 +116,24 @@ async function patchPomXml(file: string, version: SemVer) {
116116
}
117117

118118
async function patchPackageJson(file: string, version: SemVer) {
119-
const { def } = semverFormats(version);
119+
const { original } = semverFormats(version);
120120
const contents = await Deno.readTextFile(file);
121-
const edits = JSONC.modify(contents, ["version"], def, {});
121+
const edits = JSONC.modify(contents, ["version"], original, {});
122122
const result = JSONC.applyEdits(contents, edits);
123123
await Deno.writeTextFile(file, result);
124124
}
125125

126126
async function patchPackageLockJson(packageJsonPath: string, version: SemVer) {
127-
const { def } = semverFormats(version);
127+
const { original } = semverFormats(version);
128128
const dir = path.dirname(packageJsonPath);
129129
const packageLockJsonPath = path.resolve(dir, "package-lock.json");
130130
if (await exists(packageLockJsonPath)) {
131131
const contents = await Deno.readTextFile(packageLockJsonPath);
132-
const versionEdits = JSONC.modify(contents, ["version"], def, {});
132+
const versionEdits = JSONC.modify(contents, ["version"], original, {});
133133
const moduleVersionEdits = JSONC.modify(
134134
contents,
135135
["packages", "", "version"],
136-
def,
136+
original,
137137
{},
138138
);
139139
const edits = [...versionEdits, ...moduleVersionEdits];
@@ -143,11 +143,11 @@ async function patchPackageLockJson(packageJsonPath: string, version: SemVer) {
143143
}
144144

145145
async function patchChartYaml(file: string, version: SemVer) {
146-
const { def } = semverFormats(version);
146+
const { original } = semverFormats(version);
147147
const contents = await Deno.readTextFile(file);
148148
const result = contents.replace(
149149
/^version:\s*(.*)$/m,
150-
`version: ${def}`,
150+
`version: ${original}`,
151151
);
152152
await Deno.writeTextFile(file, result);
153153
}

src/util/variant.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
import { format, SemVer } from "../../deps/semver.ts";
22

33
export enum FormatKind {
4-
Default = "def",
4+
Original = "original",
55
Dotnet = "dotnet",
66
Docker = "docker",
77
Major = "major",
8+
MajorMinor = "major_minor",
89
}
910

10-
export function semverFormats(semver: SemVer, prefix = "") {
11+
export function semverFormats(semver: SemVer): Record<FormatKind, string> {
1112
const formattedVersion = format(semver);
1213
const kebabVersion = formattedVersion.replace(/[+]/g, "-");
13-
const cleanedPrefix = prefix.trim();
1414
return {
15-
def: `${cleanedPrefix}${formattedVersion}`,
16-
dotnet: `${cleanedPrefix}${kebabVersion}`,
17-
docker: `${cleanedPrefix}${kebabVersion}`,
18-
major: `${cleanedPrefix}${semver.major}`,
15+
original: formattedVersion,
16+
docker: kebabVersion,
17+
dotnet: kebabVersion,
18+
major: `${semver.major}`,
19+
major_minor: `${semver.major}.${semver.minor}`,
1920
};
2021
}
2122

2223
export function semverFormatByKey(
2324
semver: SemVer,
2425
prefix = "",
25-
variantKey: FormatKind = FormatKind.Default,
26+
variantKey: FormatKind = FormatKind.Original,
2627
) {
27-
return semverFormats(semver, prefix)[variantKey];
28+
const format = semverFormats(semver)[variantKey];
29+
return `${prefix.trim()}${format}`;
2830
}

src/util/version.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Deno.test({
166166
assertSpyCall(appendTextFile, 1, {
167167
args: [
168168
"/test/output",
169-
"sv_major=1\n",
169+
"major=1\n",
170170
{ create: true, append: true },
171171
],
172172
});
@@ -199,20 +199,13 @@ Deno.test({
199199
],
200200
});
201201
assertSpyCall(appendTextFile, 6, {
202-
args: [
203-
"/test/output",
204-
"def=1.2.3-pre.0+1\n",
205-
{ create: true, append: true },
206-
],
207-
});
208-
assertSpyCall(appendTextFile, 7, {
209202
args: [
210203
"/test/output",
211204
"dotnet=1.2.3-pre.0-1\n",
212205
{ create: true, append: true },
213206
],
214207
});
215-
assertSpyCall(appendTextFile, 8, {
208+
assertSpyCall(appendTextFile, 7, {
216209
args: [
217210
"/test/output",
218211
"docker=1.2.3-pre.0-1\n",

src/util/version.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,32 @@ export async function printVersion(
2525
context: IContext,
2626
semver: SemVer,
2727
full = false,
28-
prefix = "",
2928
) {
3029
const formatted = format(semver);
31-
const sv_major = semver.major;
32-
const { minor, patch, prerelease, build } = semver;
30+
const { major, minor, patch, prerelease, build } = semver;
3331
const pre = prerelease.join(".");
3432
const b = build.join(".");
35-
const other = semverFormats(semver, prefix);
33+
const { dotnet, docker } = semverFormats(semver);
3634
await writeGithubOutput(context, {
3735
version: formatted,
38-
sv_major,
36+
major,
3937
minor,
4038
patch,
4139
prerelease: pre,
4240
build: b,
43-
...other,
41+
dotnet,
42+
docker,
4443
});
4544
if (full) {
4645
console.log(JSON.stringify({
4746
version: formatted,
48-
sv_major,
47+
major,
4948
minor,
5049
patch,
5150
prerelease: pre,
5251
build: b,
53-
...other,
52+
dotnet,
53+
docker,
5454
}));
5555
} else {
5656
console.log(formatted);

0 commit comments

Comments
 (0)