sydapi/public.script/index.js
2021-06-29 11:32:24 +08:00

209 lines
5.0 KiB
JavaScript

const l = require("./log")
const fs = require('fs');
const path = require('path');
const util = require('util');
const cmd = require('node-cmd');
const cmdget = util.promisify(cmd.get);
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("存在旧文件,清空旧文件")
}
cmdget("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) {
cmdget('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 cmdget('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 cmdget("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 cmdget("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 cmdget('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 )
cmdget('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-(?<version>\S+)$/gm.exec(value)
l.i( r.groups.version )
version = r.groups.version
return cmdget('git log -1')
}).then(function (value) {
l.d( value )
var r = /^commit\s(?<id>\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("<dependencies>")
var tail = pom.indexOf("</dependencies>") + 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("打包完成!")
cmdget('start .\\public')
}).catch(function (reason) {
l.e( reason )
})
}
main()