Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'update-notifier' for new version notice #89

Open
WesCossick opened this issue Apr 16, 2019 · 1 comment
Open

Use 'update-notifier' for new version notice #89

WesCossick opened this issue Apr 16, 2019 · 1 comment
Labels

Comments

@WesCossick
Copy link
Member

See https://www.npmjs.com/package/update-notifier. This is probably better and easier to maintain than our own method.

@WesCossick
Copy link
Member Author

Here's the old code, which was found in actions/init.ts:

// Handle new version warning
if (settings.newVersionWarning.enabled && settings.app.packageName) {
	// Determine where to store the version
	const pathToLatestVersion = path.join(
		__dirname,
		`../app-versions/${settings.app.packageName.replace(/[^a-zA-Z0-9-.]/g, '=')}`
	);

	// Make the directory
	const appVersionsDirectory = path.dirname(pathToLatestVersion);

	if (!fs.existsSync(appVersionsDirectory)) {
		fs.mkdirSync(appVersionsDirectory);
		await verboseLog(`Created directory: ${appVersionsDirectory}`);
	}

	// Warning message
	if (fs.existsSync(pathToLatestVersion)) {
		// Get values
		const latestVersion = semver.clean(`${fs.readFileSync(pathToLatestVersion)}`);
		const currentVersion = semver.clean(settings.app.version || '');

		// Only continue if we have both
		if (latestVersion && currentVersion) {
			const bothVersionsAreValid = semver.valid(latestVersion) && semver.valid(currentVersion);

			// Verbose output
			await verboseLog(`Previously retrieved latest app version: ${latestVersion}`);
			await verboseLog(`Cleaned-up current app version: ${currentVersion}`);
			await verboseLog(`Both versions are valid: ${bothVersionsAreValid ? 'yes' : 'no'}`);

			// Determine if warning is needed
			if (bothVersionsAreValid && semver.gt(latestVersion, currentVersion)) {
				console.log(
					chalk.yellow(
						`You're using an outdated version of ${
							settings.app.name
						} (${currentVersion}). The latest version is ${chalk.bold(latestVersion)}`
					)
				);
				console.log(
					`${chalk.yellow(
						`  > Upgrade by running: ${chalk.bold(
							`npm install ${settings.newVersionWarning.installedGlobally ? '--global ' : ''}${
								settings.app.packageName
							}@${latestVersion}`
						)}`
					)}\n`
				);
			}
		}
	}

	// Check asynchronously if there's a new published version
	const versionCheck = spawn('npm', ['view', settings.app.packageName, 'version']);

	versionCheck.stdout.on('data', (stdout: string) => {
		fs.writeFile(pathToLatestVersion, semver.clean(`${stdout}`) ?? '', 'utf8', () => {
			// Do nothing
		});
	});
}

@WesCossick WesCossick changed the title Refactor to use 'update-notifier' Use 'update-notifier' for new version notice Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant