-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·74 lines (69 loc) · 2.1 KB
/
index.js
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
#! /usr/bin/env node
//this is just an opinionated order of package manager lock files :)
const packageManagers = {
npm: {
name: "npm",
lockFile: "package-lock.json",
installCommand:
"npm i next@latest eslint-config-next@latest react@latest react-dom@latest",
},
yarn: {
name: "yarn",
lockFile: "yarn.lock",
installCommand:
"yarn add next@latest eslint-config-next@latest react@latest react-dom@latest",
},
pnpm: {
name: "pnpm",
lockFile: "pnpm-lock.yaml",
installCommand:
"pnpm i next@latest eslint-config-next@latest react@latest react-dom@latest",
},
bun: {
name: "bun",
lockFile: "bun.lock",
installCommand:
"bun add next@latest eslint-config-next@latest react@latest react-dom@latest",
},
};
async function run() {
try {
const currentDir = process.cwd();
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
// Check for package manager lock file with content check
let currentPackageManager;
for (const manager in packageManagers) {
const lockFile = path.join(currentDir, packageManagers[manager].lockFile);
if (fs.existsSync(lockFile)) {
currentPackageManager = manager;
break;
}
}
if (!currentPackageManager) {
throw new Error(
"Could not identify package manager. Supported: npm, yarn, pnpm, bun"
);
}
const installCommand =
packageManagers[currentPackageManager].installCommand;
const output = execSync(installCommand, { stdio: "pipe" }).toString();
console.log(`stdout:\n${output}`);
const nextVersionMatch = output.match(/next@(.*)/);
if (nextVersionMatch) {
console.log(
`Successfully upgraded Next.js to version: ${nextVersionMatch[1]}`
);
} else {
console.warn(
"Next.js version not found in output. Upgrade might be incomplete."
);
}
} catch (error) {
console.error(error.message);
process.exit(1); // Exit with non-zero code to indicate failure
}
}
run();
console.log("Upgraded Next.js to the latest version");