-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate-git-dependency.ts
More file actions
116 lines (100 loc) · 3.35 KB
/
Copy pathupdate-git-dependency.ts
File metadata and controls
116 lines (100 loc) · 3.35 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
104
105
106
107
108
109
110
111
112
113
114
115
116
// Gets the latest commit of a specific GitHub repository and a specific branch
// Then updates the specified dependency in the package.json file to point to that commit
// Used to update the @kompakkt/* dependencies in the package.json file to the latest version
/// <reference types="bun-types" />
import { spawnSync } from 'bun';
import { parseArgs } from 'node:util';
const { values } = parseArgs({
args: Bun.argv,
options: {
dependency: {
type: 'string',
short: 'd',
description: 'The dependency to update',
required: true,
},
owner: {
type: 'string',
short: 'o',
description: 'The owner of the repository',
required: true,
},
repo: {
type: 'string',
short: 'r',
description: 'The name of the repository',
required: true,
},
branch: {
type: 'string',
short: 'b',
description: 'The name of the branch',
required: true,
},
isDevDependency: {
type: 'boolean',
short: 'D',
description: 'Whether the dependency is a dev dependency',
default: false,
},
},
allowPositionals: true,
});
const { dependency, owner, repo, branch } = values;
if (!dependency || !owner || !repo || !branch) {
console.error('Missing required arguments: --dependency, --owner, --repo, --branch');
process.exit(1);
}
// Get current commit of the dependency from package.json
const packageJson = await Bun.file('./package.json').json();
const currentDependency = values.isDevDependency
? packageJson.devDependencies?.[dependency]
: packageJson.dependencies?.[dependency];
if (!currentDependency) {
console.error(`Dependency ${dependency} not found in package.json`);
process.exit(1);
}
if (typeof currentDependency !== 'string' || !currentDependency.startsWith('git+')) {
console.error(`Dependency ${dependency} is not a git dependency`);
process.exit(1);
}
const currentCommit = currentDependency.split('#').at(1);
if (!currentCommit) {
console.error(`Failed to parse current commit for ${dependency} from package.json`);
process.exit(1);
}
const gitProcess = spawnSync({
cmd: ['git', 'ls-remote', `https://github.com/${owner}/${repo}.git`, branch],
});
const result = gitProcess.stdout?.toString().trim();
if (!result) {
console.error(`Failed to get the latest commit for ${owner}/${repo} on branch ${branch}`);
process.exit(1);
}
const [latestCommit] = result.split('\t');
if (latestCommit.slice(0, 7) === currentCommit.slice(0, 7)) {
console.log(
`Dependency ${dependency} is already up to date with commit ${currentCommit} from ${owner}/${repo} on branch ${branch}`,
);
process.exit(0);
}
console.log(
`Updating ${dependency} to latest commit ${latestCommit} from ${owner}/${repo} on branch ${branch}`,
);
const removeDepProcess = spawnSync({
cmd: ['bun', 'remove', dependency],
});
if (removeDepProcess.exitCode !== 0) {
console.error(`Failed to remove ${dependency}: ${removeDepProcess.stderr?.toString()}`);
process.exit(1);
}
const addDepProcess = spawnSync({
cmd: ['bun', 'add', `git+https://github.com/${owner}/${repo}.git#${latestCommit}`],
});
if (addDepProcess.exitCode !== 0) {
console.error(`Failed to add ${dependency}: ${addDepProcess.stderr?.toString()}`);
process.exit(1);
}
console.log(
`Successfully updated ${dependency} to latest commit ${latestCommit} from ${owner}/${repo} on branch ${branch}`,
);