watchr icon indicating copy to clipboard operation
watchr copied to clipboard

typescript types

Open dheerajbhaskar opened this issue 6 years ago • 10 comments

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

dheerajbhaskar avatar Jun 14 '18 08:06 dheerajbhaskar

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
}
*/

dheerajbhaskar avatar Jun 14 '18 10:06 dheerajbhaskar

I wouldn’t know, as I don’t use typescript

balupton avatar Jul 24 '18 18:07 balupton

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

balupton avatar Aug 19 '18 12:08 balupton

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;
}

jbreckmckye avatar Nov 22 '18 15:11 jbreckmckye

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

balupton avatar Nov 26 '18 11:11 balupton

To convert this project to TypeScript:

  1. clone watchr
  2. create a dev-typescript branch
  3. run https://github.com/bevry/boundation on it, changing the language from esnext to typescript
  4. perform the manual conversion, fix all typescript errors
  5. run boundation again
  6. send a PR

balupton avatar Dec 07 '18 02:12 balupton

I'll be picking this up soon, if anyone wants to help out.

balupton avatar Nov 24 '23 14:11 balupton

I've updated the downstream packages of watchr, and will be tackling watchr next. Will be converting watchr to typescript.

balupton avatar Jan 01 '24 22:01 balupton

mostly done, need to sort out https://github.com/bevry/taskgroup/issues/241

balupton avatar Jan 02 '24 00:01 balupton

WIP on dev branch, need to close https://github.com/bevry/taskgroup/issues/136 first

balupton avatar Jan 03 '24 11:01 balupton