This repository was archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
480 additions
and
304 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
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 +1,3 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
*.ts text eol=lf |
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,7 +1,8 @@ | ||
*-debug.log | ||
*-error.log | ||
/.nyc_output | ||
/coverage | ||
/coverage.lcov | ||
/lib | ||
/node_modules | ||
/tmp | ||
/lib | ||
/.nyc_output |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,14 @@ environment: | |
nodejs_version: "9" | ||
cache: | ||
- '%LOCALAPPDATA%\Yarn -> appveyor.yml' | ||
- node_modules -> package.json | ||
- node_modules -> yarn.lock | ||
|
||
install: | ||
- ps: Install-Product node $env:nodejs_version x64 | ||
- git submodule sync | ||
- git submodule update --init --recursive | ||
- git config --global user.email "[email protected]" | ||
- git config --global user.name "dxcli" | ||
- yarn | ||
test_script: | ||
- yarn test | ||
|
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,25 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
import cli from 'cli-ux' | ||
import * as fs from 'fs' | ||
|
||
import Command, {flags} from '..' // use @dxcli/command outside this repo | ||
|
||
class LS extends Command { | ||
static flags = { | ||
// run with --dir= or -d= | ||
dir: flags.string({ | ||
char: 'd', | ||
default: process.cwd(), | ||
}), | ||
} | ||
|
||
async run() { | ||
let files = fs.readdirSync(this.flags.dir) | ||
for (let f of files) { | ||
cli.log(f) | ||
} | ||
} | ||
} | ||
|
||
LS.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,11 @@ | ||
{ | ||
"extends": "../tsconfig", | ||
"compilerOptions": { | ||
"rootDir": ".." | ||
}, | ||
"include": [ | ||
"../examples/**/*", | ||
"../src/**/*", | ||
"../test/**/*" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const pjson = require('../package.json') | ||
import {ConfigOptions, IConfig, IPlugin, isIConfig, PluginConfig} from '@dxcli/config' | ||
import {args} from '@dxcli/parser' | ||
import {HTTP} from 'http-call' | ||
|
||
import deps from './deps' | ||
import * as flags from './flags' | ||
|
||
export type CommandRunFn = <T extends Command>(this: ICommandClass<T>, argv?: string[], config?: IConfig | ConfigOptions) => Promise<T> | ||
|
||
export interface ICommandClass<T extends Command> { | ||
run: CommandRunFn | ||
new (config: IConfig): T | ||
} | ||
|
||
const parentModule = module.parent && module.parent.parent && module.parent.parent.filename | ||
|
||
export default abstract class Command { | ||
static id?: string | ||
static description: string | undefined | ||
static hidden: boolean | ||
static usage: string | undefined | ||
static help: string | undefined | ||
static aliases: string[] = [] | ||
static variableArgs = false | ||
static flags: flags.Input | ||
static args: args.IArg[] = [] | ||
static _base = `${pjson.name}@${pjson.version}` | ||
static plugin: IPlugin | undefined | ||
|
||
/** | ||
* instantiate and run the command | ||
*/ | ||
static run: CommandRunFn = async function (argv: string[] = process.argv.slice(2), config: IConfig | ConfigOptions = {}) { | ||
if (!isIConfig(config)) config = await PluginConfig.create({root: parentModule!, ...config}) | ||
const cmd = new this(config as any) | ||
try { | ||
await cmd.init(argv) | ||
await cmd.run() | ||
await cmd.done() | ||
} catch (err) { | ||
// throw HelpErr to allow the CLI to do something with it | ||
if (err.code === 'EHELP') throw err | ||
deps.cli.error(err) | ||
} | ||
return cmd | ||
} | ||
|
||
flags: { [name: string]: any } = {} | ||
argv: string[] | ||
args: { [name: string]: string } = {} | ||
|
||
// prevent setting things that need to be static | ||
topic: null | ||
command: null | ||
description: null | ||
hidden: null | ||
usage: null | ||
help: null | ||
aliases: null | ||
|
||
protected debug: (...args: any[]) => void | ||
|
||
get ctor(): typeof Command { | ||
return this.constructor as typeof Command | ||
} | ||
|
||
get http(): typeof HTTP { return require('http-call').HTTP } | ||
|
||
constructor(protected config: IConfig) { | ||
global['http-call'] = global['http-call'] || {} | ||
global['http-call']!.userAgent = config.userAgent | ||
this.debug = require('debug')(`cli:command:${this.ctor.id || config.name}`) | ||
} | ||
|
||
/** | ||
* actual command run code goes here | ||
*/ | ||
abstract async run(): Promise<void> | ||
|
||
protected async init(argv: string[]) { | ||
this.debug('init version: %s argv: %o', this.ctor._base, argv) | ||
deps.cli.config.errlog = this.config.errlog | ||
try { | ||
const parse = await deps.Parser.parse({ | ||
argv, | ||
args: this.ctor.args || [], | ||
flags: this.ctor.flags || {}, | ||
strict: !this.ctor.variableArgs, | ||
}) | ||
this.flags = parse.flags | ||
this.argv = parse.argv | ||
this.args = parse.args | ||
} catch (err) { | ||
if (err.message.match(/^Unexpected argument: (-h|help|--help)/)) { | ||
throw new deps.HelpErr(err.message) | ||
} | ||
throw err | ||
} | ||
} | ||
|
||
protected async done() { | ||
try { | ||
await deps.cli.done() | ||
} catch (err) { | ||
deps.cli.warn(err) | ||
} | ||
} | ||
} |
Oops, something went wrong.