forked from microsoft/live-share-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensure-packages-built.js
112 lines (101 loc) · 3.56 KB
/
ensure-packages-built.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
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
107
108
109
110
111
112
/**
* This file is used to ensure that the live-share-sdk packages are built prior to being used by the samples.
* postinstall step may be removed from sample package.json if the sample has been moved outside of the repository.
*/
const childProcess = require("child_process");
const fs = require("fs");
ensurePackagesBuilt();
/**
* if packages are not built or if they are old builds then:
*
* 1. run npm install from "packages/live-share-react".
* live-share-react uses all other live-share packages as dependencies, hence will install all packages needed to build live-share packages.
* 2. run npm run build:packages from root
*/
async function ensurePackagesBuilt() {
const rootFolderPath = await getRootFolder();
const currentGitHash = await getGitHash()
const packagesNotBuilt = !fs.existsSync(`${rootFolderPath}/node_modules/@microsoft/live-share-react/bin`);
const isOldBuild = currentGitHash !== getBuildData()?.lastGitHashBuilt;
if (packagesNotBuilt || isOldBuild) {
await npmInstallFromLiveShareReact();
build();
}
async function getGitHash() {
const currentGitHash = childProcess.spawn("git", ["rev-parse", "HEAD"], {
shell: true,
cwd: process.cwd(),
});
return new Promise((resolve, reject) => {
currentGitHash.stdout.on("data", (data) => {
const hash = data.toString().slice(0, -1);
resolve(hash);
});
currentGitHash.on("close", (code) => {
if (code != 0) {
reject(code);
}
});
})
}
async function getRootFolder() {
const rootDir = childProcess.spawn("git", ["rev-parse", " --show-toplevel"], {
shell: true,
cwd: process.cwd(),
});
return new Promise((resolve, reject) => {
rootDir.stdout.on("data", (data) => {
const path = data.toString().slice(0, -1);
resolve(path);
});
rootDir.on("close", (code) => {
if (code != 0) {
reject(code);
}
});
})
}
// live-share-react uses all other live-share packages as dependencies
async function npmInstallFromLiveShareReact() {
return new Promise((resolve, reject) => {
const installProcess = childProcess.spawn("npm", ["install"], {
shell: true,
cwd: `${rootFolderPath}/packages/live-share-react`,
stdio: "inherit",
});
installProcess.on("close", (code) => {
if (code == 0) {
resolve();
} else {
reject(code);
}
});
})
}
function build() {
const buildProcess = childProcess.spawn("npm", ["run", "build:packages"], {
shell: true,
cwd: rootFolderPath,
stdio: "inherit",
});
buildProcess.on("close", (code) => {
if (code === 0) {
fs.writeFileSync(
`${rootFolderPath}/build-data.json`,
JSON.stringify({
lastGitHashBuilt: currentGitHash,
})
);
} else {
throw new Error(`${code}`);
}
});
}
function getBuildData() {
try {
return require(`${rootFolderPath}/build-data.json`);
} catch {
return undefined;
}
}
}