Skip to content

Commit

Permalink
feat: added new cli package
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Weber committed Apr 27, 2019
1 parent bb2e83d commit b363f2f
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 43 deletions.
6 changes: 6 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @averjs/cli

> TODO: description
## Usage

6 changes: 6 additions & 0 deletions packages/cli/bin/aver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

const requireModule = require('esm')(module);
const Cli = requireModule('../lib').default;
const cli = new Cli();
cli.run();
18 changes: 18 additions & 0 deletions packages/cli/lib/commands/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Command from './command';
import Core from '@averjs/core';

export default class BuildCommand extends Command {
constructor() {
super();

this.name = 'build';
this.description = 'Build for production usage.';
}

async run() {
if (typeof process.env.NODE_ENV === 'undefined') process.env.NODE_ENV = 'production';

const core = new Core();
await core.build();
}
}
15 changes: 15 additions & 0 deletions packages/cli/lib/commands/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* This class serves as an interface
*/
export default class Command {
constructor() {
this.name = '';
this.description = '';
this.aliases = [];
this.args = [];
}

run() {}

generateCommandLineUsage() {}
}
18 changes: 18 additions & 0 deletions packages/cli/lib/commands/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Command from './command';
import Core from '@averjs/core';

export default class DevCommand extends Command {
constructor() {
super();

this.name = 'dev';
this.description = 'Start averjs in development mode.';
}

run() {
if (typeof process.env.NODE_ENV === 'undefined') process.env.NODE_ENV = 'development';

const core = new Core();
core.run();
}
}
55 changes: 55 additions & 0 deletions packages/cli/lib/commands/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Command from './command';
import commandLineUsage from 'command-line-usage';

export default class HelpCommand extends Command {
constructor(commands = []) {
super();

this.name = 'help';
this.aliases = [
'h'
];
this.description = 'Shows this help or for a specific command.';
this.commands = commands;
}

getLogo() {
return `
.........\\\\~~~~~\\\\....../~~~~\\\\....../~~~~~/.........
..........\\\\ \\\\..../ \\\\..../ /..........
...........\\\\ \\\\~~/ \\\\~~/ /...........
............\\\\ \\\\/ \\\\/ /............
.............\\\\ / /\\\\ \\\\ /.............
..............\\\\ / / \\\\ \\\\ /.............. {bold.underline averjs-CLI}
...............\\\\/ / \\\\ \\\\/...............
.............../ / \\\\ \\\\............... The cli tool for averjs
............../ / \\\\ / \\\\ \\\\..............
............./ / \\\\~~/ \\\\ \\\\............. Usage: \`aver <command> [options ...]\`
............/ /\\\\ /.\\\\ \\\\............
.........../ /..\\\\ /...\\\\ \\\\...........
........../ /....\\\\ /.....\\\\ \\\\..........
........./ /......\\\\ /.......\\\\ \\\\.........
......../~~~~~/........\\\\ /.........\\\\~~~~~\\\\........
`.trim();
}

generateCommandLineUsage() {
return commandLineUsage([
{
content: this.getLogo(),
raw: true
},
{
header: 'Available Commands',
content: this.commands.map(command => ({
name: command.name,
summary: command.description
}))
}
]);
}

run() {
console.log(this.generateCommandLineUsage());
}
}
16 changes: 16 additions & 0 deletions packages/cli/lib/commands/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Command from './command';
import Core from '@averjs/core';

export default class InitCommand extends Command {
constructor() {
super();

this.name = 'init';
this.description = 'Initialize the project by creating all necessary files in the working directory.';
}

run() {
const core = new Core();
core.init();
}
}
42 changes: 42 additions & 0 deletions packages/cli/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import HelpCommand from './commands/help';
import DevCommand from './commands/dev';
import InitCommand from './commands/init';
import BuildCommand from './commands/build';
import parseArgs from 'minimist';

export default class Usage {
constructor() {
this.aliases = {};
this.availableCommands = [];

this.addCommand(new HelpCommand(this.availableCommands));
this.addCommand(new DevCommand());
this.addCommand(new InitCommand());
this.addCommand(new BuildCommand());

this.argv = parseArgs(process.argv.slice(2), {
alias: this.aliases
});
}

addCommand(command) {
this.availableCommands.push(command);

for (const alias of command.aliases) {
this.aliases[alias] = command.name;
}
}

async run() {
for (const command of this.availableCommands) {
if (this.argv._.indexOf(command.name) !== -1) {
try {
await command.run();
} catch (err) {
console.error(err);
}
break;
}
}
}
}
26 changes: 26 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@averjs/cli",
"version": "1.5.2",
"description": "cli package vor averjs",
"author": "Florian Weber",
"homepage": "https://github.com/exreplay/aver.js#readme",
"license": "MIT",
"main": "lib/index.js",
"bin": {
"aver": "./bin/aver.js"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/exreplay/aver.js.git",
"directory": "packages/cli"
},
"bugs": {
"url": "https://github.com/exreplay/aver.js/issues"
},
"dependencies": {
"command-line-usage": "5.0.5"
}
}
39 changes: 0 additions & 39 deletions packages/core/bin/aver

This file was deleted.

4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"author": "Florian Weber",
"homepage": "https://github.com/exreplay/aver.js#readme",
"main": "index.js",
"bin": {
"aver": "./bin/aver"
},
"publishConfig": {
"access": "public"
},
Expand All @@ -20,6 +17,7 @@
"url": "https://github.com/exreplay/aver.js/issues"
},
"dependencies": {
"@averjs/cli": "1.5.2",
"@averjs/config": "1.5.2",
"@averjs/vuex-decorators": "2.1.4",
"@babel/runtime-corejs2": "7.4.3",
Expand Down
12 changes: 11 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,16 @@ command-line-commands@^2.0.1:
dependencies:
array-back "^2.0.0"

command-line-usage@5.0.5:
version "5.0.5"
resolved "https://registry.yarnpkg.com/command-line-usage/-/command-line-usage-5.0.5.tgz#5f25933ffe6dedd983c635d38a21d7e623fda357"
integrity sha512-d8NrGylA5oCXSbGoKz05FkehDAzSmIm4K03S5VDh4d5lZAtTWfc3D1RuETtuQCn8129nYfJfDdF7P/lwcz1BlA==
dependencies:
array-back "^2.0.0"
chalk "^2.4.1"
table-layout "^0.4.3"
typical "^2.6.1"

command-line-usage@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/command-line-usage/-/command-line-usage-4.1.0.tgz#a6b3b2e2703b4dcf8bd46ae19e118a9a52972882"
Expand Down Expand Up @@ -11900,7 +11910,7 @@ symbol-tree@^3.2.2:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6"
integrity sha1-rifbOPZgp64uHDt9G8KQgZuFGeY=

table-layout@^0.4.2:
table-layout@^0.4.2, table-layout@^0.4.3:
version "0.4.4"
resolved "https://registry.yarnpkg.com/table-layout/-/table-layout-0.4.4.tgz#bc5398b2a05e58b67b05dd9238354b89ef27be0f"
integrity sha512-uNaR3SRMJwfdp9OUr36eyEi6LLsbcTqTO/hfTsNviKsNeyMBPICJCC7QXRF3+07bAP6FRwA8rczJPBqXDc0CkQ==
Expand Down

0 comments on commit b363f2f

Please sign in to comment.