|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import { join as pathJoin, relative as pathRelative } from "path"; |
| 3 | +import { exclude } from "tsafe/exclude"; |
| 4 | +import * as fs from "fs"; |
| 5 | + |
| 6 | +const rootDirPath = pathJoin(__dirname, "..", ".."); |
| 7 | + |
| 8 | +const commonThirdPartyDeps = (() => { |
| 9 | + const namespaceModuleNames = ["@emotion"]; |
| 10 | + const standaloneModuleNames = ["react", "@types/react"]; |
| 11 | + |
| 12 | + return [ |
| 13 | + ...namespaceModuleNames |
| 14 | + .map(namespaceModuleName => |
| 15 | + fs |
| 16 | + .readdirSync(pathJoin(rootDirPath, "node_modules", namespaceModuleName)) |
| 17 | + .map(submoduleName => `${namespaceModuleName}/${submoduleName}`) |
| 18 | + ) |
| 19 | + .reduce((prev, curr) => [...prev, ...curr], []), |
| 20 | + ...standaloneModuleNames |
| 21 | + ]; |
| 22 | +})(); |
| 23 | + |
| 24 | +const yarnHomeDirPath = pathJoin(rootDirPath, ".yarn_home"); |
| 25 | + |
| 26 | +fs.rmSync(yarnHomeDirPath, { "recursive": true, "force": true }); |
| 27 | +fs.mkdirSync(yarnHomeDirPath); |
| 28 | + |
| 29 | +const execYarnLink = (params: { targetModuleName?: string; cwd: string }) => { |
| 30 | + const { targetModuleName, cwd } = params; |
| 31 | + |
| 32 | + const cmd = [ |
| 33 | + "yarn", |
| 34 | + "link", |
| 35 | + ...(targetModuleName !== undefined ? [targetModuleName] : []) |
| 36 | + ].join(" "); |
| 37 | + |
| 38 | + console.log(`$ cd ${pathRelative(rootDirPath, cwd) || "."} && ${cmd}`); |
| 39 | + |
| 40 | + execSync(cmd, { |
| 41 | + cwd, |
| 42 | + "env": { |
| 43 | + ...process.env, |
| 44 | + "HOME": yarnHomeDirPath |
| 45 | + } |
| 46 | + }); |
| 47 | +}; |
| 48 | + |
| 49 | +const testAppPaths = (() => { |
| 50 | + const [, , ...testAppNames] = process.argv; |
| 51 | + |
| 52 | + return testAppNames |
| 53 | + .map(testAppName => { |
| 54 | + const testAppPath = pathJoin(rootDirPath, "..", testAppName); |
| 55 | + |
| 56 | + if (fs.existsSync(testAppPath)) { |
| 57 | + return testAppPath; |
| 58 | + } |
| 59 | + |
| 60 | + console.warn(`Skipping ${testAppName} since it cant be found here: ${testAppPath}`); |
| 61 | + |
| 62 | + return undefined; |
| 63 | + }) |
| 64 | + .filter(exclude(undefined)); |
| 65 | +})(); |
| 66 | + |
| 67 | +if (testAppPaths.length === 0) { |
| 68 | + console.error("No test app to link into!"); |
| 69 | + process.exit(-1); |
| 70 | +} |
| 71 | + |
| 72 | +testAppPaths.forEach(testAppPath => execSync("yarn install", { "cwd": testAppPath })); |
| 73 | + |
| 74 | +console.log("=== Linking common dependencies ==="); |
| 75 | + |
| 76 | +const total = commonThirdPartyDeps.length; |
| 77 | +let current = 0; |
| 78 | + |
| 79 | +commonThirdPartyDeps.forEach(commonThirdPartyDep => { |
| 80 | + current++; |
| 81 | + |
| 82 | + console.log(`${current}/${total} ${commonThirdPartyDep}`); |
| 83 | + |
| 84 | + const localInstallPath = pathJoin( |
| 85 | + ...[ |
| 86 | + rootDirPath, |
| 87 | + "node_modules", |
| 88 | + ...(commonThirdPartyDep.startsWith("@") |
| 89 | + ? commonThirdPartyDep.split("/") |
| 90 | + : [commonThirdPartyDep]) |
| 91 | + ] |
| 92 | + ); |
| 93 | + |
| 94 | + execYarnLink({ "cwd": localInstallPath }); |
| 95 | +}); |
| 96 | + |
| 97 | +commonThirdPartyDeps.forEach(commonThirdPartyDep => |
| 98 | + testAppPaths.forEach(testAppPath => |
| 99 | + execYarnLink({ |
| 100 | + "cwd": testAppPath, |
| 101 | + "targetModuleName": commonThirdPartyDep |
| 102 | + }) |
| 103 | + ) |
| 104 | +); |
| 105 | + |
| 106 | +console.log("=== Linking in house dependencies ==="); |
| 107 | + |
| 108 | +execYarnLink({ "cwd": pathJoin(rootDirPath, "dist") }); |
| 109 | + |
| 110 | +testAppPaths.forEach(testAppPath => |
| 111 | + execYarnLink({ |
| 112 | + "cwd": testAppPath, |
| 113 | + "targetModuleName": JSON.parse( |
| 114 | + fs.readFileSync(pathJoin(rootDirPath, "package.json")).toString("utf8") |
| 115 | + )["name"] |
| 116 | + }) |
| 117 | +); |
| 118 | + |
| 119 | +export {}; |
0 commit comments