-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathnpm-version-checker.js
More file actions
106 lines (94 loc) · 2.93 KB
/
npm-version-checker.js
File metadata and controls
106 lines (94 loc) · 2.93 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
const { execSync } = require('child_process');
const { readFileSync } = require('fs');
const path = require('path');
const https = require('https');
const fs = require('fs');
function getAllPackagePaths() {
const baseDirs = ['packages', 'wallets'];
const packagePaths = [];
baseDirs.forEach((dir) => {
const fullDir = path.resolve(__dirname, dir);
if (fs.existsSync(fullDir)) {
fs.readdirSync(fullDir).forEach((subdir) => {
const pkgJsonPath = path.join(fullDir, subdir, 'package.json');
if (fs.existsSync(pkgJsonPath)) {
packagePaths.push(pkgJsonPath);
}
});
}
});
return packagePaths;
}
function getPackageJson(packagePath) {
const file = readFileSync(path.resolve(packagePath), 'utf8');
return JSON.parse(file);
}
function getGitTags() {
try {
const tags = execSync('git tag', { encoding: 'utf8' })
.split('\n')
.map((tag) => tag.trim())
.filter((tag) => tag);
return tags;
} catch (e) {
console.error('Failed to get git tags:', e.message);
process.exit(1);
}
}
function fetchNpmVersion(packageName) {
return new Promise((resolve, reject) => {
const url = `https://registry.npmjs.org/${packageName}`;
https
.get(url, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
try {
const json = JSON.parse(data);
resolve(json['dist-tags'].latest); // 获取最新发布的版本
} catch (err) {
reject(`Failed to parse npm data: ${err.message}`);
}
});
})
.on('error', (err) => {
reject(`Request failed: ${err.message}`);
});
});
}
async function main() {
const packagePaths = getAllPackagePaths();
for (const packagePath of packagePaths) {
const pkg = getPackageJson(packagePath);
console.log({
name: pkg.name,
version: pkg.version,
npmVersion: await fetchNpmVersion(pkg.name),
});
}
// for (const packagePath of packagePaths) {
// const pkg = getPackageJson(packagePath);
// const packageName = pkg.name;
// const npmVersion = await fetchNpmVersion(packageName);
// const expectedTag = `${npmVersion}`;
// const gitTags = getGitTags();
// // console.log(`📦 npm latest version: ${npmVersion}`);
// // console.log(`🔖 Checking for Git tag: ${expectedTag}`);
// // console.log(`${gitTags} is ${expectedTag}`);
// // if (gitTags.includes(expectedTag)) {
// // console.log(
// // `✅ npm version ${npmVersion} has a matching Git tag "${expectedTag}"`
// // );
// // process.exit(0);
// // } else {
// // console.error(
// // `❌ npm version ${npmVersion} has NO matching Git tag "${expectedTag}"`
// // );
// // process.exit(1);
// // }
// }
}
main().catch((err) => {
console.error(err);
process.exit(1);
});