diff --git a/types/nssm/index.d.ts b/types/nssm/index.d.ts new file mode 100644 index 0000000000..c1bc88bdf1 --- /dev/null +++ b/types/nssm/index.d.ts @@ -0,0 +1,57 @@ +// Type definitions for nssm 0.1 +// Project: https://github.com/alykoshin/nssm +// Definitions by: Joram van den Boezem +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +type Command = + | 'install' + | 'remove' + | 'start' + | 'stop' + | 'restart' + | 'status' + | 'pause' + | 'continue' + | 'rotate' + | 'get' + | 'set' + | 'reset'; + +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 +) => 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; + +type Nssm = { + [key in Command]: NssmCommandFn +}; + +interface NssmOptions { + nssmExe?: string; +} + +declare function nssm(serviceName: string, options?: NssmOptions): Nssm; + +export = nssm; diff --git a/types/nssm/nssm-tests.ts b/types/nssm/nssm-tests.ts new file mode 100644 index 0000000000..c0ace009c6 --- /dev/null +++ b/types/nssm/nssm-tests.ts @@ -0,0 +1,42 @@ +// https://github.com/alykoshin/nssm/blob/master/examples/promise_chain.js + +import nssm = require('nssm'); + +const svcName = 'test'; +const options = { nssmExe: 'nssm.exe' }; +const testService = nssm(svcName, options); + +const propertyName = 'Start'; + +const console: { log: (...message: any[]) => void } = { log: (...args) => void (args) }; + +testService.set('start', 'manual') + .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, (error, stdout) => { + if (error) { + console.log('error:', error, ' stderr:', stdout); + return; + } + console.log(`stdout: ${stdout}`); +}); diff --git a/types/nssm/tsconfig.json b/types/nssm/tsconfig.json new file mode 100644 index 0000000000..6b9d947f9b --- /dev/null +++ b/types/nssm/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": 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" }