Skip to content

Commit cb91187

Browse files
committed
fix: types updated
Created by @zAlweNy26 PR: #2232 Modified commit to allow for squash and conventional commit
1 parent a021683 commit cb91187

File tree

7 files changed

+89
-102
lines changed

7 files changed

+89
-102
lines changed

index.d.ts

Lines changed: 59 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { WatchOptions } from 'chokidar'
2+
13
export type NodemonEventHandler =
24
| 'start'
35
| 'crash'
@@ -16,77 +18,34 @@ export type NodemonEventListener = {
1618
on(event: 'stdout' | 'stderr', listener: (e: string) => void): Nodemon;
1719
on(event: 'restart', listener: (e?: NodemonEventRestart) => void): Nodemon;
1820
on(event: 'quit', listener: (e?: NodemonEventQuit) => void): Nodemon;
19-
on(event: 'exit', listener: (e?: NodemonEventExit) => void): Nodemon;
20-
on(
21-
event: 'config:update',
22-
listener: (e?: NodemonEventConfig) => void
23-
): Nodemon;
21+
on(event: 'exit', listener: (e?: number) => void): Nodemon;
22+
on(event: 'config:update', listener: (e?: NodemonEventConfig) => void): Nodemon;
2423
};
2524

2625
export type Nodemon = {
27-
(options?: NodemonSettings): Nodemon;
28-
on(event: 'start' | 'crash', listener: () => void): Nodemon;
29-
on(event: 'log', listener: (e: NodemonEventLog) => void): Nodemon;
30-
on(event: 'restart', listener: (e?: NodemonEventRestart) => void): Nodemon;
31-
on(event: 'quit', listener: (e?: NodemonEventQuit) => void): Nodemon;
32-
on(event: 'exit', listener: (e?: NodemonEventExit) => void): Nodemon;
33-
on(
34-
event: 'config:update',
35-
listener: (e?: NodemonEventConfig) => void
36-
): Nodemon;
37-
38-
// this is repeated because VS Code doesn't autocomplete otherwise
39-
addEventListener(event: 'start' | 'crash', listener: () => void): Nodemon;
40-
addEventListener(
41-
event: 'log',
42-
listener: (e: NodemonEventLog) => void
43-
): Nodemon;
44-
addEventListener(
45-
event: 'restart',
46-
listener: (e?: NodemonEventRestart) => void
47-
): Nodemon;
48-
addEventListener(
49-
event: 'quit',
50-
listener: (e?: NodemonEventQuit) => void
51-
): Nodemon;
52-
addEventListener(
53-
event: 'exit',
54-
listener: (e?: NodemonEventExit) => void
55-
): Nodemon;
56-
addEventListener(
57-
event: 'config:update',
58-
listener: (e?: NodemonEventConfig) => void
59-
): Nodemon;
60-
61-
once(event: 'start' | 'crash', listener: () => void): Nodemon;
62-
once(event: 'log', listener: (e: NodemonEventLog) => void): Nodemon;
63-
once(event: 'restart', listener: (e?: NodemonEventRestart) => void): Nodemon;
64-
once(event: 'quit', listener: (e?: NodemonEventQuit) => void): Nodemon;
65-
once(event: 'exit', listener: (e?: NodemonEventExit) => void): Nodemon;
66-
once(
67-
event: 'config:update',
68-
listener: (e?: NodemonEventConfig) => void
69-
): Nodemon;
70-
7126
removeAllListeners(event: NodemonEventHandler): Nodemon;
7227
emit(type: NodemonEventHandler, event?: any): Nodemon;
7328
reset(callback: Function): Nodemon;
7429
restart(): Nodemon;
7530
config: NodemonSettings;
31+
} & NodemonEventListener & {
32+
[K in keyof NodemonEventListener as "addListener"]: NodemonEventListener[K];
33+
} & {
34+
[K in keyof NodemonEventListener as "once"]: NodemonEventListener[K];
7635
};
7736

7837
export type NodemonEventLog = {
7938
/**
80-
detail*: what you get with nodemon --verbose.
81-
status: subprocess starting, restarting.
82-
fail: is the subprocess crashing.
83-
error: is a nodemon system error.
39+
- detail: what you get with nodemon --verbose.
40+
- status: subprocess starting, restarting.
41+
- fail: is the subprocess crashing.
42+
- error: is a nodemon system error.
8443
*/
8544
type: 'detail' | 'log' | 'status' | 'error' | 'fail';
8645
/** the plain text message */
87-
message: String;
46+
message: string;
8847
/** contains the terminal escape codes to add colour, plus the "[nodemon]" prefix */
89-
colour: String;
48+
colour: string;
9049
};
9150

9251
export interface NodemonEventRestart {
@@ -97,15 +56,36 @@ export interface NodemonEventRestart {
9756
}
9857

9958
export type NodemonEventQuit = 143 | 130;
100-
export type NodemonEventExit = number;
10159

102-
// TODO: Define the type of NodemonEventConfig
103-
export type NodemonEventConfig = any;
60+
export type NodemonEventConfig = {
61+
run: boolean;
62+
system: {
63+
cwd: string;
64+
};
65+
required: boolean;
66+
dirs: string[];
67+
timeout: number;
68+
options: NodemonConfig;
69+
lastStarted: number
70+
loaded: string[]
71+
load: (settings: NodemonSettings, ready: (config: NodemonEventConfig) => void) => void
72+
reset: () => void
73+
};
74+
75+
export interface NodemonExecOptions {
76+
script: string;
77+
scriptPosition?: number;
78+
args?: string[]
79+
ext?: string; // "js,mjs" etc (should really support an array of strings, but I don't think it does right now)
80+
exec?: string; // node, python, etc
81+
execArgs?: string[]; // args passed to node, etc,
82+
nodeArgs?: string[]; // args passed to node, etc,
83+
}
10484

10585
export interface NodemonConfig {
106-
/* restartable defaults to "rs" as a string the user enters */
107-
restartable?: false | String;
108-
colours?: Boolean;
86+
/** restartable defaults to "rs" as a string the user enters */
87+
restartable?: false | string;
88+
colours?: boolean;
10989
execMap?: { [key: string]: string };
11090
ignoreRoot?: string[];
11191
watch?: string[];
@@ -116,25 +96,28 @@ export interface NodemonConfig {
11696
signal?: string;
11797
stdout?: boolean;
11898
watchOptions?: WatchOptions;
99+
help?: string
100+
version?: boolean
101+
cwd?: string
102+
dump?: boolean
103+
ignore?: string[]
104+
watch?: string[]
105+
monitor?: string[]
106+
spawn?: boolean
107+
noUpdateNotifier?: boolean
108+
legacyWatch?: boolean
109+
pollingInterval?: number
110+
/** @deprecated as this is "on" by default */
111+
js?: boolean
112+
quiet?: boolean
113+
configFile?: string
114+
exitCrash?: boolean
115+
execOptions?: NodemonExecOptions
119116
}
120117

121-
export interface NodemonSettings extends NodemonConfig {
122-
script: string;
123-
ext?: string; // "js,mjs" etc (should really support an array of strings, but I don't think it does right now)
118+
export interface NodemonSettings extends NodemonConfig, NodemonExecOptions {
124119
events?: { [key: string]: string };
125120
env?: { [key: string]: string };
126-
exec?: string; // node, python, etc
127-
execArgs?: string[]; // args passed to node, etc,
128-
nodeArgs?: string[]; // args passed to node, etc,
129-
delay?: number;
130-
}
131-
132-
export interface WatchOptions {
133-
ignorePermissionErrors: boolean;
134-
ignored: string;
135-
persistent: boolean;
136-
usePolling: boolean;
137-
interval: number;
138121
}
139122

140123
const nodemon: Nodemon = (settings: NodemonSettings): Nodemon => {};

lib/cli/parse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = parse;
2525
* Parses the command line arguments `process.argv` and returns the
2626
* nodemon options, the user script and the executable script.
2727
*
28-
* @param {Array} full process arguments, including `node` leading arg
28+
* @param {Array<string> | string} argv full process arguments, including `node` leading arg
2929
* @return {Object} { options, script, args }
3030
*/
3131
function parse(argv) {
@@ -97,9 +97,9 @@ function parse(argv) {
9797
* Given an argument (ie. from process.argv), sets nodemon
9898
* options and can eat up the argument value
9999
*
100-
* @param {Object} options object that will be updated
101-
* @param {Sting} current argument from argv
102-
* @param {Function} the callback to eat up the next argument in argv
100+
* @param {import('../..').NodemonSettings} options object that will be updated
101+
* @param {String} arg current argument from argv
102+
* @param {Function} eatNext the callback to eat up the next argument in argv
103103
* @return {Boolean} false if argument was not a nodemon arg
104104
*/
105105
function nodemonOption(options, arg, eatNext) {
@@ -161,7 +161,7 @@ function nodemonOption(options, arg, eatNext) {
161161
} else
162162

163163
if (arg === '--exitcrash') {
164-
options.exitcrash = true;
164+
options.exitCrash = true;
165165
} else
166166

167167
if (arg === '--delay' || arg === '-d') {
@@ -210,7 +210,7 @@ function nodemonOption(options, arg, eatNext) {
210210
* Given an argument (ie. from nodemonOption()), will parse and return the
211211
* equivalent millisecond value or 0 if the argument cannot be parsed
212212
*
213-
* @param {String} argument value given to the --delay option
213+
* @param {String} value argument value given to the --delay option
214214
* @return {Number} millisecond equivalent of the argument
215215
*/
216216
function parseDelay(value) {

lib/config/load.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ function findAppScript() {
3030
* the local nodemon.json to the exec and then overwriting using any user
3131
* specified settings (i.e. from the cli)
3232
*
33-
* @param {Object} settings user defined settings
34-
* @param {Function} ready callback that receives complete config
33+
* @param {Object} settings user defined settings
34+
* @param {Object} options global options
35+
* @param {Object} config the config object to be updated
36+
* @param {Function} callback that receives complete config
3537
*/
3638
function load(settings, options, config, callback) {
3739
config.loaded = [];

lib/monitor/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ function run(options) {
249249
}
250250
} else {
251251
bus.emit('crash');
252-
if (options.exitcrash) {
252+
if (options.exitCrash) {
253253
utils.log.fail('app crashed');
254254
if (!config.required) {
255255
process.exit(1);

lib/nodemon.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var util = require('util');
77
var utils = require('./utils');
88
var bus = utils.bus;
99
var help = require('./help');
10+
/** @type {import('..').NodemonEventConfig} */
1011
var config = require('./config');
1112
var spawn = require('./spawn');
1213
const defaults = require('./config/defaults')
@@ -17,12 +18,15 @@ var eventHandlers = {};
1718
config.required = utils.isRequired;
1819

1920
/**
20-
* @param {NodemonSettings} settings
21-
* @returns {Nodemon}
21+
* @param {import('..').NodemonSettings | string} settings
22+
* @returns {import('..').Nodemon}
2223
*/
2324
function nodemon(settings) {
2425
bus.emit('boot');
2526
nodemon.reset();
27+
28+
/** @type {import('..').NodemonSettings} */
29+
let options
2630

2731
// allow the cli string as the argument to nodemon, and allow for
2832
// `node nodemon -V app.js` or just `-V app.js`
@@ -34,25 +38,25 @@ function nodemon(settings) {
3438
}
3539
settings = 'node ' + settings;
3640
}
37-
settings = cli.parse(settings);
38-
}
41+
options = cli.parse(settings);
42+
} else options = settings;
3943

4044
// set the debug flag as early as possible to get all the detailed logging
41-
if (settings.verbose) {
45+
if (options.verbose) {
4246
utils.debug = true;
4347
}
4448

45-
if (settings.help) {
49+
if (options.help) {
4650
if (process.stdout.isTTY) {
4751
process.stdout._handle.setBlocking(true); // nodejs/node#6456
4852
}
49-
console.log(help(settings.help));
53+
console.log(help(options.help));
5054
if (!config.required) {
5155
process.exit(0);
5256
}
5357
}
5458

55-
if (settings.version) {
59+
if (options.version) {
5660
version().then(function (v) {
5761
console.log(v);
5862
if (!config.required) {
@@ -65,17 +69,15 @@ function nodemon(settings) {
6569
// nodemon tools like grunt-nodemon. This affects where
6670
// the script is being run from, and will affect where
6771
// nodemon looks for the nodemon.json files
68-
if (settings.cwd) {
72+
if (options.cwd) {
6973
// this is protection to make sure we haven't dont the chdir already...
7074
// say like in cli/parse.js (which is where we do this once already!)
71-
if (process.cwd() !== path.resolve(config.system.cwd, settings.cwd)) {
72-
process.chdir(settings.cwd);
75+
if (process.cwd() !== path.resolve(config.system.cwd, options.cwd)) {
76+
process.chdir(options.cwd);
7377
}
7478
}
7579

76-
const cwd = process.cwd();
77-
78-
config.load(settings, function (config) {
80+
config.load(options, function (config) {
7981
if (!config.options.dump && !config.options.execOptions.script &&
8082
config.options.execOptions.exec === 'node') {
8183
if (!config.required) {
@@ -175,7 +177,7 @@ function nodemon(settings) {
175177
// don't notify of default ignores
176178
if (defaults.ignoreRoot.indexOf(rule) !== -1) {
177179
return false;
178-
return rule.slice(3).slice(0, -3);
180+
// return rule.slice(3).slice(0, -3);
179181
}
180182

181183
if (rule.startsWith(cwd)) {

lib/rules/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = add;
2828
* @param {Object} rules containing `watch` and `ignore`. Also updated during
2929
* execution
3030
* @param {String} which must be either "watch" or "ignore"
31-
* @param {String|RegExp} the actual rule.
31+
* @param {String|RegExp} rule the actual rule.
3232
*/
3333
function add(rules, which, rule) {
3434
if (!{ ignore: 1, watch: 1}[which]) {

test/cli/parse.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('nodemon argument parser', function () {
251251
assert(settings.exec === 'java', 'exec');
252252
assert(settings.quiet, 'quiet');
253253
assert(settings.stdin === false, 'read stdin');
254-
assert(settings.exitcrash, 'exit if crash');
254+
assert(settings.exitCrash, 'exit if crash');
255255
assert(settings.watch[0] === 'fixtures', 'watch');
256256
assert(settings.ignore[0] === 'fixtures', 'ignore');
257257
assert(settings.delay === 5000, 'delay 5 seconds');

0 commit comments

Comments
 (0)