diff --git a/types/hostile/hostile-tests.ts b/types/hostile/hostile-tests.ts new file mode 100644 index 0000000000..1e12346f9f --- /dev/null +++ b/types/hostile/hostile-tests.ts @@ -0,0 +1,16 @@ +import hostile = require('hostile'); + +const getLines: hostile.Lines = hostile.get(false); +hostile.get(true, (error: Error | null, lines: hostile.Lines) => {}); + +const getFileLines: hostile.Lines = hostile.getFile('path/to/hosts', false); +hostile.getFile(hostile.HOSTS, true, (error: Error | null, lines: hostile.Lines) => {}); + +hostile.set('192.168.0.1', 'definitelytyped.org'); +hostile.set('192.168.0.1', 'definitelytyped.org', (error: Error | null) => {}); + +hostile.remove('192.168.0.1', 'definitelytyped.org'); +hostile.remove('192.168.0.1', 'definitelytyped.org', (error: Error | null) => {}); + +hostile.writeFile(getFileLines, false); +hostile.writeFile(getLines, true, (error: Error | null) => {}); diff --git a/types/hostile/index.d.ts b/types/hostile/index.d.ts new file mode 100644 index 0000000000..c0c1110bb8 --- /dev/null +++ b/types/hostile/index.d.ts @@ -0,0 +1,73 @@ +// Type definitions for hostile 1.3 +// Project: https://github.com/feross/hostile +// Definitions by: Andrew Leedham +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export const HOSTS: 'C:/Windows/System32/drivers/etc/hosts' | '/etc/hosts'; + +export type Line = string | [string /* host */, string /* ip */]; +export type Lines = Line[]; +export type Callback = (error: Error | null) => void; +export type GetCallback = (error: Error | null, lines: Lines) => void; + +/** + * Get a list of the lines that make up the filePath. + * + * @param filePath - Path to the hosts file to read. + * @param preserveFormatting - Whether to include comments, blank lines, etc. + * @returns lines of file. + */ +export function getFile(filePath: string, preserveFormatting: boolean): Lines; +/** + * Get a list of the lines that make up the filePath. + * + * @param filePath - Path to the hosts file to read. + * @param preserveFormatting - Whether to include comments, blank lines, etc. + * @param cb - Called when finished or failed (passing error or lines of file). + */ +export function getFile(filePath: string, preserveFormatting: boolean, cb: GetCallback): void; +export function getFile(filePath: string, preserveFormatting: boolean, cb?: GetCallback): void | Lines; + +/** + * Synchronous wrapper of `getFile` for getting a list of lines in the Host file + * + * @param preserveFormatting - Whether to include comments, blank lines, etc. + * @returns lines of file. + */ +export function get(preserveFormatting: boolean): Lines; +/** + * Wrapper of `getFile` for getting a list of lines in the Host file + * + * @param preserveFormatting - Whether to include comments, blank lines, etc. + * @param cb - Called when finished or failed (passing error or lines of file). + */ +export function get(preserveFormatting: boolean, cb: GetCallback): void; +export function get(preserveFormatting: boolean, cb?: GetCallback): void | Lines; + +/** + * Add a rule to /etc/hosts. If the rule already exists, then this does nothing. + * + * @param ip - IP of the entry to set. + * @param host - Host of the entry to set. + * @param cb - Called when finished or failed (passing error). + */ +export function set(ip: string, host: string, cb?: Callback): void; + +/** + * Remove a rule from /etc/hosts. If the rule does not exist, then this does + * nothing. + * + * @param ip - IP of the entry to remove. + * @param host - Host of the entry to remove. + * @param cb - Called when finished or failed (passing error). + */ +export function remove(ip: string, host: string, cb?: Callback): void; + +/** + * Write out an array of lines to the host file. Assumes that they're in the + * format that `get` returns. + * + * @param lines - Lines to write to the file. + * @param cb - Called when finished or failed (passing error). + */ +export function writeFile(lines: ReadonlyArray, preserveFormatting: boolean, cb?: Callback): void; diff --git a/types/hostile/tsconfig.json b/types/hostile/tsconfig.json new file mode 100644 index 0000000000..abcea9317a --- /dev/null +++ b/types/hostile/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "hostile-tests.ts" + ] +} diff --git a/types/hostile/tslint.json b/types/hostile/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/hostile/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }