-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from jrson83/feat/add-cli-tests
refactor: each command its own module
- Loading branch information
Showing
11 changed files
with
159 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export type CommandOption = boolean | undefined | ||
|
||
export interface SparkeeConfig { | ||
$schema?: string | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |