commander.js:可以自动的解析命令和参数,用于处理用户输入的命令。
download-git-repo:下载并提取 git 仓库中的配置文件,用于下载项目模板。
Inquirer.js:通用的命令行用户界面集合,用于和用户进行交互。
ora:下载过程久的话,可以用于显示下载中的动画效果。(暂时未用到)
typescript:typescript模块
@types/node: 在typescript中使用node
@type/commander:typescript可以自动的解析命令和参数,用于处理用户输入的命令。
@types/inquirer:typescript中实现用户交互
npm install typescript chalk commander download-git-repo inquirer ora @types/node @type/commander @types/inquirer --save
npm install tslint --save-dev
完成安装tsc —init
初始化project-cli项目,生产tsconfig.json文件,将outDir": "./"
取消注释并修改为"outDir": "./bld"
(outDir:配置typescript中编译文件存储路径)"bin": {
"tcli": "./bld/bin/main.js"
},
#! /usr/bin/env node
process.env.NODE_PATH = __dirname + "/../node_modules";
import command = require("commander");
import * as path from "path";
const res = (command: string) => path.resolve(__dirname, "../commands/", command);
command
.version("1.0.0");
command
.usage("");
command
.command("init")
.description("Generate a new my project")
.alias("i")
.action( () => {
require(res("init"));
});
command.parse(process.argv);
if (!command.args || !command.args.length) {
command.help();
}
import * as chalk from "chalk";
import command = require("commander");
import download = require("download-git-repo");
import * as fs from "fs";
import * as inquirer from "inquirer";
import ora = require("ora");
import * as path from "path";
import { branchNodeConf, branchPythonConf, branchTypescriptConf, url, Answers } from "../config/setting";
const spinner = ora("Downloading please wait......");
const option: string = command.parse(process.argv).args[0];
const defaultName: string = typeof option === "string" ? option : "My-project";
const questionList: any[] = [
{
type: "input",
name: "Project name",
message: "Project name",
default: defaultName,
filter(val: string): string {
return val.trim();
},
validate(val: string): any {
const validate = (val.trim().split(" ")).length === 1;
return validate || "Project name is not allowed to have spaces ";
},
transformer(val: string): string {
return chalk.default(val);
}
},
{
type: "input",
name: "description",
message: "Project description",
default: "My project",
validate(val: string): boolean {
return true;
},
transformer(val: string): string {
return chalk.default(val);
}
},
{
type: "input",
name: "author",
message: "Author",
default: "project author",
validate(val: string): boolean {
return true;
},
transformer(val: string): string {
return chalk.default(val);
}
},
{
type: "list",
name: "language type",
message: "language type",
choices: [
"Nodejs",
"Typescript",
"Python"
],
default: "nodejs",
filter: function(val: string): string {
return val.toLowerCase();
}
}
];
// ts的泛型(重点了解)
inquirer.prompt(questionList).then(answers => {
const answer = answers["Project name"];
const type = answers["language type"];
if (type === "nodejs") {
spinner.start();
download(url + branchNodeConf, answer, { clone: true }, (err) => {
if (err) {
spinner.stop();
console.log(err);
} else {
spinner.stop();
console.log(chalk.default("项目初始化成功"));
}
});
} else if (type === "typescript") {
spinner.start();
download(url + branchTypescriptConf, answer, { clone: true }, (err) => {
if (err) {
spinner.stop();
console.log(err);
} else {
spinner.stop();
console.log(chalk.default("项目初始化成功"));
}
});
} else {
spinner.start();
download(url + branchPythonConf, answer, { clone: true }, (err) => {
if (err) {
spinner.stop();
console.log(err);
} else {
spinner.stop();
console.log(chalk.default("项目初始化成功"));
}
});
}
});
npm install -g
tcli init
项目构建是否成功项目代码