From 348dc50ba17dede3c06e07bbebc204bee4d8df6c Mon Sep 17 00:00:00 2001 From: Jrson <45284565+jrson83@users.noreply.github.com> Date: Mon, 17 Apr 2023 22:01:13 +0200 Subject: [PATCH 1/2] refactor: each command its own module --- src/commands/index.ts | 6 +++ src/commands/info.ts | 15 ++++++++ src/commands/init-cliff.ts | 13 +++++++ src/commands/init.ts | 13 +++++++ src/commands/log.ts | 13 +++++++ src/commands/publish.ts | 21 +++++++++++ src/commands/run.ts | 13 +++++++ src/index.ts | 77 ++++++++------------------------------ src/types/index.ts | 2 + test/cli.test.ts | 37 ++++++++++++++++++ test/helper/index.ts | 15 ++++++++ 11 files changed, 164 insertions(+), 61 deletions(-) create mode 100644 src/commands/index.ts create mode 100644 src/commands/info.ts create mode 100644 src/commands/init-cliff.ts create mode 100644 src/commands/init.ts create mode 100644 src/commands/log.ts create mode 100644 src/commands/publish.ts create mode 100644 src/commands/run.ts create mode 100644 test/cli.test.ts create mode 100644 test/helper/index.ts diff --git a/src/commands/index.ts b/src/commands/index.ts new file mode 100644 index 0000000..552854c --- /dev/null +++ b/src/commands/index.ts @@ -0,0 +1,6 @@ +export * from './info' +export * from './init-cliff' +export * from './init' +export * from './log' +export * from './publish' +export * from './run' diff --git a/src/commands/info.ts b/src/commands/info.ts new file mode 100644 index 0000000..f7259c1 --- /dev/null +++ b/src/commands/info.ts @@ -0,0 +1,15 @@ +import info from '../core/info' +import type { CommandOption } from '../types' +import type { CommandModule } from 'yargs' + +const InfoCommand: CommandModule = { + command: 'info', + describe: 'Prints information about the monorepo.', + builder: (args) => args.option('tree', { demand: false }).alias('t', 'tree'), + handler: async (argv) => { + const tree = argv.tree as CommandOption + await info(tree ?? tree) + }, +} + +export { InfoCommand } diff --git a/src/commands/init-cliff.ts b/src/commands/init-cliff.ts new file mode 100644 index 0000000..e9d2165 --- /dev/null +++ b/src/commands/init-cliff.ts @@ -0,0 +1,13 @@ +import initCliff from '../core/init-cliff' +import type { CommandModule } from 'yargs' + +const InitCliffCommand: CommandModule = { + command: 'init-cliff', + describe: 'Gererate config file of CHANGELOG.md.', + builder: {}, + handler: async (_argv) => { + await initCliff() + }, +} + +export { InitCliffCommand } diff --git a/src/commands/init.ts b/src/commands/init.ts new file mode 100644 index 0000000..a5119df --- /dev/null +++ b/src/commands/init.ts @@ -0,0 +1,13 @@ +import init from '../core/init' +import type { CommandModule } from 'yargs' + +const InitCommand: CommandModule = { + command: 'init', + describe: 'Create a new monorepo or upgrade an existing repo to monorepo.', + builder: {}, + handler: async (_argv) => { + await init() + }, +} + +export { InitCommand } diff --git a/src/commands/log.ts b/src/commands/log.ts new file mode 100644 index 0000000..b61e81b --- /dev/null +++ b/src/commands/log.ts @@ -0,0 +1,13 @@ +import log from '../core/log' +import type { CommandModule } from 'yargs' + +const LogCommand: CommandModule = { + command: 'log', + describe: 'Only generate changelog.', + builder: {}, + handler: async (_argv) => { + await log() + }, +} + +export { LogCommand } diff --git a/src/commands/publish.ts b/src/commands/publish.ts new file mode 100644 index 0000000..3589ea3 --- /dev/null +++ b/src/commands/publish.ts @@ -0,0 +1,21 @@ +import publish from '../core/publish' +import type { CommandOption } from '../types' +import type { CommandModule } from 'yargs' + +const PublishCommand: CommandModule = { + command: 'publish', + describe: 'Publish packages in the current project.', + builder: (args) => + args + .option('force', { demand: false }) + .alias('f', 'force') + .option('noPublish', { demand: false }) + .alias('np', 'noPublish'), + handler: async (argv) => { + const force = argv.force as CommandOption + const noPublish = argv.noPublish as CommandOption + await publish(force, noPublish) + }, +} + +export { PublishCommand } diff --git a/src/commands/run.ts b/src/commands/run.ts new file mode 100644 index 0000000..df4916e --- /dev/null +++ b/src/commands/run.ts @@ -0,0 +1,13 @@ +import run from '../core/run' +import type { CommandModule } from 'yargs' + +const RunCommand: CommandModule = { + command: 'run', + describe: 'Run the script of package.', + builder: {}, + handler: async (_argv) => { + await run() + }, +} + +export { RunCommand } diff --git a/src/index.ts b/src/index.ts index 477d135..3ffc985 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,72 +1,27 @@ #!/usr/bin/env node -import info from './core/info' -import init from './core/init' -import initCliff from './core/init-cliff' -import log from './core/log' -import publish from './core/publish' -import run from './core/run' import { hideBin } from 'yargs/helpers' import yargs from 'yargs/yargs' -type Option = boolean | undefined +import { InfoCommand } from './commands' +import { InitCommand } from './commands' +import { InitCliffCommand } from './commands' +import { LogCommand } from './commands' +import { PublishCommand } from './commands' +import { RunCommand } from './commands' yargs(hideBin(process.argv)) + .scriptName('sparkee') .usage('Usage: $0 [options]') - .command( - 'init', - 'Create a new monorepo or upgrade an existing repo to monorepo.', - () => {}, - () => init() - ) - .command( - 'init-cliff', - 'Gererate config file of CHANGELOG.md.', - () => {}, - () => initCliff() - ) - .command( - 'info', - 'Prints information about the monorepo.', - (args) => { - args.option('tree', { demand: false }).alias('t', 'tree') - }, - (argv) => { - const tree = argv.tree as Option - info(tree) - } - ) - .command( - 'run', - 'Run the script of package.', - () => {}, - (argv) => { - run() - } - ) - .command( - 'log', - 'Only generate changelog.', - () => {}, - (argv) => { - log() - } - ) - .command( - 'publish', - 'Publish packages in the current project.', - (args) => { - args.option('force', { demand: false }).alias('f', 'force') - args.option('noPublish', { demand: false }).alias('np', 'noPublish') - }, - (argv) => { - const force = argv.force as Option - const noPublish = argv.noPublish as Option - publish(force, noPublish) - } - ) + .command(InitCommand) + .command(InitCliffCommand) + .command(InfoCommand) + .command(RunCommand) + .command(LogCommand) + .command(PublishCommand) .demandCommand(1, 'A command is required. Pass --help to see all available commands and options.') .strict() + .help() + .version() .alias('h', 'help') - .alias('v', 'version') - .parse() + .alias('v', 'version').argv diff --git a/src/types/index.ts b/src/types/index.ts index a1d718e..5f8f709 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,5 @@ +export type CommandOption = boolean | undefined + export interface SparkeeConfig { $schema?: string /** diff --git a/test/cli.test.ts b/test/cli.test.ts new file mode 100644 index 0000000..5b41a0f --- /dev/null +++ b/test/cli.test.ts @@ -0,0 +1,37 @@ +import pkgJson from '../package.json' +import { InfoCommand } from '../src/commands' +import { initYargs } from './helper' +import { describe, expect, it } from 'vitest' +import type { Arguments } from 'yargs' + +describe('Commands: --help & --version', () => { + it('should print help message', async () => { + const parser = initYargs(InfoCommand) + + const output = await new Promise((resolve) => { + parser.parse('--help', (err: unknown, _argv: Arguments, output: string) => { + if (err) { + resolve(err) + } + resolve(output) + }) + }) + + expect(output).toContain('Show help') + }) + + it('should print version message', async () => { + const parser = initYargs(InfoCommand) + + const output = await new Promise((resolve) => { + parser.parse('--version', (err: unknown, _argv: Arguments, output: string) => { + if (err) { + resolve(err) + } + resolve(output) + }) + }) + + expect(output).toContain(pkgJson.version) + }) +}) diff --git a/test/helper/index.ts b/test/helper/index.ts new file mode 100644 index 0000000..6ec437b --- /dev/null +++ b/test/helper/index.ts @@ -0,0 +1,15 @@ +import yargs from 'yargs' +import type { CommandModule } from 'yargs' + +export const initYargs = (cmd: CommandModule) => { + return yargs([]) + .scriptName('sparkee') + .usage('Usage: $0 [options]') + .command(cmd) + .demandCommand(1, 'A command is required. Pass --help to see all available commands and options.') + .strict() + .help() + .version() + .alias('h', 'help') + .alias('v', 'version') +} From b9d427e99f4ceb90e871ff650dbd54d8a47c80d2 Mon Sep 17 00:00:00 2001 From: Jrson <45284565+jrson83@users.noreply.github.com> Date: Tue, 18 Apr 2023 01:25:54 +0200 Subject: [PATCH 2/2] fix: imports --- src/index.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3ffc985..d0ce022 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,9 @@ #!/usr/bin/env node +import { InfoCommand, InitCliffCommand, InitCommand, LogCommand, PublishCommand, RunCommand } from './commands' import { hideBin } from 'yargs/helpers' import yargs from 'yargs/yargs' -import { InfoCommand } from './commands' -import { InitCommand } from './commands' -import { InitCliffCommand } from './commands' -import { LogCommand } from './commands' -import { PublishCommand } from './commands' -import { RunCommand } from './commands' - yargs(hideBin(process.argv)) .scriptName('sparkee') .usage('Usage: $0 [options]')