Commit 6968a4fe authored by rockyl's avatar rockyl

game.json 改为 project.json

parent 88341b82
...@@ -10,4 +10,5 @@ program ...@@ -10,4 +10,5 @@ program
.command('build', 'Build project').alias('b') .command('build', 'Build project').alias('b')
.command('dev', 'Build project automatic').alias('d') .command('dev', 'Build project automatic').alias('d')
.command('publish', 'Publish project').alias('p') .command('publish', 'Publish project').alias('p')
.command('upload', 'Upload project to oss').alias('u')
.parse(process.argv); .parse(process.argv);
const program = require('commander'); const program = require('commander');
const {exit, executeBuildProcess} = require('./tools'); const {exit, executeBuildProcess, updateProjectConfig} = require('./tools');
program program
.option('-v, --version', 'Version of publish')
.parse(process.argv); .parse(process.argv);
async function execute() { async function execute() {
const version = await executeBuildProcess('publish'); const version = await executeBuildProcess('publish');
console.log(version); updateProjectConfig('version', version);
} }
execute().catch(e => { execute().catch(e => {
......
const program = require('commander');
const {exit, getProjectConfig} = require('./tools');
const ossUpload = require('./oss-upload');
program
.option('-v, --version-code [string]', 'Version to upload')
.option('-m, --mode [string]', 'prod or test', 'prod')
.parse(process.argv);
async function execute() {
let remotePath;
let version = program.versionCode;
const gameProjectConfig = getProjectConfig();
if(gameProjectConfig){
remotePath = gameProjectConfig['remote-path'];
version = version ? version : gameProjectConfig['version'];
}
if(version){
if(remotePath){
try {
await ossUpload({
localDir: './bin-release/web/' + version,
remoteDir: `/db_games/${remotePath}/${version}`
}, program.mode);
}catch (e) {
exit(e.message, 3)
}
}else{
exit('remote-path setting is not exist', 2)
}
}else{
exit('Can\'t find version argument or project setting', 3)
}
}
execute().catch(e => {
exit(e);
});
const OSS = require('ali-oss');
const chalk = require('chalk');
const ProgressBar = require('progress');
const fs = require('fs');
const path = require('path');
let _type, _options, _store, _bar, _files;
let _uploadFiles, _errorFiles;
module.exports = async function (props, type) {
_type = type;
const defaultOptions = {
localDir: undefined,
remoteDir: undefined
};
_options = Object.assign({}, defaultOptions, props);
if (!_options.localDir || !_options.remoteDir) {
console.log(chalk.red('缺少参数,初始化失败'));
return;
}
init();
await start();
};
function init() {
_files = [];
flattenFileTree(_options.localDir, function(filePath){
_files.push(path.relative(_options.localDir, filePath))
});
_store = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: 'LTAIdGi1IOap7fkF',
accessKeySecret: 'SKrOOp6EVtDGEV47yn0t2h97gyNioQ',
bucket: _type === 'prod' ? 'duiba' : 'daily-duiba'
});
_bar = new ProgressBar(chalk.yellow(` 文件上传中 [:bar] :current/${_files.length} :percent :elapseds`), {
complete: '●',
incomplete: '○',
width: 20,
total: _files.length,
callback: () => {
console.log(chalk.blue(` 本次队列文件共${_files.length}个,上传文件${_uploadFiles}个,上传失败文件${_errorFiles}个`));
}
});
return this;
}
function flattenFileTree(parent, callback){
const filesIn = fs.readdirSync(parent);
for(let file of filesIn){
if(file.indexOf('.DS_Store') < 0){
const filePath = path.join(parent, file);
const stat = fs.lstatSync(filePath);
if(stat.isDirectory()){
flattenFileTree(filePath, callback);
}else{
callback(filePath);
}
}
}
}
async function start(){
_uploadFiles = 0;
_errorFiles = 0;
for(let file of _files){
try {
const localFile = path.join(_options.localDir, file);
const remotePath = path.join(_options.remoteDir, file);
await _store.put(remotePath, localFile);
_uploadFiles += 1;
_bar.tick();
}catch (e) {
_errorFiles += 1;
console.log(e);
}
}
}
...@@ -51,38 +51,55 @@ exports.npmRun = function (path, scriptName) { ...@@ -51,38 +51,55 @@ exports.npmRun = function (path, scriptName) {
return childProcess('npm', ['run', scriptName], path); return childProcess('npm', ['run', scriptName], path);
}; };
const projectFile = './project.json';
const getProjectConfig = exports.getProjectConfig = function () {
const gameFile = path.resolve(projectFile);
if (fs.existsSync(gameFile)) {
return require(gameFile)
}
};
exports.updateProjectConfig = function (key, value) {
const config = getProjectConfig();
if (config) {
config[key] = value;
}
fs.writeFileSync(projectFile, JSON.stringify(config, null, '\t'));
};
exports.executeBuildProcess = function (cmd) { exports.executeBuildProcess = function (cmd) {
return new Promise((resolve, reject)=>{ return new Promise((resolve, reject) => {
let buildProcessName; let buildProcessName;
const gameFile = path.resolve('./game.json');
if(fs.existsSync(gameFile)){ const gameProjectConfig = getProjectConfig();
const gameProjectConfig = require(gameFile); if (gameProjectConfig) {
buildProcessName = gameProjectConfig['build-process'] buildProcessName = gameProjectConfig['build-process'];
} }
if(buildProcessName){ if (buildProcessName) {
try { try {
const npm = require('global-npm'); const npm = require('global-npm');
const modulePath = path.resolve(npm.GLOBAL_NPM_PATH, '../game-cli-build-process-' + buildProcessName); const modulePath = path.resolve(npm.GLOBAL_NPM_PATH, '../game-cli-build-process-' + buildProcessName);
const buildProcess = require(modulePath); const buildProcess = require(modulePath);
if(buildProcess.hasOwnProperty(cmd)){ if (buildProcess.hasOwnProperty(cmd)) {
resolve(buildProcess); resolve(buildProcess);
}else{ } else {
console.log(`Build process [${name}] has not implement ${cmd} function`) console.log(`Build process [${name}] has not implement ${cmd} function`)
} }
}catch (e) { } catch (e) {
reject('Build process is not found, please install with --global'); reject('Build process is not found, please install with --global');
} }
} }
reject('Build process name is not found, please setup game.json'); reject('Build process name is not found, please setup ' + projectFile);
}).then( }).then(
(buildProcess)=>{ (buildProcess) => {
return buildProcess[cmd]() return buildProcess[cmd]()
} }
).then( ).then(
(version)=>{ (version) => {
console.log(`Build process [${cmd}] execute success`); console.log(`Build process [${cmd}] execute success`);
return version; return version;
} }
......
...@@ -10,8 +10,11 @@ ...@@ -10,8 +10,11 @@
"author": "rocky.l", "author": "rocky.l",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"ali-oss": "^6.0.1",
"chalk": "^2.4.1",
"commander": "^2.18.0", "commander": "^2.18.0",
"fs-extra": "^6.0.1", "fs-extra": "^6.0.1",
"global-npm": "^0.3.0" "global-npm": "^0.3.0",
"progress": "^2.0.0"
} }
} }
This diff is collapsed.
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