-
-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): avoid global side effect, improve tests
- Loading branch information
Showing
8 changed files
with
130 additions
and
148 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,37 @@ | ||
import { cac } from 'cac' | ||
import { version } from '../package.json' | ||
import type { CliOptions } from './types' | ||
import { build } from './index' | ||
|
||
export async function startCli(cwd = process.cwd(), argv = process.argv, options: CliOptions = {}) { | ||
const cli = cac('unocss') | ||
|
||
cli | ||
.command('[...patterns]', 'Glob patterns', { | ||
ignoreOptionDefaultValue: true, | ||
}) | ||
.option('-o, --out-file <file>', 'Output file', { | ||
default: cwd, | ||
}) | ||
.option('-c, --config [file]', 'Config file') | ||
.option('-w, --watch', 'Watch for file changes') | ||
.action(async (patterns: Array<string>, flags) => { | ||
Object.assign(options, { | ||
cwd, | ||
...flags, | ||
}) | ||
|
||
if (patterns) | ||
options.patterns = patterns | ||
|
||
await build(options) | ||
}) | ||
|
||
cli.help() | ||
cli.version(version) | ||
|
||
// Parse CLI args without running the command to | ||
// handle command errors globally | ||
cli.parse(argv, { run: false }) | ||
await cli.runMatchedCommand() | ||
} |
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,42 +1,4 @@ | ||
import { cac } from 'cac' | ||
import { version } from '../package.json' | ||
import { startCli } from './cli-start' | ||
import { handleError } from './errors' | ||
import type { CliOptions } from './types' | ||
import { build } from './index' | ||
|
||
const name = 'unocss' | ||
|
||
async function main(options: CliOptions = {}) { | ||
const cli = cac(name) | ||
|
||
cli | ||
.command('[...patterns]', 'Glob patterns', { | ||
ignoreOptionDefaultValue: true, | ||
}) | ||
.option('-o, --out-file <file>', 'Output file', { | ||
default: process.cwd(), | ||
}) | ||
.option('-c, --config [file]', 'Config file') | ||
.option('-w, --watch', 'Watch for file changes') | ||
.action(async (patterns: Array<string>, flags) => { | ||
Object.assign(options, { | ||
...flags, | ||
}) | ||
|
||
if (patterns) | ||
options.patterns = patterns | ||
|
||
await build(options) | ||
}) | ||
|
||
cli.help() | ||
|
||
cli.version(version) | ||
|
||
// Parse CLI args without running the command to | ||
// handle command errors globally | ||
cli.parse(process.argv, { run: false }) | ||
await cli.runMatchedCommand() | ||
} | ||
|
||
main().catch(handleError) | ||
startCli().catch(handleError) |
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.