@eluvio/elv-js-helpers
Version:
A collection of Javascript helper functions used by several Eluvio libraries.
49 lines (40 loc) • 1.58 kB
JavaScript
// builds (or rebuilds) the main.js file
const fs = require('fs')
const path = require('path')
const {jsFileBasenamesList, SRC_DIR, subdirNameList} = require('./dirUtils')
// recursive
const codeBlocksAndFileList = (startingPath, currentPath) => {
const currentDirName = path.basename(currentPath)
let codeBlocks = []
let fileList = []
const subDirs = subdirNameList(currentPath)
for (const subDir of subDirs) {
let [subCodeBlocks, subFileList] = codeBlocksAndFileList(startingPath, path.join(currentPath, subDir))
codeBlocks = codeBlocks.concat(subCodeBlocks)
fileList = fileList.concat(subFileList)
}
const jsFiles = jsFileBasenamesList(currentPath).filter(x => x !== 'main')
for (const jsFile of jsFiles) {
const filePath = path.join(currentPath, jsFile).replace(startingPath, '.')
fileList.push(filePath)
}
codeBlocks.push(
startingPath === currentPath ?
'module.exports = {' :
`const ${currentDirName} = {`
)
const kvList = []
for (const subDir of subDirs) {
kvList.push(` ${subDir}`)
}
for (const jsFile of jsFiles) {
const filePath = path.join(currentPath, jsFile).replace(startingPath, '.')
kvList.push(` ${jsFile}: require('${filePath}')`)
}
codeBlocks.push(kvList.join(',\n'))
codeBlocks.push('}\n')
return [codeBlocks, fileList]
}
const [codeBlocks] = codeBlocksAndFileList(SRC_DIR, SRC_DIR)
const fileContents = '// This file is auto-generated by ./bin/buildMain.js\n\n\'use strict\'\n' + codeBlocks.join('\n')
fs.writeFileSync(path.join(SRC_DIR, 'main.js'), fileContents)