Skip to content

Commit

Permalink
Create version checker to remove possability of forgetting to update …
Browse files Browse the repository at this point in the history
…postinstall.js with a version change.
  • Loading branch information
ztalbot2000 committed Jan 12, 2021
1 parent 59f34c1 commit cef81d6
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 16 deletions.
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@ function checkForUpdates( )
if ( process.argv.includes( "test/mocha-setup" ) )
return;

const latestVersion = require( "latest-version" );

const { getLatestVersion, isVersionNewerThanPackagedVersion } = require( "./utils/versionChecker" );
const myPkg = require( "./package.json" );

( async( ) =>
{
let lv = await latestVersion( myPkg.name );
let lv = await getLatestVersion( );

if ( lv != myPkg.version )
if ( isVersionNewerThanPackagedVersion( lv ) )
{
console.log( chalk.green( `[UPDATE AVAILABLE] ` ) + `Version ${lv} of ${myPkg.name} is available. Any release notes can be found here: ` + chalk.underline( `${myPkg.changelog}` ) );
}
Expand Down
32 changes: 20 additions & 12 deletions postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
// Fun colour stuff
const chalk = require( "chalk" );

const package = require( "./package.json")
const myPkg = require( "./package.json" );

if (package.version.includes( "3.0.0" ) ||
package.version.includes( "3.0.2" ) ||
package.version.includes( "3.0.3" )
)
const { isUpgrade } = require( "./utils/versionChecker" );

// To use await you must be in an async function, so put it in one.
( async( ) =>
{
console.log( chalk.underline( `HomeBridge-Cmd4 Version 3.0.0+ Notes:\n` ) );
console.log( chalk.green( `* ` ) + `For new users, you will need to follow the README to continue the configuration of HomeBridge-CMD4.\n` );
console.log( chalk.green( `* ` ) + `With this version, it is ` + chalk.red( `*Very* ` ) + `important that you read the Changelogs as there are some new options avaliable..\n` );
console.log(`\n As always, if you like this plugin, don't forget to star it on GitHub.\n`);
console.log(`\n Enjoy`);
console.log(` John Talbot\n\n`);
}
// Wait for the Promise of isUpgrade to complete.
let lv = await isUpgrade( );

if ( lv == true )
{
console.log( chalk.green( `[UPDATE AVAILABLE] ` ) + `Version ${lv} of ${myPkg.name} is available. Any release notes can be found here: ` + chalk.underline( `${myPkg.changelog}` ) );
console.log( chalk.underline( `HomeBridge-Cmd4 Version 3.0.0+ Notes:\n` ) );
console.log( chalk.green( `* ` ) + `For new users, you will need to follow the README to continue the configuration of HomeBridge-CMD4.\n` );
console.log( chalk.green( `* ` ) + `With this version, it is ` + chalk.red( `*Very* ` ) + `important that you read the Changelogs as there are some new options avaliable..\n` );
console.log(`\n As always, if you like this plugin, don't forget to star it on GitHub.\n`);
console.log(`\n Enjoy`);
console.log(` John Talbot\n\n`);
}
})( );

94 changes: 94 additions & 0 deletions test/versionChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';

const { isUpgrade, getLatestVersion, isVersionNewerThanPackagedVersion, getPackagedVersion } = require( "../utils/versionChecker" );

const isJSON = require( "../utils/isJSON" );
const myPkg = require( "../package.json" );

describe( "Quick Testing Setup", ( ) =>
{
it( "isJSON should be a function", ( ) =>
{
assert.isFunction( isJSON, "isJSON is not a function" );
});

it( "myPkg should be a JSON object", ( ) =>
{
assert.isTrue( isJSON( myPkg ), "myPkg is not a JSON object" );
});
});

describe( "Testing versionChecker init", ( ) =>
{
it( "isUpgrade should be a function", ( ) =>
{
assert.isFunction( isUpgrade, "isUpgrade is not a function" );
});

it( "getLatestVersion should be a function", ( ) =>
{
assert.isFunction( getLatestVersion, "getLatestVersion is not a function" );
});

it( "isVersionNewerThanPackagedVersion should be a function", ( ) =>
{
assert.isFunction( isVersionNewerThanPackagedVersion, "isVersionNewerThanPackagedVersion is not a function" );
});

it( "getPackagedVersion should be a function", ( ) =>
{
assert.isFunction( getPackagedVersion, "getPackagedVersion is not a function" );
});
});


describe( "Testing versionChecker functionality", ( ) =>
{
it( "getPackagedVersion should return: " + myPkg.version, ( ) =>
{
let result = getPackagedVersion( );
assert.equal( result, myPkg.version, "getPackagedVersion expected: " + myPkg.version + " found: " + result );
});

it( "getLatestVersion should return: " + myPkg.version, async ( ) =>
{
let result = await getLatestVersion( );
assert.equal( result, myPkg.version, "getLatestVersion expected: " + myPkg.version + " found: " + result );
}).timeout(5000);

it( "isVersionNewerThanPackagedVersion should return a true for a high version", ( ) =>
{
let result = isVersionNewerThanPackagedVersion( "9.0.0" );
assert.isTrue( result, "isVersionNewerThanPackagedVersion expected: true: found: " + result );
});
it( "isVersionNewerThanPackagedVersion should return a false for a lower version", ( ) =>
{
let result = isVersionNewerThanPackagedVersion( "1.0.0" );
assert.isFalse( result, "isVersionNewerThanPackagedVersion expected: false: found: " + result );
});

it( "isVersionNewerThanPackagedVersion should return a false for same version", ( ) =>
{
let result = isVersionNewerThanPackagedVersion( myPkg.version );
assert.isFalse( result, "isVersionNewerThanPackagedVersion expected: false: found: " + result );
});

it( "isUpgrade should return: false/true depending on package.version", async ( ) =>
{
// See if upgrade is needed
let result = await isUpgrade( );

// Get the latest version. This would have already passed above.
let lv = await getLatestVersion( );

// Handle possablility of checking package.json that is not upgraded yet
// NOTE: It is possible that myPkg.version is higher, which may fail as this is not checked
if ( myPkg.version == lv )
{
assert.isFalse( result, "isUpgrade expected: false as: myPkg.version=" + myPkg.version + " latestVersion=: " + lv + " Found: " + result );
} else {
assert.isTrue( result, "isUpgrade expected: true as: myPkg.version=" + myPkg.version + " latestVersion=: " + lv + " Found: " + result );
}
}).timeout(5000);
})

81 changes: 81 additions & 0 deletions utils/versionChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

// These routines are used to get this packages
// version information.
// It not only uses Promises but creates Promises
// and is documented as such.

const latestVersion = require( "latest-version" );

// Retrieve the package information that contains
// things like the current version.
const myPkg = require( "../package.json" );

// Split the version into its sub components.
function splitVersion( version )
{
let parts = version.split( "." );
return { "version": parts[ 0 ], "major": parts[ 1 ], "minor": parts[ 2 ] };
}

// getLatestVersion could just be defined as:
// function getLatestVersion ( )
// However, defining it this way signifies it
// returns a Promise. In this case the Promise
// given to us by latestVersion.
const getLatestVersion = async ( ) =>
{
return latestVersion( myPkg.name );
}

function getPackagedVersion( )
{
return myPkg.version;
}

// Check that there is a possible upgrade out there.
function isVersionNewerThanPackagedVersion( version )
{
// The default return code.
let rc = false;

// Split the version components into their sub components
let oldVersionInfo = splitVersion( myPkg.version );
let newVersionInfo = splitVersion( version );

// Set the return code appropriately
if ( newVersionInfo.version > oldVersionInfo.version )
rc = true;
else if ( newVersionInfo.major > oldVersionInfo.major )
rc = true;
else if ( newVersionInfo.minor > oldVersionInfo.minor )
rc = true;
return rc;
}


// Check that there is a possible upgrade out there.
function isUpgrade( )
{
// Create a new Promise that will be fufilled when we processed the
// information provided to us by the Promise of getLatestVersion.
// You cannot take an asynchronous call and convert it to a synchronous
// call unless you would create a timer and wait forever? for it
// to complete, which defeats the purpose of node.js.
return new Promise( ( resolve ) =>
{
// To use the promise of getLatestVersion, it must be in an async function
// so put it in one.
( async( ) =>
{
// Wait for the Promise of getLatestVersion to complete
let lv = await getLatestVersion( );

resolve( isVersionNewerThanPackagedVersion( lv ) );
}
)( );
});
}

// Export the internal functions we wish to expose.
module.exports = { isUpgrade, getLatestVersion, isVersionNewerThanPackagedVersion, getPackagedVersion };

0 comments on commit cef81d6

Please sign in to comment.