Skip to content

Commit

Permalink
feat(commands): add new, generate, test, and build commands
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-lieben committed Sep 25, 2016
1 parent 28b0a8f commit a4fcea9
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 3 deletions.
86 changes: 86 additions & 0 deletions dist/src/aureliaCLICommands.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/src/aureliaCLICommands.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dist/src/extension.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/extension.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,33 @@
"categories": [
"Other"
],
"activationEvents": [],
"activationEvents": [
"onCommand:extension.auNew",
"onCommand:extension.auGenerate",
"onCommand:extension.auTest",
"onCommand:extension.auBuild"
],
"main": "./dist/src/extension",
"contributes": {},
"contributes": {
"commands": [
{
"command": "extension.auNew",
"title": "au new"
},
{
"command": "extension.auGenerate",
"title": "au generate"
},
{
"command": "extension.auBuild",
"title": "au build"
},
{
"command": "extension.auTest",
"title": "au test"
}
]
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
Expand All @@ -42,5 +66,6 @@
"vscode": "^0.11.18"
},
"dependencies": {
"run-in-terminal": "0.0.3"
}
}
90 changes: 90 additions & 0 deletions src/aureliaCLICommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as vscode from 'vscode';
import * as cp from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import { runInTerminal } from 'run-in-terminal';

export default class AureliaCliCommands {

public static registerCommands(outputChannel: vscode.OutputChannel): vscode.Disposable {
return vscode.Disposable.from(
vscode.commands.registerCommand('extension.auNew', () => this.auNew(outputChannel)),
vscode.commands.registerCommand('extension.auGenerate', () => this.auGenerate(outputChannel)),
vscode.commands.registerCommand('extension.auTest', () => this.runCommand(['test'], outputChannel, false)),
vscode.commands.registerCommand('extension.auBuild', () => this.auBuild(outputChannel)));
}

private static async auNew(outputChannel) {
let projectName = await vscode.window.showInputBox({ placeHolder: 'Please enter a name for your new project' });
this.runCommand(['new', projectName], outputChannel, true);
}

private static async auGenerate(outputChannel) {

let param: string[] = ['generate'];
let type = await vscode.window.showQuickPick(
this.getGeneratorTypes(),
{ matchOnDescription: false, placeHolder: 'Select type' });
param.push(type);

let name = await vscode.window.showInputBox({ placeHolder: `What would you like to call the ${type}?` });
param.push(name);

this.runCommand(param, outputChannel, false);
}

private static async auBuild(outputChannel) {
let param: string[] = ['build'];
let auTemplatePath = path.join(vscode.workspace.rootPath, 'aurelia_project/environments');
let items = [];

fs.readdirSync(auTemplatePath).forEach((name) => {
if (path.extname(name) === '.ts' || path.extname(name) === '.js') {
items.push(path.basename(name.replace('.ts', '').replace('.js', '')));
}
});

let options = { matchOnDescription: false, placeHolder: 'Select environment to build' };
vscode.window.showQuickPick(items, options).then((data) => {
param.push('--env ' + data);
this.runCommand(param, outputChannel, false);
});
}

private static getGeneratorTypes() {
let items = [];
fs.readdirSync(path.join(vscode.workspace.rootPath, 'aurelia_project/generators')).forEach(name => {
if (path.extname(name) === '.ts' || path.extname(name) === '.js') {
items.push(path.basename(name.replace('.ts', '').replace('.js', '')));
}
});
return items;
}

private static showOutput(outputChannel: vscode.OutputChannel): void {
outputChannel.show(vscode.ViewColumn.One);
}

private static runCommand(args: string[], outputChannel: vscode.OutputChannel, useTerminal?: boolean): void {
let cwd = vscode.workspace.rootPath;
if (useTerminal) {
this.runCommandInTerminal(args, cwd);
} else {
this.runCommandInOutputWindow(args, cwd, outputChannel);
}
}

private static runCommandInOutputWindow(args: string[], cwd: string, outputChannel: vscode.OutputChannel) {

let cmd = 'au ' + args.join(' ');
let childProcess = cp.exec(cmd, { cwd: cwd, env: process.env });
childProcess.stderr.on('data', data => outputChannel.append(<string> data));
childProcess.stdout.on('data', data => outputChannel.append(<string> data));

this.showOutput(outputChannel);
}

private static runCommandInTerminal(args: string[], cwd: string): void {
runInTerminal('au', args, { cwd: cwd, env: process.env });
}
}
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExtensionContext, OutputChannel, window } from 'vscode';
import AureliaCliCommands from './aureliaCliCommands';

let outputChannel: OutputChannel;

Expand All @@ -7,4 +8,7 @@ export function activate(context: ExtensionContext) {
// Create default output channel
outputChannel = window.createOutputChannel('aurelia');
context.subscriptions.push(outputChannel);

// Register CLI commands
context.subscriptions.push(AureliaCliCommands.registerCommands(outputChannel));
}
65 changes: 65 additions & 0 deletions test/aureliaCLICommands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as assert from 'assert';
import using from './test-util';
import { commands } from 'vscode';
import AureliaCliCommands from '../src/aureliaCLICommands';

let sut = AureliaCliCommands;

suite('The Aurelia CLI Commands', () => {

test('must register command au new', () => {

// arrange
let outputChannel = undefined;

// act
using(sut.registerCommands(outputChannel), async () => {

// assert
let result = (await commands.getCommands(true)).filter(x => x === 'extension.auNew');
assert.equal(result.length, 1);
});
});

test('must register command au generate', () => {

// arrange
let outputChannel = undefined;

// act
using(sut.registerCommands(outputChannel), async () => {

// assert
let result = (await commands.getCommands(true)).filter(x => x === 'extension.auGenerate');
assert.equal(result.length, 1);
});
});

test('must register command au test', () => {

// arrange
let outputChannel = undefined;

// act
using(sut.registerCommands(outputChannel), async () => {

// assert
let result = (await commands.getCommands(true)).filter(x => x === 'extension.auTest');
assert.equal(result.length, 1);
});
});

test('must register command au build', () => {

// arrange
let outputChannel = undefined;

// act
using(sut.registerCommands(outputChannel), async () => {

// assert
let result = (await commands.getCommands(true)).filter(x => x === 'extension.auBuild');
assert.equal(result.length, 1);
});
});
});
Loading

0 comments on commit a4fcea9

Please sign in to comment.