UNPKG

@bitrix/cli

Version:
113 lines (101 loc) 3.31 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var os = _interopDefault(require('os')); var minimist = _interopDefault(require('minimist')); var slash = _interopDefault(require('slash')); var glob = _interopDefault(require('fast-glob')); var path = _interopDefault(require('path')); var fs = _interopDefault(require('fs')); const appRoot = path.resolve(__dirname, '../'); const lockFile = path.resolve(os.homedir(), '.bitrix.lock'); const type = process.platform.includes('win') ? 'junction' : null; function createSymlink(src, dest) { const resolvedSrc = path.resolve(src); const resolvedDest = path.resolve(dest); if (fs.existsSync(resolvedSrc)) { if (fs.lstatSync(resolvedSrc).isDirectory()) { const destDirPath = path.resolve(resolvedDest, `../${path.basename(resolvedSrc)}`); if (!fs.existsSync(destDirPath)) { fs.mkdirSync(destDirPath); } const children = glob.sync(path.join(resolvedSrc, '**')); children.forEach(child => { if (typeof child === 'string') { const destPath = path.resolve(resolvedDest, path.basename(child)); createSymlink(child, destPath); } }); } else { fs.symlinkSync(resolvedSrc, resolvedDest, type); } } } var alias = { w: 'watch', p: 'path', m: 'modules', t: 'test', h: 'help', v: 'version', c: 'create', n: 'name', e: 'extensions' }; var argv = minimist(process.argv.slice(2), { alias }); function getDirectories(dir) { if (fs.existsSync(path.resolve(dir))) { const pattern = slash(path.resolve(dir, '**')); const options = { onlyDirectories: true, deep: 0 }; return glob.sync(pattern, options).map(dirPath => path.basename(dirPath)); } return []; } function isRepositoryRoot(dirPath) { const dirs = getDirectories(dirPath); return dirs.includes('main') && dirs.includes('fileman') && dirs.includes('iblock') && dirs.includes('ui') && dirs.includes('translate'); } var params = { get path() { return path.resolve(argv.path || process.cwd()); }, get modules() { const modules = (argv.modules || '').split(',').map(module => module.trim()).filter(module => !!module).map(module => path.resolve(this.path, module)); if (isRepositoryRoot(this.path) && modules.length === 0) { return getDirectories(this.path); } return modules; }, get extensions() { if (typeof argv.extensions === 'string') { return argv.extensions.split(',').map(module => module.trim()); } return []; }, get name() { return argv.name || argv._[1]; } }; const srcConfigPath = path.resolve(appRoot, 'src/templates/.flowconfig'); const srcFlowTypedPath = path.resolve(appRoot, 'src/templates/flow-typed'); function bitrixFlow({ path: path$$1 } = params, { init } = argv) { if (init) { const currentDir = path$$1; if (typeof currentDir !== 'string') { throw new Error('path or p not string'); } const distFlowTypedPath = path.resolve(currentDir, 'flow-typed'); const distConfigPath = path.resolve(currentDir, '.flowconfig'); createSymlink(srcFlowTypedPath, distFlowTypedPath); createSymlink(srcConfigPath, distConfigPath); } } module.exports = bitrixFlow;