diff --git a/package.json b/package.json index e1d448d8..60954ca9 100755 --- a/package.json +++ b/package.json @@ -4,8 +4,6 @@ "types": "index.d.ts", "private": true, "scripts": { - "postinstall": "npm rebuild @tensorflow/tfjs-node --build-from-source && node ./scripts/postinstall.js", - "release": "node ./scripts/release.js", "wrapper:dev": "node ./packages/wrapper/src/index.js --dev", "dev": "concurrently -k -n Client,Server,Marketplace,Chat,File,Music,Sync -c bgCyan,auto \"yarn dev:client\" \"yarn dev:server\" \"yarn dev:marketplace_server\" \"yarn dev:chat_server\" \"yarn dev:file_server\" \"yarn dev:music_server\" \"yarn dev:sync_server\"", "dev:file_server": "cd packages/file_server && yarn dev", @@ -15,7 +13,9 @@ "dev:sync_server": "cd packages/sync_server && yarn dev", "dev:server": "cd packages/server && yarn dev", "dev:client": "cd packages/app && yarn dev", - "post:deploy": "node ./scripts/post-deploy.js" + "release": "node ./scripts/release.js", + "post:deploy": "node ./scripts/post-deploy.js", + "postinstall": "npm rebuild @tensorflow/tfjs-node --build-from-source && node ./scripts/post-install.js" }, "workspaces": [ "packages/**" diff --git a/scripts/post-install.js b/scripts/post-install.js new file mode 100644 index 00000000..96d0c4a0 --- /dev/null +++ b/scripts/post-install.js @@ -0,0 +1,82 @@ +const fs = require("node:fs") +const path = require("node:path") + +const sharedRootPath = path.resolve(process.cwd(), "shared") + +const rootPath = process.cwd() +const packagesPath = path.resolve(rootPath, "packages") + +const getPackages = require("./utils/getPackages") + +async function linkSharedResources(pkgJSON, packagePath) { + if (typeof pkgJSON !== "object") { + throw new Error("Package must be an object") + } + + const { shared } = pkgJSON + + if (!shared) { + return + } + + if (typeof shared === "string") { + const finalLinkPath = path.resolve(packagePath, shared) + if (fs.existsSync(finalLinkPath)) { + console.warn(`⚠️ Resource [${shared}] link already exists in [${finalLinkPath}]`) + return + } + + // link entire folder + fs.symlinkSync(sharedRootPath, finalLinkPath, "dir") + } else { + for (const [resource, linkPath] of Object.entries(shared)) { + const originClassPath = path.resolve(sharedRootPath, resource) + const finalLinkPath = path.resolve(packagePath, linkPath) + + if (!fs.existsSync(originClassPath)) { + throw new Error(`Resource [${resource}] does not exist`) + } + + if (fs.existsSync(finalLinkPath)) { + console.warn(`⚠️ Resource [${resource}] link already exists in [${finalLinkPath}]`) + continue + } else { + fs.mkdirSync(path.resolve(packagePath, linkPath), { recursive: true }) + } + + // link folder recursively + fs.symlinkSync(originClassPath, finalLinkPath, "dir") + + console.log(`🔗 Linked resouce [${resource}] to [${finalLinkPath}]`) + } + } +} + +async function main() { + console.time("✅ post-install tooks:") + + // read dir with absolute paths + let packages = await getPackages() + + for (const packageName of packages) { + const packagePath = path.resolve(packagesPath, packageName) + + const packageJsonPath = path.resolve(packagePath, "package.json") + + if (!fs.existsSync(packageJsonPath)) { + continue + } + + const packageJson = require(packageJsonPath) + + if (packageJson.shared) { + console.log(`📦 Package [${packageName}] has declared shared resources.`) + + await linkSharedResources(packageJson, packagePath) + } + } + + console.timeEnd("✅ post-install tooks:") +} + +main().catch(console.error) \ No newline at end of file diff --git a/scripts/postinstall.js b/scripts/postinstall.js deleted file mode 100644 index 5113e2b3..00000000 --- a/scripts/postinstall.js +++ /dev/null @@ -1,71 +0,0 @@ -const fs = require("node:fs") -const path = require("node:path") - -const sharedRootPath = path.resolve(process.cwd(), "shared") -const sharedClassesPath = path.resolve(sharedRootPath, "classes") - -const rootPath = process.cwd() -const packagesPath = path.resolve(rootPath, "packages") - -const getPackages = require("./utils/getPackages") - -async function linkSharedClasses(pkgJSON, packagePath) { - if (typeof pkgJSON !== "object") { - throw new Error("Package must be an object") - } - - const { sharedClasses } = pkgJSON - - if (!sharedClasses) { - return - } - - for (const [className, linkPath] of Object.entries(sharedClasses)) { - const originClassPath = path.resolve(sharedClassesPath, className) - const finalLinkPath = path.resolve(packagePath, linkPath, className) - - if (!fs.existsSync(originClassPath)) { - throw new Error(`Class [${className}] does not exist`) - } - if (fs.existsSync(finalLinkPath)) { - console.warn(`⚠️ Class [${className}] already exists in [${finalLinkPath}]`) - continue - } else { - fs.mkdirSync(path.resolve(packagePath, linkPath), { recursive: true }) - } - - // link folder recursively - fs.symlinkSync(originClassPath, finalLinkPath, "dir") - - console.log(`🔗 Linked [${className}] to [${finalLinkPath}]`) - } -} - -async function main() { - console.time("Postinstall tooks:") - - // read dir with absolute paths - let packages = await getPackages() - - for (const packageName of packages) { - const packagePath = path.resolve(packagesPath, packageName) - - const packageJsonPath = path.resolve(packagePath, "package.json") - - if (!fs.existsSync(packageJsonPath)) { - continue - } - - const packageJson = require(packageJsonPath) - - if (packageJson.sharedClasses) { - console.log(`📦 Package [${packageName}] has shared classes.`) - - await linkSharedClasses(packageJson, packagePath) - } - } - - console.timeEnd("Postinstall tooks:") -} - -main().catch(console.error) \ No newline at end of file