Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: download template from CDN instead of git #113 #197

Merged
merged 6 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/lavas-cli/bin/dev-lavas-cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node

process.env.NODE_ENV = 'development';
require('../src/commander');
3 changes: 2 additions & 1 deletion packages/lavas-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lavas",
"version": "2.2.9",
"version": "2.2.10",
"description": "Lavas solution cli tool",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -38,6 +38,7 @@
"babel-runtime": "^6.26.0",
"chalk": "^1.1.3",
"commander": "^2.11.0",
"compressing": "^1.3.1",
"connect-history-api-fallback": "^1.3.0",
"etpl": "^3.2.0",
"express": "^4.16.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/lavas-cli/src/lib/scaffold/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module.exports = {
development: 'https://bos.nj.bpc.baidu.com/mms-res/lavas-scaffold/config_dev.json'
},

TAR_GZ_ENDPOINT: 'https://bos.nj.bpc.baidu.com/v1/assets/lavas/',

/**
* render common data
*
Expand Down
58 changes: 24 additions & 34 deletions packages/lavas-cli/src/lib/scaffold/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,49 @@
/* eslint-disable fecs-prefer-async-await */
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const glob = require('glob');
const archiver = require('archiver');
const etpl = require('etpl');
const Ajv = require('ajv');
const exec = require('mz/child_process').exec;
const axios = require('axios');
const compressing = require('compressing');

const conf = require('./config');
const getMeta = require('./getMeta');
const store = require('./store');
const schema = require('./schema');
const utils = require('../utils');
const locals = require('../../locals')();

/**
* 从 git 上 download 代码下来
* 通过指定框架名和模版名从服务器上拉取模版(要求在模版 relase 的时候注意上传的 CDN 路径)
*
* @param {string} repo git repo
* @param {string} targetPath 存储的目标目录
* @param {string} branchName 分支名
* @return {Promise} promise 对象
* @param {string} framework 框架名称
* @param {string} template 模版名称
* @param {string} targetPath 模版下载后存放路径
*/
async function downloadFromGit(repo, targetPath, branchName) {
async function downloadTemplateFromCloud(framework, template, targetPath) {
const outputFilename = path.resolve(targetPath, 'template.tar.gz');
fs.existsSync(targetPath) && fs.removeSync(targetPath);
fs.mkdirsSync(targetPath);

if (!utils.hasCommand('git')) {
throw new Error(locals.NO_GIT_COMMAND + ',' + locals.NO_GIT_COMMAND_DESC);
}
framework = (framework || 'vue').toLowerCase();
template = (template || 'basic').toLowerCase();

try {
// Fixed: windows 下跨盘 cd 命令,必须先要指定盘名
let diskCommand = '';

if (os.platform() === 'win32') {
let diskName = targetPath.split(':')[0];
if (diskName) {
diskCommand = diskName + ': &&';
let result = await axios.request({
responseType: 'arraybuffer',
url: `${conf.TAR_GZ_ENDPOINT}${framework}/${template}/templates.tar.gz`,
method: 'get',
headers: {
'Content-Type': 'application/x-gzip'
}
}
});

// 如果当前文件系统有 download 的缓存,就不重新 clone 了,将代码直接 pull 下来就好了。
if (fs.existsSync(targetPath) && fs.existsSync(path.resolve(targetPath, '.git'))) {
await exec(`${diskCommand}cd "${targetPath}" && git checkout ${branchName} && git pull`);
}
else {
fs.existsSync(targetPath) && fs.removeSync(targetPath);
fs.mkdirsSync(targetPath);
await exec(`git clone ${repo} "${targetPath}"`);
await exec(`${diskCommand}cd "${targetPath}" && git checkout ${branchName}`);
}
fs.writeFileSync(outputFilename, result.data);

return targetPath;
// 解压缩是反响过程,接口都统一为 uncompress
await compressing.tgz.uncompress(outputFilename, targetPath);
fs.removeSync(outputFilename);
}
catch (e) {
throw new Error(locals.DOWNLOAD_TEMPLATE_ERROR);
Expand Down Expand Up @@ -212,12 +204,10 @@ async function getTemplateInfo(metaParam) {
*/
exports.download = async function (metaParams = {}) {
let {framework, template, version} = await getTemplateInfo(metaParams);
let gitRepo = template.git;
let storeDir = path.resolve(
conf.LOCAL_TEMPLATES_DIR,
framework.value, template.value + '_' + version
);
let branchName = template.branch || 'master';
let ajv = new Ajv({allErrors: true});
let metaJsonSchema = store.get('metaJsonSchema') || await schema.getMetaJsonSchema();
let validate = ajv.compile(metaJsonSchema);
Expand All @@ -227,7 +217,7 @@ exports.download = async function (metaParams = {}) {
throw new Error(JSON.stringify(validate.errors));
}

await downloadFromGit(gitRepo, storeDir, branchName);
await downloadTemplateFromCloud(framework.value, template.value, storeDir);
store.set('storeDir', storeDir);

let templateConfigContent = fs.readFileSync(path.resolve(storeDir, 'meta.json'), 'utf-8');
Expand Down
8 changes: 4 additions & 4 deletions packages/lavas-cli/src/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ exports.isNetworkConnect = function () {
/**
* 获取 Lavas 项目的根目录
*
* @params {Object} options 选项
* @params {boolean} options.containsFound 结果包含是否是Lavas项目
* @param {Object} options 选项
* @param {boolean} options.containsFound 结果包含是否是Lavas项目
*
* @return {string|Object} 项目根目录,当options.containsFound 为 true时,一并返回是否是Lavas项目
*/
Expand Down Expand Up @@ -99,7 +99,7 @@ exports.getLavasProjectRoot = function (options = {}) {
};

exports.getLavasCoreVersion = function () {
let lavasProject = exports.getLavasProjectRoot({containsFound: true})
let lavasProject = exports.getLavasProjectRoot({containsFound: true});

if (!lavasProject.found) {
return;
Expand All @@ -112,4 +112,4 @@ exports.getLavasCoreVersion = function () {

let version = fs.readJsonSync(packageJsonPath).version;
return version;
}
};