Skip to content

Commit

Permalink
Merge pull request #87 from sparksuite/new-version-warning
Browse files Browse the repository at this point in the history
New version warning
  • Loading branch information
WesCossick authored Mar 29, 2019
2 parents 3cd0319 + 23143c4 commit d28f7c5
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.nyc_output
coverage
app-versions/
52 changes: 44 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waterfall-cli",
"version": "0.1.2",
"version": "0.2.0",
"description": "Effortlessly create CLIs powered by Node.js",
"main": "src/index.js",
"files": [
Expand All @@ -27,7 +27,9 @@
"dependencies": {
"cli-table": "^0.3.1",
"colors": "^1.3.3",
"fuse.js": "^3.4.3"
"deepmerge": "^3.2.0",
"fuse.js": "^3.4.3",
"semver": "^6.0.0"
},
"devDependencies": {
"eslint": "^5.15.1",
Expand Down
5 changes: 5 additions & 0 deletions src/default-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ const utils = require('./utils.js');
module.exports = {
app: {
name: null,
packageName: null,
version: null,
},
arguments: utils({}).processArguments(process.argv),
mainFilename: require.main.filename,
newVersionWarning: {
enabled: true,
installedGlobally: true,
},
onStart: null,
packageFilePath: '../package.json',
spacing: {
Expand Down
53 changes: 51 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require('colors');
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const deepmerge = require('deepmerge');
const defaultSettings = require('./default-settings.js');
const ErrorWithoutStack = require('./error-without-stack.js');
const screens = require('./screens.js');
Expand All @@ -19,8 +21,7 @@ process.on('uncaughtException', (error) => {
// The constructor, for use at the entry point
module.exports = function Constructor(customSettings) {
// Merge custom settings into default settings
const settings = Object.assign({}, defaultSettings);
Object.assign(settings, customSettings);
const settings = deepmerge(defaultSettings, customSettings);


// Add spacing before
Expand Down Expand Up @@ -72,6 +73,54 @@ module.exports = function Constructor(customSettings) {
}


// 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);
utils(settings).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);
const bothVersionsAreValid = semver.valid(latestVersion) && semver.valid(currentVersion);


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


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


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

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


// Form execution paths
const executionPaths = [];
let currentPathPrefix = path.dirname(settings.mainFilename);
Expand Down
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = function Constructor(currentSettings) {

// Store information
app.name = app.name || packageInfo.name;
app.packageName = packageInfo.name;
app.version = app.version || packageInfo.version;
} else {
module.exports(settings).verboseLog(`Could not find package.json at: ${pathToPackageFile}`);
Expand Down
2 changes: 2 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('Utils', () => {

assert.deepEqual(utils(settings).retrieveAppInformation(), {
name: 'pizza-ordering',
packageName: 'pizza-ordering',
version: '1.2.3',
});
});
Expand All @@ -80,6 +81,7 @@ describe('Utils', () => {

assert.deepEqual(utils(settings).retrieveAppInformation(), {
name: null,
packageName: null,
version: null,
});
});
Expand Down

0 comments on commit d28f7c5

Please sign in to comment.