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

feat: 构建时替换所有的uuid

parent 0843d964
const { spawn } = require("child_process");
const fs = require("fs");
const compressing = require('compressing');
const { v4, parse } = require('uuid');
const { Base64 } = require('js-base64');
const { copyDir, removeDir, fix2 } = require("./utils");
const { spawn } = require("child_process");
const fs = require("fs");
const compressing = require('compressing');
const { v4, parse } = require('uuid');
const { Base64 } = require('js-base64');
const { copyDir, removeDir, fix2 } = require("./utils");
async function buildForm() {
const paths = fs.readdirSync('form');
......@@ -128,10 +128,10 @@ async function buildWebBundle() {
await buildCocos(args);
}
function createConfigFile (projectName, type) {
function createConfigFile(projectName, type) {
let iosVersion = "";
let androidVersion = "";
if(!type){
if (!type) {
const androidPaths = fs.readdirSync(`dist/android/${projectName}`);
const androidConfigFileName = androidPaths.find(path => path.indexOf('config') == 0);
androidVersion = androidConfigFileName.split('.')[1];
......@@ -139,17 +139,17 @@ function createConfigFile (projectName, type) {
const iosConfigFileName = iosPaths.find(path => path.indexOf('config') == 0);
iosVersion = iosConfigFileName.split('.')[1];
} else {
if(type=="android"){
if (type == "android") {
const androidPaths = fs.readdirSync(`dist/android/${projectName}`);
const androidConfigFileName = androidPaths.find(path => path.indexOf('config') == 0);
androidVersion = androidConfigFileName.split('.')[1];
}else{
} else {
const iosPaths = fs.readdirSync(`dist/ios/${projectName}`);
const iosConfigFileName = iosPaths.find(path => path.indexOf('config') == 0);
iosVersion = iosConfigFileName.split('.')[1];
}
}
const config = {
"ios": {
"sceneName": projectName,
......@@ -164,7 +164,7 @@ function createConfigFile (projectName, type) {
fs.writeFileSync('dist/config.json', JSON.stringify(config));
}
function compressAll (projectName) {
function compressAll(projectName) {
const tarStream = new compressing.zip.Stream();
tarStream.addEntry('dist/play');
tarStream.addEntry('dist/form');
......@@ -176,7 +176,7 @@ function compressAll (projectName) {
tarStream.pipe(destStream);
}
function build_check () {
function build_check() {
const dirNames = process.cwd().split(/\/|\\/);
const projectName = dirNames[dirNames.length - 1];
const path = 'assets'
......@@ -211,58 +211,22 @@ function build_check () {
return projectName;
}
function changeSettingToWebDesktop () {
function changeSettingToWebDesktop() {
const path = 'assets'
const folderName = getFolderName(path);
editFolderMeta(path, folderName, false);
}
function changeSettingsToBundle () {
function changeSettingsToBundle() {
const path = 'assets'
const folderName = getFolderName(path);
editFolderMeta(path, folderName, true);
}
function replaceUuids () {
async function replaceUuids() {
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) {
const metaPath = `${path}/${folderName}.meta`;
const metaDataStr = fs.readFileSync(metaPath);
......@@ -274,7 +238,6 @@ function replaceUuids () {
if (!fs.existsSync(path)) {
return;
}
const fileStr = fs.readFileSync(path);
const newFileStr = fileStr.toString().replace(new RegExp(replaceStr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), 'g'), newStr);
fs.writeFileSync(path, newFileStr);
......@@ -284,26 +247,92 @@ function replaceUuids () {
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();
fileReplace(`assets/${folderName}/scene/${folderName}.fire.meta`, oldFireUuid, newFireUuid);
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldFireUuid, newFireUuid);
fileReplace('settings/builder.json', oldFireUuid, newFireUuid);
async function fileForEach(src, func) {
//读取目录
const st = fs.statSync(src);
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();
const newJsShortUuid = getShortUuid(newJsUuid);
const newJsId = v4().replace(/-/g, '').substring(0, oldJsId.length);
fileReplace(`assets/${folderName}/scene/${folderName}.ts.meta`, oldJsUuid, newJsUuid);
fileReplace(`assets/${folderName}/scene/${folderName}.js.meta`, oldJsUuid, newJsUuid);
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldFireUuid, newFireUuid);
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldJsShortUuid, newJsShortUuid);
fileReplace(`assets/${folderName}/scene/${folderName}.fire`, oldJsId, newJsId);
function createUuidData(uuid) {
const shortUuid = getShortUuid(uuid);
const newUuid = v4();
const newShortUuid = getShortUuid(newUuid);
return {
oldUuid: uuid,
oldShortUuid: shortUuid,
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 完成~!');
}
function replaceIndexHtml () {
function replaceIndexHtml() {
const data = fs.readFileSync('index.html');
fs.writeFileSync('dist/play/index.html', data);
}
......@@ -313,52 +342,52 @@ module.exports = {
build: async function () {
const startTime = new Date().getTime();
// 构建前检查
const projectName = build_check();
// 清理旧文件
// 构建form
await removeDir('dist/form');
await buildForm();
// 替换uuid
replaceUuids();
await replaceUuids();
// 改设置为非bundle
changeSettingToWebDesktop();
// 构建play
await removeDir('dist/play');
await buildWebDesktop();
await copyDir('build/web-desktop', 'dist/play');
replaceIndexHtml();
console.log('构建 web desktop 成功!');
// 改设置为bundle
changeSettingsToBundle();
await removeDir('dist/android');
await buildAndroidBundle();
await copyDir('build_android/jsb-link/remote', 'dist/android');
console.log('构建 android bundle 成功!');
await removeDir('dist/ios');
await buildIosBundle();
await copyDir('build_ios/jsb-link/remote', 'dist/ios');
console.log('构建 ios bundle 成功!');
await removeDir('dist/web_desktop');
await buildWebBundle();
await copyDir(`build_web_desktop/web-desktop/assets/${projectName}`, 'dist/web_desktop');
console.log('构建 web bundle 成功!');
// 改设置为非bundle
changeSettingToWebDesktop();
createConfigFile(projectName);
compressAll(projectName);
await removeDir('build');
......@@ -377,14 +406,14 @@ module.exports = {
// 构建前检查
const projectName = build_check();
// 替换uuid
replaceUuids();
await replaceUuids();
// 改设置为bundle
changeSettingsToBundle();
await removeDir('dist/android');
await buildAndroidBundle();
await copyDir('build_android/jsb-link/remote', 'dist/android');
// 改设置为非bundle
changeSettingToWebDesktop();
createConfigFile(projectName, "android");
......@@ -397,10 +426,10 @@ module.exports = {
// 构建前检查
const projectName = build_check();
// 替换uuid
replaceUuids();
await replaceUuids();
// 改设置为bundle
changeSettingsToBundle();
await removeDir('dist/ios');
await buildIosBundle();
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