Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
feat(cli): make default command dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
rudxde committed Mar 20, 2022
1 parent a1ba0a0 commit 886aa19
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
16 changes: 13 additions & 3 deletions packages/cli/src/index.ts
Expand Up @@ -2,8 +2,11 @@ import { getLogger, initLogger, loggerYargsOptions } from '@lerna-dockerize/logg
import { existsSync, promises } from 'fs';
import yargs, { CommandModule } from 'yargs';

export type { CommandModule } from 'yargs';

export async function cli(
commands: CommandModule<{}, any>[],
defaultCommand: string,
): Promise<void> {
let config = {};
const lernaConfigPath = 'lerna.json';
Expand All @@ -25,12 +28,19 @@ export async function cli(
});

for (const command of commands) {
builder = builder.command(command);
let yargsCommand: CommandModule<{}, any> = command;
if (command.command) {
if (typeof command.command === 'string' && command.command === defaultCommand) {
yargsCommand.command = [command.command, '$0'];
}
if (Array.isArray(command.command) && command.command[0] === defaultCommand) {
yargsCommand.command = [...command.command, '$0'];
}
}
builder = builder.command(yargsCommand);
}

builder
.demandCommand()
.argv;
}

export { CommandModule } from 'yargs';
9 changes: 6 additions & 3 deletions packages/generate/src/bin.ts
Expand Up @@ -3,9 +3,12 @@ import { getLogger } from '@lerna-dockerize/logger';
import { cli } from '@lerna-dockerize/cli';


cli([
generateCommand,
])
cli(
[
generateCommand,
],
<string>generateCommand.command,
)
.catch(err => {
getLogger().error(err);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/generate/src/index.ts
Expand Up @@ -2,7 +2,7 @@ import type { CommandModule } from '@lerna-dockerize/cli';
import { IGenerateArgs } from './args';

export const generateCommand: CommandModule<any, IGenerateArgs> = {
command: ['generate', '$0'],
command: 'generate',
describe: 'Generates the lerna-dockerize dockerfile',
builder: {
baseDockerfileName: {
Expand Down
9 changes: 6 additions & 3 deletions packages/init/src/bin.ts
Expand Up @@ -3,9 +3,12 @@ import { getLogger } from '@lerna-dockerize/logger';
import { cli } from '@lerna-dockerize/cli';


cli([
initCommand,
])
cli(
[
initCommand,
],
<string>initCommand.command,
)
.catch(err => {
getLogger().error(err);
process.exit(1);
Expand Down
11 changes: 7 additions & 4 deletions packages/lerna-dockerize/src/index.ts
Expand Up @@ -4,10 +4,13 @@ import { getLogger } from '@lerna-dockerize/logger';
import { cli } from '@lerna-dockerize/cli';


cli([
generateCommand,
initCommand,
])
cli(
[
generateCommand,
initCommand,
],
<string>generateCommand.command,
)
.catch(err => {
getLogger().error(err);
process.exit(1);
Expand Down

0 comments on commit 886aa19

Please sign in to comment.