Skip to content

Commit

Permalink
headlamp-plugin: Make update check asynchronous
Browse files Browse the repository at this point in the history
This change makes the update check for headlamp-plugin asynchronous
to avoid slowing down and blocking the user on start.

Fixes: #1899

Signed-off-by: Evangelos Skopelitis <eskopelitis@microsoft.com>
  • Loading branch information
skoeva committed Jun 20, 2024
1 parent ce248d6 commit b9f62bb
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions plugins/headlamp-plugin/bin/headlamp-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function create(name, link) {
.split('$${name}')
.join(name)
.split('$${headlamp-plugin-version}')
.join(headlampPluginPkg.version)
.join(headlampPluginPkg.version),
);
}

Expand All @@ -84,7 +84,7 @@ function create(name, link) {
});
} catch (e) {
console.error(
`Problem running npm install inside of "${dstFolder}" abs: "${resolve(dstFolder)}"`
`Problem running npm install inside of "${dstFolder}" abs: "${resolve(dstFolder)}"`,
);
return 3;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ function extract(pluginPackagesPath, outputPlugins) {

/**
* Start watching for changes, and build again if there are changes.
* @returns {0} Exit code, where 0 is success.
* @returns {number} - Exit code, where 0 is success.
*/
function start() {
/**
Expand Down Expand Up @@ -271,22 +271,24 @@ function start() {
/**
* Inform if @kinvolk/headlamp-plugin is outdated.
*/
function informIfOutdated() {
console.log('Checking if headlamp-plugin is up to date...');
async function informIfOutdated() {
console.log('Checking if @kinvolk/headlamp-plugin is up to date...');
const outdated = getNpmOutdated();
if ('@kinvolk/headlamp-plugin' in outdated) {
const url = `https://github.com/kinvolk/headlamp/releases`;
console.warn(
' @kinvolk/headlamp-plugin is out of date. Run the following command to upgrade \n' +
` See release notes here: ${url}` +
' npx @kinvolk/headlamp-plugin upgrade'
' npx @kinvolk/headlamp-plugin upgrade',
);
} else {
console.log(' @kinvolk/headlamp-plugin is up to date.');
}
}

informIfOutdated();
informIfOutdated().catch(error => {
console.error('Error checking if @kinvolk/headlamp-plugin is up to date:', error);
});

config.watch = true;
config.mode = 'development';
Expand Down Expand Up @@ -373,7 +375,7 @@ function runScriptOnPackages(packageFolder, scriptName, cmdLine, env) {
'..',
'..',
'..',
nodeModulesBinCmd
nodeModulesBinCmd,
);

if (fs.existsSync(nodeModulesBinCmd)) {
Expand All @@ -385,8 +387,8 @@ function runScriptOnPackages(packageFolder, scriptName, cmdLine, env) {
} else {
console.warn(
`"${scriptCmd}" not found in "${resolve(nodeModulesBinCmd)}" or "${resolve(
upNodeModulesBinCmd
)}" or "${resolve(npxBinCmd)}".`
upNodeModulesBinCmd,
)}" or "${resolve(npxBinCmd)}".`,
);
}

Expand Down Expand Up @@ -432,7 +434,7 @@ function runScriptOnPackages(packageFolder, scriptName, cmdLine, env) {
};
});
const failedErrorFolders = errorFolders.filter(
errFolder => errFolder.error !== runOnPackageReturn.success
errFolder => errFolder.error !== runOnPackageReturn.success,
);

if (failedErrorFolders.length === 0) {
Expand All @@ -453,14 +455,14 @@ function runScriptOnPackages(packageFolder, scriptName, cmdLine, env) {
const folderErr = runOnFolderOfPackages(packageFolder);
if (folderErr.error === runOnPackageReturn.notThere) {
console.error(
`"${resolve(packageFolder)}" does not contain a package or packages. Not ${scriptName}-ing.`
`"${resolve(packageFolder)}" does not contain a package or packages. Not ${scriptName}-ing.`,
);
return 1; // failed
} else if (folderErr.error === runOnPackageReturn.issue) {
console.error(
`Some in "${resolve(packageFolder)}" failed. Failed folders: ${folderErr.failedFolders.join(
', '
)}`
', ',
)}`,
);
return 1; // failed
}
Expand Down Expand Up @@ -681,7 +683,7 @@ function upgrade(packageFolder, skipPackageUpdates, headlampPluginVersion) {
const templateFolder = path.resolve(__dirname, '..', 'template');
const packageJsonPath = path.join('.', 'package.json');
const templatePackageJson = JSON.parse(
fs.readFileSync(path.join(templateFolder, 'package.json'), 'utf8')
fs.readFileSync(path.join(templateFolder, 'package.json'), 'utf8'),
);
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
let configChanged = false;
Expand All @@ -702,8 +704,8 @@ function upgrade(packageFolder, skipPackageUpdates, headlampPluginVersion) {
configChanged = true;
console.log(
`Updated package.json field ${keyName}.${key}: ${JSON.stringify(
packageJson[keyName][key]
)}`
packageJson[keyName][key],
)}`,
);
}
});
Expand Down Expand Up @@ -859,7 +861,7 @@ function upgrade(packageFolder, skipPackageUpdates, headlampPluginVersion) {
for (const folder of packageFolders) {
if (failed) {
console.error(
`Skipping "${folder.name}", because "${failed}" did not upgrade successfully.`
`Skipping "${folder.name}", because "${failed}" did not upgrade successfully.`,
);
continue;
}
Expand Down Expand Up @@ -940,11 +942,11 @@ function storybook(packageFolder) {
stdio: 'inherit',
cwd: packageFolder,
encoding: 'utf8',
}
},
);
} catch (e) {
console.error(
`Problem running storybook dev inside of "${packageFolder}" abs: "${resolve(packageFolder)}"`
`Problem running storybook dev inside of "${packageFolder}" abs: "${resolve(packageFolder)}"`,
);
return 1;
}
Expand Down Expand Up @@ -990,7 +992,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = build(argv.package);
}
},
)
.command('start', 'Watch for changes and build plugin.', {}, () => {
process.exitCode = start();
Expand All @@ -1012,7 +1014,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = create(argv.name, argv.link);
}
},
)
.command(
'extract <pluginPackages> <outputPlugins>',
Expand All @@ -1035,7 +1037,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = extract(argv.pluginPackages, argv.outputPlugins);
}
},
)
.command(
'format [package]',
Expand All @@ -1055,7 +1057,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = format(argv.package, argv.check);
}
},
)
.command(
'lint [package]',
Expand All @@ -1076,7 +1078,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = lint(argv.package, argv.fix);
}
},
)
.command(
'tsc [package]',
Expand All @@ -1092,7 +1094,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = tsc(argv.package);
}
},
)
.command(
'storybook [package]',
Expand All @@ -1106,7 +1108,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = storybook(argv.package);
}
},
)
.command(
'storybook-build [package]',
Expand All @@ -1121,7 +1123,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = storybook_build(argv.package);
}
},
)
.command(
'upgrade [package]',
Expand All @@ -1147,7 +1149,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = upgrade(argv.package, argv.skipPackageUpdates, argv.headlampPluginVersion);
}
},
)
.command(
'test [package]',
Expand All @@ -1161,7 +1163,7 @@ yargs(process.argv.slice(2))
},
argv => {
process.exitCode = test(argv.package);
}
},
)
.demandCommand(1, '')
.strict()
Expand Down

0 comments on commit b9f62bb

Please sign in to comment.