Skip to content

Commit

Permalink
fix: add types to help with required nodemon usage (#2204)
Browse files Browse the repository at this point in the history
  • Loading branch information
remy authored May 25, 2024
1 parent 1707f92 commit cd27c0b
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 18.x]
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: npm
node-version: 16
node-version: 18
- name: Install dependencies
run: npm ci
- name: Release
Expand Down
126 changes: 126 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
type NodemonEventHandler =
| 'start'
| 'crash'
| 'exit'
| 'quit'
| 'restart'
| 'config:update'
| 'log'
| 'readable'
| 'stdout'
| 'stderr';

type NodemonEventListener = {
on(event: 'start' | 'crash' | 'readable', listener: () => void): Nodemon;
on(event: 'log', listener: (e: NodemonEventLog) => void): Nodemon;
on(event: 'stdout' | 'stderr', listener: (e: string) => void): Nodemon;
on(event: 'restart', listener: (e?: NodemonEventRestart) => void): Nodemon;
on(event: 'quit', listener: (e?: NodemonEventQuit) => void): Nodemon;
on(event: 'exit', listener: (e?: NodemonEventExit) => void): Nodemon;
on(
event: 'config:update',
listener: (e?: NodemonEventConfig) => void
): Nodemon;
};

type Nodemon = {
(options?: NodemonSettings): Nodemon;
on(event: 'start' | 'crash', listener: () => void): Nodemon;
on(event: 'log', listener: (e: NodemonEventLog) => void): Nodemon;
on(event: 'restart', listener: (e?: NodemonEventRestart) => void): Nodemon;
on(event: 'quit', listener: (e?: NodemonEventQuit) => void): Nodemon;
on(event: 'exit', listener: (e?: NodemonEventExit) => void): Nodemon;
on(
event: 'config:update',
listener: (e?: NodemonEventConfig) => void
): Nodemon;

// this is repeated because VS Code doesn't autocomplete otherwise
addEventListener(event: 'start' | 'crash', listener: () => void): Nodemon;
addEventListener(
event: 'log',
listener: (e: NodemonEventLog) => void
): Nodemon;
addEventListener(
event: 'restart',
listener: (e?: NodemonEventRestart) => void
): Nodemon;
addEventListener(
event: 'quit',
listener: (e?: NodemonEventQuit) => void
): Nodemon;
addEventListener(
event: 'exit',
listener: (e?: NodemonEventExit) => void
): Nodemon;
addEventListener(
event: 'config:update',
listener: (e?: NodemonEventConfig) => void
): Nodemon;

once(event: 'start' | 'crash', listener: () => void): Nodemon;
once(event: 'log', listener: (e: NodemonEventLog) => void): Nodemon;
once(event: 'restart', listener: (e?: NodemonEventRestart) => void): Nodemon;
once(event: 'quit', listener: (e?: NodemonEventQuit) => void): Nodemon;
once(event: 'exit', listener: (e?: NodemonEventExit) => void): Nodemon;
once(
event: 'config:update',
listener: (e?: NodemonEventConfig) => void
): Nodemon;

removeAllListeners(event: NodemonEventHandler): Nodemon;
emit(type: NodemonEventHandler, event?: any): Nodemon;
reset(callback: Function): Nodemon;
restart(): Nodemon;
config: NodemonSettings;
};

type NodemonEventLog = {
/**
detail*: what you get with nodemon --verbose.
status: subprocess starting, restarting.
fail: is the subprocess crashing.
error: is a nodemon system error.
*/
type: 'detail' | 'log' | 'status' | 'error' | 'fail';
/** the plain text message */
message: String;
/** contains the terminal escape codes to add colour, plus the "[nodemon]" prefix */
colour: String;
};

interface NodemonEventRestart {
matched?: {
result: string[];
total: number;
};
}

type NodemonEventQuit = 143 | 130;
type NodemonEventExit = number;

// TODO: Define the type of NodemonEventConfig
type NodemonEventConfig = any;

interface NodemonSettings {
/* restartable defaults to "rs" as a string the user enters */
restartable?: false | String;
colours?: Boolean;
execMap?: { [key: string]: string };
ignoreRoot?: string[];
watch?: string[];
stdin?: boolean;
runOnChangeOnly?: boolean;
verbose?: boolean;
signal?: string;
stdout?: boolean;
watchOptions?: WatchOptions;
}

interface WatchOptions {
ignorePermissionErrors: boolean;
ignored: string;
persistent: boolean;
usePolling: boolean;
interval: number;
}
7 changes: 7 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"typeRoots": ["./index.d.ts", "./node_modules/@types"],
"checkJs": true
},
"exclude": ["node_modules"]
}
4 changes: 4 additions & 0 deletions lib/nodemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var eventHandlers = {};
// stable module API
config.required = utils.isRequired;

/**
* @param {NodemonSettings} settings
* @returns {Nodemon}
*/
function nodemon(settings) {
bus.emit('boot');
nodemon.reset();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"terminal"
],
"license": "MIT",
"types": "./index.d.ts",
"main": "./lib/nodemon",
"scripts": {
"commitmsg": "commitlint -e",
Expand Down

0 comments on commit cd27c0b

Please # to comment.