Add latest rancher version annotation to extension pkg package.json (#9599)

This commit is contained in:
Jordon Leach 2023-08-28 08:54:53 -04:00 committed by GitHub
parent 54f605b7b7
commit b730a96ff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 1 deletions

View File

@ -2,6 +2,7 @@
const fs = require('fs-extra');
const path = require('path');
const https = require('https');
const targets = {
dev: './node_modules/.bin/nuxt dev',
@ -88,11 +89,68 @@ Object.keys(targets).forEach((target) => {
}
});
// Add annotation for the latest Rancher version by default
function fetchLatestVersion() {
console.log(' Fetching latest Rancher Version');
const options = { headers: { 'User-Agent': 'nodejs' } };
https.get('https://api.github.com/repos/rancher/rancher/releases/latest', options, (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if ( statusCode !== 200 ) {
error = new Error(' Request Failed.\n' +
` Status Code: ${ statusCode }`);
} else if ( !/^application\/json/.test(contentType) ) {
error = new Error(' Invalid content-type.\n' +
` Expected application/json but received ${ contentType }`);
}
if ( error ) {
console.log(error.message);
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const release = JSON.parse(rawData);
if ( release.tag_name ) {
console.log(` Adding rancher-version annotation '>= ${ release.tag_name }' to package.json`);
pkg.rancher = { annotations: { 'catalog.cattle.io/rancher-version': `>= ${ release.tag_name }` } };
writePackageJson();
}
} catch (e) {
console.log(' Error parsing release data', e);
}
});
}).on('error', (e) => {
console.log(' Error fetching latest Rancher Version', e);
});
}
fetchLatestVersion();
writePackageJson();
// Add dependencies
// pkg.dependencies['@rancher/shell'] = '^0.6.2';
// pkg.dependencies['core-js'] = '3.18.3';
fs.writeFileSync(path.join(pkgFolder, 'package.json'), JSON.stringify(pkg, null, 2));
function writePackageJson() {
fs.writeFileSync(path.join(pkgFolder, 'package.json'), JSON.stringify(pkg, null, 2));
}
// Create type folders if needed
if (addTypeFolders) {