1/* 2 * Copyright (C) 2024 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 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 fs = require('fs'); 17const path = require('path'); 18const spawn = require('child_process').spawn; 19const csvParser = require('csv-parser'); 20const { parseJson } = require("../../previewer_host/utils/json.js"); 21function readCSVFile() { 22 const filePath = 'data.csv'; 23 const readStream = fs.createReadStream(filePath); 24 const rows = []; 25 return new Promise((resolve, reject) => { 26 readStream.pipe(csvParser()) 27 .on('data', (row) => { 28 rows.push(row); 29 }) 30 .on('end', () => { 31 resolve(rows); 32 }) 33 .on('error', (error) => { 34 throw error; 35 }); 36 }) 37} 38 39function promiseRun(list, pr) { 40 const hostPath = path.join('./previewer_host/main.js'); 41 let errorMes; 42 return new Promise((resolve, reject) => { 43 let path = setFile(JSON.stringify(list)); 44 let workerProcess = spawn('node', [hostPath, pr, path], { encoding: 'utf-8', stdio: 'pipe' }); 45 try { 46 workerProcess.stdout.on('data', function (data) { 47 console.log("stdout:", `${new Date().toTimeString()}-${data.toString()}\n`); 48 }); 49 workerProcess.stderr.on('data', function (data) { 50 console.log("stderr:", `${new Date().toTimeString()}-${data.toString()}\n`); 51 errorMes += `${formatDate(new Date())}-错误:${data.toString()}\n`; 52 }); 53 workerProcess.on('exit', (code) => { 54 if (code === 0) { 55 console.log('执行成功'); 56 resolve({ code: 0 }); 57 } else { 58 console.log('执行失败,退出码:', code); 59 resolve({ code: 1, errorMes: errorMes }); 60 } 61 }); 62 } catch (error) { 63 throw error; 64 } finally { 65 errorMes = ''; 66 } 67 }) 68} 69function setFile(flieData) { 70 const fs = require('fs'); 71 const data = flieData; 72 let jsonData = parseJson('./param.json'); 73 const filePathData = jsonData.filePathData; 74 75 76 fs.writeFile(filePathData, data, (err) => { 77 if (err) throw err; 78 }); 79 try { 80 fs.writeFileSync(filePathData, data); 81 return filePathData; 82 } catch (err) { 83 throw err; 84 } 85} 86 87function formatDate(date) { 88 const year = date.getFullYear(); 89 const month = date.getMonth() + 1; 90 const day = date.getDate(); 91 const hours = date.getHours(); 92 const minutes = date.getMinutes(); 93 const seconds = date.getSeconds(); 94 let format_date = `${year}-${month}-${day}` + 95 `${hours.toString().padStart(2, '0')}` + 96 `:${minutes.toString().padStart(2, '0')}` + 97 `:${seconds.toString().padStart(2, '0')}`; 98 return format_date; 99} 100 101module.exports = { 102 readCSVFile, 103 promiseRun 104}