Skip to content

Commit

Permalink
Merge pull request #12 from jrson83/feat/add-cli-tests
Browse files Browse the repository at this point in the history
refactor: each command its own module
  • Loading branch information
Geocld authored May 5, 2023
2 parents a3ac29a + b9d427e commit b7277a9
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 62 deletions.
6 changes: 6 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './info'
export * from './init-cliff'
export * from './init'
export * from './log'
export * from './publish'
export * from './run'
15 changes: 15 additions & 0 deletions src/commands/info.ts
Original file line number Diff line number Diff line change
@@ -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 }
13 changes: 13 additions & 0 deletions src/commands/init-cliff.ts
Original file line number Diff line number Diff line change
@@ -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 }
13 changes: 13 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -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 }
13 changes: 13 additions & 0 deletions src/commands/log.ts
Original file line number Diff line number Diff line change
@@ -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 }
21 changes: 21 additions & 0 deletions src/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -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 }
13 changes: 13 additions & 0 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -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 }
73 changes: 11 additions & 62 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,21 @@
#!/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 { InfoCommand, InitCliffCommand, InitCommand, LogCommand, PublishCommand, RunCommand } from './commands'
import { hideBin } from 'yargs/helpers'
import yargs from 'yargs/yargs'

type Option = boolean | undefined

yargs(hideBin(process.argv))
.scriptName('sparkee')
.usage('Usage: $0 <command> [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
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type CommandOption = boolean | undefined

export interface SparkeeConfig {
$schema?: string
/**
Expand Down
37 changes: 37 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
15 changes: 15 additions & 0 deletions test/helper/index.ts
Original file line number Diff line number Diff line change
@@ -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 <command> [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')
}

0 comments on commit b7277a9

Please sign in to comment.