-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathupdate-dockerfile.ts
More file actions
90 lines (76 loc) · 2.85 KB
/
Copy pathupdate-dockerfile.ts
File metadata and controls
90 lines (76 loc) · 2.85 KB
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
import execa from 'execa';
import fs from 'fs';
type ManagedModule = {
name: string;
location: string;
version: string;
private: boolean;
};
/**
* Create a function which can run lerna commands
* @param {String} lernaPath - path to lerna binary
* @returns {function(string, string[], Object.<string, string>): Promise<string>}
*/
function getLernaRunner(lernaPath: string) {
return async (command: string, args: string[] = [], options = {}) => {
const { stdout } = await execa(lernaPath, [command, ...args], options);
return stdout;
};
}
const walkDependencies = (
packageName: string,
setDeps: Set<ManagedModule>,
graph: Record<string, string[]>,
managedModules: ManagedModule[]
) => {
const managedDeps = graph[packageName]
.filter((dep) => graph[dep])
.map((name) => managedModules.find((mod) => mod.name === name));
managedDeps.forEach((module) => {
if (module && !module.private && !setDeps.has(module)) {
setDeps.add(module);
walkDependencies(module.name, setDeps, graph, managedModules);
}
});
};
/**
* Get information on the modules in this repo that are managed by lerna.
* @param {Function} lerna
* @returns {Promise<{path: *, name: *, deps: *, version: *}[]>}
*/
async function updateDockerFile(lerna) {
const depGraph: Record<string, string[]> = JSON.parse(
await lerna('list', ['--loglevel', 'silent', '--graph', '--all'])
);
const managedModules: ManagedModule[] = JSON.parse(await lerna('list', ['--loglevel', 'silent', '--json', '--all']));
const setDeps = new Set<ManagedModule>();
walkDependencies('@bitgo/express', setDeps, depGraph, managedModules);
let dockerContents = fs.readFileSync('Dockerfile', { encoding: 'utf-8' });
let copyContent = ``;
setDeps.forEach((module) => {
const modPath = module.location.substring(module.location.indexOf('/modules/'));
copyContent += `COPY --from=builder /tmp/bitgo${modPath} /var${modPath}/\n`;
});
copyContent += '\nRUN ';
const setDepsLinkers = Array.from(setDeps)
.map((module) => {
const modPath = module.location.substring(module.location.indexOf('/modules/'));
return `cd /var${modPath} && yarn link`;
})
.join(' && \\\n');
copyContent += setDepsLinkers + '\n';
const linkers = Array.from(setDeps)
.map((dep) => ` yarn link ${dep.name}`)
.join(' && \\\n');
const linkContent = `RUN cd /var/bitgo-express && \\\n${linkers}\n`;
dockerContents = dockerContents
.replace(/#COPY_START((.|\n)*)#COPY_END/, `#COPY_START\n${copyContent}#COPY_END`)
.replace(/#LINK_START((.|\n)*)#LINK_END/, `#LINK_START\n${linkContent}#LINK_END`);
fs.writeFileSync('Dockerfile', dockerContents);
}
const main = async () => {
const { stdout: lernaBinary } = await execa('yarn', ['bin', 'lerna'], { cwd: process.cwd() });
const lerna = getLernaRunner(lernaBinary);
await updateDockerFile(lerna);
};
main();