From 6b39be3a4a596fe7585b6f5d024162d26921de81 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Wed, 31 May 2023 20:36:54 +0000 Subject: [PATCH] fix `_shared` links for production --- .gitignore | 2 + packages/chat_server/src/index.js | 6 ++- packages/file_server/src/index.js | 3 +- packages/marketplace_server/src/index.js | 6 ++- packages/music_server/src/index.js | 3 +- scripts/docker-build.js | 38 ++++++++++++++++++ scripts/postinstall.js | 38 +----------------- scripts/utils/getPackages.js | 49 ++++++++++++++++++++++++ 8 files changed, 105 insertions(+), 40 deletions(-) create mode 100644 scripts/docker-build.js create mode 100644 scripts/utils/getPackages.js diff --git a/.gitignore b/.gitignore index a32c8cb5..418b3367 100755 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ /**/**/server.manifest /**/**/server.registry +/**/**/_shared + # Trash /**/**/*.log /**/**/dumps.log diff --git a/packages/chat_server/src/index.js b/packages/chat_server/src/index.js index 1670b175..d1b83a65 100755 --- a/packages/chat_server/src/index.js +++ b/packages/chat_server/src/index.js @@ -9,13 +9,17 @@ global.isProduction = process.env.NODE_ENV === "production" import path from "path" import { registerBaseAliases } from "linebridge/dist/server" +globalThis["__root"] = path.resolve(__dirname) + const customAliases = { - "@shared-classes": path.resolve(__dirname, "shared-classes"), + "root": globalThis["__root"], + "@shared-classes": path.resolve(__dirname, "_shared/classes"), "@services": path.resolve(__dirname, "services"), } if (!global.isProduction) { customAliases["comty.js"] = path.resolve(__dirname, "../../comty.js/src") + customAliases["@shared-classes"] = path.resolve(__dirname, "shared-classes") } registerBaseAliases(undefined, customAliases) diff --git a/packages/file_server/src/index.js b/packages/file_server/src/index.js index f3fc2194..1176d8d4 100644 --- a/packages/file_server/src/index.js +++ b/packages/file_server/src/index.js @@ -9,12 +9,13 @@ globalThis["__root"] = path.resolve(__dirname) const customAliases = { "root": globalThis["__root"], + "@shared-classes": path.resolve(__dirname, "_shared/classes"), "@services": path.resolve(__dirname, "services"), - "@shared-classes": path.resolve(__dirname, "shared-classes"), } if (!global.isProduction) { customAliases["comty.js"] = path.resolve(__dirname, "../../comty.js/src") + customAliases["@shared-classes"] = path.resolve(__dirname, "shared-classes") } registerBaseAliases(undefined, customAliases) diff --git a/packages/marketplace_server/src/index.js b/packages/marketplace_server/src/index.js index 7a233534..eae1eb45 100644 --- a/packages/marketplace_server/src/index.js +++ b/packages/marketplace_server/src/index.js @@ -5,13 +5,17 @@ import { webcrypto as crypto } from "crypto" import path from "path" import { registerBaseAliases } from "linebridge/dist/server" +globalThis["__root"] = path.resolve(__dirname) + const customAliases = { + "root": globalThis["__root"], + "@shared-classes": path.resolve(__dirname, "_shared/classes"), "@services": path.resolve(__dirname, "services"), - "@shared-classes": path.resolve(__dirname, "shared-classes"), } if (!global.isProduction) { customAliases["comty.js"] = path.resolve(__dirname, "../../comty.js/src") + customAliases["@shared-classes"] = path.resolve(__dirname, "shared-classes") } registerBaseAliases(undefined, customAliases) diff --git a/packages/music_server/src/index.js b/packages/music_server/src/index.js index ee335acd..cf65ad47 100755 --- a/packages/music_server/src/index.js +++ b/packages/music_server/src/index.js @@ -13,12 +13,13 @@ globalThis["__root"] = path.resolve(__dirname) const customAliases = { "root": globalThis["__root"], + "@shared-classes": path.resolve(__dirname, "_shared/classes"), "@services": path.resolve(__dirname, "services"), - "@shared-classes": path.resolve(__dirname, "shared-classes"), } if (!global.isProduction) { customAliases["comty.js"] = path.resolve(__dirname, "../../comty.js/src") + customAliases["@shared-classes"] = path.resolve(__dirname, "shared-classes") } registerBaseAliases(undefined, customAliases) diff --git a/scripts/docker-build.js b/scripts/docker-build.js new file mode 100644 index 00000000..3d36114f --- /dev/null +++ b/scripts/docker-build.js @@ -0,0 +1,38 @@ +const path = require("path") +const fs = require("fs") +const exec = require("child_process").execSync + +const sharedRootPath = path.resolve(process.cwd(), "shared") + +const rootPath = process.cwd() +const packagesPath = path.resolve(rootPath, "packages") + +const getPackages = require("./utils/getPackages") + +async function main() { + const packages = await getPackages() + + // copy shared dir to each root package path + for await (const packageName of packages) { + const packagePath = path.resolve(packagesPath, packageName) + const sharedPath = path.resolve(packagePath, "src", "_shared") + + if (fs.existsSync(sharedPath)) { + // remove old shared folder + fs.rmdirSync(sharedPath, { recursive: true }) + } + + // copy entire shared folder + // shared/* => /_shared/* + fs.mkdirSync(sharedPath, { recursive: true }) + + await exec(`cp -r ${sharedRootPath}/* ${sharedPath}`) + } + + console.log("📦 Shared classes copied to each package.") + + // run docker build + await exec("sudo docker compose build --no-cache") +} + +main().catch(console.error) \ No newline at end of file diff --git a/scripts/postinstall.js b/scripts/postinstall.js index 66ed10fe..5113e2b3 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -7,39 +7,7 @@ const sharedClassesPath = path.resolve(sharedRootPath, "classes") const rootPath = process.cwd() const packagesPath = path.resolve(rootPath, "packages") -const excludedPackages = ["comty.js"] - -function filterPackages(packages) { - const gitIgnore = fs.readFileSync(path.resolve(rootPath, ".gitignore"), "utf-8") - - // create a regex to match all packages that are in the gitignore file - const gitIgnoreRegex = gitIgnore.split("\n").map((line) => { - // remove comments - if (line.startsWith("#")) return - - return line.replace(/(\/)/g, "\\/").replace(/(\*)/g, "(.*)") - }).filter((line) => line) - - // filter packages that are in the gitignore file - packages = packages.filter((packageName) => { - // filter excluded packages - if (excludedPackages.includes(packageName)) { - return false - } - - const resolvedPath = path.resolve(packagesPath, packageName) - - return !gitIgnoreRegex.some((regex) => { - return resolvedPath.match(regex) - }) - }) - - packages = packages.filter((packageName) => { - return fs.statSync(path.resolve(packagesPath, packageName)).isDirectory() - }) - - return packages -} +const getPackages = require("./utils/getPackages") async function linkSharedClasses(pkgJSON, packagePath) { if (typeof pkgJSON !== "object") { @@ -77,9 +45,7 @@ async function main() { console.time("Postinstall tooks:") // read dir with absolute paths - let packages = await fs.promises.readdir(packagesPath) - - packages = filterPackages(packages) + let packages = await getPackages() for (const packageName of packages) { const packagePath = path.resolve(packagesPath, packageName) diff --git a/scripts/utils/getPackages.js b/scripts/utils/getPackages.js new file mode 100644 index 00000000..6d6f0415 --- /dev/null +++ b/scripts/utils/getPackages.js @@ -0,0 +1,49 @@ +const fs = require("fs") +const path = require("path") + +const rootPath = process.cwd() +const packagesPath = path.resolve(rootPath, "packages") + +const excludedPackages = ["comty.js"] + +function filterPackages(packages) { + const gitIgnore = fs.readFileSync(path.resolve(rootPath, ".gitignore"), "utf-8") + + // create a regex to match all packages that are in the gitignore file + const gitIgnoreRegex = gitIgnore.split("\n").map((line) => { + // remove comments + if (line.startsWith("#")) return + + return line.replace(/(\/)/g, "\\/").replace(/(\*)/g, "(.*)") + }).filter((line) => line) + + // filter packages that are in the gitignore file + packages = packages.filter((packageName) => { + // filter excluded packages + if (excludedPackages.includes(packageName)) { + return false + } + + const resolvedPath = path.resolve(packagesPath, packageName) + + return !gitIgnoreRegex.some((regex) => { + return resolvedPath.match(regex) + }) + }) + + packages = packages.filter((packageName) => { + return fs.statSync(path.resolve(packagesPath, packageName)).isDirectory() + }) + + return packages +} + +async function getPackages() { + let packages = await fs.promises.readdir(packagesPath) + + packages = filterPackages(packages) + + return packages +} + +module.exports = getPackages \ No newline at end of file