const helixLayers = ['Feature', 'Foundation', 'Project']; const helixComponentRootPaths = ['src/Feature/components', 'src/Foundation/components', 'src/Project/components']; //... function watchComponentFactory() { console.log(`Watching for changes to component factory sources in ${helixComponentRootPaths.join(',')}...`); chokidar .watch(helixComponentRootPaths, { ignoreInitial: true, awaitWriteFinish: true }) .on('add', writeComponentFactory) .on('unlink', writeComponentFactory); } //... function generateComponentFactory() { const imports = []; const registrations = []; helixLayers.forEach((layerName) => { const layerFolderFullPath = path.join('src', layerName); fs.readdirSync(layerFolderFullPath).forEach((moduleName) => { const moduleFolderFullPath = path.join(layerFolderFullPath, moduleName + '/components'); if (fs.existsSync(moduleFolderFullPath)) { fs.readdirSync(moduleFolderFullPath).forEach((componentName) => { const componentFolderFullPath = path.join(moduleFolderFullPath, componentName); if (fs.existsSync(path.join(componentFolderFullPath, 'index.js')) || fs.existsSync(path.join(componentFolderFullPath, 'index.jsx'))) { const importVarName = componentName.replace(/[^\w]+/g, ''); console.debug(`Registering JSS component ${componentName}`); imports.push(`import ${importVarName} from '../${layerName}/${moduleName}/components/${componentName}';`); registrations.push(`components.set('${componentName}', ${importVarName});`); } }); } }); }); //... }