wip: general approach for cli
parent
c0e0e84a07
commit
b1a7be5e41
|
|
@ -16,6 +16,9 @@
|
||||||
"es6": true,
|
"es6": true,
|
||||||
"jest/globals": true
|
"jest/globals": true
|
||||||
},
|
},
|
||||||
|
"globals": {
|
||||||
|
"Deno": true
|
||||||
|
},
|
||||||
"rules": {
|
"rules": {
|
||||||
// Error out for code formatting errors
|
// Error out for code formatting errors
|
||||||
"prettier/prettier": "error",
|
"prettier/prettier": "error",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { parseArgv } from '../core/parse-argv.ts';
|
||||||
|
|
||||||
|
export class ArgumentsParser {
|
||||||
|
static parse(cliArguments: string[]) {
|
||||||
|
const [commandName, ...arguments] = cliArguments;
|
||||||
|
|
||||||
|
return {
|
||||||
|
commandName,
|
||||||
|
options: parseArgv(arguments),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { CommandFactory } from './commands/command-factory.ts';
|
||||||
|
import { ArgumentsParser } from './arguments-parser/arguments-parser.ts';
|
||||||
|
import { Options } from './config/options.ts';
|
||||||
|
import { CommandInterface } from './commands/command/CommandInterface.ts';
|
||||||
|
|
||||||
|
export class Bootstrapper {
|
||||||
|
private readonly commandFactory: CommandFactory;
|
||||||
|
private readonly argumentsParser: ArgumentsParser;
|
||||||
|
|
||||||
|
private options?: Options;
|
||||||
|
private command?: CommandInterface;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.commandFactory = new CommandFactory();
|
||||||
|
this.argumentsParser = ArgumentsParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async run(cliArguments: string[]) {
|
||||||
|
const { commandName, options } = this.argumentsParser.parse(cliArguments);
|
||||||
|
|
||||||
|
this.options = new Options(options);
|
||||||
|
this.command = this.commandFactory.createCommand(commandName);
|
||||||
|
|
||||||
|
// Command agnostic stuff here
|
||||||
|
|
||||||
|
await this.command.execute(this.options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { NonExistentCommand } from './command/non-existent-command.ts';
|
||||||
|
import { BuildCommand } from './command/build-command.ts';
|
||||||
|
|
||||||
|
export class CommandFactory {
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
|
public createCommand(commandName) {
|
||||||
|
switch (commandName) {
|
||||||
|
case 'build':
|
||||||
|
return new BuildCommand();
|
||||||
|
default:
|
||||||
|
return new NonExistentCommand(commandName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { Options } from '../../config/options.ts';
|
||||||
|
|
||||||
|
export interface CommandInterface {
|
||||||
|
name: string;
|
||||||
|
execute: (options: Options) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
@ -1,17 +1,15 @@
|
||||||
/* eslint-disable no-console */
|
import { CommandInterface } from './CommandInterface.ts';
|
||||||
import { exec, OutputMode } from 'https://deno.land/x/exec@0.0.5/mod.ts';
|
import { exec, OutputMode } from 'https://deno.land/x/exec@0.0.5/mod.ts';
|
||||||
import { CliOptions } from '../core/cli-options.ts';
|
import { Options } from '../../config/options.ts';
|
||||||
|
|
||||||
export class Bootstrapper {
|
export class BuildCommand implements CommandInterface {
|
||||||
private readonly options: CliOptions;
|
public readonly name: string;
|
||||||
|
|
||||||
constructor(cliOptions: CliOptions) {
|
constructor(name: string) {
|
||||||
this.options = cliOptions;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async run() {
|
public async execute(options: Options) {
|
||||||
console.log('using options', this.options);
|
|
||||||
|
|
||||||
const result = await exec('docker run -it unityci/editor:2020.3.15f2-base-1 /bin/bash -c "echo test"', {
|
const result = await exec('docker run -it unityci/editor:2020.3.15f2-base-1 /bin/bash -c "echo test"', {
|
||||||
output: OutputMode.Capture,
|
output: OutputMode.Capture,
|
||||||
continueOnError: true,
|
continueOnError: true,
|
||||||
|
|
@ -19,6 +17,7 @@ export class Bootstrapper {
|
||||||
// verbose: true,
|
// verbose: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(options);
|
||||||
console.log(result.output);
|
console.log(result.output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { CommandInterface } from './CommandInterface.ts';
|
||||||
|
import { Options } from '../../config/options.ts';
|
||||||
|
|
||||||
|
export class NonExistentCommand implements CommandInterface {
|
||||||
|
public name: string;
|
||||||
|
|
||||||
|
constructor(name: string) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async execute(options: Options) {
|
||||||
|
throw new Error(`Command ${this.name} does not exist`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { CliOptions } from '../core/cli-options.ts';
|
||||||
|
|
||||||
|
export class Options {
|
||||||
|
public options: CliOptions;
|
||||||
|
|
||||||
|
constructor(optionsFromCli) {
|
||||||
|
this.options = optionsFromCli;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
import { parseArgv } from './parse-argv.ts';
|
|
||||||
import { Bootstrapper } from '../controller/bootstrapper.ts';
|
|
||||||
|
|
||||||
export class Kernel {
|
|
||||||
public async run() {
|
|
||||||
const cliOptions = parseArgv(Deno.args);
|
|
||||||
|
|
||||||
const bootstrapper = new Bootstrapper(cliOptions);
|
|
||||||
bootstrapper.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
12
cli/index.ts
12
cli/index.ts
|
|
@ -1,7 +1,11 @@
|
||||||
import { Kernel } from './core/kernel.ts';
|
/* eslint-disable no-console */
|
||||||
|
import { Bootstrapper } from './bootstrapper.ts';
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const kernel = new Kernel();
|
try {
|
||||||
|
await new Bootstrapper().run(Deno.args);
|
||||||
await kernel.run();
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
Deno.exit(1);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
|
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
|
||||||
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
|
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
|
||||||
"outDir": "./lib" /* Redirect output structure to the directory. */,
|
"outDir": "./lib" /* Redirect output structure to the directory. */,
|
||||||
"rootDir": "./" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
|
"rootDir": "./cli" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
|
||||||
"strict": true /* Enable all strict type-checking options. */,
|
"strict": true /* Enable all strict type-checking options. */,
|
||||||
"noImplicitAny": false /* Re-enable after fixing compatibility */ /* Raise error on expressions and declarations with an implied 'any' type. */,
|
"noImplicitAny": false /* Re-enable after fixing compatibility */ /* Raise error on expressions and declarations with an implied 'any' type. */,
|
||||||
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue