-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.mjs
More file actions
executable file
·103 lines (93 loc) · 3.46 KB
/
version.mjs
File metadata and controls
executable file
·103 lines (93 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
/**
* This runs after `npm version` command updates the version but before changes are commited.
* This will update the `cargo.toml` version to match the new `package.json` verson.
* This will also update the `package.json` optional native dependencies
* to match the same version as the version of this package.
* This maintains the same version between this master package
* and the optional native dependencies.
* At the same time, the `package-lock.json` is also regenerated.
* Note that at this point, the new optional native dependencies have
* not yet been published, so the `--package-lock-only` flag is used
* to prevent `npm` from attempting to download unpublished packages.
*/
import path from 'node:path';
import fs from 'node:fs';
import os from 'node:os';
import url from 'node:url';
import childProcess from 'node:child_process';
import packageJSON from '../package.json' assert { type: 'json' };
const platform = os.platform();
const projectPath = path.dirname(
path.dirname(url.fileURLToPath(import.meta.url)),
);
/* eslint-disable no-console */
async function main() {
const cargoTOMLPath = path.join(projectPath, 'Cargo.toml');
const cargoLockPath = path.join(projectPath, 'Cargo.lock');
console.error('Updating the Cargo.toml version to match new version');
const cargoTOML = await fs.promises.readFile(cargoTOMLPath, 'utf-8');
const cargoTOMLMatch = cargoTOML.match(/version\s*=\s*"(.*)"/);
const cargoTOMLUpdated = cargoTOML.replace(
cargoTOMLMatch[0],
`version = "${packageJSON.version}"`,
);
await fs.promises.writeFile(cargoTOMLPath, cargoTOMLUpdated, 'utf-8');
console.error('Updating the Cargo.lock version to match new version');
childProcess.execFileSync('cargo', ['update', '--package', 'exec'], {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
});
console.error('Staging Cargo.toml and Cargo.lock');
childProcess.execFileSync('git', ['add', cargoTOMLPath, cargoLockPath], {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
});
console.error(
'Updating the package.json with optional native dependencies and package-lock.json',
);
const optionalDepsNative = [];
for (const key in packageJSON.optionalDependencies) {
if (key.startsWith(packageJSON.name)) {
optionalDepsNative.push(`${key}@${packageJSON.version}`);
}
}
if (optionalDepsNative.length > 0) {
const installArgs = [
'install',
'--ignore-scripts',
'--silent',
'--package-lock-only',
'--save-optional',
'--save-exact',
...optionalDepsNative,
];
console.error('Running npm install:');
console.error(['npm', ...installArgs].join(' '));
childProcess.execFileSync('npm', installArgs, {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
});
console.error('Running npm install again to update the package-lock.json:');
const installArgs_ = [
'install',
'--ignore-scripts',
'--silent',
'--package-lock-only',
];
childProcess.execFileSync('npm', installArgs_, {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
});
}
}
/* eslint-enable no-console */
void main();