1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const path = require('path'); 17const fs = require("fs"); 18const child_process = require("child_process"); 19const os = require("os"); 20const log4js = require("log4js"); 21 22const compileServer = true 23const outDir = "dist" 24 25const sdkWams = [ 26 "trace_streamer_sdk_builtin.js", 27 "trace_streamer_sdk_builtin.wasm" 28] 29 30const staticPath = [ 31 "/src/img", 32 "/server/cert", 33 "/src/doc", 34 "/src/figures", 35] 36 37const staticFiles = [ 38 "/server/version.txt", 39 "/src/index.html", 40 "/src/base-ui/icon.svg", 41 "/server/wasm.json" 42] 43 44const thirdParty = [ 45 {srcFilePath: "/third-party/sql-wasm.wasm", distFilePath: "/trace/database/sql-wasm.wasm"}, 46 {srcFilePath: "/third-party/sql-wasm.js", distFilePath: "/trace/database/sql-wasm.js"}, 47 {srcFilePath: "/third-party/worker.sql-wasm.js", distFilePath: "/trace/database/worker.sql-wasm.js"} 48] 49 50let log; 51 52function cpFile(from, to) { 53 if (fs.existsSync(from)) { 54 fs.writeFileSync(to, fs.readFileSync(from)) 55 log.info("cp file %s to %s", from, to) 56 } else { 57 log.warn("file %s is not exists", from, to) 58 } 59} 60 61function checkEnvironment() { 62 let goVersion = child_process.execSync("go version", { 63 encoding: "utf-8" 64 }) 65 log.info("go is", goVersion) 66 let nodeVersion = child_process.execSync("node -v", { 67 encoding: "utf-8" 68 }) 69 log.info("node version is", nodeVersion) 70 let tscVersion = child_process.execSync("tsc -v", { 71 encoding: "utf-8" 72 }) 73 log.info("tsc version is", tscVersion) 74 if (goVersion == "" || nodeVersion == "" || tscVersion == "") { 75 return false; 76 } 77 return true; 78} 79 80 81function initLog() { 82 log4js.configure({ 83 appenders: { 84 out: {type: "stdout"}, 85 }, 86 categories: { 87 default: {appenders: ["out"], level: "debug"}, 88 }, 89 }); 90 return log4js.getLogger("smartPerf"); 91} 92 93 94function main() { 95 log = initLog(); 96 if (!checkEnvironment()) { 97 return; 98 } 99 // clean outDir 100 let outPath = path.normalize(path.join(__dirname, "/", outDir)); 101 if (checkDirExist(outPath)) { 102 log.info("delete the last compilation result") 103 removeDir(outPath) 104 log.info("delete the last compilation success") 105 } 106 // run tsc compile 107 log.info("start compiling typeScript code") 108 let rootPath = path.join(__dirname, "/"); 109 child_process.execSync("tsc -p " + rootPath, { 110 encoding: "utf-8" 111 }) 112 log.info("compiling typeScript code success") 113 // run cp to mv all staticFile 114 staticFiles.forEach(value => { 115 let filePath = path.join(__dirname, value) 116 let distFile; 117 if (value.startsWith("/src")) { 118 distFile = path.join(__dirname, outDir, value.substring(4, value.length + 1)) 119 } else if (value.startsWith("/server")) { 120 distFile = path.join(__dirname, outDir, value.substring(7, value.length + 1)) 121 } 122 cpFile(filePath, distFile); 123 }) 124 staticPath.forEach(value => { 125 let pa = path.join(__dirname, value) 126 let distPath; 127 if (value.startsWith("/src")) { 128 distPath = path.join(__dirname, outDir, value.substring(4, value.length + 1)) 129 } else if (value.startsWith("/server")) { 130 distPath = path.join(__dirname, outDir, value.substring(7, value.length + 1)) 131 } 132 copyDirectory(pa, distPath); 133 }) 134 thirdParty.forEach(value => { 135 let thirdFile = path.join(__dirname, value.srcFilePath) 136 let thirdDistFile = path.join(__dirname, outDir, value.distFilePath) 137 cpFile(thirdFile, thirdDistFile); 138 }) 139 let traceStreamer = path.normalize(path.join(__dirname, "/bin")); 140 if (checkDirExist(traceStreamer)) { 141 let dest = path.normalize(path.join(__dirname, outDir, "/bin")); 142 copyDirectory(traceStreamer, dest) 143 // to mv traceStream Wasm and js 144 cpFile(traceStreamer + "/trace_streamer_builtin.js", rootPath + outDir + "/trace/database/trace_streamer_builtin.js") 145 cpFile(traceStreamer + "/trace_streamer_builtin.wasm", rootPath + outDir + "/trace/database/trace_streamer_builtin.wasm") 146 if (sdkWams.length > 0) { 147 sdkWams.forEach(fileName => { 148 cpFile(traceStreamer + "/" + fileName, rootPath + outDir + "/trace/database/"+ fileName) 149 }) 150 } 151 } else { 152 log.error("traceStreamer dir is not Exits") 153 return; 154 } 155 // compile server 156 if (compileServer) { 157 log.log("start compile server") 158 let serverSrc = path.normalize(path.join(__dirname, "/server/main.go")); 159 let rs; 160 if (os.type() === "Windows_NT") { 161 rs = child_process.spawnSync("go", ["build", "-o", outPath, serverSrc], { 162 encoding: "utf-8" 163 }) 164 } else if (os.type() == "Darwin") { 165 rs = child_process.spawnSync("go", ["build", "-o", outPath + "/main", serverSrc], { 166 encoding: "utf-8" 167 }) 168 } else { 169 rs = child_process.spawnSync("go", ["build", "-o", outPath + "/main", serverSrc], { 170 encoding: "utf-8" 171 }) 172 } 173 if (rs.status == 0) { 174 log.log("compile server success") 175 } else { 176 log.error("compile server failed", rs) 177 } 178 } else { 179 log.warn("skip compile server") 180 } 181 log.log("smartPerf compile success") 182} 183 184 185function copyDirectory(src, dest) { 186 if (checkDirExist(dest) == false) { 187 fs.mkdirSync(dest); 188 } 189 if (checkDirExist(src) == false) { 190 return false; 191 } 192 let directories = fs.readdirSync(src); 193 directories.forEach((value) => { 194 let filePath = path.join(src, value); 195 let fileSys = fs.statSync(filePath); 196 if (fileSys.isFile()) { 197 let destPath = path.join(dest, value); 198 log.info("cp file %s to %s", filePath, destPath) 199 fs.copyFileSync(filePath, destPath); 200 } else if (fileSys.isDirectory()) { 201 copyDirectory(filePath, path.join(dest, value)); 202 } 203 }); 204} 205 206function checkDirExist(dirPath) { 207 return fs.existsSync(dirPath) 208} 209 210function removeDir(outPath) { 211 let files = []; 212 if (fs.existsSync(outPath)) { 213 files = fs.readdirSync(outPath); 214 files.forEach((file, index) => { 215 let curPath = outPath + "/" + file; 216 if (fs.statSync(curPath).isDirectory()) { 217 removeDir(curPath); 218 } else { 219 fs.unlinkSync(curPath); 220 } 221 }); 222 fs.rmdirSync(outPath); 223 } 224} 225 226main();