|
| 1 | +import {join} from 'path'; |
| 2 | +import ImportMappingBuilder from './importMappingBuilder'; |
| 3 | +import {Config} from '../config/config'; |
| 4 | +import {execSync} from 'child_process'; |
| 5 | +import {buildInputObject, buildSources} from './buildUitls'; |
| 6 | + |
| 7 | +const CONTAINER_PATH = '/home/project'; |
| 8 | +const NPM_PATH = '/home/npm'; |
| 9 | + |
| 10 | +export function compileDocker(config: Config) { |
| 11 | + return async function compile(sources: string[]) { |
| 12 | + const command = createBuildCommand(config); |
| 13 | + const input = JSON.stringify(buildInputJson(sources, config), null, 2); |
| 14 | + return JSON.parse(execSync(command, {input}).toString()); |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +export function createBuildCommand(config: Config) { |
| 19 | + const configTag = config['docker-tag']; |
| 20 | + const tag = configTag ? `:${configTag}` : ':stable'; |
| 21 | + const allowedPaths = `"${CONTAINER_PATH},${NPM_PATH}"`; |
| 22 | + return `docker run ${getVolumes(config)} -i -a stdin -a stdout ` + |
| 23 | + `ethereum/solc${tag} solc --standard-json --allow-paths ${allowedPaths}`; |
| 24 | +} |
| 25 | + |
| 26 | +export function getVolumes(config: Config) { |
| 27 | + const hostPath = process.cwd(); |
| 28 | + const hostNpmPath = join(hostPath, config.npmPath); |
| 29 | + return `-v ${hostPath}:${CONTAINER_PATH} -v ${hostNpmPath}:${NPM_PATH}`; |
| 30 | +} |
| 31 | + |
| 32 | +export function buildInputJson(sources: string[], config: Config) { |
| 33 | + return buildInputObject( |
| 34 | + buildSources(sources, (input) => join(CONTAINER_PATH, input)), |
| 35 | + getMappings(sources, config) |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +function getMappings(sources: string[], config: Config) { |
| 40 | + const mappingBuilder = new ImportMappingBuilder( |
| 41 | + config.sourcesPath, |
| 42 | + config.npmPath, |
| 43 | + CONTAINER_PATH, |
| 44 | + NPM_PATH |
| 45 | + ); |
| 46 | + return mappingBuilder.getMappings(sources); |
| 47 | +} |
0 commit comments