thinkjs/test/lib/utils.js
2017-07-01 16:13:24 +08:00

45 lines
985 B
JavaScript

const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const exec = require('child_process').exec;
const CONFIG_PATH = path.resolve(__dirname, '../runtime/config');
function createFile(dir, filename, content, cb) {
if (!fs.existsSync(dir)) {
mkdirp.sync(dir);
}
let filePath = path.join(dir, filename);
fs.openSync(filePath, 'w');
if (content) {
fs.writeFile(filePath, content, cb);
}
}
function touchConfig(config = {}, cb) {
let json = JSON.stringify(config,function(key, val) {
if (typeof val === 'function') {
return val + ''; // implicitly `toString` it
}
return val;
})
let content = `module.exports = ${json}`;
createFile(CONFIG_PATH, 'config.js', content);
}
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
const killByPort = (port)=>{
if(!port){
return
}
exec(`kill -9 $(lsof -t -i:${port})`)
}
module.exports = {
touchConfig,
sleep,
killByPort
}