Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
297e65ebe5 | |||
a5c019419b | |||
815694dd8b | |||
083786795e | |||
284f4753e0 | |||
335e9160a6 | |||
94c22b65e5 | |||
f1d07f968d | |||
ac53d801a6 | |||
a75bb09d3f | |||
01f70cf1a4 | |||
f9fccd2dff |
5428
package-lock.json
generated
5428
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartupdate",
|
"name": "@pushrocks/smartupdate",
|
||||||
"version": "1.0.23",
|
"version": "1.0.29",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "update your tools in a smart way",
|
"description": "update your tools in a smart way",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -12,22 +12,21 @@
|
|||||||
"build": "(tsbuild)"
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.17",
|
"@gitzone/tsbuild": "^2.1.24",
|
||||||
"@gitzone/tsrun": "^1.2.8",
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.33",
|
||||||
"@pushrocks/tapbundle": "^3.0.13",
|
"@pushrocks/tapbundle": "^3.2.1",
|
||||||
"@types/node": "^12.7.4",
|
"@types/node": "^14.0.14",
|
||||||
"tslint": "^5.19.0",
|
"tslint": "^6.1.2",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/consolecolor": "^2.0.1",
|
"@pushrocks/consolecolor": "^2.0.1",
|
||||||
"@pushrocks/npmextra": "^3.0.5",
|
"@pushrocks/npmextra": "^3.0.7",
|
||||||
"@pushrocks/smartlog": "^2.0.19",
|
"@pushrocks/smartnpm": "^1.0.17",
|
||||||
"@pushrocks/smartnpm": "^1.0.10",
|
"@pushrocks/smartopen": "^1.0.22",
|
||||||
"@pushrocks/smartopen": "^1.0.21",
|
"@pushrocks/smarttime": "^3.0.18",
|
||||||
"@pushrocks/smarttime": "^3.0.12",
|
"@pushrocks/smartversion": "^2.0.4"
|
||||||
"@pushrocks/smartversion": "^2.0.0"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/*",
|
"ts/*",
|
||||||
|
12
test/test.ts
12
test/test.ts
@ -1,12 +1,14 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartupdate from '../ts/index';
|
import * as smartupdate from '../ts/index';
|
||||||
|
|
||||||
|
let testSmartUpdate: smartupdate.SmartUpdate;
|
||||||
|
|
||||||
|
tap.test('should create an instance of SmartUpdate', async () => {
|
||||||
|
testSmartUpdate = new smartupdate.SmartUpdate();
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('should check for a npm module', async () => {
|
tap.test('should check for a npm module', async () => {
|
||||||
await smartupdate.standardHandler.check(
|
await testSmartUpdate.check('lodash', '1.0.5');
|
||||||
'lodash',
|
|
||||||
'1.0.5',
|
|
||||||
'http://gitzone.gitlab.io/npmts/changelog.html'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
106
ts/index.ts
106
ts/index.ts
@ -1,105 +1 @@
|
|||||||
import * as plugins from './smartupdate.plugins';
|
export { SmartUpdate } from './smartupdate.classes.smartupdate';
|
||||||
|
|
||||||
import { TimeStamp } from '@pushrocks/smarttime';
|
|
||||||
|
|
||||||
interface ICacheStatus {
|
|
||||||
lastCheck: number;
|
|
||||||
latestVersion: string;
|
|
||||||
performedUpgrade: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
import { KeyValueStore } from '@pushrocks/npmextra';
|
|
||||||
|
|
||||||
export class SmartUpdate {
|
|
||||||
kvStore = new plugins.npmextra.KeyValueStore('custom', 'global_smartupdate');
|
|
||||||
|
|
||||||
async check(npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
|
|
||||||
// the newData to write
|
|
||||||
let timeStamp = new TimeStamp();
|
|
||||||
let newData = {
|
|
||||||
lastCheck: timeStamp.milliSeconds,
|
|
||||||
latestVersion: 'x.x.x',
|
|
||||||
performedUpgrade: false
|
|
||||||
};
|
|
||||||
|
|
||||||
// the comparison data from the keyValue store
|
|
||||||
let result: ICacheStatus = await this.kvStore.readKey(npmnameArg);
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
let lastCheckTimeStamp = TimeStamp.fromMilliSeconds(result.lastCheck);
|
|
||||||
let tresholdTime = plugins.smarttime.getMilliSecondsFromUnits({ hours: 1 });
|
|
||||||
if (!lastCheckTimeStamp.isOlderThan(timeStamp, tresholdTime)) {
|
|
||||||
newData.lastCheck = lastCheckTimeStamp.milliSeconds;
|
|
||||||
let nextCheckInMinutes =
|
|
||||||
(tresholdTime - (timeStamp.milliSeconds - lastCheckTimeStamp.milliSeconds)) / 60000;
|
|
||||||
console.log(
|
|
||||||
`next update check in less than ${Math.floor(nextCheckInMinutes) + 1} minute(s): ` +
|
|
||||||
`${plugins.consolecolor.coloredString(
|
|
||||||
`${npmnameArg} has already been checked within the last hour.`,
|
|
||||||
'pink'
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let npmPackage = await this.getNpmPackageFromRegistry(npmnameArg);
|
|
||||||
if (!npmPackage) {
|
|
||||||
plugins.smartlog.defaultLogger.log('warn', 'failed to retrieve package information...');
|
|
||||||
plugins.smartlog.defaultLogger.log('info', 'npms.io might be down');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
newData.latestVersion = npmPackage.version;
|
|
||||||
let upgradeBool = await this.checkIfUpgrade(npmPackage, compareVersion, changelogUrlArg);
|
|
||||||
if (upgradeBool) {
|
|
||||||
// TODO:
|
|
||||||
}
|
|
||||||
this.kvStore.writeKey(npmnameArg, newData);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async getNpmPackageFromRegistry(npmnameArg): Promise<plugins.smartnpm.NpmPackage> {
|
|
||||||
console.log(
|
|
||||||
`smartupdate: checking for newer version of ${plugins.consolecolor.coloredString(
|
|
||||||
npmnameArg,
|
|
||||||
'pink'
|
|
||||||
)}...`
|
|
||||||
);
|
|
||||||
let npmRegistry = new plugins.smartnpm.NpmRegistry();
|
|
||||||
let npmPackage: plugins.smartnpm.NpmPackage;
|
|
||||||
npmPackage = (await npmRegistry.search({ name: npmnameArg, boostExact: true }))[0];
|
|
||||||
return npmPackage;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async checkIfUpgrade(
|
|
||||||
npmPackage: plugins.smartnpm.NpmPackage,
|
|
||||||
localVersionStringArg: string,
|
|
||||||
changelogUrlArg?: string
|
|
||||||
) {
|
|
||||||
// create Version objects
|
|
||||||
let versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version);
|
|
||||||
let versionLocal = new plugins.smartversion.SmartVersion(localVersionStringArg);
|
|
||||||
if (!versionNpm.greaterThan(versionLocal)) {
|
|
||||||
console.log(
|
|
||||||
`smartupdate: You are running the latest version of ${plugins.consolecolor.coloredString(
|
|
||||||
npmPackage.name,
|
|
||||||
'pink'
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
plugins.smartlog.defaultLogger.log(
|
|
||||||
'warn',
|
|
||||||
`There is a newer version of ${npmPackage.name} available on npm.`
|
|
||||||
);
|
|
||||||
plugins.smartlog.defaultLogger.log(
|
|
||||||
'warn',
|
|
||||||
`Your version: ${versionLocal.versionString} | version on npm: ${versionNpm.versionString}`
|
|
||||||
);
|
|
||||||
if (!process.env.CI && changelogUrlArg) {
|
|
||||||
console.log('trying to open changelog...');
|
|
||||||
plugins.smartopen.openUrl(changelogUrlArg);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export let standardHandler = new SmartUpdate();
|
|
109
ts/smartupdate.classes.smartupdate.ts
Normal file
109
ts/smartupdate.classes.smartupdate.ts
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
import * as plugins from './smartupdate.plugins';
|
||||||
|
|
||||||
|
import { TimeStamp } from '@pushrocks/smarttime';
|
||||||
|
|
||||||
|
interface ICacheStatus {
|
||||||
|
lastCheck: number;
|
||||||
|
latestVersion: string;
|
||||||
|
performedUpgrade: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
import { KeyValueStore } from '@pushrocks/npmextra';
|
||||||
|
|
||||||
|
export class SmartUpdate {
|
||||||
|
public npmRegistry: plugins.smartnpm.NpmRegistry;
|
||||||
|
public kvStore = new plugins.npmextra.KeyValueStore('custom', 'global_smartupdate');
|
||||||
|
|
||||||
|
constructor(optionsArg: plugins.smartnpm.INpmRegistryConstructorOptions = {}) {
|
||||||
|
this.npmRegistry = new plugins.smartnpm.NpmRegistry(optionsArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async checkForCli(npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
|
||||||
|
// the newData to write
|
||||||
|
const timeStamp = new TimeStamp();
|
||||||
|
const newCacheData: ICacheStatus = {
|
||||||
|
lastCheck: timeStamp.milliSeconds,
|
||||||
|
latestVersion: 'x.x.x',
|
||||||
|
performedUpgrade: false
|
||||||
|
};
|
||||||
|
|
||||||
|
// the comparison data from the keyValue store
|
||||||
|
const retrievedCacheData: ICacheStatus = await this.kvStore.readKey(npmnameArg);
|
||||||
|
|
||||||
|
if (retrievedCacheData) {
|
||||||
|
const lastCheckTimeStamp = TimeStamp.fromMilliSeconds(retrievedCacheData.lastCheck);
|
||||||
|
const tresholdTime = plugins.smarttime.getMilliSecondsFromUnits({ hours: 1 });
|
||||||
|
if (!lastCheckTimeStamp.isOlderThan(timeStamp, tresholdTime)) {
|
||||||
|
newCacheData.lastCheck = lastCheckTimeStamp.milliSeconds;
|
||||||
|
const nextCheckInMinutes =
|
||||||
|
(tresholdTime - (timeStamp.milliSeconds - lastCheckTimeStamp.milliSeconds)) / 60000;
|
||||||
|
console.log(
|
||||||
|
`next update check in less than ${Math.floor(nextCheckInMinutes) + 1} minute(s): ` +
|
||||||
|
`${plugins.consolecolor.coloredString(
|
||||||
|
`${npmnameArg} has already been checked within the last hour.`,
|
||||||
|
'pink'
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
return false; // don't upgrade if checked within reasonable time
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const upgradeBool = await this.check(npmnameArg, compareVersion, changelogUrlArg);
|
||||||
|
if (upgradeBool) {
|
||||||
|
const npmPackage = await this.npmRegistry.getPackageInfo(npmnameArg);
|
||||||
|
newCacheData.latestVersion = npmPackage.version;
|
||||||
|
this.kvStore.writeKey(npmnameArg, newCacheData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return upgradeBool;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getNpmPackageFromRegistry(npmnameArg): Promise<plugins.smartnpm.NpmPackage> {
|
||||||
|
console.log(
|
||||||
|
`smartupdate: checking for newer version of ${plugins.consolecolor.coloredString(
|
||||||
|
npmnameArg,
|
||||||
|
'pink'
|
||||||
|
)}...`
|
||||||
|
);
|
||||||
|
const npmPackage = this.npmRegistry.getPackageInfo(npmnameArg);
|
||||||
|
return npmPackage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async check(
|
||||||
|
npmPackageName: string,
|
||||||
|
localVersionStringArg: string,
|
||||||
|
changelogUrlArg?: string
|
||||||
|
) {
|
||||||
|
const npmPackage = await this.getNpmPackageFromRegistry(npmPackageName);
|
||||||
|
if (!npmPackage) {
|
||||||
|
console.log('warn: failed to retrieve package information...');
|
||||||
|
console.log('info: is the registry down?');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create Version objects
|
||||||
|
const versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version);
|
||||||
|
const versionLocal = new plugins.smartversion.SmartVersion(localVersionStringArg);
|
||||||
|
if (!versionNpm.greaterThan(versionLocal)) {
|
||||||
|
console.log(
|
||||||
|
`smartupdate: You are running the latest version of ${plugins.consolecolor.coloredString(
|
||||||
|
npmPackage.name,
|
||||||
|
'pink'
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
`warn: There is a newer version of ${npmPackage.name} available on npm.`
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
`warn: Your version: ${versionLocal.versionString} | version on npm: ${versionNpm.versionString}`
|
||||||
|
);
|
||||||
|
if (!process.env.CI && changelogUrlArg) {
|
||||||
|
console.log('trying to open changelog...');
|
||||||
|
plugins.smartopen.openUrl(changelogUrlArg);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
import * as smartlog from '@pushrocks/smartlog';
|
|
||||||
smartlog.defaultLogger.enableConsole();
|
|
||||||
import * as consolecolor from '@pushrocks/consolecolor';
|
import * as consolecolor from '@pushrocks/consolecolor';
|
||||||
import * as npmextra from '@pushrocks/npmextra';
|
import * as npmextra from '@pushrocks/npmextra';
|
||||||
import * as smartnpm from '@pushrocks/smartnpm';
|
import * as smartnpm from '@pushrocks/smartnpm';
|
||||||
@ -7,4 +5,4 @@ import * as smartopen from '@pushrocks/smartopen';
|
|||||||
import * as smarttime from '@pushrocks/smarttime';
|
import * as smarttime from '@pushrocks/smarttime';
|
||||||
import * as smartversion from '@pushrocks/smartversion';
|
import * as smartversion from '@pushrocks/smartversion';
|
||||||
|
|
||||||
export { smartlog, consolecolor, npmextra, smartnpm, smartopen, smarttime, smartversion };
|
export { consolecolor, npmextra, smartnpm, smartopen, smarttime, smartversion };
|
||||||
|
Reference in New Issue
Block a user