From f2731469da8e84f964dc335ab56c8f8ff7175049 Mon Sep 17 00:00:00 2001 From: e-cloud Date: Fri, 2 Dec 2016 15:07:56 +0800 Subject: [PATCH] feat: add typings for watchpack --- watchpack/DirectoryWatcher.d.ts | 59 +++++++++++++++++++++++++++++++++ watchpack/Watcher.d.ts | 18 ++++++++++ watchpack/index.d.ts | 54 ++++++++++++++++++++++++++++++ watchpack/tsconfig.json | 21 ++++++++++++ watchpack/tslint.json | 1 + watchpack/watchpack-tests.ts | 7 ++++ 6 files changed, 160 insertions(+) create mode 100644 watchpack/DirectoryWatcher.d.ts create mode 100644 watchpack/Watcher.d.ts create mode 100644 watchpack/index.d.ts create mode 100644 watchpack/tsconfig.json create mode 100644 watchpack/tslint.json create mode 100644 watchpack/watchpack-tests.ts diff --git a/watchpack/DirectoryWatcher.d.ts b/watchpack/DirectoryWatcher.d.ts new file mode 100644 index 0000000000..0fac7ebeff --- /dev/null +++ b/watchpack/DirectoryWatcher.d.ts @@ -0,0 +1,59 @@ +/// +/// +import { EventEmitter } from 'events'; +import fs = require('graceful-fs'); +import Watcher = require('./Watcher'); +import Watchpack = require('watchpack'); + +declare class DirectoryWatcher extends EventEmitter { + options: Watchpack.WatcherOptions; + directories: { + [path: string]: Watcher | true; + }; + files: { + [path: string]: [number, number]; + }; + initialScan: boolean; + initialScanRemoved: string[]; + nestedWatching: boolean; + path: string; + refs: number; + watcher: fs.FSWatcher; + watchers: { + [path: string]: Watcher[]; + }; + + constructor(directoryPath: string, options: Watchpack.WatcherOptions); + + setFileTime(filePath: string, mtime: number, initial: boolean, type?: string | boolean): void; + + setDirectory(directoryPath: string, exist: boolean, initial: boolean): void; + + createNestedWatcher(directoryPath: string): void; + + setNestedWatching(flag: boolean): void; + + watch(filePath: string, startTime: number): Watcher; + + onFileAdded(filePath: string, stat: fs.Stats): void; + + onDirectoryAdded(directoryPath: string): void; + + onChange(filePath: string, stat: fs.Stats): void; + + onFileUnlinked(filePath: string): void; + + onDirectoryUnlinked(directoryPath: string): void; + + onWatcherError(): void; + + doInitialScan(): void; + + getTimes(): { + [path: string]: number; + }; + + close(): void; +} + +export = DirectoryWatcher; diff --git a/watchpack/Watcher.d.ts b/watchpack/Watcher.d.ts new file mode 100644 index 0000000000..041b693da8 --- /dev/null +++ b/watchpack/Watcher.d.ts @@ -0,0 +1,18 @@ +/// +import { EventEmitter } from 'events'; +import DirectoryWatcher = require('./DirectoryWatcher'); + +declare class Watcher extends EventEmitter { + data: number; + directoryWatcher: DirectoryWatcher; + path: string; + startTime: number; + + constructor(directoryWatcher: DirectoryWatcher, filePath: string, startTime: number); + + checkStartTime(mtime: number, initial: boolean): boolean; + + close(): void; +} + +export = Watcher; diff --git a/watchpack/index.d.ts b/watchpack/index.d.ts new file mode 100644 index 0000000000..9a987be3da --- /dev/null +++ b/watchpack/index.d.ts @@ -0,0 +1,54 @@ +// Type definitions for watchpack 1.1 +// Project: https://github.com/webpack/watchpack +// Definitions by: e-cloud +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { EventEmitter } from 'events'; +import Watcher = require('./Watcher'); + +declare class Watchpack extends EventEmitter { + aggregatedChanges: string[]; + aggregateTimeout: NodeJS.Timer; + dirWatchers: Watcher[]; + fileWatchers: Watcher[]; + mtimes: { + [path: string]: number; + }; + options: Watchpack.WatchOptions; + paused: boolean; + watcherOptions: Watchpack.WatcherOptions; + + constructor(options: Watchpack.WatchOptions); + + watch(files: string[], directories: string[], startTime: number): void; + + close(): void; + + pause(): void; + + getTimes(): { + [path: string]: number; + }; + + _fileWatcher(file: string, watcher: Watcher): Watcher; + + _dirWatcher(item: string, watcher: Watcher): Watcher; + + _onChange(item: string, mtime: number, file?: string): void; + + _onTimeout(): void; +} + +declare namespace Watchpack { + interface WatcherOptions { + ignored?: string[] | string | RegExp | ((path: string) => boolean); + poll?: boolean | number; + } + interface WatchOptions extends WatcherOptions { + aggregateTimeout?: number; + } +} + +export = Watchpack; diff --git a/watchpack/tsconfig.json b/watchpack/tsconfig.json new file mode 100644 index 0000000000..d492948f55 --- /dev/null +++ b/watchpack/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "DirectoryWatcher.d.ts", + "Watcher.d.ts", + "watchpack-tests.ts" + ] +} diff --git a/watchpack/tslint.json b/watchpack/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/watchpack/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } diff --git a/watchpack/watchpack-tests.ts b/watchpack/watchpack-tests.ts new file mode 100644 index 0000000000..77b78d8c23 --- /dev/null +++ b/watchpack/watchpack-tests.ts @@ -0,0 +1,7 @@ +import Watchpack = require('watchpack'); +const watch = new Watchpack({}); + +watch.watch(['test.js'], ['lib/'], 1000); + +let time: number +time = watch.getTimes()['test.js']