From 0c7c88a56ede5165ff9b42d8a28c666ed030a726 Mon Sep 17 00:00:00 2001 From: Le Vivilet Date: Wed, 1 Jul 2026 23:02:57 +0200 Subject: [PATCH] feature: update postinstall --- packages/server/src/postinstall.js | 101 ++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 29 deletions(-) diff --git a/packages/server/src/postinstall.js b/packages/server/src/postinstall.js index ad90bb3..ca091cc 100644 --- a/packages/server/src/postinstall.js +++ b/packages/server/src/postinstall.js @@ -1,39 +1,82 @@ -// import { readFile, readdir, writeFile } from 'node:fs/promises' -// import { join } from 'node:path' -// import { pathToFileURL } from 'node:url' +import { readFile, readdir, writeFile } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { fileURLToPath, pathToFileURL } from 'node:url' -// const __dirname = import.meta.dirname +const __dirname = dirname(fileURLToPath(import.meta.url)) -// const root = join(__dirname, '..', '..', '..') +const root = join(__dirname, '..', '..', '..') -// export const getRemoteUrl = (path) => { -// const url = pathToFileURL(path).toString().slice(8) -// return `/remote/${url}` -// } +export const getRemoteUrl = (path) => { + const url = pathToFileURL(path).toString().slice(8) + return `/remote/${url}` +} -// const nodeModulesPath = join(root, 'packages', 'server', 'node_modules') +const replace = async ({ path, marker, occurrence, replacement }) => { + const content = await readFile(path, 'utf8') + if (content.includes(marker)) { + return + } + if (!content.includes(occurrence)) { + throw new Error(`Could not find expected content in ${path}`) + } + const newContent = content.replace(occurrence, replacement) + await writeFile(path, newContent) +} -// const workerPath = join(root, '.tmp', 'dist', 'dist', 'explorerViewWorkerMain.js') +const nodeModulesPath = join(root, 'packages', 'server', 'node_modules') +const workerPath = join( + root, + '.tmp', + 'dist-process-explorer-worker', + 'index.js', +) +const processExplorerPath = join(root, 'dist', 'dist', 'index.js') -// const serverStaticPath = join(nodeModulesPath, '@lvce-editor', 'static-server', 'static') +const serverStaticPath = join( + nodeModulesPath, + '@lvce-editor', + 'static-server', + 'static', +) -// const RE_COMMIT_HASH = /^[a-z\d]+$/ -// const isCommitHash = (dirent) => { -// return dirent.length === 7 && dirent.match(RE_COMMIT_HASH) -// } +const RE_COMMIT_HASH = /^[a-z\d]+$/ +const isCommitHash = (dirent) => { + return dirent.length === 7 && dirent.match(RE_COMMIT_HASH) +} -// const dirents = await readdir(serverStaticPath) -// const commitHash = dirents.find(isCommitHash) || '' -// const rendererWorkerMainPath = join(serverStaticPath, commitHash, 'packages', 'renderer-worker', 'dist', 'rendererWorkerMain.js') +const dirents = await readdir(serverStaticPath) +const commitHash = dirents.find(isCommitHash) || '' +const rendererWorkerMainPath = join( + serverStaticPath, + commitHash, + 'packages', + 'renderer-worker', + 'dist', + 'rendererWorkerMain.js', +) +const processExplorerPathPath = join( + nodeModulesPath, + '@lvce-editor', + 'shared-process', + 'src', + 'parts', + 'ProcessExplorerPath', + 'ProcessExplorerPath.js', +) -// const content = await readFile(rendererWorkerMainPath, 'utf-8') +const workerRemoteUrl = getRemoteUrl(workerPath) +await replace({ + path: rendererWorkerMainPath, + marker: '// const processExplorerWorkerUrl = ', + occurrence: `const processExplorerWorkerUrl = \`\${assetDir}/packages/process-explorer-worker/index.js\``, + replacement: `// const processExplorerWorkerUrl = \`\${assetDir}/packages/process-explorer-worker/index.js\` +const processExplorerWorkerUrl = \`${workerRemoteUrl}\``, +}) -// const remoteUrl = getRemoteUrl(workerPath) -// if (!content.includes('// const explorerWorkerUrl = ')) { -// const occurrence = `const explorerWorkerUrl = \`\${assetDir}/packages/explorer-worker/dist/explorerViewWorkerMain.js\`` -// const replacement = `// const explorerWorkerUrl = \`\${assetDir}/packages/explorer-worker/dist/explorerViewWorkerMain.js\` -// const explorerWorkerUrl = \`${remoteUrl}\`` - -// const newContent = content.replace(occurrence, replacement) -// await writeFile(rendererWorkerMainPath, newContent) -// } +await replace({ + path: processExplorerPathPath, + marker: '// export const processExplorerPath = ', + occurrence: `export const processExplorerPath = Path.join(Root.root, 'packages', 'shared-process', 'node_modules', '@lvce-editor', 'process-explorer', 'dist', 'index.js');`, + replacement: `// export const processExplorerPath = Path.join(Root.root, 'packages', 'shared-process', 'node_modules', '@lvce-editor', 'process-explorer', 'dist', 'index.js'); +export const processExplorerPath = ${JSON.stringify(processExplorerPath)};`, +})