const l = require("./log") const os = require("os"); const fs = require('fs'); const path = require('path'); const util = require('util'); const { exec } = require('child_process') function sync( cmds , options = {}){ if ( options.detached ) { throw "sync 不支持 detached" } options = Object.assign({ // cwd : os.tmpdir(), encoding: 'buffer' }, options) // 支持多命令 let cmd = cmds // win 乱码问题 let encode = "win32" === os.platform() ? 'cp936' : "utf-8" return new Promise(function (resolve, reject) { let process = exec( cmd , options , function (e, stdout, stderr) { // console.log( e, // iconv.decode( stdout, 'cp936'), // iconv.decode( stderr, 'cp936') // ) if ( e ) { reject( e ) } else { resolve( stdout.toString() ) // resolve({ // pid : process.pid, // exitCode : process.exitCode, // stdout : stdout.toString(), // stderr : stderr.toString() // }) } }) console.log( "process", process.pid ) }) } var version = "" const project_path = path.join(__dirname, "../") const out_path = path.join(project_path, "public") // 用户文档 const man = "工银亚洲加密机JAVA接口说明文档V1.00.pdf" function prepareDir(){ return new Promise(function (cbok, cberr) { if ( fs.existsSync( out_path ) ){ l.i("存在旧文件,清空旧文件") } sync("rd /s/q .\\public").catch(function (reason) { // pass }).finally(function () { l.i("创建新文件") fs.mkdirSync( out_path ) fs.mkdirSync( path.join( out_path, "jar" )) fs.mkdirSync( path.join( out_path, "jar", "dependences")) fs.mkdirSync( path.join( out_path, "doc" )) fs.mkdirSync( path.join( out_path, "html" )) //fs.mkdirSync( path.join( out_path, "sample" )) l.i("目录准备完成") cbok() }) }) } function mvn(){ l.i("调用 maven 打包,可能需要好几分钟。") return new Promise(function (cbok, cberr) { sync('mvn package -DskipTests').then(function (value, error) { l.d( value ) if ( value.indexOf("BUILD SUCCESS") < 0 ){ throw new Error("请检查 maven package 命令") } // 移动文件到发布目录 fs.renameSync(path.join(project_path, "target", "sydapi4j-jar-with-dependencies.jar") , path.join(out_path, "jar", "sydapi4j-jar-with-dependencies.jar")) fs.renameSync(path.join(project_path, "target", "sydapi4j.jar") , path.join(out_path, "jar", "sydapi4j.jar")) cbok() }) }) } function doc(){ return sync('mvn javadoc:javadoc').then(function (value, error) { l.d( value ) // if ( value.indexOf("BUILD SUCCESS") < 0 ){ // throw new Error("请检查 maven doc 命令") // } }).catch(function (reason) { l.d("忽略文档生成错误") }) } function zipjar(){ return sync("7z a .\\public\\sydapi-"+ version +".zip .\\public\\jar\\").then(function (value, error) { l.d( value ) if ( value.indexOf("Everything is Ok") < 0 ){ throw new Error("请检查 7z 命令") } }) } function zipdoc(){ return sync("7z a .\\public\\sydapi-doc-"+ version +".zip .\\public\\doc\\ .\\public\\html\\ .\\src\\test\\java\\com\\sunyard\\sydapi\\sample .\\release.txt").then(function (value, error) { l.d( value ) if ( value.indexOf("Everything is Ok") < 0 ){ throw new Error("请检查 7z 命令") } }) } function dependencies(){ return sync('mvn dependency:copy-dependencies -DoutputDirectory=./public/jar/dependences').then(function (value, error) { l.d( value ) if ( value.indexOf("BUILD SUCCESS") < 0 ){ throw new Error("请检查 maven dependencies 命令") } }) } async function main() { l.i("项目目录 : " + project_path ) sync('git status').then(function (value, error) { l.d( value ) if ( value.indexOf("Command failed") > 0 ){ throw new Error("请检查 git 命令") } if ( value.indexOf("Changes not staged for commit") > 0){ throw new Error("项目有文件未保存") } if ( value.indexOf("Changes to be committed") > 0 ){ throw new Error("项目有文件未提交") } // 匹配分支,获取前两位版本 var r = /^On branch dev-(?\S+)$/gm.exec(value) l.i( r.groups.version ) version = r.groups.version return sync('git log -1') }).then(function (value) { l.d( value ) var r = /^commit\s(?\S+)$/gm.exec(value) l.i( r.groups.id ) // 缩短 HASH var short = r.groups.id.substr(0, 6) version += "." + short l.i("目前版本: " + version ) return prepareDir() }).then(function (value) { return mvn() }).then(function (value) { return dependencies() }).then(function (value) { var pom = fs.readFileSync("pom.xml", { encoding : "UTF-8" }) var head = pom.indexOf("") var tail = pom.indexOf("") + 15 var dependencies = pom.substring( head, tail ) fs.writeFileSync( path.join( out_path, "jar", "dependences.xml") , dependencies ) }).then(function (value) { fs.copyFileSync( path.join(project_path, "docs", man) , path.join(out_path, "doc", man )) }).then(function (value) { return doc() }).then(function (value) { return zipdoc() }).then(function (value) { return zipjar() }).then(function () { l.i("打包完成!") sync('start .\\public') }).catch(function (reason) { l.e( reason ) }) } main()