@arc-fusion/cli
Version:
CLI for running Arc Fusion on your local machine
235 lines (202 loc) • 7.92 kB
JavaScript
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
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')
console.log('THEMES_BLOCKS_REPO variable was found. Using local 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
if (!existsSync(enginePath)) return console.log('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('ENGINE_SDK_REPO variable was found. Using local 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
if (!existsSync(cssPath)) return console.log('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('CSS_FRAMEWORK_REPO variable was found. Using local 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
if (!existsSync(themePath)) return console.log('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('THEME_COMPONENTS_REPO variable was found. Using local 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 getLinkedCacheProxyRepo = (cacheProxyPath) => {
if (!cacheProxyPath) return
if (!existsSync(cacheProxyPath)) return console.log('CACHEPROXY_SOURCE_DIR variable points to an invalid location. Using image')
console.log('CACHEPROXY_SOURCE_DIR variable was found. Using local source code')
return {
cacheProxy: [
`${path.resolve(cacheProxyPath, 'src')}:/opt/cache-proxy/src:ro,cached`
]
}
}
const getLinkedFusionEngineRepo = (fusionPath) => {
if (!fusionPath) return
if (!existsSync(fusionPath)) return console.log('FUSION_REPO variable points to an invalid location. Using image')
console.log('FUSION_REPO variable was found. Using local source code')
return {
engine: [
`${path.resolve(fusionPath, 'engine', 'src')}:/opt/engine-dist/src:ro,cached`
]
}
}
const getLinkedEngineRepo = (fusionPath) => {
if (!fusionPath) return
if (!existsSync(fusionPath)) return console.log('ENGINE_SOURCE_DIR variable points to an invalid location. Using image')
console.log('ENGINE_SOURCE_DIR variable was found. Using local source code')
return {
engine: [
`${path.resolve(fusionPath, 'src')}:/opt/engine-dist/src:ro,cached`
]
}
}
// Until the rest of Pagebuilder is separated out, the "miscellaneous" parts outside of engine will be
// grouped as just Fusion
const getLinkedFusionRepo = (fusionPath) => {
if (!fusionPath) return
if (!existsSync(fusionPath)) return console.log('The FUSION_REPO variable points to an invalid location. Using image')
console.log('FUSION_REPO variable was found. Using local source code')
return {
}
}
const getLinkedOriginRepo = (originPath) => {
if (!originPath) return
if (!existsSync(originPath)) return console.log('The ORIGIN_SOURCE_DIR variable points to an invalid location. Using image')
console.log('ORIGIN_SOURCE_DIR variable was found. Using local source code')
return {
origin: [
`${path.resolve(originPath, 'src')}:/opt/origin/src:ro,cached`
]
}
}
const getLinkedResolverRepo = (resolverPath) => {
if (!resolverPath) return
if (!existsSync(resolverPath)) return console.log('RESOLVER_SOURCE_DIR variable points to an invalid location. Using image')
console.log('RESOLVER_SOURCE_DIR variable was found. Using local source code')
return {
resolver: [
`${path.resolve(resolverPath, 'config')}:/opt/resolver/config:ro,cached`,
`${path.resolve(resolverPath, 'src')}:/opt/resolver/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,
getLinkedCacheProxyRepo,
getLinkedEngineSdkBlock,
getLinkedCssFrameworkBlock,
getLinkedThemeComponentsBlock,
getLinkedFusionVersion,
getLinkedFusionEngineRepo,
getLinkedEngineRepo,
getLinkedFusionRepo,
getLinkedOriginRepo,
getLinkedResolverRepo,
createDockerVolume,
deleteDockerVolume
}