command line usage and command line routing

This commit is contained in:
Leo Lee (DEVDIV) 2015-02-06 00:44:13 -08:00
Родитель 6b41d7493e
Коммит 480a373207
10 изменённых файлов: 228 добавлений и 24 удалений

Просмотреть файл

@ -1,19 +1,23 @@
{
"create": {
"synopsis": "<PATH> [ID] [NAME] [options]",
"modulePath": "../cli/create",
"description": "Create a project",
"description": "Create a Cordova project",
"args": [
{ "name": "<PATH>", "description": "Where to create the project" },
{ "name": "[ID]", "description": "reverse-domain-style package name - used in <widget id>" },
{ "name": "[NAME]", "description": "human readable field" },
{ "name": "[options]", "description": "" }
{ "name": "PATH", "description": "Where to create the project" },
{ "name": "ID", "description": "Package ID for the project" },
{ "name": "NAME", "description": "Human readable app name" }
],
"options": [
{ "name": "template", "description": "template name" },
{ "name": "kit", "description": "kit version" }
{ "name": "--kit <NAME>", "description": "Use the specified Cordova Dev Kit for this project rather than the default. Use \"taco kit list\" to see a list of available Kits." },
{ "name": "--template <NAME>", "description": "Use an alternate project template instead of blank." },
{ "name": "--cli <VERSION>", "description": "Use a version of the Cordova CLI rather than a Cordova Dev Kit." },
{ "name": "--copy-from|src <PATH>", "description": "Use custom www assets instead using a template." },
{ "name": "--link-to <PATH>", "description": "Symlink to custom www assets without creating a copy." }
]
},
"plugin": {
"Synopsis" : "",
"modulePath": "../cli/cordova",
"description": "Add/remove a plugin",
"args": [

Просмотреть файл

@ -11,11 +11,7 @@ import tacoUtility = require("taco-utils");
*/
class Cordova extends tacoUtility.Commands.Command {
public run(): void {
console.log("Cordova!!!");
//console.log("args: " + this.info.args.length);
//console.log("options: " + this.info.options.length);
console.log(this.cliArgs);
cordova.cli(["cc", "dd"]);
cordova.cli(this.cliArgs); //call into Cordova CLI
}
}

Просмотреть файл

@ -1,8 +1,12 @@
/// <reference path="../../typings/taco-utils.d.ts" />
/// <reference path="../../typings/node.d.ts" />
/// <reference path="../../typings/nopt.d.ts" />
import tacoUtility = require("taco-utils");
import cordovaCommand = require("./cordova");
var nopt = require("nopt");
import logger = tacoUtility.Logger;
import level = logger.Level;
/*
* Create
@ -12,6 +16,21 @@ import cordovaCommand = require("./cordova");
class Create extends cordovaCommand {
run() {
console.log("create!!!");
var knownOpts: any = {
"template": String
};
var shortHands: any = {
"t": ["--template"]
};
var args = nopt(knownOpts, shortHands, process.argv, 2);
//sample getting args specific to taco
logger.logNewLine("Creating new project using template : " + args.template, level.Success);
//sample routing remaining args to Cordova
this.cliArgs = args.argv.remain;
super.run();
}
}

71
src/taco-cli/cli/help.ts Normal file
Просмотреть файл

@ -0,0 +1,71 @@
/// <reference path="../../typings/taco-utils.d.ts" />
/// <reference path="../../typings/node.d.ts" />
/// <reference path="../../typings/colors.d.ts" />
/// <reference path="../../typings/nopt.d.ts" />
import nopt = require("nopt");
var colors = require("colors");
import tacoUtility = require("taco-utils");
import commandsFactory = tacoUtility.Commands.CommandFactory;
import logger = tacoUtility.Logger;
import level = logger.Level;
/*
* Help
*
* handles "Taco Help"
*/
class Help extends tacoUtility.Commands.Command {
private indent: string = " ";
private charsToDescription: number = 35;
private maxRight = 70;
run() {
this.printHeader();
if (this.cliArgs.length == 0) {
this.printGeneralUsage();
} else if (this.cliArgs.length == 1) {
this.printCommandUsage(this.cliArgs[0])
}
}
public printHeader(): void {
logger.logNewLine("\n=================================================================", level.Normal);
}
public printGeneralUsage(): void {
console.log("General Help!!");
}
public printCommandUsage(command: string): void {
if (!commandsFactory.Listings || !commandsFactory.Listings[command]) {
this.printGeneralUsage();
return;
}
var list: tacoUtility.Commands.ICommandInfo = commandsFactory.Listings[command];
logger.logNewLine("Synopsis\n", level.NormalBold);
logger.logNewLine(this.indent + list.synopsis + "\n", level.Success);
logger.logNewLine(list.description + "\n", level.NormalBold);
this.printCommandTable(list.args, this.indent);
logger.logNewLine("\n" + this.indent + "Options:\n", level.NormalBold);
this.printCommandTable(list.options, this.indent + this.indent);
}
public printCommandTable(nameValuePairs: tacoUtility.Commands.INameDescription[], indentFromLeft: string) {
nameValuePairs.forEach(nvp => {
logger.log(indentFromLeft + nvp.name, level.Warn);
logger.log(" ", level.Normal);
for (var i: number = indentFromLeft.length + nvp.name.length + 2;
i < this.charsToDescription; i++) {
logger.log(".", level.Normal);
}
if (this.charsToDescription + nvp.description.length > this.maxRight) {
}
logger.log(" " + nvp.description + "\n", level.Normal);
});
}
}
export = Help;

Просмотреть файл

@ -1,12 +1,10 @@
/// <reference path="../../typings/taco-utils.d.ts" />
/// <reference path="../../typings/node.d.ts" />
/// <reference path="../../typings/nopt.d.ts" />
/// <reference path="../../typings/cordova.d.ts" />
import tacoUtility = require("taco-utils");
import resourcesManager = tacoUtility.ResourcesManager;
import commandsFactory = tacoUtility.Commands.CommandFactory;
import nopt = require("nopt");
import fs = require("fs");
import path = require("path");
@ -14,10 +12,10 @@ class Taco {
constructor() {
var resourcePath: string = path.resolve("../resources");
resourcesManager.init("en", resourcePath);
commandsFactory.init("../cli/commands.json");
}
public run(): void {
commandsFactory.init("../cli/commands.json");
commandsFactory.runTask();
}
}

Просмотреть файл

@ -39,6 +39,7 @@
"nconf": "^0.7.1",
"nopt": "^3.0.1",
"q": "^1.1.2",
"colors": "^1.0.3",
"taco-utils": "file:../taco-utils"
},
"bundledDependencies": ["taco-utils"],

Просмотреть файл

@ -7,6 +7,7 @@
"version": "0.0.1",
"main": "taco-utils.js",
"dependencies": {
"colors": "^1.0.3"
},
"private": true,
"devDependencies": {

Просмотреть файл

@ -1,10 +1,60 @@
/// <reference path="../typings/node.d.ts" />
"use strict";
/// <reference path="../typings/colors.d.ts" />
import fs = require ("fs");
"use strict";
import fs = require("fs");
var colors = require("colors");
import path = require ("path");
module TacoUtility {
export module Logger {
export enum Level { Warn, Error, Link, Normal, Success, NormalBold };
export function colorize(msg: string, level: Level): string {
//not yet possible to combine themes yet so still need to wrap this: https://github.com/Marak/colors.js/issues/72
colors.setTheme({
error: "red",
warn: "yellow",
link: "blue",
normalBold: "bold",
success: "green"
});
switch (level) {
case Level.Error: return msg.error.bold;
case Level.Warn: return msg.warn.bold;
case Level.Link: return msg.link.underline;
case Level.Normal: return msg;
case Level.NormalBold: return msg.normalBold;
case Level.Success: return msg.success.bold;
}
}
export function logNewLine(msg: string, level: Level): void {
log(msg + "\n", level);
}
/**
*
*
*/
export function log(msg: string, level: Level): void {
msg = colorize(msg, level);
switch (level) {
case Level.Error:
case Level.Warn:
process.stderr.write(msg);
return;
case Level.Link:
case Level.Success:
case Level.Normal:
case Level.NormalBold:
process.stdout.write(msg);
break;
}
}
}
export module Commands {
export interface INameDescription {
name: string;
@ -12,6 +62,7 @@ module TacoUtility {
}
export interface ICommandInfo {
synopsis: string;
modulePath: string;
description: string;
args: INameDescription[];
@ -23,7 +74,7 @@ module TacoUtility {
cliArgs: string[];
constructor(info: ICommandInfo) {
this.info = info;
this.cliArgs = process.argv.slice(2);
this.cliArgs = process.argv.slice(3);
}
public run() {
@ -31,9 +82,8 @@ module TacoUtility {
}
export class CommandFactory {
private static Listings: any;
public static Listings: any;
private static Instance: Command;
public static CommandsInfoFile: string;
// initialize with json file containing commands
public static init(commandsInfoPath: string) {
@ -42,7 +92,6 @@ module TacoUtility {
throw new Error("Cannot find commands listing file");
}
CommandFactory.CommandsInfoFile = commandsInfoPath;
CommandFactory.Listings = require(commandsInfoPath);
}

47
src/typings/colors.d.ts поставляемый Normal file
Просмотреть файл

@ -0,0 +1,47 @@
// Type definitions for Colors.js 0.6.0-1
// Project: https://github.com/Marak/colors.js
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module colors {
export function setTheme(theme: any): any;
export function black(text: string): string;
export function red(text: string): string;
export function green(text: string): string;
export function yellow(text: string): string;
export function blue(text: string): string;
export function magenta(text: string): string;
export function cyan(text: string): string;
export function white(text: string): string;
export function gray(text: string): string;
export function grey(text: string): string;
}
interface String {
bold: string;
italic: string;
underline: string;
inverse: string;
yellow: string;
cyan: string;
white: string;
magenta: string;
green: string;
red: string;
grey: string;
blue: string;
rainbow: string;
zebra: string;
random: string;
error: string;
warn: string;
link: string;
normalBold: string;
success: string;
}
declare module "colors" {
export = colors;
}

22
src/typings/taco-utils.d.ts поставляемый
Просмотреть файл

@ -1,11 +1,30 @@
/// <reference path="../typings/node.d.ts" />
/// <reference path="../typings/colors.d.ts" />
declare module TacoUtility {
module Logger {
enum Level {
Warn = 0,
Error = 1,
Link = 2,
Normal = 3,
Success = 4,
NormalBold = 5,
}
function colorize(msg: string, level: Level): string;
function logNewLine(msg: string, level: Level): void;
/**
*
*
*/
function log(msg: string, level: Level): void;
}
module Commands {
interface INameDescription {
name: string;
description: string;
}
interface ICommandInfo {
synopsis: string;
modulePath: string;
description: string;
args: INameDescription[];
@ -18,9 +37,8 @@ declare module TacoUtility {
run(): void;
}
class CommandFactory {
private static Listings;
static Listings: any;
private static Instance;
static CommandsInfoFile: string;
static init(commandsInfoPath: string): void;
static getTask(name: string): Command;
static runTask(): void;