Skip to content

Commit

Permalink
feat: make entries list accept entry name too
Browse files Browse the repository at this point in the history
  • Loading branch information
swashata committed Jul 27, 2021
1 parent 98f6ac4 commit 2c397dc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions e2e/wpackio.project.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/extensions */
const pkg = require('./package.json');

module.exports = {
Expand Down
7 changes: 2 additions & 5 deletions packages/scripts/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { bootstrap } from './bootstrap';
import { build } from './build';
import { pack } from './pack';
import { serve } from './serve';
import { bulletSymbol, contextHelp, printIntro } from './utils';
import { bulletSymbol, contextHelp, entriesHelp, printIntro } from './utils';

dotenv.config();

Expand Down Expand Up @@ -80,10 +80,7 @@ program
'-s, --server-config <path>',
'Path to server config. If it differs from ./wpackio.server.js'
)
.option(
'-e, --entries <numbers...>',
'Select entries from wpackio.project.js for which we start the server.'
)
.option('-e, --entries <entries...>', entriesHelp)
.action((options: ProgramOptions | undefined) => {
isValidCommand = true;
serve(options);
Expand Down
53 changes: 46 additions & 7 deletions packages/scripts/src/bin/serve.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-lonely-if */
/* eslint-disable @typescript-eslint/no-var-requires */
import chalk from 'chalk';
import logSymbols from 'log-symbols';
Expand Down Expand Up @@ -72,13 +73,51 @@ export function serve(options: ProgramOptions | undefined): void {
let entries: number[] = [];

if (options?.entries) {
entries = options.entries.map(e => {
let entry = Number.parseInt(e, 10);
if (Number.isNaN(entry)) {
entry = 0;
}
return entry;
});
console.log(
`${logSymbols.info} ${chalk.bold('cli')}: ${chalk.cyan(
`starting with selective ${options.entries.length} ${
options.entries.length === 1 ? 'entry' : 'entries'
}`
)}`
);
// make sure to remove duplicates from the entries before looping
entries = [...new Set(options.entries)]
.map(e => {
let entry = Number.parseInt(e, 10);
if (Number.isNaN(entry)) {
entry = projectConfig.files.findIndex(file => file.name === e);
if (entry === -1) {
console.log(
`${logSymbols.error} ${chalk.bold('cli')}: ${chalk.red(
`no entry found for `
)}${chalk.bold(chalk.yellow(e))} ${chalk.bold('skipping...')}`
);
return false;
}
console.log(
`${logSymbols.success} ${chalk.bold('cli')}: ${chalk.green(
`entry found for `
)}${chalk.bold(chalk.yellow(e))}`
);
} else {
if (projectConfig.files[entry]) {
console.log(
`${logSymbols.success} ${chalk.bold('cli')}: ${chalk.green(
`entry found for ${chalk.bold(e)} is`
)} ${chalk.bold(chalk.yellow(projectConfig.files[entry].name))}`
);
} else {
console.log(
`${logSymbols.error} ${chalk.bold('cli')}: ${chalk.red(
`no entry found for `
)}${chalk.bold(chalk.yellow(e))} ${chalk.bold('skipping...')}`
);
return false;
}
}
return entry;
})
.filter(entry => typeof entry === 'number') as number[];
} else if (projectConfig.files.length > 1) {
serveEntryInfo();
}
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/src/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export function isYarn(): boolean {
}

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.`;
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.`;
export function printIntro(): void {
console.log(wpackIntro);
}
Expand Down

0 comments on commit 2c397dc

Please sign in to comment.