Skip to content

Commit c9e2720

Browse files
Copilotjeswr
andauthored
chore: move build config from package.json to separate file to fix npm warnings (#1040)
* Initial plan * Move npm config to separate build-config.json file Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com> * Remove unnecessary load-build-config.mjs script Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com> * Add error handling to build config scripts Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com> * Add type safety and validation to build config Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com> * Replace get-build-vars.sh with inline jq commands and use import for build config Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com> * chore: update getBuildConfig --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com>
1 parent eadd8b9 commit c9e2720

File tree

6 files changed

+70
-38
lines changed

6 files changed

+70
-38
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
with:
2929
node-version: 22.x
3030
- run: npm ci
31-
- run: echo "CACHE_KEY=$(cat ./package.json | jq '.config | [.[]] | map(.version, .commit) | @tsv')" >> $GITHUB_ENV
31+
- run: echo "CACHE_KEY=$(cat ./build-config.json | jq '[.[]] | map(.version, .commit) | @tsv')" >> $GITHUB_ENV
3232

3333
- name: Restore cached build
3434
id: cache

.github/workflows/update.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ jobs:
2323
run: |
2424
npm ci
2525
26-
pth=".config.${{ matrix.dependency }}.version"
26+
pth=".${{ matrix.dependency }}.version"
2727
28-
before=$(cat ./package.json | jq "$pth")
28+
before=$(cat ./build-config.json | jq "$pth")
2929
npm run "update:dep:${{ matrix.dependency }}"
30-
current=$(cat ./package.json | jq "$pth")
30+
current=$(cat ./build-config.json | jq "$pth")
3131
3232
readarray -d . -t before <<<"${before:1:-1}"
3333
readarray -d . -t after <<<"${current:1:-1}"

build-config.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"swipl": {
3+
"version": "9.3.33",
4+
"commit": "24cf6b20a91a3f389e22bd8ac915251c34ea30bb",
5+
"name": "V9.3.33"
6+
},
7+
"emsdk": {
8+
"version": "4.0.18",
9+
"commit": "eb5eb4b3b2b792f44ded198bfe615c985da5be2c",
10+
"name": "4.0.18"
11+
},
12+
"zlib": {
13+
"version": "1.3.1"
14+
},
15+
"pcre2": {
16+
"version": "10.47.0",
17+
"commit": "f454e231fe5006dd7ff8f4693fd2b8eb94333429",
18+
"name": "pcre2-10.47"
19+
}
20+
}

package.json

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"dist/**/*.wasm"
4949
],
5050
"scripts": {
51-
"build:wasm-docker:build": "docker build --build-arg SWIPL_VERSION=$npm_package_config_swipl_version --build-arg SWIPL_COMMIT=$npm_package_config_swipl_commit --build-arg EMSDK_VERSION=$npm_package_config_emsdk_version --build-arg ZLIB_VERSION=$npm_package_config_zlib_version --build-arg PCRE2_NAME=$npm_package_config_pcre2_name -t swipl-wasm-image docker",
51+
"build:wasm-docker:build": "docker build --build-arg SWIPL_VERSION=$(cat build-config.json | jq -r '.swipl.version') --build-arg SWIPL_COMMIT=$(cat build-config.json | jq -r '.swipl.commit') --build-arg EMSDK_VERSION=$(cat build-config.json | jq -r '.emsdk.version') --build-arg ZLIB_VERSION=$(cat build-config.json | jq -r '.zlib.version') --build-arg PCRE2_NAME=$(cat build-config.json | jq -r '.pcre2.name') -t swipl-wasm-image docker",
5252
"build:wasm-docker:create": "docker create --name swipl-wasm swipl-wasm-image",
5353
"build:wasm-docker:remove": "docker rm swipl-wasm",
5454
"build:wasm-docker:extract:data": "docker cp swipl-wasm:/swipl-devel/build.wasm/src/swipl-web.data dist/swipl/swipl-web.data",
@@ -77,26 +77,6 @@
7777
"bundle:webpack": "webpack --config webpack.config.js",
7878
"bundle:latest": "ts-node scripts/post-webpack"
7979
},
80-
"config": {
81-
"swipl": {
82-
"version": "9.3.33",
83-
"commit": "24cf6b20a91a3f389e22bd8ac915251c34ea30bb",
84-
"name": "V9.3.33"
85-
},
86-
"emsdk": {
87-
"version": "4.0.18",
88-
"commit": "eb5eb4b3b2b792f44ded198bfe615c985da5be2c",
89-
"name": "4.0.18"
90-
},
91-
"zlib": {
92-
"version": "1.3.1"
93-
},
94-
"pcre2": {
95-
"version": "10.47.0",
96-
"commit": "f454e231fe5006dd7ff8f4693fd2b8eb94333429",
97-
"name": "pcre2-10.47"
98-
}
99-
},
10080
"bin": {
10181
"swipl-generate": "dist/bin/index.js"
10282
},

scripts/get-latest-zlib.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable import/no-extraneous-dependencies, no-console */
2-
import { getPackage, savePackage } from "./util.mts";
2+
import { getBuildConfig, saveBuildConfig } from "./util.mts";
33

44
(async () => {
55
const res = (await fetch('https://api.github.com/repos/madler/zlib/releases/latest'));
66
if (res.status === 200) {
7-
const pkg = getPackage();
7+
const buildConfig = getBuildConfig();
88
const tag = await res.json();
9-
pkg.config.zlib.version = tag.name.slice(5);
10-
savePackage(pkg);
9+
buildConfig.zlib.version = tag.name.slice(5);
10+
saveBuildConfig(buildConfig);
1111
} else {
1212
console.error(await res.text());
1313
process.exit(1);

scripts/util.mts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ import { dirname } from 'path';
77
// Function to mimic __dirname in ES modules
88
const __dirname = dirname(fileURLToPath(import.meta.url));
99

10+
export interface BuildConfig {
11+
swipl: {
12+
version: string;
13+
commit: string;
14+
name: string;
15+
};
16+
emsdk: {
17+
version: string;
18+
commit: string;
19+
name: string;
20+
};
21+
zlib: {
22+
version: string;
23+
};
24+
pcre2: {
25+
version: string;
26+
commit: string;
27+
name: string;
28+
};
29+
}
30+
1031
export function isHigherVersion(v1: string, v2: string) {
1132
const [major1, minor1, patch1] = v1.split('.').map(e => parseInt(e));
1233
const [major2, minor2, patch2] = v2.split('.').map(e => parseInt(e));
@@ -16,12 +37,23 @@ export function isHigherVersion(v1: string, v2: string) {
1637
|| ((major1 === major2) && (minor1 === minor2) && patch1 > patch2);
1738
}
1839

19-
export function getPackage() {
20-
return JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json')).toString())
40+
export function getBuildConfig(): BuildConfig {
41+
const configPath = path.join(__dirname, '..', 'build-config.json');
42+
try {
43+
const data = fs.readFileSync(configPath, 'utf-8');
44+
return JSON.parse(data) as BuildConfig;
45+
} catch (error) {
46+
throw new Error(`Failed to read build-config.json: ${(error as Error).message}`);
47+
}
2148
}
2249

23-
export function savePackage(packageJson: any) {
24-
fs.writeFileSync(path.join(__dirname, '..', 'package.json'), `${JSON.stringify(packageJson, null, 2)}\n`);
50+
export function saveBuildConfig(buildConfig: BuildConfig) {
51+
const configPath = path.join(__dirname, '..', 'build-config.json');
52+
try {
53+
fs.writeFileSync(configPath, `${JSON.stringify(buildConfig, null, 2)}\n`, 'utf-8');
54+
} catch (error) {
55+
throw new Error(`Failed to write build-config.json: ${(error as Error).message}`);
56+
}
2557
}
2658

2759
export type Tag = Awaited<ReturnType<Octokit['repos']['listTags']>>['data'][0];
@@ -62,11 +94,11 @@ export async function updateTag(options: IUpdateTagOptions) {
6294
}
6395
}
6496

65-
const pkg = getPackage();
97+
const buildConfig = getBuildConfig();
6698

67-
// If a higher version exists, update the package.json
68-
if (bestElem && isHigherVersion(bestElem.version, pkg.config[options.entry].version)) {
69-
pkg.config[options.entry] = bestElem;
70-
savePackage(pkg);
99+
// If a higher version exists, update the build-config.json
100+
if (bestElem && isHigherVersion(bestElem.version, buildConfig[options.entry].version)) {
101+
buildConfig[options.entry] = bestElem;
102+
saveBuildConfig(buildConfig);
71103
}
72104
}

0 commit comments

Comments
 (0)