book-cliiii
Version:
Command line interface for front end project
143 lines (131 loc) • 4.03 kB
JavaScript
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
/* eslint-disable no-unused-expressions */
/* eslint-disable babel/no-unused-expressions */
/* eslint-disable class-methods-use-this */
/* eslint-disable no-underscore-dangle */
/**
* 客户端创建使用
* @private
*/
// const debug = require('debug')('BOOK');
// const NpmApi = require('npm-api');
const path = require('path');
const execa = require('execa');
const { createRequire } = require('module');
// const { stopSpinner } = require('./utils/spinner');
const Install = require('./utils/install');
const Generator = require('./utils/generator');
const repository = new Install();
// const REPOSITORY_FOLDER = '.book-repository';
// const npm = new NpmApi();
class Book {
constructor() {
this.generatorPath;
}
/**
* @private
* @method
* 校验当前模板是否存在
* @param {String} packageName - Package name.
* @throw 如果查询不到包则抛错
*/
// TODO
// async checkPackage(packageName) {
// const npmRepo = npm.repo(packageName);
// let packages;
// try {
// packages = await npmRepo.package('all');
// if (packages.error) {
// throw new Error(packages.error);
// }
// } catch (error) {
// stopSpinner(false);
// debug(`Could not find npm package for ${packageName}`, error);
// }
// }
/**
* 下载模板包
* @method
* @param {String} packageName - 模板名称
* @param {String} version - 版本号
* @param {Function} callback - 下载完成回调配置信息(将prompt作为参数传递)
*/
install(packageName, version = '', callback) {
// 拉取模板并安装
this.generatorPath = repository.requireModule(packageName, version); // 获取模板下载后的绝对路径地址
const require = createRequire('/');
typeof (callback === 'function')
&& callback(require(`${this.generatorPath}/prompts.js`));
// typeof (callback === 'function') && callback(require('./mock/prompts'));
}
/**
* 创建项目文件
* @method
* @param {Object} options - 对话结果集
* @param {String} projectPath - 项目创建路径
* @param {Function} callback - 创建完成回调
*/
async createProject(answer = {}, projectPath, callback) {
const repositoryPath = path.join(projectPath, answer.projectName);
// 初始化模板 generator
const generator = new Generator(repositoryPath);
const require = createRequire('/');
// 读取模板 generator 文件并执行
require(`${this.generatorPath}/generator/index.js`)(generator, answer);
let result = {
data: true,
msg: '创建成功',
};
// 执行 generate
try {
await generator.generate();
} catch (error) {
result = {
data: false,
msg: '创建失败!',
};
}
typeof callback === 'function' && callback(result);
// const copyFile = require('./utils/copyFile');
// const target = path.resolve(__dirname, '..', 'lib/mock/generator/template');
// const toDir = path.resolve(projectPath, answer.name);
// copyFile(target, toDir, (err) => {
// const result = {
// data: true,
// msg: '创建成功',
// };
// if (err) {
// result = {
// data: false,
// msg: '创建失败!',
// };
// }
// (typeof callback === 'function') && callback(result);
// });
}
/**
* 安装项目依赖
* @method
* @param {String} projectPath - 安装地址
*/
installPkg(projectPath) {
execa.sync('npm', [
'config',
'set',
'@jd:registry=http://registry.m.jd.com',
]);
execa.sync('npm', [
'config',
'set',
'@jdpop:registry=http://registry.m.jd.com',
]);
execa.sync('npm', [
'config',
'set',
'@jdbk:registry=http://registry.m.jd.com',
]);
execa.sync('npm', ['install', '--prefix', path.resolve(projectPath)]);
}
}
module.exports = Book;