-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathversions.js
More file actions
executable file
·110 lines (90 loc) · 2.72 KB
/
versions.js
File metadata and controls
executable file
·110 lines (90 loc) · 2.72 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
#!/usr/bin/env node
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const configPath = path.join(__dirname, 'versions-config.json');
const toolsConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
const tools = toolsConfig.map(tool => ({
...tool,
versionRegex: new RegExp(tool.regex, 'im')
}));
function extractVersion(output, regex) {
const match = output.match(regex);
return match ? match[1] : 'unknown';
}
function getToolVersion(dockerImage, tool) {
try {
const output = execSync(
`docker run --rm --platform linux/amd64 "${dockerImage}" ${tool.cmd} 2>&1`,
{ encoding: 'utf-8' }
);
return extractVersion(output, tool.versionRegex);
} catch (error) {
const output = error.stdout || '';
return extractVersion(output, tool.versionRegex);
}
}
function generateMarkdown(dockerImage, sectionTitle) {
let markdown = `${sectionTitle}\n\n`;
for (const tool of tools) {
const version = getToolVersion(dockerImage, tool);
markdown += `- [${tool.name}](${tool.url}): \`${version}\`\n`;
}
return markdown;
}
function updateReadme(readmePath, sectionTitle, newContent) {
const readme = fs.readFileSync(readmePath, 'utf-8');
const lines = readme.split('\n');
const result = [];
let shouldSkip = false;
for (const line of lines) {
if (line === sectionTitle) {
result.push(newContent.trimEnd());
shouldSkip = true;
continue;
}
if (shouldSkip && line.match(/^##\s/)) {
shouldSkip = false;
}
if (!shouldSkip) {
result.push(line);
}
}
const newReadme = result.join('\n') + "\n\n";
if (readme === newReadme) {
return false;
}
fs.writeFileSync(readmePath, newReadme);
return true;
}
const args = process.argv.slice(2);
let dockerImage, shouldUpdateReadme = false, readmePath, sectionTitle = '## Included packages';
for (let i = 0; i < args.length; i++) {
if (args[i] === '--update-readme') {
shouldUpdateReadme = true;
readmePath = args[i + 1] || 'README.md';
i++;
} else if (args[i] === '--section') {
sectionTitle = args[i + 1];
i++;
} else if (!dockerImage) {
dockerImage = args[i];
}
}
if (!dockerImage) {
console.error('Usage: node versions.js <docker-image> [--update-readme [readme-path]] [--section "## Section Title"]');
process.exit(1);
}
const markdown = generateMarkdown(dockerImage, sectionTitle);
if (shouldUpdateReadme) {
const hasChanged = updateReadme(readmePath, sectionTitle, markdown);
if (hasChanged) {
console.log(`${readmePath} updated with new package versions`);
process.exit(1);
} else {
console.log(`${readmePath} is already up to date`);
process.exit(0);
}
} else {
console.log(markdown);
}