Commit acf7817b authored by shiyunjie's avatar shiyunjie

init

parent 9ca94fd3
# OSX
#
.DS_Store
.history
# Xcode
#
......
# Installation
```
npm install agera-h5-cli -g
```
# Usage
```
agera-h5 init
```
#!/usr/bin/env node --harmony
'use strict'
process.env.NODE_PATH = __dirname + '/../node_modules/'
const program = require('commander')
const Project = require('../command/init');
program
.version(require('../package').version)
program
.usage('<command>')
program
.command('add')
.description('Add a new template')
.alias('a')
.action(() => {
require('../command/add')()
})
program
.command('list')
.description('List all the templates')
.alias('l')
.action(() => {
require('../command/list')()
})
program
.command('init')
.alias('i')
.action(() => {
const project = new Project({
projectName: '',
description: ''
});
project.create();
})
program
.command('delete')
.description('Delete a template')
.alias('d')
.action(() => {
require('../command/delete')()
})
program.parse(process.argv)
if (!program.args.length) {
program.help()
}
'use strict'
const co = require('co')
const prompt = require('co-prompt')
const config = require('../template')
const chalk = require('chalk')
const fs = require('fs')
module.exports = () => {
co(function *() {
// 分步接收用户输入的参数
let tplName = yield prompt('Template name: ')
let gitUrl = yield prompt('Git https link: ')
let branch = yield prompt('Branch: ')
// 避免重复添加
if (!config.tpl[tplName]) {
config.tpl[tplName] = {}
config.tpl[tplName]['url'] = gitUrl.replace(/[\u0000-\u0019]/g, '') // 过滤unicode字符
config.tpl[tplName]['branch'] = branch
} else {
console.log(chalk.red('Template has already existed!'))
process.exit()
}
// 把模板信息写入templates.json
fs.writeFile(__dirname + '/../template.json', JSON.stringify(config), 'utf-8', (err) => {
if (err) console.log(err)
console.log(chalk.green('New template added!\n'))
console.log(chalk.grey('The last template list is: \n'))
console.log(config)
console.log('\n')
process.exit()
})
})
}
\ No newline at end of file
exports.INJECT_FILES = ['package.json'];
\ No newline at end of file
'use strict'
const co = require('co')
const prompt = require('co-prompt')
const config = require('../template')
const chalk = require('chalk')
const fs = require('fs')
module.exports = () => {
co(function *() {
// 接收用户输入的参数
let tplName = yield prompt('Template name: ')
// 删除对应的模板
if (config.tpl[tplName]) {
config.tpl[tplName] = undefined
} else {
console.log(chalk.red('Template does not exist!'))
process.exit()
}
// 写入template.json
fs.writeFile(__dirname + '/../template.json', JSON.stringify(config), 'utf-8', (err) => {
if (err) console.log(err)
console.log(chalk.green('Template deleted!'))
console.log(chalk.grey('The last template list is: \n'))
console.log(config)
console.log('\n')
process.exit()
})
})
}
\ No newline at end of file
const inquirer = require('inquirer');
const fse = require('fs-extra');
const download = require('download-git-repo');
const { INJECT_FILES } = require('./constants');
const chalk = require('chalk');
const ora = require('ora');
const path = require('path');
const memFs = require('mem-fs');
const editor = require('mem-fs-editor');
const { getDirFileName } = require('./utils');
const templateList = require('../template')
const { exec } = require('child_process');
function Project(options) {
this.config = Object.assign({
projectName: '',
description: ''
}, options);
const store = memFs.create();
this.memFsEditor = editor.create(store);
}
Project.prototype.create = function() {
this.inquire()
.then((answer) => {
this.config = Object.assign(this.config, answer);
this.generate();
});
};
Project.prototype.inquire = function() {
const prompts = [];
const { projectName, description } = this.config;
prompts.push({
type: 'list',
name: 'templateName',
message: '请选择模版:',
choices: ['vue-ts-vw', 'jquery-MPA'],
});
prompts.push({
type: 'input',
name: 'projectName',
message: '请输入项目名:',
validate(input) {
if (!input) {
return '项目名不能为空';
}
if (fse.existsSync(input)) {
return '当前目录已存在同名项目,请更换项目名';
}
return true;
}
});
if (fse.existsSync(projectName)) {
prompts.push({
type: 'input',
name: 'projectName',
message: '当前目录已存在同名项目,请更换项目名',
validate(input) {
if (!input) {
return '项目名不能为空';
}
if (fse.existsSync(input)) {
return '当前目录已存在同名项目,请更换项目名';
}
return true;
}
});
}
prompts.push({
type: 'input',
name: 'description',
message: '请输入项目描述'
});
return inquirer.prompt(prompts);
};
/**
* 模板替换
* @param {string} source 源文件路径
* @param {string} dest 目标文件路径
* @param {object} data 替换文本字段
*/
Project.prototype.injectTemplate = function(source, dest, data) {
this.memFsEditor.copyTpl(
source,
dest,
data
);
}
Project.prototype.generate = function() {
const { templateName, projectName, description } = this.config;
console.log(`templateName${templateName}`)
const projectPath = path.join(process.cwd(), projectName);
const downloadPath = path.join(projectPath, '__download__');
const downloadSpinner = ora('正在下载模板,请稍等...');
downloadSpinner.start();
const templateUrl = templateList.tpl[templateName].url;
console.log(templateUrl)
// 下载git repo
download(templateUrl, downloadPath, { clone: true }, (err) => {
if (err) {
downloadSpinner.color = 'red';
downloadSpinner.fail(err.message);
return;
}
downloadSpinner.color = 'green';
downloadSpinner.succeed('下载成功');
// 复制文件
console.log();
const copyFiles = getDirFileName(downloadPath);
copyFiles.forEach((file) => {
fse.copySync(path.join(downloadPath, file), path.join(projectPath, file));
console.log(`${chalk.green('✔ ')}${chalk.grey(`创建: ${projectName}/${file}`)}`);
});
INJECT_FILES.forEach((file) => {
console.log(file);
this.injectTemplate(path.join(downloadPath, file), path.join(projectName, file), {
projectName,
description,
});
});
this.memFsEditor.commit(() => {
INJECT_FILES.forEach((file) => {
console.log(`${chalk.green('✔ ')}${chalk.grey(`创建: ${projectName}/${file}`)}`);
})
fse.remove(downloadPath);
process.chdir(projectPath);
// git 初始化
console.log();
const gitInitSpinner = ora(`cd ${chalk.green.bold(projectName)}目录, 执行 ${chalk.green.bold('git init')}`);
gitInitSpinner.start();
const gitInit = exec('git init');
gitInit.on('close', (code) => {
if (code === 0) {
gitInitSpinner.color = 'green';
gitInitSpinner.succeed(gitInit.stdout.read());
} else {
gitInitSpinner.color = 'red';
gitInitSpinner.fail(gitInit.stderr.read());
}
// 安装依赖
console.log();
const installSpinner = ora(`安装项目依赖 ${chalk.green.bold('npm install')}, 请稍后...`);
installSpinner.start();
exec('npm install', (error, stdout, stderr) => {
if (error) {
installSpinner.color = 'red';
installSpinner.fail(chalk.red('安装项目依赖失败,请自行重新安装!'));
console.log(error);
} else {
installSpinner.color = 'green';
installSpinner.succeed('安装依赖成功');
console.log(`${stderr}${stdout}`);
console.log();
console.log(chalk.green('创建项目成功!'));
console.log(chalk.green('Let\'s Coding吧!嘿嘿😝'));
}
})
})
});
});
}
module.exports = Project;
\ No newline at end of file
'use strict'
const config = require('../template')
module.exports = () => {
console.log(config.tpl)
process.exit()
}
\ No newline at end of file
const path = require('path');
const fse = require('fs-extra');
const { INJECT_FILES } = require('./constants');
function getRootPath() {
return path.resolve(__dirname, './..');
}
function getPackageVersion() {
const version = require(path.join(getRootPath(), 'package.json')).version;
return version;
}
function logPackageVersion() {
const msg = `agera-h5-cli version: ${getPackageVersion()}`;
console.log();
console.log(msg);
console.log();
}
exports.logPackageVersion = logPackageVersion;
function getDirFileName(dir) {
try {
const files = fse.readdirSync(dir);
const filesToCopy = [];
files.forEach((file) => {
if (file.indexOf(INJECT_FILES) > -1) return;
filesToCopy.push(file);
});
return filesToCopy;
} catch (e) {
return [];
}
}
exports.getDirFileName = getDirFileName;
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "agera-h5-cli",
"version": "1.0.0",
"description": "agera h5 cli",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"agera-h5": "bin/start"
},
"url": "https://github.com/johnbian/agera-h5-cli",
"email": "byh19920310@126.com",
"author": {
"name": "johnbian",
"email": "byh19920310@126.com"
},
"repository": {
"type": "git",
"url": "https://github.com/johnbian/agera-h5-cli.git"
},
"license": "ISC",
"dependencies": {
"chalk": "^2.4.2",
"co": "^4.6.0",
"co-prompt": "^1.0.0",
"commander": "^2.19.0",
"download-git-repo": "^3.0.2",
"fs-extra": "^9.0.0",
"inquirer": "^7.1.0",
"mem-fs": "^1.1.3",
"mem-fs-editor": "^6.0.0",
"ora": "^4.0.3"
}
}
{
"tpl":{
"vue-ts-vw":{
"url":"direct:http://10.29.36.2/john.bian/vue-ts-vw.git",
"branch":"master"
},
"jquery-MPA":{
"url":"direct:http://10.29.36.2/john.bian/jquery-MPA.git",
"branch":"master"
}}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment