diff --git a/types/update-notifier/index.d.ts b/types/update-notifier/index.d.ts index b8d6e02f52..df44b6cd3f 100644 --- a/types/update-notifier/index.d.ts +++ b/types/update-notifier/index.d.ts @@ -1,33 +1,47 @@ -// Type definitions for update-notifier 2.5 +// Type definitions for update-notifier 4.1 // Project: https://github.com/yeoman/update-notifier // Definitions by: vvakame // Noah Chen // Jason Dreyzehner // Michael Grinich +// Piotr Błażejewicz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +import ConfigStore = require('configstore'); + export = UpdateNotifier; -declare function UpdateNotifier( - settings?: UpdateNotifier.Settings -): UpdateNotifier.UpdateNotifier; +/** Checks if there is an available update */ +declare function UpdateNotifier(settings?: UpdateNotifier.Settings): UpdateNotifier.UpdateNotifier; declare namespace UpdateNotifier { class UpdateNotifier { constructor(settings?: Settings); - update?: UpdateInfo; + readonly config: ConfigStore; + readonly update?: UpdateInfo; check(): void; - checkNpm(): void; + /** Check update information */ + fetchInfo(): UpdateInfo | Promise; + /** Convenience method to display a notification message */ notify(customMessage?: NotifyOptions): void; } interface Settings { + /** Which dist-tag to use to find the latest version */ + distTag?: string; pkg?: Package; - callback?(error: Error | null, update?: UpdateInfo): any; + /** + * @deprecated use `pkg.name` + */ packageName?: string; + /** + * @deprecated use `pkg.version` + */ packageVersion?: string; - updateCheckInterval?: number; // in milliseconds, default 1000 * 60 * 60 * 24 (1 day) + /** How often to check for updates */ + updateCheckInterval?: number; + /** Allows notification to be shown when running as an npm script */ shouldNotifyInNpmScript?: boolean; } @@ -40,10 +54,14 @@ declare namespace UpdateNotifier { } interface NotifyOptions { + /** Message that will be shown when an update is available */ message?: string; + /** Defer showing the notification to after the process has exited */ defer?: boolean; + /** Include the -g argument in the default message's npm i recommendation */ isGlobal?: boolean; - boxenOpts?: BoxenOptions; + /** Options object that will be passed to `boxen` */ + boxenOptions?: BoxenOptions; } interface Package { @@ -52,9 +70,13 @@ declare namespace UpdateNotifier { } interface UpdateInfo { - latest: string; - current: string; - type: string; + /** Latest version */ + readonly latest: string; + /** Current version */ + readonly current: string; + /** Type of current update */ + readonly type: 'latest' | 'major' | 'minor' | 'patch' | 'prerelease' | 'build'; + /** Package name */ name: string; } } diff --git a/types/update-notifier/tsconfig.json b/types/update-notifier/tsconfig.json index df8ad3f8ed..7ce9885785 100644 --- a/types/update-notifier/tsconfig.json +++ b/types/update-notifier/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -21,4 +21,4 @@ "index.d.ts", "update-notifier-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/update-notifier/update-notifier-tests.ts b/types/update-notifier/update-notifier-tests.ts index c09fcf556a..baf45bf3de 100644 --- a/types/update-notifier/update-notifier-tests.ts +++ b/types/update-notifier/update-notifier-tests.ts @@ -1,3 +1,5 @@ +/// + import UpdateNotifier = require('update-notifier'); let notifier = UpdateNotifier(); @@ -10,7 +12,7 @@ console.log(notifier.update); // Also exposed as a class notifier = new UpdateNotifier.UpdateNotifier({ - updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week + updateCheckInterval: 1000 * 60 * 60 * 24 * 7, // 1 week }); if (notifier.update) { @@ -22,27 +24,24 @@ if (notifier.update) { message: 'Update available: ' + notifier.update.latest, defer: false, isGlobal: true, - boxenOpts: { + boxenOptions: { padding: 1, margin: 1, align: 'center', borderColor: 'yellow', - borderStyle: 'round' - } + borderStyle: 'round', + }, }); } -// Using the callback option -notifier = new UpdateNotifier.UpdateNotifier({ - callback: (err, update) => { - if (err) throw err; - if (update) { - console.log( - update.current, - update.latest, - update.name, - update.type - ); - } +(async () => { + const update = await notifier.fetchInfo(); + update.current; // $ExpectType string + update.latest; // $ExpectType string + update.name; // $ExpectType string + update.type; // $ExpectType "latest" | "major" | "minor" | "patch" | "prerelease" | "build" + notifier.config.set('lastUpdateCheck', Date.now()); + if (update.type && update.type !== 'latest') { + notifier.config.set('update', update); } -}); +})(); diff --git a/types/update-notifier/v2/index.d.ts b/types/update-notifier/v2/index.d.ts new file mode 100644 index 0000000000..b8d6e02f52 --- /dev/null +++ b/types/update-notifier/v2/index.d.ts @@ -0,0 +1,60 @@ +// Type definitions for update-notifier 2.5 +// Project: https://github.com/yeoman/update-notifier +// Definitions by: vvakame +// Noah Chen +// Jason Dreyzehner +// Michael Grinich +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export = UpdateNotifier; + +declare function UpdateNotifier( + settings?: UpdateNotifier.Settings +): UpdateNotifier.UpdateNotifier; + +declare namespace UpdateNotifier { + class UpdateNotifier { + constructor(settings?: Settings); + + update?: UpdateInfo; + check(): void; + checkNpm(): void; + notify(customMessage?: NotifyOptions): void; + } + + interface Settings { + pkg?: Package; + callback?(error: Error | null, update?: UpdateInfo): any; + packageName?: string; + packageVersion?: string; + updateCheckInterval?: number; // in milliseconds, default 1000 * 60 * 60 * 24 (1 day) + shouldNotifyInNpmScript?: boolean; + } + + interface BoxenOptions { + padding?: number; + margin?: number; + align?: string; + borderColor?: string; + borderStyle?: string; + } + + interface NotifyOptions { + message?: string; + defer?: boolean; + isGlobal?: boolean; + boxenOpts?: BoxenOptions; + } + + interface Package { + name: string; + version: string; + } + + interface UpdateInfo { + latest: string; + current: string; + type: string; + name: string; + } +} diff --git a/types/update-notifier/v2/tsconfig.json b/types/update-notifier/v2/tsconfig.json new file mode 100644 index 0000000000..528451cbf9 --- /dev/null +++ b/types/update-notifier/v2/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "paths": { + "update-notifier": [ + "update-notifier/v2" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "update-notifier-tests.ts" + ] +} diff --git a/types/update-notifier/v2/tslint.json b/types/update-notifier/v2/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/update-notifier/v2/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/update-notifier/v2/update-notifier-tests.ts b/types/update-notifier/v2/update-notifier-tests.ts new file mode 100644 index 0000000000..c09fcf556a --- /dev/null +++ b/types/update-notifier/v2/update-notifier-tests.ts @@ -0,0 +1,48 @@ +import UpdateNotifier = require('update-notifier'); + +let notifier = UpdateNotifier(); + +if (notifier.update) { + notifier.notify(); +} + +console.log(notifier.update); + +// Also exposed as a class +notifier = new UpdateNotifier.UpdateNotifier({ + updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week +}); + +if (notifier.update) { + notifier.notify(); // test no options + notifier.notify({}); // test empty object + + // test all options + notifier.notify({ + message: 'Update available: ' + notifier.update.latest, + defer: false, + isGlobal: true, + boxenOpts: { + padding: 1, + margin: 1, + align: 'center', + borderColor: 'yellow', + borderStyle: 'round' + } + }); +} + +// Using the callback option +notifier = new UpdateNotifier.UpdateNotifier({ + callback: (err, update) => { + if (err) throw err; + if (update) { + console.log( + update.current, + update.latest, + update.name, + update.type + ); + } + } +});