[@types/hostile] Add new module hostile (#37799)

This commit is contained in:
Andrew Leedham 2019-08-21 17:05:53 +01:00 committed by Sheetal Nandi
parent 7c4dafb166
commit af57590cb4
4 changed files with 113 additions and 0 deletions

View File

@ -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) => {});

73
types/hostile/index.d.ts vendored Normal file
View File

@ -0,0 +1,73 @@
// Type definitions for hostile 1.3
// Project: https://github.com/feross/hostile
// Definitions by: Andrew Leedham <https://github.com/AndrewLeedham>
// 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<Line>, preserveFormatting: boolean, cb?: Callback): void;

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }