-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathlinkWorkspaceDeps.js
75 lines (67 loc) · 2.33 KB
/
linkWorkspaceDeps.js
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const fs = require("fs");
const path = require("path");
const watch = process.argv[2] && process.argv[2] === "--watch";
const packages = [
{
name: "@itwin/tree-widget-react",
dir: "tree-widget",
},
];
linkPackages();
function linkPackages() {
for (const pack of packages) {
const sourcePath = getSourceLibPath(pack.dir, pack.libDirName);
if (!fs.existsSync(sourcePath)) {
console.warn(`Package ${pack.name} source path does not exist: ${sourcePath}`);
continue;
}
const targetPath = getTargetLibPath(pack.name, pack.libDirName);
copyChangedFiles(pack.name, sourcePath, targetPath);
if (watch) {
let lastChange = undefined;
fs.watch(sourcePath, { recursive: true }, () => {
const now = new Date();
if (now === lastChange) {
return;
}
lastChange = now;
setTimeout(() => {
if (now === lastChange) {
copyChangedFiles(pack.name, sourcePath, targetPath);
lastChange = undefined;
}
}, 100);
});
}
}
}
function copyChangedFiles(packageName, sourceDir, targetDir) {
console.log(`[${new Date().toLocaleTimeString()}] Updating ${packageName}`);
fs.cpSync(sourceDir, targetDir, {
recursive: true,
filter: (source, dest) => {
if (!fs.existsSync(dest)) {
return true;
}
const sourceStat = fs.statSync(source);
if (sourceStat.isDirectory()) {
return true;
}
const destStat = fs.statSync(dest);
return sourceStat.mtime.getTime() > destStat.mtime.getTime();
},
});
}
function getTargetLibPath(packageName, distDirName) {
const packagePath = path.resolve(__dirname, "../node_modules", packageName);
const realPath = fs.realpathSync(packagePath);
return path.resolve(realPath, distDirName ?? "lib");
}
function getSourceLibPath(packageDir, distDirName) {
const sourcePath = path.resolve(__dirname, "../../../packages/itwin", packageDir);
return path.resolve(sourcePath, distDirName ?? "lib");
}