sparksuite / waterfall-cli

Coming soon... Effortlessly create CLIs powered by Node.js
MIT License
5 stars 3 forks source link

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

Open WesCossick opened 5 years ago

WesCossick commented 5 years ago

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

WesCossick commented 3 years ago

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
        });
    });
}