const ncu = require('npm-check-updates');
const path = require('path');
const currentJobrunnerVersion = require('../../package.json').version;
const fs = require('fs-extra');
var timestamp = require('time-stamp');
const chalk = require('chalk');
const warning = chalk.keyword('orange');
const announce = chalk.keyword('green');
const _ = require('lodash');
function Updates(){
let basePackageJson = {
name: "jobrunner-updatecheck",
version: "1.3.5",
description: "Assist with launching and managing Bitrise builds",
dependencies: {
jobrunner: `${currentJobrunnerVersion}`,
}
}
let updateCheckFileName = timestamp('YYYYMMDDss').toString()+'-package.json';
console.log('Executing jobrunner Version:', currentJobrunnerVersion);
/**
* Checks running jobrunner version to see if updates are available, and notify user in log if true;
* @param packageJsonDir Expects directory containing jobrunner's package.json
* usually value would be arg[1]/..
* @param callback
*/
this.checkJobrunner = function(){
return new Promise((resolve,reject)=>{
let packagePath = path.join(process.cwd().toString(),updateCheckFileName).toString();
fs.writeJsonSync(packagePath, basePackageJson)
ncu.run({
// Always specify the path to the package file
packageFile: path.join(process.cwd(),updateCheckFileName),
// Any command-line option can be specified here.
// These are set by default:
silent: false,
jsonUpgraded: true
}).then((upgraded) => {
// Remove temporary package.json, to keep clutter down.
fs.remove(packagePath, err => {
if (err) reject(err);
if (!_.isNil(upgraded['jobrunner'])){
console.log(warning(`Available Jobrunner update to version ${upgraded.jobrunner}`));
console.log(announce('Please update with jobrunner by executing: npm install jobrunner -g'));
process.exit(-1);
} else {
console.log(announce('Jobrunner is up to date!'))
resolve(true);
}
})
});
})
}
}
module.exports = new Updates();