Skip to content

Commit

Permalink
Tackle issues with migration script (rancher#12730)
Browse files Browse the repository at this point in the history
* fix issue with migration script updating resolutions + mark package updates for future adjustment after analysis

* add more code comments

* fix migration script to originate correct builds

* bump creators version

* Add completion log

---------

Co-authored-by: Jordon Leach <[email protected]>
  • Loading branch information
aalves08 and jordojordo authored Dec 3, 2024
1 parent 5fe5004 commit d47451e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 64 deletions.
3 changes: 2 additions & 1 deletion creators/extension/migrate/init
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const { setParams, printLog, printUsage } = require('./utils/content');
const { setParams, printLog, printUsage, printCompletion } = require('./utils/content');
const {
packageUpdates,
nvmUpdates,
Expand Down Expand Up @@ -32,4 +32,5 @@ const params = require('./params');
stylesUpdates(params);

printLog();
printCompletion();
})();
120 changes: 58 additions & 62 deletions creators/extension/migrate/tasks/packageUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,74 +64,66 @@ function packageUpdatesLibraries(file, oldContent) {
const replaceLibraries = [];
const types = ['dependencies', 'devDependencies', 'peerDependencies'];

// [Library name, new version or new library, new library version]
// [Where to apply, Library name, new version or new library, new library version]
const librariesUpdates = [
['@rancher/shell', '^3.0.0'],
['@rancher/components', '^0.3.0-alpha.1'],
['@nuxt/babel-preset-app', removePlaceholder],
['@types/jest', '^29.5.2'],
['@typescript-eslint/eslint-plugin', '~5.4.0'],
['@typescript-eslint/parser', '~5.4.0'],
['@vue/cli-plugin-babel', '~5.0.0'],
['@vue/cli-plugin-e2e-cypress', '~5.0.0'],
['@vue/cli-plugin-eslint', '~5.0.0'],
['@vue/cli-plugin-router', '~5.0.0'],
['@vue/cli-plugin-typescript', '~5.0.0'],
['@vue/cli-plugin-unit-jest', '~5.0.0'],
['@vue/cli-plugin-vuex', '~5.0.0'],
['@vue/cli-service', '~5.0.0'],
['@vue/eslint-config-typescript', '~9.1.0'],
['@vue/vue2-jest', '@vue/vue3-jest', '^27.0.0-alpha.1'],
['@vue/test-utils', '~2.0.0-0'],
['core-js', '3.25.3'],
['cache-loader', '^4.1.0'],
['node-polyfill-webpack-plugin', '^3.0.0'],
['portal-vue', '~3.0.0'],
['require-extension-hooks-babel', '1.0.0'],
['require-extension-hooks-vue', '3.0.0'],
['require-extension-hooks', '0.3.3'],
['sass-loader', '~12.0.0'],
['typescript', '~4.5.5'],
['vue-router', '~4.0.3'],
['vue-virtual-scroll-list', 'vue3-virtual-scroll-list', '0.2.1'],
['vue', '~3.2.13'],
['vuex', '~4.0.0'],
['xterm', '5.2.1'],
[['root', 'dependencies'], '@rancher/shell', '^3.0.0'],
[['root', 'dependencies'], '@rancher/components', '^0.3.0-alpha.1'],
[['root', 'dependencies'], '@nuxt/babel-preset-app', removePlaceholder],
[['root', 'dependencies'], '@typescript-eslint/eslint-plugin', removePlaceholder],
[['root', 'dependencies'], '@typescript-eslint/parser', removePlaceholder],
[['pkg', 'devDependencies'], '@vue/cli-plugin-babel', '~5.0.0'],
[['pkg', 'devDependencies'], '@vue/cli-service', '~5.0.0'],
[['pkg', 'devDependencies'], '@vue/cli-plugin-typescript', '~5.0.0'],
[['root', 'dependencies'], '@vue/test-utils', removePlaceholder],
[['root', 'dependencies'], 'core-js', removePlaceholder],
[['root', 'dependencies'], 'cache-loader', removePlaceholder],
[['root', 'dependencies'], 'node-polyfill-webpack-plugin', removePlaceholder],
[['root', 'dependencies'], 'portal-vue', removePlaceholder],
[['root', 'dependencies'], 'sass-loader', removePlaceholder],
[['root', 'dependencies'], 'typescript', removePlaceholder],
[['root', 'dependencies'], 'vue-router', removePlaceholder],
[['root', 'dependencies'], 'vue', removePlaceholder],
[['root', 'dependencies'], 'vuex', removePlaceholder],
[['root', 'dependencies'], 'xterm', removePlaceholder],
];

types.forEach((type) => {
if (parsedJson[type]) {
librariesUpdates.forEach(([library, newVersion, newLibraryVersion]) => {
if (parsedJson[type][library]) {
const version = semver.coerce(parsedJson[type][library]);

if (newVersion === removePlaceholder) {
// Remove library
replaceLibraries.push([library, [parsedJson[type][library], removePlaceholder]]);
delete parsedJson[type][library];
librariesUpdates.forEach(([[fileType, place], library, newVersion, newLibraryVersion]) => {
const currFileType = parsedJson.scripts && parsedJson.scripts.dev && parsedJson.scripts.build ? 'root' : 'pkg';

if (currFileType === fileType && type === place) {
if (parsedJson[type][library]) {
const version = semver.coerce(parsedJson[type][library]);

if (newVersion === removePlaceholder) {
// Remove library
replaceLibraries.push([library, [parsedJson[type][library], removePlaceholder]]);
delete parsedJson[type][library];
content = JSON.stringify(parsedJson, null, 2);
} else if (newLibraryVersion) {
// Replace with a new library
replaceLibraries.push([library, [parsedJson[type][library], newVersion, newLibraryVersion]]);
content = content.replace(
`"${ library }": "${ parsedJson[type][library] }"`,
`"${ newVersion }": "${ newLibraryVersion }"`
);
parsedJson = JSON.parse(content);
} else if (version && semver.lt(version, semver.coerce(newVersion))) {
// Update library version if outdated
replaceLibraries.push([library, [parsedJson[type][library], newVersion]]);
content = content.replace(
`"${ library }": "${ parsedJson[type][library] }"`,
`"${ library }": "${ newVersion }"`
);
parsedJson = JSON.parse(content);
}
} else if (newLibraryVersion && library === newVersion) {
// Add new library if it doesn't exist
parsedJson[type][library] = newLibraryVersion;
replaceLibraries.push([library, [null, newLibraryVersion]]);
content = JSON.stringify(parsedJson, null, 2);
} else if (newLibraryVersion) {
// Replace with a new library
replaceLibraries.push([library, [parsedJson[type][library], newVersion, newLibraryVersion]]);
content = content.replace(
`"${ library }": "${ parsedJson[type][library] }"`,
`"${ newVersion }": "${ newLibraryVersion }"`
);
parsedJson = JSON.parse(content);
} else if (version && semver.lt(version, semver.coerce(newVersion))) {
// Update library version if outdated
replaceLibraries.push([library, [parsedJson[type][library], newVersion]]);
content = content.replace(
`"${ library }": "${ parsedJson[type][library] }"`,
`"${ library }": "${ newVersion }"`
);
parsedJson = JSON.parse(content);
}
} else if (newLibraryVersion && library === newVersion) {
// Add new library if it doesn't exist
parsedJson[type][library] = newLibraryVersion;
replaceLibraries.push([library, [null, newLibraryVersion]]);
content = JSON.stringify(parsedJson, null, 2);
}
});
}
Expand Down Expand Up @@ -166,18 +158,22 @@ function packageUpdatesResolution(file, oldContent) {
let parsedJson = JSON.parse(content);
const replaceResolution = [];
const resolutions = [
['@vue/cli-service/html-webpack-plugin', '^5.0.0'],
['@vue/cli-service/html-webpack-plugin', removePlaceholder],
['**/webpack', removePlaceholder],
['@types/node', '~20.10.0'],
['@types/lodash', '4.17.5'],
];

if (parsedJson.resolutions) {
resolutions.forEach(([library, newVersion]) => {
if (newVersion === removePlaceholder) {
replaceResolution.push([library, [parsedJson.resolutions[library], removePlaceholder]]);
delete parsedJson.resolutions[library];
content = JSON.stringify(parsedJson, null, 2);
parsedJson = JSON.parse(content);
} else if (!parsedJson.resolutions[library]) {
parsedJson.resolutions[library] = newVersion;
replaceResolution.push([library, [null, newVersion]]);
content = JSON.stringify(parsedJson, null, 2);
parsedJson = JSON.parse(content);
} else {
Expand Down
8 changes: 8 additions & 0 deletions creators/extension/migrate/utils/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ function printLog() {
}
}

function printCompletion() {
if (!isSuggest || !isDry) {
console.log('\nMigration completed.\n');
console.log(`You will need to reinstall the dependencies, run the following command:\n\n\tyarn install\n`);
}
}

function replaceCases(fileType, files, replacementCases, printText, params) {
files.forEach((file) => {
const originalContent = fs.readFileSync(file, 'utf8');
Expand Down Expand Up @@ -161,5 +168,6 @@ module.exports = {
escapeRegExp,
setParams,
printLog,
printCompletion,
replaceCases
};
2 changes: 1 addition & 1 deletion creators/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@rancher/create-extension",
"description": "Rancher UI Extension generator",
"version": "3.0.3",
"version": "3.0.4",
"license": "Apache-2.0",
"author": "SUSE",
"packageManager": "[email protected]",
Expand Down

0 comments on commit d47451e

Please sign in to comment.