Commit 3d36cbfc authored by 范雪寒's avatar 范雪寒

feat: 构建时替换所有的uuid

parent 0843d964
const { spawn } = require("child_process"); const { spawn } = require("child_process");
const fs = require("fs"); const fs = require("fs");
const compressing = require('compressing'); const compressing = require('compressing');
const { v4, parse } = require('uuid'); const { v4, parse } = require('uuid');
const { Base64 } = require('js-base64'); const { Base64 } = require('js-base64');
const { copyDir, removeDir, fix2 } = require("./utils"); const { copyDir, removeDir, fix2 } = require("./utils");
async function buildForm() { async function buildForm() {
const paths = fs.readdirSync('form'); const paths = fs.readdirSync('form');
...@@ -128,10 +128,10 @@ async function buildWebBundle() { ...@@ -128,10 +128,10 @@ async function buildWebBundle() {
await buildCocos(args); await buildCocos(args);
} }
function createConfigFile (projectName, type) { function createConfigFile(projectName, type) {
let iosVersion = ""; let iosVersion = "";
let androidVersion = ""; let androidVersion = "";
if(!type){ if (!type) {
const androidPaths = fs.readdirSync(`dist/android/${projectName}`); const androidPaths = fs.readdirSync(`dist/android/${projectName}`);
const androidConfigFileName = androidPaths.find(path => path.indexOf('config') == 0); const androidConfigFileName = androidPaths.find(path => path.indexOf('config') == 0);
androidVersion = androidConfigFileName.split('.')[1]; androidVersion = androidConfigFileName.split('.')[1];
...@@ -139,17 +139,17 @@ function createConfigFile (projectName, type) { ...@@ -139,17 +139,17 @@ function createConfigFile (projectName, type) {
const iosConfigFileName = iosPaths.find(path => path.indexOf('config') == 0); const iosConfigFileName = iosPaths.find(path => path.indexOf('config') == 0);
iosVersion = iosConfigFileName.split('.')[1]; iosVersion = iosConfigFileName.split('.')[1];
} else { } else {
if(type=="android"){ if (type == "android") {
const androidPaths = fs.readdirSync(`dist/android/${projectName}`); const androidPaths = fs.readdirSync(`dist/android/${projectName}`);
const androidConfigFileName = androidPaths.find(path => path.indexOf('config') == 0); const androidConfigFileName = androidPaths.find(path => path.indexOf('config') == 0);
androidVersion = androidConfigFileName.split('.')[1]; androidVersion = androidConfigFileName.split('.')[1];
}else{ } else {
const iosPaths = fs.readdirSync(`dist/ios/${projectName}`); const iosPaths = fs.readdirSync(`dist/ios/${projectName}`);
const iosConfigFileName = iosPaths.find(path => path.indexOf('config') == 0); const iosConfigFileName = iosPaths.find(path => path.indexOf('config') == 0);
iosVersion = iosConfigFileName.split('.')[1]; iosVersion = iosConfigFileName.split('.')[1];
} }
} }
const config = { const config = {
"ios": { "ios": {
"sceneName": projectName, "sceneName": projectName,
...@@ -164,7 +164,7 @@ function createConfigFile (projectName, type) { ...@@ -164,7 +164,7 @@ function createConfigFile (projectName, type) {
fs.writeFileSync('dist/config.json', JSON.stringify(config)); fs.writeFileSync('dist/config.json', JSON.stringify(config));
} }
function compressAll (projectName) { function compressAll(projectName) {
const tarStream = new compressing.zip.Stream(); const tarStream = new compressing.zip.Stream();
tarStream.addEntry('dist/play'); tarStream.addEntry('dist/play');
tarStream.addEntry('dist/form'); tarStream.addEntry('dist/form');
...@@ -176,7 +176,7 @@ function compressAll (projectName) { ...@@ -176,7 +176,7 @@ function compressAll (projectName) {
tarStream.pipe(destStream); tarStream.pipe(destStream);
} }
function build_check () { function build_check() {
const dirNames = process.cwd().split(/\/|\\/); const dirNames = process.cwd().split(/\/|\\/);
const projectName = dirNames[dirNames.length - 1]; const projectName = dirNames[dirNames.length - 1];
const path = 'assets' const path = 'assets'
...@@ -211,58 +211,22 @@ function build_check () { ...@@ -211,58 +211,22 @@ function build_check () {
return projectName; return projectName;
} }
function changeSettingToWebDesktop () { function changeSettingToWebDesktop() {
const path = 'assets' const path = 'assets'
const folderName = getFolderName(path); const folderName = getFolderName(path);
editFolderMeta(path, folderName, false); editFolderMeta(path, folderName, false);
} }
function changeSettingsToBundle () { function changeSettingsToBundle() {
const path = 'assets' const path = 'assets'
const folderName = getFolderName(path); const folderName = getFolderName(path);
editFolderMeta(path, folderName, true); editFolderMeta(path, folderName, true);
} }
function replaceUuids () { async function replaceUuids() {
console.log('build_step_0 开始~!'); console.log('build_step_0 开始~!');
const path = 'assets'
function getFolderName(path) {
let folderName = '';
fs.readdirSync(path).find(fileName => {
const st = fs.statSync(`${path}/${fileName}`);
if (st.isDirectory()) {
folderName = fileName;
}
});
return folderName;
}
const folderName = getFolderName(path);
let oldFireUuid = '';
let oldJsUuid = '';
let oldJsShortUuid = '';
let oldJsId = '';
const fireMetaStr = fs.readFileSync(`assets/${folderName}/scene/${folderName}.fire.meta`);
if (fireMetaStr.indexOf('57ea7c61-9b8b-498a-b024-c98ee9124beb') > 0) {
// 老Cocos脚手架
oldFireUuid = '57ea7c61-9b8b-498a-b024-c98ee9124beb';
oldJsUuid = 'f4ede462-f8d7-4069-ba80-915611c058ca';
oldJsShortUuid = 'f4edeRi+NdAabqAkVYRwFjK';
oldJsId = 'e687yyoRBIzZAOVRL8Sseh';
}
if (fireMetaStr.indexOf('0737ce42-24f0-45c6-8e1a-8bdab4f74ba3') > 0) {
// 新Cocos脚手架
oldFireUuid = '0737ce42-24f0-45c6-8e1a-8bdab4f74ba3';
oldJsUuid = '408a67f8-65fa-4cf1-8cf2-83e20e1a0fd5';
oldJsShortUuid = '408a6f4ZfpM8Yzyg+IOGg/V';
oldJsId = 'eaTVUpqahPfZeO9+sUI7RP';
}
if (oldFireUuid === '') {
return;
}
function editFolderMeta(path, folderName) { function editFolderMeta(path, folderName) {
const metaPath = `${path}/${folderName}.meta`; const metaPath = `${path}/${folderName}.meta`;
const metaDataStr = fs.readFileSync(metaPath); const metaDataStr = fs.readFileSync(metaPath);
...@@ -274,7 +238,6 @@ function replaceUuids () { ...@@ -274,7 +238,6 @@ function replaceUuids () {
if (!fs.existsSync(path)) { if (!fs.existsSync(path)) {
return; return;
} }
const fileStr = fs.readFileSync(path); const fileStr = fs.readFileSync(path);
const newFileStr = fileStr.toString().replace(new RegExp(replaceStr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), 'g'), newStr); const newFileStr = fileStr.toString().replace(new RegExp(replaceStr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), 'g'), newStr);
fs.writeFileSync(path, newFileStr); fs.writeFileSync(path, newFileStr);
...@@ -284,26 +247,92 @@ function replaceUuids () { ...@@ -284,26 +247,92 @@ function replaceUuids () {
return uuid.substring(0, 5) + Base64.fromUint8Array(bytes).substring(2); return uuid.substring(0, 5) + Base64.fromUint8Array(bytes).substring(2);
} }
editFolderMeta(path, folderName); const path = 'assets'
function getFolderName(path) {
let folderName = '';
fs.readdirSync(path).find(fileName => {
const st = fs.statSync(`${path}/${fileName}`);
if (st.isDirectory()) {
folderName = fileName;
}
});
return folderName;
}
const newFireUuid = v4(); async function fileForEach(src, func) {
fileReplace(`assets/${folderName}/scene/${folderName}.fire.meta`, oldFireUuid, newFireUuid); //读取目录
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldFireUuid, newFireUuid); const st = fs.statSync(src);
fileReplace('settings/builder.json', oldFireUuid, newFireUuid); if (st.isFile()) {
await func(src);
return;
}
const paths = fs.readdirSync(src);
for (let i = 0; i < paths.length; i++) {
let path = paths[i];
const newSrc = `${src}/${path}`;
const st = fs.statSync(newSrc);
if (st.isFile()) {
await func(newSrc);
} else if (st.isDirectory()) {
await fileForEach(newSrc, func);
}
}
}
const newJsUuid = v4(); function createUuidData(uuid) {
const newJsShortUuid = getShortUuid(newJsUuid); const shortUuid = getShortUuid(uuid);
const newJsId = v4().replace(/-/g, '').substring(0, oldJsId.length); const newUuid = v4();
fileReplace(`assets/${folderName}/scene/${folderName}.ts.meta`, oldJsUuid, newJsUuid); const newShortUuid = getShortUuid(newUuid);
fileReplace(`assets/${folderName}/scene/${folderName}.js.meta`, oldJsUuid, newJsUuid); return {
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldFireUuid, newFireUuid); oldUuid: uuid,
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldJsShortUuid, newJsShortUuid); oldShortUuid: shortUuid,
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldJsId, newJsId); newUuid: newUuid,
newShortUuid: newShortUuid,
};
}
const uuidList = [];
await fileForEach(path, (path) => {
const nameList = path.split('.');
if (nameList[nameList.length - 1] == 'meta') {
const data = JSON.parse(fs.readFileSync(path));
uuidList.push(createUuidData(data.uuid));
if (data.subMetas) {
for (const key in data.subMetas) {
const subMet = data.subMetas[key];
if (subMet.uuid) {
uuidList.push(createUuidData(subMet.uuid));
}
}
}
}
});
const pathList = [path, 'settings', 'project.json'];
for (let i = 0; i < pathList.length; i++) {
const path = pathList[i];
await fileForEach(path, (path) => {
const nameList = path.split('.');
const expectNameList = ['png', 'jpg', 'mp3', 'wav'];
if (expectNameList.includes(nameList[nameList.length - 1])) {
return;
}
const data = fs.readFileSync(path);
uuidList.forEach(uuiddata => {
if (data.includes(uuiddata.oldUuid)) {
fileReplace(path, uuiddata.oldUuid, uuiddata.newUuid);
}
if (data.includes(uuiddata.oldShortUuid)) {
fileReplace(path, uuiddata.oldShortUuid, uuiddata.newShortUuid);
}
});
});
}
console.log('build_step_0 完成~!'); console.log('build_step_0 完成~!');
} }
function replaceIndexHtml () { function replaceIndexHtml() {
const data = fs.readFileSync('index.html'); const data = fs.readFileSync('index.html');
fs.writeFileSync('dist/play/index.html', data); fs.writeFileSync('dist/play/index.html', data);
} }
...@@ -313,52 +342,52 @@ module.exports = { ...@@ -313,52 +342,52 @@ module.exports = {
build: async function () { build: async function () {
const startTime = new Date().getTime(); const startTime = new Date().getTime();
// 构建前检查 // 构建前检查
const projectName = build_check(); const projectName = build_check();
// 清理旧文件 // 清理旧文件
// 构建form // 构建form
await removeDir('dist/form'); await removeDir('dist/form');
await buildForm(); await buildForm();
// 替换uuid // 替换uuid
replaceUuids(); await replaceUuids();
// 改设置为非bundle // 改设置为非bundle
changeSettingToWebDesktop(); changeSettingToWebDesktop();
// 构建play // 构建play
await removeDir('dist/play'); await removeDir('dist/play');
await buildWebDesktop(); await buildWebDesktop();
await copyDir('build/web-desktop', 'dist/play'); await copyDir('build/web-desktop', 'dist/play');
replaceIndexHtml(); replaceIndexHtml();
console.log('构建 web desktop 成功!'); console.log('构建 web desktop 成功!');
// 改设置为bundle // 改设置为bundle
changeSettingsToBundle(); changeSettingsToBundle();
await removeDir('dist/android'); await removeDir('dist/android');
await buildAndroidBundle(); await buildAndroidBundle();
await copyDir('build_android/jsb-link/remote', 'dist/android'); await copyDir('build_android/jsb-link/remote', 'dist/android');
console.log('构建 android bundle 成功!'); console.log('构建 android bundle 成功!');
await removeDir('dist/ios'); await removeDir('dist/ios');
await buildIosBundle(); await buildIosBundle();
await copyDir('build_ios/jsb-link/remote', 'dist/ios'); await copyDir('build_ios/jsb-link/remote', 'dist/ios');
console.log('构建 ios bundle 成功!'); console.log('构建 ios bundle 成功!');
await removeDir('dist/web_desktop'); await removeDir('dist/web_desktop');
await buildWebBundle(); await buildWebBundle();
await copyDir(`build_web_desktop/web-desktop/assets/${projectName}`, 'dist/web_desktop'); await copyDir(`build_web_desktop/web-desktop/assets/${projectName}`, 'dist/web_desktop');
console.log('构建 web bundle 成功!'); console.log('构建 web bundle 成功!');
// 改设置为非bundle // 改设置为非bundle
changeSettingToWebDesktop(); changeSettingToWebDesktop();
createConfigFile(projectName); createConfigFile(projectName);
compressAll(projectName); compressAll(projectName);
await removeDir('build'); await removeDir('build');
...@@ -377,14 +406,14 @@ module.exports = { ...@@ -377,14 +406,14 @@ module.exports = {
// 构建前检查 // 构建前检查
const projectName = build_check(); const projectName = build_check();
// 替换uuid // 替换uuid
replaceUuids(); await replaceUuids();
// 改设置为bundle // 改设置为bundle
changeSettingsToBundle(); changeSettingsToBundle();
await removeDir('dist/android'); await removeDir('dist/android');
await buildAndroidBundle(); await buildAndroidBundle();
await copyDir('build_android/jsb-link/remote', 'dist/android'); await copyDir('build_android/jsb-link/remote', 'dist/android');
// 改设置为非bundle // 改设置为非bundle
changeSettingToWebDesktop(); changeSettingToWebDesktop();
createConfigFile(projectName, "android"); createConfigFile(projectName, "android");
...@@ -397,10 +426,10 @@ module.exports = { ...@@ -397,10 +426,10 @@ module.exports = {
// 构建前检查 // 构建前检查
const projectName = build_check(); const projectName = build_check();
// 替换uuid // 替换uuid
replaceUuids(); await replaceUuids();
// 改设置为bundle // 改设置为bundle
changeSettingsToBundle(); changeSettingsToBundle();
await removeDir('dist/ios'); await removeDir('dist/ios');
await buildIosBundle(); await buildIosBundle();
await copyDir('build_ios/jsb-link/remote', 'dist/ios'); await copyDir('build_ios/jsb-link/remote', 'dist/ios');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment