Skip to content

Commit

Permalink
add eos-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
YvetteLau committed Jul 24, 2019
1 parent 0c82347 commit f10d147
Show file tree
Hide file tree
Showing 23 changed files with 5,047 additions and 0 deletions.
12 changes: 12 additions & 0 deletions eos-cli/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"env",
{
"targets": {
"node": "current"
}
}
]
]
}
23 changes: 23 additions & 0 deletions eos-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### 安装依赖

`npm install`

### 启动

`npm run watch`

### 执行 `npm link`

此时就可以使用 `eos` 命令了。

- `eos init vue-template myVue`
- `eos config get`
- `eos config set type orgs`
- `eos config set registry vuejs-templates`

- `eos config set type users`
- `eos config set registry YvetteLau`

### 发布

开发完成后,即可发布至 npm.
2 changes: 2 additions & 0 deletions eos-cli/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /usr/bin/env node
require('../dist/main.js');
29 changes: 29 additions & 0 deletions eos-cli/dist/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

var _rc = require('./utils/rc');

let config = async (action, key, value) => {
switch (action) {
case 'get':
if (key) {
let result = await (0, _rc.get)(key);
console.log(result);
} else {
let obj = await (0, _rc.getAll)();
Object.keys(obj).forEach(key => {
console.log(`${key}=${obj[key]}`);
});
}
break;
case 'set':
(0, _rc.set)(key, value);
break;
case 'remove':
(0, _rc.remove)(key);
break;
default:
break;
}
}; // 管理 .eosrc 文件 (当前用户目录下)

module.exports = config;
12 changes: 12 additions & 0 deletions eos-cli/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
// 主的流程控制
let apply = (action, ...args) => {
//babel-env
require(`./${action}`)(...args);
};

exports.default = apply;
65 changes: 65 additions & 0 deletions eos-cli/dist/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

var _get = require('./utils/get');

var _ora = require('ora');

var _ora2 = _interopRequireDefault(_ora);

var _inquirer = require('inquirer');

var _inquirer2 = _interopRequireDefault(_inquirer);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _logSymbols = require('log-symbols');

var _logSymbols2 = _interopRequireDefault(_logSymbols);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

let init = async (templateName, projectName) => {
//项目不存在
if (!_fs2.default.existsSync(projectName)) {
//命令行交互
_inquirer2.default.prompt([{
name: 'description',
message: 'Please enter the project description: '
}, {
name: 'author',
message: 'Please enter the author name: '
}]).then(async answer => {
//下载模板 选择模板
//通过配置文件,获取模板信息
let loading = (0, _ora2.default)('downloading template ...');
loading.start();
(0, _get.downloadLocal)(templateName, projectName).then(() => {
loading.succeed();
const fileName = `${projectName}/package.json`;
if (_fs2.default.existsSync(fileName)) {
const data = _fs2.default.readFileSync(fileName).toString();
let json = JSON.parse(data);
json.name = projectName;
json.author = answer.author;
json.description = answer.description;
//修改项目文件夹中 package.json 文件
_fs2.default.writeFileSync(fileName, JSON.stringify(json, null, '\t'), 'utf-8');
console.log(_logSymbols2.default.success, _chalk2.default.green('Project initialization finished!'));
}
}, () => {
loading.fail();
});
});
} else {
//项目已经存在
console.log(_logSymbols2.default.error, _chalk2.default.red('The project already exists'));
}
};

module.exports = init;
60 changes: 60 additions & 0 deletions eos-cli/dist/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _get = require('./utils/get');

var _ora = require('ora');

var _ora2 = _interopRequireDefault(_ora);

var _inquirer = require('inquirer');

var _inquirer2 = _interopRequireDefault(_inquirer);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

let install = async () => {
//下载模板 选择模板
//通过配置文件,获取模板信息
let loading = (0, _ora2.default)('fetching template ...');
loading.start();
let list = await (0, _get.repoList)();
loading.succeed();
list = list.map(({ name }) => name);

let result = await _inquirer2.default.prompt([{
type: 'list',
name: 'project',
choices: list,
questions: 'choice your template'
}]);
//项目名字
let project = result.project;
//获取当前项目的版本号
loading = (0, _ora2.default)('fetching tag ...');
loading.start();
list = await (0, _get.tagList)(project);
loading.succeed();
list = list.map(({ name }) => name);

let answer = await _inquirer2.default.prompt([{
type: 'list',
name: 'tag',
choices: list,
questions: 'choice tag'
}]);
let tag = answer.tag;
console.log(project, tag);

//下载文件(先下载到缓存文件)
//ly-cli init
loading = (0, _ora2.default)('download project...');
loading.start();
await (0, _get.downloadLocal)(project, tag);
loading.succeed();
};

exports.default = install;
77 changes: 77 additions & 0 deletions eos-cli/dist/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

var _commander = require('commander');

var _commander2 = _interopRequireDefault(_commander);

var _constants = require('./utils/constants');

var _index = require('./index');

var _index2 = _interopRequireDefault(_index);

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
* eos commands
* - config
* - init
*/

let actionMap = {
init: {
description: 'generate a new project from a template',
usages: ['eos init templateName projectName']
},
config: {
alias: 'cfg',
description: 'config .eosrc',
usages: ['eos config set <k> <v>', 'eos config get <k>', 'eos config remove <k>']

}
//other commands
};

Object.keys(actionMap).forEach(action => {
_commander2.default.command(action).description(actionMap[action].description).alias(actionMap[action].alias) //别名
.action(() => {
switch (action) {
case 'config':
//配置
(0, _index2.default)(action, ...process.argv.slice(3));
break;
case 'init':
(0, _index2.default)(action, ...process.argv.slice(3));
break;
default:
break;
}
});
});

function help() {
console.log('\r\nUsage:');
Object.keys(actionMap).forEach(action => {
actionMap[action].usages.forEach(usage => {
console.log(' - ' + usage);
});
});
console.log('\r');
}

_commander2.default.usage('<command> [options]');
_commander2.default.on('-h', help);
_commander2.default.on('--help', help);
_commander2.default.version(_constants.VERSION, '-V --version').parse(process.argv);

// eos 不带参数时
if (!process.argv.slice(2).length) {
_commander2.default.outputHelp(make_green);
}
function make_green(txt) {
return _chalk2.default.green(txt);
}
1 change: 1 addition & 0 deletions eos-cli/dist/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";
12 changes: 12 additions & 0 deletions eos-cli/dist/utils/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
let betterRequire = exports.betterRequire = absPath => {
let module = require(absPath);
if (module.default) {
return module.default;
}
return module;
};
26 changes: 26 additions & 0 deletions eos-cli/dist/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DEFAULTS = exports.RC = exports.VERSION = undefined;

var _package = require('../../package.json');

//当前 package.json 的版本号
const VERSION = exports.VERSION = _package.version;

// 用户的根目录
const HOME = process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'];

// 配置文件目录
const RC = exports.RC = `${HOME}/.eosrc`;

// RC 配置下载模板的地方,给 github 的 api 使用
// https://api.github.com/users/YvetteLau/repos
// https://api.github.com/${type}/${registry}/repos
// 模板下载地址可配置
const DEFAULTS = exports.DEFAULTS = {
registry: 'YvetteLau',
type: 'users'
};
27 changes: 27 additions & 0 deletions eos-cli/dist/utils/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.downloadLocal = undefined;

var _rc = require('./rc');

var _downloadGitRepo = require('download-git-repo');

var _downloadGitRepo2 = _interopRequireDefault(_downloadGitRepo);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const downloadLocal = exports.downloadLocal = async (templateName, projectName) => {
let config = await (0, _rc.getAll)();
let api = `${config.registry}/${templateName}`;
return new Promise((resolve, reject) => {
(0, _downloadGitRepo2.default)(api, projectName, err => {
if (err) {
reject(err);
}
resolve();
});
});
};
Loading

0 comments on commit f10d147

Please sign in to comment.