eapp
Version:
386 lines (350 loc) • 16.8 kB
JavaScript
const config = require('../FilePathConfig.js');
var fileManage = require("./FileManage.js");
//将xml解析成对象
var parser = require('fast-xml-parser');
var he = require('he');
//将对象解析成xml
var ParserToXml = require("fast-xml-parser").j2xParser;
var options = {
attributeNamePrefix: "@_",
attrNodeName: "attr", //default is 'false'
textNodeName: "#text",
ignoreAttributes: true,
ignoreNameSpace: false,
allowBooleanAttributes: false,
parseNodeValue: true,
parseAttributeValue: false,
trimValues: true,
cdataTagName: "__cdata", //default is 'false'
cdataPositionChar: "\\c",
parseTrueNumberOnly: false,
arrayMode: false, //"strict"
attrValueProcessor: (val, attrName) => he.decode(val, { isAttributeValue: true }),//default is a=>a
tagValueProcessor: (val, tagName) => he.decode(val), //default is a=>a
stopNodes: ["parse-me-as-string"]
};
var defaultOptions = {
attributeNamePrefix: "@_",
attrNodeName: "@", //default is false
textNodeName: "#text",
ignoreAttributes: true,
cdataTagName: "__cdata", //default is false
cdataPositionChar: "\\c",
format: false,
indentBy: " ",
supressEmptyNode: false,
tagValueProcessor: a => he.encode(a, { useNamedReferences: true }),// default is a=>a
attrValueProcessor: a => he.encode(a, { isAttributeValue: isAttribute, useNamedReferences: true })// default is a=>a
};
/**
* 初始化配置文件
*/
exports.initConfig = function () {
var targetConfigFullPath = fileManage.getWholePath('./config.xml', 1);
if (fileManage.fileExist(targetConfigFullPath) == 1) {
console.log("配置文件已存在!");
return 0;
}
var originConfigFullPath = fileManage.getWholePath('EappResources/initConfig/config.xml', 0);
fileManage.copyFileForFullPath(originConfigFullPath, targetConfigFullPath);
console.log("初始化配置文件成功!");
return 1;
}
/**
* 初始化plist
*/
exports.initPlist = function (param) {
var appPackage = `${param.initplist}`;
var appPackagePath = './' + appPackage + '.plist';
var targetConfigFullPath = fileManage.getWholePath(appPackagePath, 1);
if (fileManage.fileExist(targetConfigFullPath) == 1) {
console.log("plist文件已经存在!");
return 0;
}
var originConfigFullPath = fileManage.getWholePath('EappResources/plistModel/{{fileName}}.plist', 0);
fileManage.copyFileForFullPath(originConfigFullPath, targetConfigFullPath);
console.log('初始化plist文件成功!');
return 1;
}
/**
* 读取配置文件
* @param {设置配置文件}
*/
exports.readXML = function () {
var configFullPath = fileManage.getWholePath('./config.xml', 1);
if (fileManage.fileExist(configFullPath) != 1) { // 文件不存在
console.log("没有读取到配置文件,将进行默认配置创建!");
return 1;
}
var xml_S = fileManage.readLocalFile(configFullPath);
if (parser.validate(xml_S) === true) { //optional (it'll return an object in case it's not valid)
var jsonObj = parser.parse(xml_S, options);
config.pathConfig.projectZhName = jsonObj.project.projectZhName;
config.pathConfig.projectEnName = jsonObj.project.projectEnName;
config.pathConfig.appVersion = jsonObj.project.appVersion;
config.pathConfig.appVersionCode = jsonObj.project.appVersionCode + '';
config.pathConfig.appBundleId = jsonObj.project.appBundleId;
config.pathConfig.appUrl = jsonObj.project.appUrl;
console.log("配置文件读取成功!");
return 1;
} else {
console.log("配置文件读取失败!");
return 0;
}
}
/**
* 读取XML配置,通用的xml读取方法
* relative_url 相对路径
* flag 相对于什么的标志
*/
exports.readXMLForGenenal = function (relative_url, flag) {
var configFullPath = fileManage.getWholePath(relative_url, 1);
if (fileManage.fileExist(configFullPath) != 1) { // 文件不存在
console.log("没有读取到配置文件");
return { code: 1 };
}
var xml_S = fileManage.readLocalFile(configFullPath);
if (parser.validate(xml_S) === true) { //optional (it'll return an object in case it's not valid)
var jsonObj = parser.parse(xml_S, options);
console.log("配置文件读取成功!");
return { code: 0, data: jsonObj };
} else {
console.log("配置文件读取失败!");
return { code: 1 };
}
}
/**
* 设置配置参数
*/
exports.setConfigParams = function (program) {
if (program.nameproject && "undefined" != typeof (program.nameproject)) {
var enName = `${program.nameproject}`;
config.pathConfig.projectZhName = enName;
config.pathConfig.projectEnName = enName;
console.log("设置工程名====>" + enName);
}
if (program.setversion && "undefined" != typeof (program.setversion)) {
var version = `${program.setversion}`;
config.pathConfig.appVersion = version;
console.log("设置版本号====>" + version);
}
}
/**
* 拷贝资源文件流程
*/
exports.copyResourceFlow = function () {
console.log('开始创建项目====>');
var delFullTargetPath = fileManage.getWholePath('./' + config.pathConfig.projectEnName, 1);
fileManage.removeDir(delFullTargetPath, 0);
var fullOriginPath = fileManage.getWholePath('EappResources/originFile/¥{{dirName}}¥_f7', 0);
var fullTargetPath = fileManage.getWholePath('./' + config.pathConfig.projectEnName, 1);
fileManage.copyDir(fullOriginPath, fullTargetPath, 0);
console.log('资源文件创建成功====>');
}
/**
* 对需要修改文件作流程
*/
exports.editFileFlow = function () {
//读取私服文件 并写入新的文件
var privateFileS = ['I¥{{fileName}}¥.js', 'I¥{{fileName}}¥.Base.js',
'I¥{{fileName}}¥.Base.min.js', 'I¥{{fileName}}¥.min.js',
'I¥{{fileName}}¥.Moco.js', 'I¥{{fileName}}¥.Moco.min.js',
'I¥{{fileName}}¥+Base.js', 'I¥{{fileName}}¥+Base.min.js'];
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/Eapp-' + config.pathConfig.projectEnName + '/eapp_lib/';
for (var i = 0; i < privateFileS.length; i++) {
var fileName = privateFileS[i];
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
var newData = data.replace(config.projectConfig.fileReplaceStr, projectFileName);
var z_fileName = fileName.replace(config.projectConfig.fileReplaceStr, projectFileName);
var newFulllPath = fileManage.getWholePath(newProjectFilePath + z_fileName, 1);
fileManage.writeLocalFile(newFulllPath, newData);
}
console.log('私服文件创建成功====>');
editBaseConfig();
editConfigXml();
editPackage();
editGulp();
editIndexHtml();
editDemoHtml();
editReplaceIOSHtml();
editMocoJs();
createEappXml();
console.log("完成====>");
}
/**
* 修改baseConfig.js
*/
function editBaseConfig() {
var fileName = 'baseConfig.js';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/Eapp-' + config.pathConfig.projectEnName + '/eapp_lib/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
var newData = data.replace(config.projectConfig.fileReplaceStr, projectFileName);
newData = newData.replace(config.projectConfig.dirReplaceStr, config.pathConfig.projectEnName);
fileManage.writeLocalFile(newFulllPath, newData);
//再写min.js
fileName = 'baseConfig.min.js';
relative_filePath = 'EappResources/rewriteFile/' + fileName;
fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
data = fileManage.readLocalFile(fullOriginPath);
newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/Eapp-' + config.pathConfig.projectEnName + '/eapp_lib/';
newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
newData = data.replace(config.projectConfig.fileReplaceStr, projectFileName);
newData = newData.replace(config.projectConfig.dirReplaceStr, config.pathConfig.projectEnName);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写baseConfig成功====>");
}
/**
* 修改config.xml
*/
function editConfigXml() {
var fileName = 'config.xml';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/MotherCordova/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
//替换工程名/工程文件名
var newData = data.replace(config.projectConfig.appEnName, config.pathConfig.projectEnName);
//替换中文名
newData = newData.replace(config.projectConfig.appZhName, config.pathConfig.projectZhName);
//替换版本号
newData = newData.replace(config.projectConfig.appVersion, config.pathConfig.appVersion);
//替换版本日期
newData = newData.replace(config.projectConfig.appVersionCode, config.pathConfig.appVersionCode);
//替换bundleId
newData = newData.replace(config.projectConfig.appBundleId, config.pathConfig.appBundleId);
//替换Url
newData = newData.replace(config.projectConfig.appUrl, config.pathConfig.appUrl);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写config成功====>");
}
/**
* 重写package.json文件
*/
function editPackage() {
var fileName = 'package.json';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/MotherCordova/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
//替换工程名/工程文件名
var newData = data.replace(config.projectConfig.appEnName, config.pathConfig.projectEnName);
//替换中文名
newData = newData.replace(config.projectConfig.appZhName, config.pathConfig.projectZhName);
//替换版本号
newData = newData.replace(config.projectConfig.appVersion, config.pathConfig.appVersion);
//替换bundleId
newData = newData.replace(config.projectConfig.appBundleId, config.pathConfig.appBundleId);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写package成功====>");
}
/**
* 修改gulpfile
*/
function editGulp() {
var fileName = 'gulpfile.js';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
var newData = data.replace(config.projectConfig.fileReplaceStr, projectFileName);
newData = newData.replace(config.projectConfig.dirReplaceStr, config.pathConfig.projectEnName);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写gulp成功====>");
}
/**
* 编辑html
*/
function editIndexHtml() {
var fileName = 'index.html';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/Eapp-' + config.pathConfig.projectEnName + '/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
var newData = data.replace(config.projectConfig.fileReplaceStr, projectFileName);
newData = newData.replace(config.projectConfig.dirReplaceStr, config.pathConfig.projectEnName);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写index成功====>");
}
/**
* 编辑demo html
*/
function editDemoHtml() {
var fileName = 'demo.html';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/Eapp-' + config.pathConfig.projectEnName + '/eapp_demo/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var projectFileName = fileManage.getFileNameByProjectEnName(config.pathConfig.projectEnName);
var newData = data.replace(config.projectConfig.fileReplaceStr, projectFileName);
newData = newData.replace(config.projectConfig.dirReplaceStr, config.pathConfig.projectEnName);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写demo成功====>");
}
/**
* 编辑replace_ios.sh
*/
function editReplaceIOSHtml() {
var fileName = 'replace_ios.sh';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/MotherCordova/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var newData = data.replace(config.projectConfig.appEnName, config.pathConfig.projectEnName);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写replace_ios成功====>");
}
/**
* 重写moco的版本号
*/
function editMocoJs() {
var fileName = 'IEappApi.Moco.js';
var relative_filePath = 'EappResources/rewriteFile/' + fileName;
var fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
var data = fileManage.readLocalFile(fullOriginPath);
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/Eapp-' + config.pathConfig.projectEnName + '/eapp_lib/';
var newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
var newData = data.replace(config.projectConfig.appVersion, config.pathConfig.appVersion);
newData = newData.replace(config.projectConfig.appVersionCode, config.pathConfig.appVersionCode);
fileManage.writeLocalFile(newFulllPath, newData);
fileName = 'IEappApi.Moco.min.js';
relative_filePath = 'EappResources/rewriteFile/' + fileName;
fullOriginPath = fileManage.getWholePath(relative_filePath, 0);
data = fileManage.readLocalFile(fullOriginPath);
newProjectFilePath = './' + config.pathConfig.projectEnName + '/SDK_h5/Eapp-' + config.pathConfig.projectEnName + '/eapp_lib/';
newFulllPath = fileManage.getWholePath(newProjectFilePath + fileName, 1);
newData = data.replace(config.projectConfig.appVersion, config.pathConfig.appVersion);
newData = newData.replace(config.projectConfig.appVersionCode, config.pathConfig.appVersionCode);
fileManage.writeLocalFile(newFulllPath, newData);
console.log("重写moco成功====>");
}
/**
* 生成配置本地eappconfig.xml
*/
function createEappXml() {
//项目中配置参数生成xml字符串
var json_or_js_obj = config.pathConfig;
var parserToXml = new ParserToXml(defaultOptions);
var xml = parserToXml.parse({ project: json_or_js_obj });
var newProjectFilePath = './' + config.pathConfig.projectEnName + '/eappconfig.xml';
var newFulllPath = fileManage.getWholePath(newProjectFilePath, 1);
fileManage.writeLocalFile(newFulllPath, xml);
console.log('生成eapp配置文件====>');
}