watchr
watchr copied to clipboard
typescript types
Purpose:
Create Typescript type definitions
- So that I can do
npm i @types/watchr
Backgorund:
I believe there is some flow type information, can you please re-purpose that for typescript so one can get intellisense.
Note: If you can guide me on how to do that, I might be able to pull it off as well
Following are the types I found in the package sources. Can anyone help me how do I create typescript defs from this and then how do I submit that definitelyTyped or elsewhere or so that the purpose of this issue is fulfilled?
/* ::
import type {Stats, FSWatcher} from 'fs'
type StateEnum = "pending" | "active" | "deleted" | "closed"
type MethodEnum = "watch" | "watchFile"
type ErrorCallback = (error: ?Error) => void
type StatCallback = (error: ?Error, stat?: Stats) => void
type WatchChildOpts = {
fullPath: string,
relativePath: string,
stat?: Stats
}
type WatchSelfOpts = {
errors?: Array<Error>,
preferredMethods?: Array<MethodEnum>
}
type ListenerOpts = {
method: MethodEnum,
args: Array<any>
}
type ResetOpts = {
reset?: boolean
}
type IgnoreOpts = {
ignorePaths?: boolean,
ignoreHiddenFiles?: boolean,
ignoreCommonPatterns?: boolean,
ignoreCustomPatterns?: RegExp
}
type WatcherOpts = IgnoreOpts & {
stat?: Stats,
interval?: number,
persistent?: boolean,
catchupDelay?: number,
preferredMethods?: Array<MethodEnum>,
followLinks?: boolean
}
type WatcherConfig = {
stat: ?Stats,
interval: number,
persistent: boolean,
catchupDelay: number,
preferredMethods: Array<MethodEnum>,
followLinks: boolean,
ignorePaths: false | Array<string>,
ignoreHiddenFiles: boolean,
ignoreCommonPatterns: boolean,
ignoreCustomPatterns: ?RegExp
}
*/
I wouldn’t know, as I don’t use typescript
Ok. If anyone wants to tackle this, it would be via the movement from the flowtype comments to the typescript jsdoc comments.
Did the migration for https://github.com/bevry/caterpillar earlier this week: https://github.com/bevry/caterpillar/commit/f2486e3d2c9f561f13aa8a1e8ea96fc4ba652827
Does this look right?
/// <reference types="node" />
declare module 'watchr' {
import {Stats, FSWatcher} from 'fs';
import {EventEmitter} from 'events';
export type ChangeType = 'update' | 'create' | 'delete';
export type State = 'pending' | 'active' | 'deleted' | 'closed';
export type Method = 'watch' | 'watchFile';
export type ErrorCallback = (error?: Error) => void;
export type StatCallback = (error?: Error, stat?: Stats) => void;
export type ChangeListener = (changeType: ChangeType, fullPath: string, currentStat: Stats, prevStat: Stats) => void;
export interface ResetOpts {
reset?: boolean
}
export interface IgnoreOpts {
ignorePaths?: boolean,
ignoreHiddenFiles?: boolean,
ignoreCommonPatterns?: boolean,
ignoreCustomPatterns?: RegExp
}
export type WatcherOpts = IgnoreOpts & {
stat?: Stats,
interval?: number,
persistent?: boolean,
catchupDelay?: number,
preferredMethods?: Method[],
followLinks?: boolean
}
export interface WatcherConfig {
stat?: Stats,
interval: number,
persistent: boolean,
catchupDelay: number,
preferredMethods: Method[],
followLinks: boolean,
ignorePaths: false | Array<string>,
ignoreHiddenFiles: boolean,
ignoreCommonPatterns: boolean,
ignoreCustomPatterns?: RegExp
}
export class Stalker extends EventEmitter {
watcher: Watcher;
watchers: {[key: string]: Watcher};
close(reason: string): Stalker;
setConfig(...args: any[]): Stalker;
watch(...args: any[]): Stalker;
}
export class Watcher extends EventEmitter {
stalkers: Stalker[];
path: string;
stat: Stats;
fswatcher: FSWatcher;
children: {[key: string]: Stalker};
state: State;
listenerTaskGroup: any;
listenerTimeout: number;
config: WatcherConfig;
setConfig(opts: WatcherOpts): Watcher;
log(...args: any[]): Watcher;
getStat(opts: ResetOpts, next: StatCallback): Watcher;
close(reason?: string): Watcher;
watch(next: Function): Watcher;
watch(opts: ResetOpts, next: Function): Watcher;
}
export function open(path: string, changeListener: ChangeListener, next: ErrorCallback): Stalker;
export function create(...args: any[]): Stalker;
}
I'm not sure all the event properties are available on every event. I think old and new stats are dependent on the event type. So a generic may be needed or multiple function signatures.
I am planning to move all edited projects to typescript. If I setup this project with our typescript conventions, will anyone here want to finalise the conversion?
If so, I'll get the foundations done on a new branch and hand it over for forking.
Details on our conversion plan is found here: https://discuss.bevry.me/t/typescript-conversions-for-bevry-packages/269?u=balupton
To convert this project to TypeScript:
- clone watchr
- create a
dev-typescript
branch - run https://github.com/bevry/boundation on it, changing the language from
esnext
totypescript
- perform the manual conversion, fix all typescript errors
- run boundation again
- send a PR
I'll be picking this up soon, if anyone wants to help out.
I've updated the downstream packages of watchr, and will be tackling watchr next. Will be converting watchr to typescript.
mostly done, need to sort out https://github.com/bevry/taskgroup/issues/241
WIP on dev branch, need to close https://github.com/bevry/taskgroup/issues/136 first