Skip to content

Commit 4770a97

Browse files
committed
fix ts errors in launchpad package
1 parent 94b8ae6 commit 4770a97

File tree

8 files changed

+78
-43
lines changed

8 files changed

+78
-43
lines changed

packages/dashboard/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"type": "module",
66
"main": "index.js",
7-
"types": "types/index.d.ts",
7+
"types": "dist/index.d.ts",
88
"bin": "index.js",
99

1010
"scripts": {

packages/launchpad/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export class StartupCommands {
2121
static SCAFFOLD = 'scaffold';
2222
}
2323

24+
/**
25+
* @param {import('yargs').Argv} argv
26+
* @param {string|string[]} commands
27+
* @param {string} description
28+
*/
2429
const addCommand = (argv, commands, description) => {
2530
if (!Array.isArray(commands)) {
2631
commands = [commands];

packages/launchpad/lib/command-center.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from 'chalk';
22
import autoBind from 'auto-bind';
33
import { LogManager, Logger, TaskQueue, TaskQueueOptions, execScript } from '@bluecadet/launchpad-utils';
4-
import CommandHooks from './command-hooks.js';
4+
import CommandHooks, { ExecHook } from './command-hooks.js';
55

66
export class CommandOptions {
77
constructor({
@@ -16,23 +16,23 @@ export class CommandOptions {
1616

1717
export class CommandCenter {
1818
/** @type {CommandOptions} */
19-
_config = {};
19+
_config;
2020

2121
/** @type {Logger} */
22-
_logger = null;
22+
_logger;
2323

2424
/** @type {Map<string, Command>} */
2525
_commands = new Map();
2626

2727
/** @type {TaskQueue} */
28-
_tasks = null;
28+
_tasks;
2929

3030
/**
3131
*
32-
* @param {CommandOptions|Object} config
33-
* @param {*} logger
32+
* @param {CommandOptions|Object} [config]
33+
* @param {Logger} [logger]
3434
*/
35-
constructor(config = null, logger = null) {
35+
constructor(config, logger) {
3636
autoBind(this);
3737
this._config = new CommandOptions(config);
3838
this._logger = logger || LogManager.getInstance().getLogger();
@@ -54,15 +54,17 @@ export class CommandCenter {
5454
/**
5555
*
5656
* @param {string} commandName The name of the command to run
57-
* @returns {Promise} The promise returned by the command function
57+
* @param {unknown[]} args
58+
* @returns {Promise<void | null>} The promise returned by the command function
5859
*/
5960
async run(commandName, ...args) {
60-
if (!this._commands.has(commandName)) {
61+
const command = this._commands.get(commandName);
62+
63+
if (!command) {
6164
return Promise.reject(new Error(`Command not found: '${commandName}'`));
6265
}
6366

6467
this._logger.info(`Running command: ${chalk.blue(commandName)}`);
65-
const command = this._commands.get(commandName);
6668

6769
if (command.queued) {
6870
return this._tasks.add(command.run, command.name, ...args);
@@ -72,22 +74,27 @@ export class CommandCenter {
7274
}
7375

7476
/**
75-
* @param {CommandHooks} hooks
77+
* @param {CommandHooks} commandHooks
7678
*/
7779
addCommandHooks(commandHooks) {
80+
/**
81+
*
82+
* @param {ExecHook[]} hooks
83+
* @param {boolean} isPre
84+
*/
7885
const add = (hooks, isPre) => {
7986
const label = isPre ? 'pre' : 'post';
8087
for (const hook of hooks) {
8188
try {
8289
this._logger.info(`Adding ${label}-hook for ${chalk.blue(hook.command)}: '${hook.script}'`);
8390
const fn = async () => {
8491
this._logger.info(`Running ${chalk.magenta(`${label}-hook`)} for ${chalk.blue(hook.command)}: '${hook.script}'`);
85-
return execScript(hook.script, null, this._logger);
92+
await execScript(hook.script, undefined, this._logger);
8693
};
8794
this.addHook(hook.command, isPre ? fn : null, isPre ? null : fn);
8895
} catch (err) {
8996
this._logger.error(`Can't add ${label}-hook '${hook.script}' for '${chalk.blue(hook.command)}':`, err);
90-
execScript(hook.command, null, this._logger);
97+
execScript(hook.command, undefined, this._logger);
9198
}
9299
}
93100
};
@@ -98,14 +105,16 @@ export class CommandCenter {
98105
/**
99106
*
100107
* @param {string} commandName
101-
* @param {function():Promise} before Called before the command is run
102-
* @param {function():Promise} after Called after the command was run
108+
* @param {(() => Promise<void>)?} before Called before the command is run
109+
* @param {(() => Promise<void>)?} after Called after the command was run
103110
*/
104111
addHook(commandName, before = null, after = null) {
105-
if (!this._commands.has(commandName)) {
112+
const command = this._commands.get(commandName);
113+
114+
if (!command) {
106115
throw new Error(`Command not found: '${commandName}'`);
107116
}
108-
const command = this._commands.get(commandName);
117+
109118
if (before) {
110119
command.preHooks.push(before);
111120
this._logger.debug(`Added pre-hook for: ${chalk.blue(commandName)}`);
@@ -117,14 +126,25 @@ export class CommandCenter {
117126
}
118127
}
119128

129+
/**
130+
* @template {(...args: any[]) => Promise<unknown>} [F=(...args: any[]) => Promise<void>]
131+
*/
120132
export class Command {
133+
/**
134+
* @param {object} options
135+
* @param {string} options.name
136+
* @param {F} options.callback
137+
* @param {string} [options.help]
138+
* @param {boolean} [options.queued]
139+
* @param {Logger | null} [options.logger]
140+
*/
121141
constructor({
122142
name,
123143
callback,
124144
help = '',
125145
queued = true,
126146
logger = null
127-
} = {}) {
147+
}) {
128148
autoBind(this);
129149
/**
130150
* The name of the command used for running.
@@ -133,7 +153,7 @@ export class Command {
133153
this.name = name;
134154
/**
135155
* The callback to trigger. Will receives args passed via run().
136-
* @type {function():Promise}
156+
* @type {F}
137157
*/
138158
this.callback = callback;
139159
/**
@@ -148,24 +168,24 @@ export class Command {
148168
this.queued = queued;
149169
/**
150170
* Callbacks that will be triggered before the command is executed.
151-
* @type {Array<function():Promise>}
171+
* @type {Array<(...args: Parameters<F>)=>Promise<void>>}
152172
*/
153173
this.preHooks = [];
154174
/**
155175
* Callbacks that will be triggered after the command was executed.
156-
* @type {Array<function():Promise>}
176+
* @type {Array<(...args: Parameters<F>)=>Promise<void>>}
157177
*/
158178
this.postHooks = [];
159179
/**
160-
* @type {Logger}
180+
* @type {Logger | null}
161181
*/
162182
this.logger = logger;
163183
}
164184

165185
/**
166186
* Runs a command with optional pre- and post-command hooks.
167-
* @param {...any} args
168-
* @returns {*} Results of this command's callback function, if any
187+
* @param {Parameters<F>} args
188+
* @returns {Promise<ReturnType<F> | null>} Results of this command's callback function, if any
169189
*/
170190
async run(...args) {
171191
const logger = this.logger || console;
@@ -176,8 +196,12 @@ export class Command {
176196
logger.error(`Could not run pre-hook for command ${chalk.blue(this.name)}:`, err);
177197
}
178198
}
199+
/**
200+
* @type {ReturnType<F> | null}
201+
*/
179202
let result = null;
180203
try {
204+
// @ts-expect-error - no clue why TS is complaining here...
181205
result = await this.callback(...args);
182206
} catch (err) {
183207
logger.error(`Could not run command ${chalk.blue(this.name)}:`, err);

packages/launchpad/lib/command-hooks.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
export class CommandHooks {
1515
/**
16-
* @param {Array<HookMapping>} hooks
16+
* @param {Record<string, HookMapping>} hooks
1717
*/
1818
constructor(hooks = {}) {
1919
/**
@@ -33,7 +33,7 @@ export class CommandHooks {
3333
}
3434

3535
/**
36-
* @param {Object<string, HookMapping>} hooks
36+
* @param {Record<string, HookMapping>} hooks
3737
*/
3838
parse(hooks = {}) {
3939
if (!hooks) {
@@ -43,11 +43,9 @@ export class CommandHooks {
4343
key = (key + '').toLowerCase();
4444
const command = key.replace('pre-', '').replace('post-', '');
4545

46-
if (!Array.isArray(scripts)) {
47-
scripts = [scripts];
48-
}
46+
const scriptArray = Array.isArray(scripts) ? scripts : [scripts];
4947

50-
for (const script of scripts) {
48+
for (const script of scriptArray) {
5149
if ((typeof script) !== 'string') {
5250
continue;
5351
}
@@ -63,10 +61,15 @@ export class CommandHooks {
6361
}
6462

6563
export class ExecHook {
64+
/**
65+
* @param {object} options
66+
* @param {string} options.command
67+
* @param {string} options.script
68+
*/
6669
constructor({
6770
command,
6871
script
69-
} = {}) {
72+
}) {
7073
/**
7174
* @type {string}
7275
*/

packages/launchpad/lib/launchpad-core.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ import CommandCenter, { Command } from './command-center.js';
1515
*/
1616
export class LaunchpadCore {
1717
/** @type {LaunchpadOptions} */
18-
_config = null;
18+
_config;
1919

2020
/** @type {Logger} */
21-
_logger = null;
21+
_logger;
2222

2323
/** @type {LaunchpadContent} */
24-
_content = null;
24+
_content;
2525

2626
/** @type {LaunchpadMonitor} */
27-
_monitor = null;
27+
_monitor;
2828

2929
/** @type {CommandCenter} */
30-
_commands = null;
30+
_commands;
3131

3232
/** @type {boolean} */
3333
_isShuttingDown = false;
@@ -73,10 +73,10 @@ export class LaunchpadCore {
7373
* Stops launchpad and exits this process.
7474
* This function is queued and waits until the queue is empty before it executes.
7575
*
76-
* @param @type {number|string|Error} eventOrExitCode
76+
* @param {number|string|Error} [eventOrExitCode]
7777
*/
7878
async shutdown(eventOrExitCode = undefined) {
79-
return this._commands.run('shutdown', eventOrExitCode);
79+
await this._commands.run('shutdown', eventOrExitCode);
8080
}
8181

8282
/**
@@ -116,7 +116,7 @@ export class LaunchpadCore {
116116
/**
117117
* @private
118118
*/
119-
async _runShutdown(eventOrExitCode = 0, ...args) {
119+
async _runShutdown(eventOrExitCode = 0) {
120120
try {
121121
this._logger.info('Launchpad exiting... 👋');
122122

packages/launchpad/lib/launchpad-options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { CommandHooks } from './command-hooks.js';
1212
* Combined options to initialize Launchpad.
1313
*/
1414
export class LaunchpadOptions {
15+
/**
16+
* @param {any} options
17+
*/
1518
constructor({
1619
content = new ContentOptions(),
1720
monitor = new MonitorOptions(),

packages/launchpad/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"type": "module",
1010
"main": "./index.js",
11-
"types": "types/index.d.ts",
11+
"types": "dist/index.d.ts",
1212
"bin": {
1313
"launchpad": "./index.js"
1414
},

packages/utils/lib/exec-script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { exec } from 'child_process';
33
/**
44
* Executes a scripts and pipes output and error to a logger
55
* @param {string} script
6-
* @param {string} cwd
7-
* @param {*} logger
6+
* @param {string} [cwd]
7+
* @param {import("./log-manager").Logger | Console} logger
88
* @returns {Promise<number | null>} A promise with the exit code passed on close
99
*/
1010
export const execScript = async (script, cwd, logger = console) => {

0 commit comments

Comments
 (0)