Skip to content

Commit 6f723a4

Browse files
committed
check for update
1 parent a478138 commit 6f723a4

File tree

7 files changed

+41
-9
lines changed

7 files changed

+41
-9
lines changed

packages/flex-dev-utils/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
export { default as fs } from './fs';
44
export { default as lodash } from './lodash';
5-
export { default as updateNotifier } from './updateNotifier';
5+
export { default as updateNotifier, checkForUpdate } from './updateNotifier';
66
export { default as progress } from './progress';
7-
export { default as logger, Logger, coloredStrings } from './logger';
7+
export { default as logger, Logger, coloredStrings, chalk } from './logger';
88
export { default as boxen } from './boxen';
99
export { default as strings, multilineString, singleLineString } from './strings';
1010
export { default as inquirer, prompt, confirm, choose, Question } from './inquirer';

packages/flex-dev-utils/src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { logger as default, Logger, coloredStrings } from 'flex-plugins-utils-logger';
1+
export { logger as default, Logger, coloredStrings, chalk } from 'flex-plugins-utils-logger';
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import updateNotifier from 'update-notifier';
1+
import updateNotifier, { NotifyOptions } from 'update-notifier';
22

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

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

15-
updateNotifier({ pkg }).notify();
15+
updateNotifier({ pkg }).notify(customMessage);
1616
};

packages/flex-plugins-utils-logger/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const boxen = {
1010
};
1111

1212
export { Logger, coloredStrings } from './lib/logger';
13+
export { default as chalk } from 'chalk';
1314
export { default as logger } from './lib/logger';
1415
export { default as strings, multilineString, singleLineString } from './lib/strings';
1516
export { default as table, printArray, printObjectArray } from './lib/table';

packages/plugin-flex/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@twilio-labs/plugin-flex",
3-
"version": "4.9.0-beta.0",
3+
"version": "0.1.0",
44
"description": "Create, develop and deploy Flex plugins using the Twilio CLI",
55
"keywords": [
66
"twilio",

packages/plugin-flex/src/__tests__/sub-commands/flex-plugin.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('SubCommands/FlexPlugin', () => {
9595
it('should call setEnvironment', async () => {
9696
const cmd = await createTest(FlexPlugin)();
9797

98+
jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
9899
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
99100
jest.spyOn(cmd, 'doRun').mockResolvedValue('any');
100101

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

113+
jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
112114
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
113115
jest.spyOn(cmd, 'doRun').mockResolvedValue('any');
114116

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

125+
jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
123126
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
124127
jest.spyOn(cmd, 'setupEnvironment').mockReturnThis();
125128
jest.spyOn(cmd, 'doRun').mockResolvedValue(null);
126129

127130
await cmd.run();
128131

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

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

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

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

174+
jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
169175
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(false);
170176
jest.spyOn(cmd, 'doRun').mockResolvedValue(null);
171177

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

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

256+
jest.spyOn(cmd, 'checkForUpdate').mockReturnThis();
249257
jest.spyOn(cmd, 'isPluginFolder').mockReturnValue(true);
250258
jest.spyOn(cmd, 'builderVersion', 'get').mockReturnValue(4);
251259
jest.spyOn(cmd, 'checkCompatibility', 'get').mockReturnValue(true);

packages/plugin-flex/src/sub-commands/flex-plugin.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
TwilioCliError,
2121
env,
2222
semver,
23+
updateNotifier,
24+
chalk,
2325
} from 'flex-dev-utils';
2426
import dayjs from 'dayjs';
2527
import * as Errors from '@oclif/errors';
@@ -82,6 +84,8 @@ const packageJsonStr = 'package.json';
8284
* This will ensure the script is running on a Flex-plugin project, otherwise will throw an error
8385
*/
8486
export default class FlexPlugin extends baseCommands.TwilioClientCommand {
87+
static checkForUpdateFrequency = 1000 * 60 * 60 * 24; // daily
88+
8589
static topicName = 'flex:plugins';
8690

8791
/**
@@ -123,6 +127,8 @@ export default class FlexPlugin extends baseCommands.TwilioClientCommand {
123127

124128
protected readonly pluginRootDir: string;
125129

130+
protected readonly cliPkg: Pkg;
131+
126132
protected readonly cliRootDir: string;
127133

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

164171
if (!this.opts.strict) {
165172
// @ts-ignore
@@ -356,6 +363,8 @@ export default class FlexPlugin extends baseCommands.TwilioClientCommand {
356363
*/
357364
async run(): Promise<void> {
358365
await super.run();
366+
this.checkForUpdate();
367+
359368
this.logger.debug(`Using Flex Plugins Config File: ${this.pluginsConfigPath}`);
360369

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

557+
/**
558+
* Checks for CLI update
559+
*/
560+
/* istanbul ignore next */
561+
checkForUpdate(): void {
562+
const notifier = updateNotifier({ pkg: this.cliPkg, updateCheckInterval: FlexPlugin.checkForUpdateFrequency });
563+
// template taken from the update-checker
564+
const message = `Update available ${chalk.dim(notifier.update?.current)}${chalk.reset(' → ')}${chalk.green(
565+
notifier.update?.latest,
566+
)} \nRun ${chalk.cyan('twilio plugins:install @twilio-labs/plugin-flex')} to update`;
567+
568+
notifier.notify({ message });
569+
}
570+
548571
/**
549572
* Abstract class method that each command should extend; this is the actual command that runs once initialization is
550573
* complete

0 commit comments

Comments
 (0)