Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Recent update warning #81

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cliStyles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {white, green, yellow} from 'chalk';
import {white, green, yellow, black} from 'chalk';

export const strong = white.bold;
export const success = green.bold;
export const attention = yellow.bold;

export const upgradeCaution = black.bgRed;
export const upgradeWarning = black.bgYellow;
export const upgradeInfo = black.bgWhite;
17 changes: 15 additions & 2 deletions src/commands/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {colorizeDiff} from 'npm-check-updates/lib/version-util';
import catchAsyncError from '../catchAsyncError';
import {makeFilterFunction} from '../filterUtils';
import {DEPS_GROUPS, loadGlobalPackages, loadPackageJson, setModuleVersion,
getModuleInfo, getModuleHomepage} from '../packageUtils';
getModuleInfo, getModuleHomepage, getVersionPublicationDate} from '../packageUtils';
import {fetchRemoteDb, findModuleChangelogUrl} from '../changelogUtils';
import {createSimpleTable} from '../cliTable';
import {strong, success, attention} from '../cliStyles';
import {strong, success, attention, upgradeCaution, upgradeWarning, upgradeInfo} from '../cliStyles';
import askUser from '../askUser';
import {toSentence} from '../stringUtils';
import {askIgnoreFields} from './ignore';
Expand Down Expand Up @@ -157,6 +157,19 @@ export const handler = catchAsyncError(async opts => {
// Adds new line
console.log('');

// This checks if the package was released less than 3 days ago, throws a warning if true
let publishedDate = await getVersionPublicationDate(name, to);
publishedDate = new Date(publishedDate);
const recommendedDatePrior = new Date(Date.now() - (1000 * 60 * 60 * 24 * 3)); // This is 3 days prior to execution time.
const isRecent = publishedDate.getTime() > recommendedDatePrior.getTime();
if (isRecent) {
const timeSincePublication = new Date(Date.now()).getTime() - publishedDate.getTime();
const warningLevel = (isRecent && timeSincePublication < (1000 * 60 * 60 * 24 * 1)) ? 'caution' : (timeSincePublication < (1000 * 60 * 60 * 24 * 2)) ? 'warning' : 'info';
let message = (warningLevel === 'caution') ? upgradeCaution("CAUTION") : (warningLevel === 'warning') ? upgradeWarning("WARN") : upgradeInfo("INFO");
message += ` ${name}@${to.replace("^", "")} was released less than ${Math.ceil(timeSincePublication / (1000*60*60*24))} days ago, be careful when upgrading.`;
console.log(message);
}

const answer = await askUser({
type: 'list',
message: `${changelogUrl === undefined ? 'U' : 'So, u'}pdate "${name}" ${opts.global ? 'globally' :
Expand Down
7 changes: 7 additions & 0 deletions src/packageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pacote from 'pacote';
import shell from 'shelljs';

import _ from 'lodash';
import got from 'got';

export const DEPS_GROUPS = [
{name: 'global', field: 'dependencies', flag: 'g', ncuValue: 'prod'},
Expand Down Expand Up @@ -108,3 +109,9 @@ export const getModuleInfo = _.memoize(async moduleName =>
fullMetadata: true
})
);

// This function returns the publication date of a specific module version.
export const getVersionPublicationDate = _.memoize(async (moduleName, version) => {
const moduleData = await got(`https://registry.npmjs.org/${moduleName}/`).json();
return moduleData.time[version.replace("^", "")];
});