visop
Version:
A simple CLI for scaffolding visible operation projects.
107 lines (93 loc) • 4.28 kB
JavaScript
// 引入依赖模块
var shell = require('shelljs'); // 调用shell来完成文件的操作,也可以使用node-extra模块
var fs = require('fs-extra');
var path = require('path');
var shellCmd = ''; // 存储shell脚本字符串
var shellResult = {} // 存储shell命令,获取返回结果
const replace = require('replace-in-file'); // 用来做替文本字符串替换
var BASE_PATH = path.resolve(path.join(__dirname, '..')); // 代码所在根目录
var CURRENT_PATH = __dirname; // 当前文件所在目录
exports.beforeAdd = function(data, callback) {
console.log('beforeAdd:', data);
addElement(data)
return callback()
}
exports.beforeUpdate = function(id, data, callback) {
console.log('beforeUpdate,', data);
// updateElement(data)
return callback()
}
exports.beforeDelete = function(id, data, callback) {
console.log('beforeDelete,', callback);
deleteElement(data)
return callback()
}
exports.beforeAll = function(){
console.log('beforeAll')
}
var addElement= function(config){
console.log('add element with data:',config, ' you can achieve this function with any program language you familiar with');
var sourceFile, targetFile
if(connfig.__fromElement){
// __fromElement 执行复制的操作
sourceFile = path.join(CURRENT_PATH, config.__fromElement.id+'.js');
targetFile = path.join(CURRENT_PATH, config.id+'.js');
fs.copySync(sourceFile, targetFile, {overwrite:false});
sourceFile = path.join(CURRENT_PATH, config.__fromElement.id+'.json');
targetFile = path.join(CURRENT_PATH, config.id+'.json');
fs.copySync(sourceFile, targetFile,{overwrite:false});
sourceFile = path.join(CURRENT_PATH, config.__fromElement.id);
targetFile = path.join(CURRENT_PATH, config.id);
fs.copySync(sourceFile, targetFile,{overwrite:false});
console.log('copy element end');
return;
}
sourceFile = path.join(CURRENT_PATH, 'index', 'elements.js');
targetFile = path.join(CURRENT_PATH, config.id+'.js');
fs.copySync(sourceFile, targetFile, {overwrite:false});
sourceFile = path.join(CURRENT_PATH, 'index', 'elements.json');
targetFile = path.join(CURRENT_PATH, config.id+'.json');
fs.copySync(sourceFile, targetFile,{overwrite:false});
sourceFile = path.join(CURRENT_PATH, 'index', 'elements');
targetFile = path.join(CURRENT_PATH, config.id);
fs.copySync(sourceFile, targetFile,{overwrite:false});
console.log('add element end');
}
var updateElement = function(config){
console.log('update element with data:',config, ' you can achieve this function with any program language you familiar with');
var sourceFile, targetFile
sourceFile = path.join(CURRENT_PATH, 'index', 'elements.js');
targetFile = path.join(CURRENT_PATH, config.id+'.js');
fs.copySync(sourceFile, targetFile,{overwrite:false});
sourceFile = path.join(CURRENT_PATH, 'index', 'elements.json');
targetFile = path.join(CURRENT_PATH, config.id+'.json');
fs.copySync(sourceFile, targetFile,{overwrite:false});
sourceFile = path.join(CURRENT_PATH, 'index', 'elements');
targetFile = path.join(CURRENT_PATH, config.id);
fs.copySync(sourceFile, targetFile,{overwrite:false});
console.log('update element end');
};
function nodereplace(file, strfrom, strto){
var options = {
files: file,
from: new RegExp(strfrom,'g'),
to: strto,
};
console.log(options)
try {
const changes = replace.sync(options);
console.log('Modified files:', changes.join(', '));
}
catch (error) {
console.error('Error occurred:', error);
}
}
var deleteElement = function(config){
// 初始化信息
var routeName = config.id;
fs.moveSync(path.join(CURRENT_PATH, config.id+".js"), path.join(CURRENT_PATH, "_"+ config.id+".js"), {overwrite: true})
fs.moveSync(path.join(CURRENT_PATH, config.id+".json"), path.join(CURRENT_PATH, "_"+ config.id+".json"), {overwrite: true})
fs.moveSync(path.join(CURRENT_PATH, config.id), path.join(CURRENT_PATH, "_"+ config.id), {overwrite: true})
console.log('delete route '+routeName+' success');
}