Skip to content

Commit 2c397dc

Browse files
committed
feat: make entries list accept entry name too
1 parent 98f6ac4 commit 2c397dc

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

e2e/wpackio.project.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/extensions */
12
const pkg = require('./package.json');
23

34
module.exports = {

packages/scripts/src/bin/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { bootstrap } from './bootstrap';
1010
import { build } from './build';
1111
import { pack } from './pack';
1212
import { serve } from './serve';
13-
import { bulletSymbol, contextHelp, printIntro } from './utils';
13+
import { bulletSymbol, contextHelp, entriesHelp, printIntro } from './utils';
1414

1515
dotenv.config();
1616

@@ -80,10 +80,7 @@ program
8080
'-s, --server-config <path>',
8181
'Path to server config. If it differs from ./wpackio.server.js'
8282
)
83-
.option(
84-
'-e, --entries <numbers...>',
85-
'Select entries from wpackio.project.js for which we start the server.'
86-
)
83+
.option('-e, --entries <entries...>', entriesHelp)
8784
.action((options: ProgramOptions | undefined) => {
8885
isValidCommand = true;
8986
serve(options);

packages/scripts/src/bin/serve.ts

+46-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-lonely-if */
12
/* eslint-disable @typescript-eslint/no-var-requires */
23
import chalk from 'chalk';
34
import logSymbols from 'log-symbols';
@@ -72,13 +73,51 @@ export function serve(options: ProgramOptions | undefined): void {
7273
let entries: number[] = [];
7374

7475
if (options?.entries) {
75-
entries = options.entries.map(e => {
76-
let entry = Number.parseInt(e, 10);
77-
if (Number.isNaN(entry)) {
78-
entry = 0;
79-
}
80-
return entry;
81-
});
76+
console.log(
77+
`${logSymbols.info} ${chalk.bold('cli')}: ${chalk.cyan(
78+
`starting with selective ${options.entries.length} ${
79+
options.entries.length === 1 ? 'entry' : 'entries'
80+
}`
81+
)}`
82+
);
83+
// make sure to remove duplicates from the entries before looping
84+
entries = [...new Set(options.entries)]
85+
.map(e => {
86+
let entry = Number.parseInt(e, 10);
87+
if (Number.isNaN(entry)) {
88+
entry = projectConfig.files.findIndex(file => file.name === e);
89+
if (entry === -1) {
90+
console.log(
91+
`${logSymbols.error} ${chalk.bold('cli')}: ${chalk.red(
92+
`no entry found for `
93+
)}${chalk.bold(chalk.yellow(e))} ${chalk.bold('skipping...')}`
94+
);
95+
return false;
96+
}
97+
console.log(
98+
`${logSymbols.success} ${chalk.bold('cli')}: ${chalk.green(
99+
`entry found for `
100+
)}${chalk.bold(chalk.yellow(e))}`
101+
);
102+
} else {
103+
if (projectConfig.files[entry]) {
104+
console.log(
105+
`${logSymbols.success} ${chalk.bold('cli')}: ${chalk.green(
106+
`entry found for ${chalk.bold(e)} is`
107+
)} ${chalk.bold(chalk.yellow(projectConfig.files[entry].name))}`
108+
);
109+
} else {
110+
console.log(
111+
`${logSymbols.error} ${chalk.bold('cli')}: ${chalk.red(
112+
`no entry found for `
113+
)}${chalk.bold(chalk.yellow(e))} ${chalk.bold('skipping...')}`
114+
);
115+
return false;
116+
}
117+
}
118+
return entry;
119+
})
120+
.filter(entry => typeof entry === 'number') as number[];
82121
} else if (projectConfig.files.length > 1) {
83122
serveEntryInfo();
84123
}

packages/scripts/src/bin/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ export function isYarn(): boolean {
233233
}
234234

235235
export const contextHelp: string = `Path to context or project root directory. Defaults to current working directory. It is recommended to use absolute path, else it is calculated from current working directory. The path you mention here should be what the URL 'localhost/wp-content/<themes|plugins>/<slug>/' map to. In most cases, you should leave it, because calling the program from npm or yarn script should automatically set it.`;
236+
export const entriesHelp: string = `Select entries from wpackio.project.js file for which we start the server. Either 0 based index of the entry, like \`-e 0 2\` will start the 0th and 2nd entry of wpackio project. You can also supply the name of the entries, like \`-e app admin\`. The tool will search wpackio for entries with name set to app and admin. If found, it will start them.`;
236237
export function printIntro(): void {
237238
console.log(wpackIntro);
238239
}

0 commit comments

Comments
 (0)