-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (135 loc) · 4.44 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
const axios = require("axios");
const Table = require("cli-table3");
const fs = require("fs");
const path = require("path");
const ini = require("ini");
const chalk = require("chalk");
// Get the latest version for each package in package.json
const getLatestVersions = async () => {
try {
// Read the package.json file
const packageJsonPath = path.join(process.cwd(), "package.json");
const packageJsonExists = fs.existsSync(packageJsonPath);
const packageJson = packageJsonExists
? require(packageJsonPath)
: { dependencies: {}, devDependencies: {} };
// Read the .npmrc file at the project level
const npmrcPath = path.join(process.cwd(), ".npmrc");
const npmrcExists = fs.existsSync(npmrcPath);
const npmrc = npmrcExists
? ini.parse(fs.readFileSync(npmrcPath, "utf-8"))
: {};
// Use the registry URL from the .npmrc file if present
const registryUrl =
npmrc.registry?.trim()?.replace(/\/?$/, "/") ||
"https://registry.npmjs.org/-/v1/search?text=";
const headers = {};
if (npmrcExists) {
const url = require("url");
const parsedUrl = new url.URL(registryUrl);
const hostname = parsedUrl.hostname;
const authTokenKey = Object.keys(npmrc).find((key) =>
key.startsWith(`//${hostname}/:_authToken`)
);
const authTokenValue = npmrc[authTokenKey];
if (authTokenValue && authTokenValue.startsWith("Basic ")) {
headers.Authorization = authTokenValue;
} else {
headers.Authorization = `Bearer ${authTokenValue}`;
}
}
const dependencies = packageJson.dependencies || {};
const devDependencies = packageJson.devDependencies || {};
const packages = [
...Object.keys(dependencies),
...Object.keys(devDependencies),
];
const latestVersions = {};
// Safeguard against missing dependencies
if (packages.length === 0) {
console.warn("No dependencies found in package.json");
return latestVersions;
}
// Loop through each package and get the latest version from the registry
for (const pkg of packages) {
try {
const response = await axios.get(`${registryUrl}${pkg}`, headers);
const { objects } = response.data;
if (Array.isArray(objects) && objects.length > 0) {
const {
package: { version },
} = objects[0];
latestVersions[pkg] = version;
} else {
console.error(
chalk.redBright(
`${pkg} package doesn't exist in the registry or has an invalid response format`
)
);
continue;
}
} catch (error) {
console.error(
chalk.redBright(`Error fetching version for ${pkg}: ${error.message}`)
);
continue;
}
}
return latestVersions;
} catch (error) {
console.error(chalk.redBright(`Unexpected error: ${error.message}`));
throw error;
}
};
// Log the latest versions in a table
const logTable = (data) => {
const table = new Table({
head: [
"Package Name",
"Current Version",
"Latest Version",
"Update Available",
],
colWidths: [30, 20, 20, 25],
});
const rows = [];
for (const [pkg, latestVersion] of Object.entries(data)) {
const packageJsonPath = path.join(
process.cwd(),
"node_modules",
pkg,
"package.json"
);
const packageJsonExists = fs.existsSync(packageJsonPath);
const currentVersion = packageJsonExists
? require(packageJsonPath).version
: "";
const isMajorChange =
currentVersion &&
latestVersion.split(".")[0] !== currentVersion.split(".")[0];
const colorFn = isMajorChange ? chalk.redBright : chalk.greenBright;
rows.push([
pkg,
currentVersion,
colorFn(latestVersion),
latestVersion > currentVersion ? chalk.green("✔") : chalk.red("✘"),
]);
}
table.push(...rows);
console.log("Latest versions of dependencies and dev-dependencies:");
console.log(table.toString());
};
const run = async () => {
// Check if the node_modules directory exists
if (!fs.existsSync(path.join(process.cwd(), "node_modules"))) {
console.error(
"Error: Please run the command in the directory with the package.json file and run `npm install` first."
);
return;
}
const latestVersions = await getLatestVersions();
logTable(latestVersions);
};
run();
module.exports = { getLatestVersions, logTable, run };