renamed post-install script

This commit is contained in:
SrGooglo 2023-08-26 08:58:08 +00:00
parent 20a59e8f3a
commit b8f789e44a
3 changed files with 85 additions and 74 deletions

View File

@ -4,8 +4,6 @@
"types": "index.d.ts", "types": "index.d.ts",
"private": true, "private": true,
"scripts": { "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", "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": "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", "dev:file_server": "cd packages/file_server && yarn dev",
@ -15,7 +13,9 @@
"dev:sync_server": "cd packages/sync_server && yarn dev", "dev:sync_server": "cd packages/sync_server && yarn dev",
"dev:server": "cd packages/server && yarn dev", "dev:server": "cd packages/server && yarn dev",
"dev:client": "cd packages/app && 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": [ "workspaces": [
"packages/**" "packages/**"

82
scripts/post-install.js Normal file
View File

@ -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)

View File

@ -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)