UNPKG

@arc-fusion/cli

Version:

CLI for running Arc Fusion on your local machine

179 lines (156 loc) 6.31 kB
'use strict' const path = require('path') const { fileExists, readFile } = require('./promises') const { existsSync, copyFileSync, unlinkSync } = require('fs') const { execSync } = require('child_process') const spawn = require('./spawn') const getEnvVariables = async (PROJECT_ROOT) => { const envPath = path.join(PROJECT_ROOT, '.env') if (!await fileExists(envPath)) { console.log('.env file was not found') return {} } return (await readFile(envPath)) .toString() .split('\n') .reduce((obj, line) => { const [key, value] = line.split('=') if (key && value) obj[key] = value return obj }, {}) } const getBlockVariables = async (PROJECT_ROOT) => { const blocksPath = path.join(PROJECT_ROOT, 'blocks.json') if (!await fileExists(blocksPath)) return false return require(blocksPath) } const normalizeBlockName = block => { // Strip versioning const packageName = block.split('@').slice(0, 2).join('@') // Strip org name const blockName = packageName.split('/').slice(1, block.length - 1).join('/') return { packageName, blockName } } const getLinkedThemeBlocks = async (PROJECT_ROOT, themePath, blocksEnv, links, production) => { if (!themePath) return console.log('The THEME_BLOCKS_REPO variable was not found. Using published blocks') const { blocks, devBlocks } = blocksEnv // Check flags if (links && production) return console.log('Unable to use both published and local blocks, using published blocks') if (production || !links) return console.log('Using published blocks') const npmrcSourcePath = path.resolve(PROJECT_ROOT, '.npmrc') const linkBlocks = blocks => blocks .map(block => { const { packageName, blockName } = normalizeBlockName(block) const blockPath = path.resolve(themePath, 'blocks', blockName) // eslint-disable-next-line array-callback-return if (!existsSync(blockPath)) return console.log(`--- Installing dependencies for ${packageName}`) const npmrcDestPath = path.resolve(blockPath, '.npmrc') copyFileSync(npmrcSourcePath, npmrcDestPath) execSync('npm install --no-save --silent', { cwd: blockPath }) unlinkSync(npmrcDestPath) return `${blockPath}:/opt/engine/bundle/linked_modules/${packageName}:ro,cached` }) .filter(v => v) const devLinks = typeof links === 'string' ? links.split(',') : [] if (!devLinks.length && (!devBlocks || !devBlocks.length)) { console.log('--- Linking all blocks') return linkBlocks(blocks) } console.log('--- Linking only development blocks') return linkBlocks(devLinks.length ? devLinks : devBlocks) } const getLinkedEngineSdkBlock = (enginePath, blocksEnv) => { if (!enginePath) return console.log('The ENGINE_SDK_REPO variable was not found. Using published version') if (!existsSync(enginePath)) return console.log('The ENGINE_SDK_REPO variable points to an invalid location. Using published version') const { engineSDK } = blocksEnv if (!engineSDK) return console.log('No engine sdk specified in blocks.json') console.log('--- Linking engine sdk') const { packageName } = normalizeBlockName(engineSDK) return `${path.resolve(enginePath, 'src')}:/opt/engine/bundle/linked_modules/${packageName}:ro,cached` } const getLinkedCssFrameworkBlock = (cssPath, blocksEnv) => { if (!cssPath) return console.log('The CSS_FRAMEWORK_REPO variable was not found. Using published version') if (!existsSync(cssPath)) return console.log('The CSS_FRAMEWORK_REPO variable points to an invalid location. Using published version') const { cssFramework } = blocksEnv if (!cssFramework) return console.log('No css framework specified in blocks.json') console.log('--- Linking css framework') const { packageName } = normalizeBlockName(cssFramework) return `${path.resolve(cssPath)}:/opt/engine/bundle/linked_modules/${packageName}:ro,cached` } const getLinkedThemeComponentsBlock = (themePath, blocksEnv) => { if (!themePath) return console.log('The THEME_COMPONENTS_REPO variable was not found. Using published version') if (!existsSync(themePath)) return console.log('The THEME_COMPONENTS_REPO variable points to an invalid location. Using published version') const { themeComponents } = blocksEnv if (!themeComponents) return console.log('No theme components specified in blocks.json') console.log('--- Linking theme components') const { packageName } = normalizeBlockName(themeComponents) return `${path.resolve(themePath)}:/opt/engine/bundle/linked_modules/${packageName}:ro,cached` } const getLinkedFusionVersion = (fusionPath) => { if (fusionPath) { const packageJSON = require(path.resolve(fusionPath, 'package.json')) // If the path to repo isn't found, return latest to progress with the build as usual if (!packageJSON) return 'latest' console.log('The package version is', packageJSON.version) return packageJSON.version } } const getLinkedFusionRepo = (fusionPath) => { if (!fusionPath) return console.log('The FUSION_REPO variable was not found. Using image') if (!existsSync(fusionPath)) return console.log('The FUSION_REPO variable points to an invalid location. Using image') console.log('--- Linking fusion repo') return { cacheProxy: [ `${path.resolve(fusionPath, 'cache-proxy', 'src')}:/opt/cache-proxy/src:ro,cached` ], engine: [ `${path.resolve(fusionPath, 'engine', 'src')}:/opt/engine-dist/src:ro,cached` ], resolver: [ `${path.resolve(fusionPath, 'resolver', 'config')}:/opt/resolver/config:ro,cached`, `${path.resolve(fusionPath, 'resolver', 'src')}:/opt/resolver/src:ro,cached` ], origin: [ `${path.resolve(fusionPath, 'origin', 'src')}:/opt/origin/src:ro,cached` ] } } const createDockerVolume = name => spawn( 'docker', [ 'volume', 'create', name ], { stdio: 'inherit' } ) const deleteDockerVolume = name => spawn( 'docker', [ 'volume', 'rm', '-f', name ], { stdio: 'inherit' } ) module.exports = { getEnvVariables, getBlockVariables, getLinkedThemeBlocks, getLinkedEngineSdkBlock, getLinkedCssFrameworkBlock, getLinkedThemeComponentsBlock, getLinkedFusionVersion, getLinkedFusionRepo, createDockerVolume, deleteDockerVolume }