Skip to content

Commit

Permalink
Merge pull request #463 from twilio/check-for-update
Browse files Browse the repository at this point in the history
check for update
  • Loading branch information
ktalebian committed May 12, 2021
2 parents a478138 + 29e7cbb commit 875908d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/flex-dev-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

export { default as fs } from './fs';
export { default as lodash } from './lodash';
export { default as updateNotifier } from './updateNotifier';
export { default as updateNotifier, checkForUpdate } from './updateNotifier';
export { default as progress } from './progress';
export { default as logger, Logger, coloredStrings } from './logger';
export { default as logger, Logger, coloredStrings, chalk } from './logger';
export { default as boxen } from './boxen';
export { default as strings, multilineString, singleLineString } from './strings';
export { default as inquirer, prompt, confirm, choose, Question } from './inquirer';
Expand Down
2 changes: 1 addition & 1 deletion packages/flex-dev-utils/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { logger as default, Logger, coloredStrings } from 'flex-plugins-utils-logger';
export { logger as default, Logger, coloredStrings, chalk } from 'flex-plugins-utils-logger';
6 changes: 3 additions & 3 deletions packages/flex-dev-utils/src/updateNotifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import updateNotifier from 'update-notifier';
import updateNotifier, { NotifyOptions } from 'update-notifier';

import { readPackageJson, findUp, readAppPackageJson } from './fs';

Expand All @@ -9,8 +9,8 @@ export default updateNotifier;
*/
/* istanbul ignore next */
// eslint-disable-next-line import/no-unused-modules
export const checkForUpdate = (): void => {
export const checkForUpdate = (customMessage?: NotifyOptions): void => {
const pkg = module.parent ? readPackageJson(findUp(module.parent.filename, 'package.json')) : readAppPackageJson();

updateNotifier({ pkg }).notify();
updateNotifier({ pkg }).notify(customMessage);
};
1 change: 1 addition & 0 deletions packages/flex-plugins-utils-logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const boxen = {
};

export { Logger, coloredStrings } from './lib/logger';
export { default as chalk } from 'chalk';
export { default as logger } from './lib/logger';
export { default as strings, multilineString, singleLineString } from './lib/strings';
export { default as table, printArray, printObjectArray } from './lib/table';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-flex/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@twilio-labs/plugin-flex",
"version": "4.9.0-beta.0",
"version": "0.1.0",
"description": "Create, develop and deploy Flex plugins using the Twilio CLI",
"keywords": [
"twilio",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('Commands/FlexPluginsDeploy', () => {

const cmd = await createTest(FlexPluginsDeploy)('--changelog', defaultChangelog, ...args);

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'builderVersion', 'get').mockReturnValue(4);
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'doRun').mockReturnThis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('SubCommands/FlexPlugin', () => {
it('should call setEnvironment', async () => {
const cmd = await createTest(FlexPlugin)();

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'doRun').mockResolvedValue('any');

Expand All @@ -109,6 +110,7 @@ describe('SubCommands/FlexPlugin', () => {
it('should set debug env to true', async () => {
const cmd = await createTest(FlexPlugin)('-l', 'debug');

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'doRun').mockResolvedValue('any');

Expand All @@ -120,12 +122,14 @@ describe('SubCommands/FlexPlugin', () => {
it('should run the main command successfully', async () => {
const cmd = await createTest(FlexPlugin)();

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'setupEnvironment').mockReturnThis();
jest.spyOn(cmd, 'doRun').mockResolvedValue(null);

await cmd.run();

expect(cmd.checkForUpdate).toHaveBeenCalledTimes(1);
expect(cmd.pluginsApiToolkit).toBeDefined();
expect(cmd.pluginsClient).toBeDefined();
expect(cmd.pluginVersionsClient).toBeDefined();
Expand All @@ -139,6 +143,7 @@ describe('SubCommands/FlexPlugin', () => {
it('should return raw format', async () => {
const cmd = await createTest(FlexPlugin)('--json');

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'setupEnvironment').mockReturnThis();
jest.spyOn(cmd, 'doRun').mockResolvedValue({ object: 'result' });
Expand All @@ -151,8 +156,8 @@ describe('SubCommands/FlexPlugin', () => {

it('should not return raw format', async () => {
const cmd = await createTest(FlexPlugin)();
// []

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'setupEnvironment').mockReturnThis();
jest.spyOn(cmd, 'doRun').mockResolvedValue({ object: 'result' });
Expand All @@ -166,6 +171,7 @@ describe('SubCommands/FlexPlugin', () => {
it('should throw exception if script needs to run in plugin directory but is not', async (done) => {
const cmd = await createTest(FlexPlugin)();

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(false);
jest.spyOn(cmd, 'doRun').mockResolvedValue(null);

Expand Down Expand Up @@ -231,6 +237,7 @@ describe('SubCommands/FlexPlugin', () => {
it('should quit if builder version is incorrect', async () => {
const cmd = await createTest(FlexPlugin)();

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'builderVersion', 'get').mockReturnValue(3);
jest.spyOn(cmd, 'checkCompatibility', 'get').mockReturnValue(true);
Expand All @@ -246,6 +253,7 @@ describe('SubCommands/FlexPlugin', () => {
it('should not quit if builder version is correct', async () => {
const cmd = await createTest(FlexPlugin)();

jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
jest.spyOn(cmd, 'builderVersion', 'get').mockReturnValue(4);
jest.spyOn(cmd, 'checkCompatibility', 'get').mockReturnValue(true);
Expand Down
25 changes: 24 additions & 1 deletion packages/plugin-flex/src/sub-commands/flex-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
TwilioCliError,
env,
semver,
updateNotifier,
chalk,
} from 'flex-dev-utils';
import dayjs from 'dayjs';
import * as Errors from '@oclif/errors';
Expand Down Expand Up @@ -82,6 +84,8 @@ const packageJsonStr = 'package.json';
* This will ensure the script is running on a Flex-plugin project, otherwise will throw an error
*/
export default class FlexPlugin extends baseCommands.TwilioClientCommand {
static checkForUpdateFrequency = 1000 * 60 * 60 * 24; // daily

static topicName = 'flex:plugins';

/**
Expand Down Expand Up @@ -123,6 +127,8 @@ export default class FlexPlugin extends baseCommands.TwilioClientCommand {

protected readonly pluginRootDir: string;

protected readonly cliPkg: Pkg;

protected readonly cliRootDir: string;

protected readonly skipEnvironmentalSetup: boolean;
Expand Down Expand Up @@ -159,7 +165,8 @@ export default class FlexPlugin extends baseCommands.TwilioClientCommand {
this.skipEnvironmentalSetup = false;
this._logger = new Logger({ isQuiet: false, markdown: true });
// eslint-disable-next-line global-require, @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
this.version = require(join(this.pluginRootDir, packageJsonStr)).version;
this.cliPkg = require(join(this.pluginRootDir, packageJsonStr));
this.version = this.cliPkg.version;

if (!this.opts.strict) {
// @ts-ignore
Expand Down Expand Up @@ -356,6 +363,8 @@ export default class FlexPlugin extends baseCommands.TwilioClientCommand {
*/
async run(): Promise<void> {
await super.run();
this.checkForUpdate();

this.logger.debug(`Using Flex Plugins Config File: ${this.pluginsConfigPath}`);

if (this._flags['clear-terminal']) {
Expand Down Expand Up @@ -545,6 +554,20 @@ export default class FlexPlugin extends baseCommands.TwilioClientCommand {
}
}

/**
* Checks for CLI update
*/
/* istanbul ignore next */
checkForUpdate(): void {
const notifier = updateNotifier({ pkg: this.cliPkg, updateCheckInterval: FlexPlugin.checkForUpdateFrequency });
// template taken from the update-checker
const message = `Update available ${chalk.dim(notifier.update?.current)}${chalk.reset(' → ')}${chalk.green(
notifier.update?.latest,
)} \nRun ${chalk.cyan('twilio plugins:install @twilio-labs/plugin-flex')} to update`;

notifier.notify({ message });
}

/**
* Abstract class method that each command should extend; this is the actual command that runs once initialization is
* complete
Expand Down

0 comments on commit 875908d

Please sign in to comment.