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.): Promise} */ 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, graph: Record, managedModules: ManagedModule[] ) => { const managedDeps = graph[packageName] .filter((dep) => graph[dep]) .map((name) => managedModules.find((mod) => mod.name === name)); managedDeps.forEach((module) => { // bitgo is private but still shipped in the express image, so keep walking it. if (module && (!module.private || module.name === 'bitgo') && !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 = 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(); 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();