@pubm/plugin-external-version-sync 0.4.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/index.js +111 -0
- package/package.json +36 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
|
|
5
|
+
// src/sync.ts
|
|
6
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
+
|
|
8
|
+
// src/types.ts
|
|
9
|
+
function isJsonTarget(target) {
|
|
10
|
+
return "jsonPath" in target;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/sync.ts
|
|
14
|
+
function setNestedValue(obj, path, value, filePath) {
|
|
15
|
+
const keys = path.split(".");
|
|
16
|
+
let current = obj;
|
|
17
|
+
for (let i = 0;i < keys.length - 1; i++) {
|
|
18
|
+
const next = current[keys[i]];
|
|
19
|
+
if (next == null || typeof next !== "object") {
|
|
20
|
+
throw new Error(`Invalid path "${path}" in ${filePath}: key "${keys[i]}" is not an object`);
|
|
21
|
+
}
|
|
22
|
+
current = next;
|
|
23
|
+
}
|
|
24
|
+
current[keys[keys.length - 1]] = value;
|
|
25
|
+
}
|
|
26
|
+
function getNestedValue(obj, path) {
|
|
27
|
+
const keys = path.split(".");
|
|
28
|
+
let current = obj;
|
|
29
|
+
for (const key of keys) {
|
|
30
|
+
if (current == null || typeof current !== "object")
|
|
31
|
+
return;
|
|
32
|
+
current = current[key];
|
|
33
|
+
}
|
|
34
|
+
return current;
|
|
35
|
+
}
|
|
36
|
+
function syncVersionInFile(filePath, newVersion, target) {
|
|
37
|
+
if (!existsSync(filePath)) {
|
|
38
|
+
throw new Error(`File not found: ${filePath}`);
|
|
39
|
+
}
|
|
40
|
+
if (isJsonTarget(target)) {
|
|
41
|
+
const content2 = readFileSync(filePath, "utf-8");
|
|
42
|
+
let json;
|
|
43
|
+
try {
|
|
44
|
+
json = JSON.parse(content2);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw new Error(`Failed to parse JSON in ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
47
|
+
}
|
|
48
|
+
const currentValue = getNestedValue(json, target.jsonPath);
|
|
49
|
+
if (currentValue === newVersion)
|
|
50
|
+
return false;
|
|
51
|
+
setNestedValue(json, target.jsonPath, newVersion, filePath);
|
|
52
|
+
const indent = content2.match(/^\s+/m)?.[0] ?? " ";
|
|
53
|
+
writeFileSync(filePath, `${JSON.stringify(json, null, indent)}
|
|
54
|
+
`, "utf-8");
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
const content = readFileSync(filePath, "utf-8");
|
|
58
|
+
const pattern = new RegExp(target.pattern.source, target.pattern.flags);
|
|
59
|
+
const updated = content.replace(pattern, (match) => {
|
|
60
|
+
return match.replace(/\d+\.\d+\.\d+(?:-[\w.]+)?/, newVersion);
|
|
61
|
+
});
|
|
62
|
+
if (updated === content)
|
|
63
|
+
return false;
|
|
64
|
+
writeFileSync(filePath, updated, "utf-8");
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/index.ts
|
|
69
|
+
function externalVersionSync(options) {
|
|
70
|
+
return {
|
|
71
|
+
name: "external-version-sync",
|
|
72
|
+
hooks: {
|
|
73
|
+
afterVersion: async (ctx) => {
|
|
74
|
+
const cwd = process.cwd();
|
|
75
|
+
const plan = ctx.runtime.versionPlan;
|
|
76
|
+
let version;
|
|
77
|
+
if (plan) {
|
|
78
|
+
if (plan.mode === "independent") {
|
|
79
|
+
if (options.version) {
|
|
80
|
+
version = options.version(plan.packages);
|
|
81
|
+
} else {
|
|
82
|
+
throw new Error("external-version-sync: 'version' callback is required in independent mode. " + "Provide a version picker, e.g. version: (pkgs) => pkgs.get('@pubm/core') ?? ''");
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
version = plan.version;
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
version = ctx.runtime.version;
|
|
89
|
+
}
|
|
90
|
+
const errors = [];
|
|
91
|
+
for (const target of options.targets) {
|
|
92
|
+
try {
|
|
93
|
+
const filePath = path.isAbsolute(target.file) ? target.file : path.resolve(cwd, target.file);
|
|
94
|
+
syncVersionInFile(filePath, version, target);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
97
|
+
errors.push(`${target.file}: ${message}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (errors.length > 0) {
|
|
101
|
+
throw new Error(`external-version-sync failed for ${errors.length} target(s):
|
|
102
|
+
${errors.join(`
|
|
103
|
+
`)}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
externalVersionSync
|
|
111
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pubm/plugin-external-version-sync",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "pubm plugin to sync versions to external files",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "bun build src/index.ts --outdir dist --target node --format esm && bunx tsc --project tsconfig.build.json",
|
|
19
|
+
"check": "biome check",
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"test": "vitest --run",
|
|
22
|
+
"coverage": "vitest --run --coverage"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@pubm/core": ">=0.3.6"
|
|
26
|
+
},
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/syi0808/pubm.git",
|
|
34
|
+
"directory": "packages/plugins/plugin-external-version-sync"
|
|
35
|
+
}
|
|
36
|
+
}
|