Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3d3125

Browse files
committed
feat: update-deps action
1 parent 6fc4cbd commit e3d3125

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

actions/unjs/update-deps.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { defineAction } from "codeup";
2+
import { updatePackageJSON } from "../../src/utils/pkg";
3+
4+
export default defineAction({
5+
meta: {
6+
name: "update-deps",
7+
description: "Upgrade dependencies to latest versions",
8+
date: "2025-10-06",
9+
},
10+
async apply({ utils }) {
11+
await utils.runPackageManagerCommand('upgrade')
12+
await updatePackageJSON(async (pkg) => {
13+
if (pkg.devDependencies) {
14+
await Promise.allSettled(Object.keys(pkg.devDependencies).map(async (name) => {
15+
const info = await getRegistryInfo(name);
16+
const latest = info["dist-tags"].latest
17+
pkg.devDependencies![name] = `^${latest}`;
18+
}));
19+
}
20+
if (pkg.packageManager) {
21+
const name = pkg.packageManager.split('@')[0];
22+
const info = await getRegistryInfo(name);
23+
const latest = info["dist-tags"].latest
24+
pkg.packageManager = `${name}@${latest}`;
25+
}
26+
})
27+
const pm = await utils.detectPackageManager();
28+
for (const lockfileName of [pm?.lockFile].flat().filter(Boolean)) {
29+
// await utils.remove(lockfileName)
30+
console.log(`Removing ${lockfileName}`)
31+
}
32+
await utils.runPackageManagerCommand('outdated', { ignoreErrors: true })
33+
},
34+
});
35+
36+
37+
interface RegistryInfo {
38+
name: string;
39+
"dist-tags": Record<string, string>;
40+
versions: Record<string, any>;
41+
}
42+
43+
async function getRegistryInfo(name: string) {
44+
return await fetch(`https://registry.npmjs.org/${name}`).then(res => res.json()) as RegistryInfo;
45+
}

src/utils/pkg.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,41 @@ export async function removeDependency(
9090
}
9191

9292
/**
93-
* Run a `package.json` script using detected package manager
93+
* Detect current package manager
94+
*
95+
* @group package.json
96+
*/
97+
export async function detectPackageManager() {
98+
const context = useContext();
99+
return await nypm.detectPackageManager(context.cwd)
100+
}
101+
102+
/**
103+
* Run a command with the detected package manager
94104
*
95105
* @group package.json
96106
*/
97-
export async function runScript(name: string) {
107+
export async function runPackageManagerCommand(command: string, opts?: { ignoreErrors?: boolean }) {
98108
const context = useContext();
99-
const pkgManager = await nypm.detectPackageManager(context.cwd);
109+
const pkgManager = await detectPackageManager();
100110
try {
101111
const { execa } = await import("execa");
102-
await execa(pkgManager?.name || "npm", ["run", ...name.split(" ")], {
112+
await execa(pkgManager?.name || "npm", command.split(" "), {
103113
cwd: useContext().cwd,
104114
stdio: "inherit",
105115
});
106116
} catch (error) {
107-
context.logger.error(error);
117+
if (!opts?.ignoreErrors) {
118+
context.logger.error(error);
119+
}
108120
}
109121
}
122+
123+
/**
124+
* Run a `package.json` script using detected package manager
125+
*
126+
* @group package.json
127+
*/
128+
export async function runScript(script: string) {
129+
await runPackageManagerCommand(`run ${script}`);
130+
}

0 commit comments

Comments
 (0)