From 7f03fea2dacd951c30bfb9ef7e5daebd1ce0e577 Mon Sep 17 00:00:00 2001 From: Joram van den Boezem Date: Mon, 11 Feb 2019 17:07:13 +0100 Subject: [PATCH 1/3] add nssm --- types/nssm/index.d.ts | 54 ++++++++++++++++++++++++++++++++++++++++ types/nssm/nssm-tests.ts | 50 +++++++++++++++++++++++++++++++++++++ types/nssm/tsconfig.json | 22 ++++++++++++++++ types/nssm/tslint.json | 1 + 4 files changed, 127 insertions(+) create mode 100644 types/nssm/index.d.ts create mode 100644 types/nssm/nssm-tests.ts create mode 100644 types/nssm/tsconfig.json create mode 100644 types/nssm/tslint.json diff --git a/types/nssm/index.d.ts b/types/nssm/index.d.ts new file mode 100644 index 0000000000..09ca82e6a0 --- /dev/null +++ b/types/nssm/index.d.ts @@ -0,0 +1,54 @@ +// Type definitions for nssm 0.1 +// Project: https://github.com/alykoshin/nssm +// Definitions by: Joram van den Boezem +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +type Command = + | 'install' + | 'remove' + | 'start' + | 'stop' + | 'restart' + | 'status' + | 'pause' + | 'continue' + | 'rotate' + | 'get' + | 'set' + | 'reset' + +type NssmThen = ( + onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, + onrejected?: ((reason: Error, stderr: string) => TResult2 | PromiseLike) | null | undefined +) => NssmPromise + +type NssmCatch = ( + onrejected?: ((reason: Error, stderr: string) => TResult | PromiseLike) | null | undefined +) => NssmPromise + +interface NssmPromise extends Promise { + then: NssmThen + catch: NssmCatch +} + +type CallbackFn = (error?: string, result?: string) => void +type ZeroArgCommandFn = (callback: CallbackFn) => void +type OneArgCommandFn = (arg1: string, callback: CallbackFn) => void +type TwoArgCommandFn = (arg1: string, arg2: string, callback: CallbackFn) => void +type PromiseCommandFn = (arg1?: string, arg2?: string) => NssmPromise + +type NssmCommandFn = + & ZeroArgCommandFn + & OneArgCommandFn + & TwoArgCommandFn + & PromiseCommandFn + +export type Nssm = { + [key in Command]: NssmCommandFn +} + +export interface NssmOptions { + nssmExe?: string +} + +export default function nssm (serviceName: string, options?: NssmOptions): Nssm diff --git a/types/nssm/nssm-tests.ts b/types/nssm/nssm-tests.ts new file mode 100644 index 0000000000..73733d91b7 --- /dev/null +++ b/types/nssm/nssm-tests.ts @@ -0,0 +1,50 @@ +// https://github.com/alykoshin/nssm/blob/master/examples/promise_chain.js + +import nssm from 'nssm' + +const svcName = 'test'; +const options = { nssmExe: 'nssm.exe' }; +const testService = nssm(svcName, options); + +const propertyName = 'Start'; + +let console: { + log: (...message: any[]) => void +}; + +testService.set('start', 'manual') + .then(function(stdout) { + console.log('\n*** Parameter set ok'); + console.log('stdout: \'' + stdout + '\''); + return testService.get('start') + }) + .then(function(stdout) { + console.log('\n*** Parameter retrieved ok'); + console.log('stdout: \'' + stdout + '\''); + return testService.start() + }) + .then(function(stdout) { + console.log('\n*** Service started ok'); + console.log('stdout: \'' + stdout + '\''); + return testService.stop() + }) + .then(function(stdout) { + console.log('\n*** Service stopped ok'); + console.log('stdout: \'' + stdout + '\''); + console.log('DONE'); + }) + .catch(function(error) { + console.log('\n*** catch(): error:', error); + console.log('ERROR:', error); + }) + ; + +// https://github.com/alykoshin/nssm/blob/master/examples/get_callback.js + +testService.get(propertyName, function(error, result) { + if (error) { + console.log('error:', error, ' stderr:', result); + return; + } + console.log('stdout: \'' + result + '\''); +}); diff --git a/types/nssm/tsconfig.json b/types/nssm/tsconfig.json new file mode 100644 index 0000000000..831eba6f50 --- /dev/null +++ b/types/nssm/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "nssm-tests.ts" + ] +} diff --git a/types/nssm/tslint.json b/types/nssm/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/nssm/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 0e282ddae081df3f92b96967a0e224627a13423d Mon Sep 17 00:00:00 2001 From: Joram van den Boezem Date: Mon, 11 Feb 2019 17:53:18 +0100 Subject: [PATCH 2/3] chore: fix lint errors --- types/nssm/index.d.ts | 45 ++++++++++++++++---------------- types/nssm/nssm-tests.ts | 56 +++++++++++++++++----------------------- types/nssm/tsconfig.json | 1 + 3 files changed, 48 insertions(+), 54 deletions(-) diff --git a/types/nssm/index.d.ts b/types/nssm/index.d.ts index 09ca82e6a0..3df4b6887e 100644 --- a/types/nssm/index.d.ts +++ b/types/nssm/index.d.ts @@ -2,8 +2,9 @@ // Project: https://github.com/alykoshin/nssm // Definitions by: Joram van den Boezem // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 -type Command = +export type Command = | 'install' | 'remove' | 'start' @@ -15,40 +16,40 @@ type Command = | 'rotate' | 'get' | 'set' - | 'reset' + | 'reset'; -type NssmThen = ( - onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, - onrejected?: ((reason: Error, stderr: string) => TResult2 | PromiseLike) | null | undefined -) => NssmPromise +export type NssmThen = ( + onfulfilled?: ((value: T) => TResult1 | PromiseLike) | null, + onrejected?: ((reason: Error, stderr: string) => TResult2 | PromiseLike) | null +) => NssmPromise; -type NssmCatch = ( - onrejected?: ((reason: Error, stderr: string) => TResult | PromiseLike) | null | undefined -) => NssmPromise +export type NssmCatch = ( + onrejected?: ((reason: Error, stderr: string) => TResult | PromiseLike) | null +) => NssmPromise; -interface NssmPromise extends Promise { - then: NssmThen - catch: NssmCatch +export interface NssmPromise extends Promise { + then: NssmThen; + catch: NssmCatch; } -type CallbackFn = (error?: string, result?: string) => void -type ZeroArgCommandFn = (callback: CallbackFn) => void -type OneArgCommandFn = (arg1: string, callback: CallbackFn) => void -type TwoArgCommandFn = (arg1: string, arg2: string, callback: CallbackFn) => void -type PromiseCommandFn = (arg1?: string, arg2?: string) => NssmPromise +export type CallbackFn = (error?: string, result?: string) => void; +export type ZeroArgCommandFn = (callback: CallbackFn) => void; +export type OneArgCommandFn = (arg1: string, callback: CallbackFn) => void; +export type TwoArgCommandFn = (arg1: string, arg2: string, callback: CallbackFn) => void; +export type PromiseCommandFn = (arg1?: string, arg2?: string) => NssmPromise; -type NssmCommandFn = +export type NssmCommandFn = & ZeroArgCommandFn & OneArgCommandFn & TwoArgCommandFn - & PromiseCommandFn + & PromiseCommandFn; export type Nssm = { [key in Command]: NssmCommandFn -} +}; export interface NssmOptions { - nssmExe?: string + nssmExe?: string; } -export default function nssm (serviceName: string, options?: NssmOptions): Nssm +export default function nssm(serviceName: string, options?: NssmOptions): Nssm; diff --git a/types/nssm/nssm-tests.ts b/types/nssm/nssm-tests.ts index 73733d91b7..cfccb3b6d3 100644 --- a/types/nssm/nssm-tests.ts +++ b/types/nssm/nssm-tests.ts @@ -1,6 +1,6 @@ // https://github.com/alykoshin/nssm/blob/master/examples/promise_chain.js -import nssm from 'nssm' +import nssm from 'nssm'; const svcName = 'test'; const options = { nssmExe: 'nssm.exe' }; @@ -8,43 +8,35 @@ const testService = nssm(svcName, options); const propertyName = 'Start'; -let console: { - log: (...message: any[]) => void -}; +const console: { log: (...message: any[]) => void } = { log: (...args) => void (args) }; testService.set('start', 'manual') - .then(function(stdout) { - console.log('\n*** Parameter set ok'); - console.log('stdout: \'' + stdout + '\''); - return testService.get('start') - }) - .then(function(stdout) { - console.log('\n*** Parameter retrieved ok'); - console.log('stdout: \'' + stdout + '\''); - return testService.start() - }) - .then(function(stdout) { - console.log('\n*** Service started ok'); - console.log('stdout: \'' + stdout + '\''); - return testService.stop() - }) - .then(function(stdout) { - console.log('\n*** Service stopped ok'); - console.log('stdout: \'' + stdout + '\''); - console.log('DONE'); - }) - .catch(function(error) { - console.log('\n*** catch(): error:', error); - console.log('ERROR:', error); - }) - ; + .then((stdout) => { + console.log(`stdout: ${stdout}`); + return testService.get('start'); + }) + .then((stdout) => { + console.log(`stdout: ${stdout}`); + return testService.start(); + }) + .then((stdout) => { + console.log(`stdout: ${stdout}`); + return testService.stop(); + }) + .then((stdout) => { + console.log(`stdout: ${stdout}`); + }) + .catch((error, stdout) => { + console.log(`error: ${error}, stdout: ${stdout}`); + }) +; // https://github.com/alykoshin/nssm/blob/master/examples/get_callback.js -testService.get(propertyName, function(error, result) { +testService.get(propertyName, (error, stdout) => { if (error) { - console.log('error:', error, ' stderr:', result); + console.log('error:', error, ' stderr:', stdout); return; } - console.log('stdout: \'' + result + '\''); + console.log(`stdout: ${stdout}`); }); diff --git a/types/nssm/tsconfig.json b/types/nssm/tsconfig.json index 831eba6f50..6b9d947f9b 100644 --- a/types/nssm/tsconfig.json +++ b/types/nssm/tsconfig.json @@ -7,6 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" From ed128eb338ac3b8ff0512911b9858608645d7bef Mon Sep 17 00:00:00 2001 From: Joram van den Boezem Date: Wed, 13 Feb 2019 16:13:14 +0100 Subject: [PATCH 3/3] refactor: use export = instead of export default See https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require --- types/nssm/index.d.ts | 28 +++++++++++++++------------- types/nssm/nssm-tests.ts | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/types/nssm/index.d.ts b/types/nssm/index.d.ts index 3df4b6887e..c1bc88bdf1 100644 --- a/types/nssm/index.d.ts +++ b/types/nssm/index.d.ts @@ -4,7 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -export type Command = +type Command = | 'install' | 'remove' | 'start' @@ -18,38 +18,40 @@ export type Command = | 'set' | 'reset'; -export type NssmThen = ( +type NssmThen = ( onfulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onrejected?: ((reason: Error, stderr: string) => TResult2 | PromiseLike) | null ) => NssmPromise; -export type NssmCatch = ( +type NssmCatch = ( onrejected?: ((reason: Error, stderr: string) => TResult | PromiseLike) | null ) => NssmPromise; -export interface NssmPromise extends Promise { +interface NssmPromise extends Promise { then: NssmThen; catch: NssmCatch; } -export type CallbackFn = (error?: string, result?: string) => void; -export type ZeroArgCommandFn = (callback: CallbackFn) => void; -export type OneArgCommandFn = (arg1: string, callback: CallbackFn) => void; -export type TwoArgCommandFn = (arg1: string, arg2: string, callback: CallbackFn) => void; -export type PromiseCommandFn = (arg1?: string, arg2?: string) => NssmPromise; +type CallbackFn = (error?: string, result?: string) => void; +type ZeroArgCommandFn = (callback: CallbackFn) => void; +type OneArgCommandFn = (arg1: string, callback: CallbackFn) => void; +type TwoArgCommandFn = (arg1: string, arg2: string, callback: CallbackFn) => void; +type PromiseCommandFn = (arg1?: string, arg2?: string) => NssmPromise; -export type NssmCommandFn = +type NssmCommandFn = & ZeroArgCommandFn & OneArgCommandFn & TwoArgCommandFn & PromiseCommandFn; -export type Nssm = { +type Nssm = { [key in Command]: NssmCommandFn }; -export interface NssmOptions { +interface NssmOptions { nssmExe?: string; } -export default function nssm(serviceName: string, options?: NssmOptions): Nssm; +declare function nssm(serviceName: string, options?: NssmOptions): Nssm; + +export = nssm; diff --git a/types/nssm/nssm-tests.ts b/types/nssm/nssm-tests.ts index cfccb3b6d3..c0ace009c6 100644 --- a/types/nssm/nssm-tests.ts +++ b/types/nssm/nssm-tests.ts @@ -1,6 +1,6 @@ // https://github.com/alykoshin/nssm/blob/master/examples/promise_chain.js -import nssm from 'nssm'; +import nssm = require('nssm'); const svcName = 'test'; const options = { nssmExe: 'nssm.exe' };