-
Notifications
You must be signed in to change notification settings - Fork 960
/
Copy pathinstall-workspace-packages-in-target.mjs
106 lines (90 loc) · 3.72 KB
/
install-workspace-packages-in-target.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env zx
// @ts-check
import "zx/globals";
import { ROOT_DIR } from "./tools/path.mjs";
import { getWorkspaceList } from "./tools/workspace.mjs";
/**
* This script copies production versions of this repositories workspace packages to a target node_modules directory.
* This is useful for testing changes to the reactotron-* packages in other apps, such as Ignite.
*
* Usage: npx zx scripts/install-workspace-packages-in-target.mjs <target directory>
* Example: npx zx scripts/install-workspace-packages-in-target.mjs ~/Code/ignite/boilerplate/node_modules
*
* This exists because yarn 3 and yarn 1 linking interop is stupid and this was easier :(
*/
// #region validate target path
// parse the target directory from the first argument passed to the script
const [_nodePath, _zxPath, _fileName, targetDir] = process.argv;
console.log(`Target directory: ${targetDir}`);
// if no target directory is passed, exit the script
if (!targetDir) {
console.error("Please specify the target directory");
process.exit(1);
}
// validate that the target directory exists
if (!fs.existsSync(targetDir)) {
console.error(`Target directory "${targetDir}" does not exist`);
process.exit(1);
}
// validate that the target directory is a directory
if (!fs.statSync(targetDir).isDirectory()) {
console.error(`Target directory "${targetDir}" is not a directory`);
process.exit(1);
}
// get the path to node_modules in the target directory
const nodeModulesPath = path.join(targetDir, "node_modules");
// if the node_modules directory does not exist, exit the script
if (!fs.existsSync(nodeModulesPath)) {
console.error(
`Target directory "${targetDir}" does not have a node_modules directory`
);
process.exit(1);
}
// validate that the node_modules directory is a directory
if (!fs.statSync(nodeModulesPath).isDirectory()) {
console.error(
`Target directory "${targetDir}" node_modules directory is not a directory`
);
process.exit(1);
}
console.log(`Merging local dependencies into "${nodeModulesPath}"`);
// #endregion
// #region find packages in target node_modules that match existing workspace packages
const workspaces = await getWorkspaceList();
const workspaceNames = workspaces.map((w) => w.name);
const packages = fs
.readdirSync(nodeModulesPath)
.filter((file) => workspaceNames.includes(file))
.filter((file) =>
fs.statSync(path.join(nodeModulesPath, file)).isDirectory()
);
console.log(
`Found ${packages.length} packages to merge: ${packages.join(", ")}`
);
// #endregion
// #region copy local packages into target node_modules
for (const pkg of packages) {
console.log(`Removing ${pkg}...`);
await $`rm -rf ${nodeModulesPath}/${pkg}`;
}
console.log("Removing existing tarballs...");
await $`rm -rf ${nodeModulesPath}/*.tgz`;
// for each package, run 'yarn pack --out nodeModulesPath/<package>.tgz' in the local workspace
for (const pkg of packages) {
console.log(`Packing ${pkg}...`);
const workspacePath = path.join(ROOT_DIR, "lib", pkg);
console.log("cd to workspace: ", workspacePath);
await $`cd ${workspacePath} && yarn pack --out ${nodeModulesPath}/${pkg}.tgz`;
}
// for each package, unzip tarball into nodeModulesPath/<package>
for (const pkg of packages) {
console.log(`Unpacking ${pkg}...`);
await $`tar -xzf ${nodeModulesPath}/${pkg}.tgz -C ${nodeModulesPath}`;
console.log(`Rename ${nodeModulesPath}/package to ${nodeModulesPath}/${pkg}`); // not sure why but yarn pack always creates a package directory
await fs.rename(`${nodeModulesPath}/package`, `${nodeModulesPath}/${pkg}`);
console.log(`Remove ${nodeModulesPath}/${pkg}.tgz`);
await $`rm ${nodeModulesPath}/${pkg}.tgz`;
console.log(`Remove ${nodeModulesPath}/package temp directory`);
await $`rm -rf ${nodeModulesPath}/package`;
}
// #endregion