@foxpage/foxpage-component-shared
Version:
foxpage component shared
41 lines (40 loc) • 2.05 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.findLocalPackageIndexFilePath = exports.getPackageAbsolutePath = exports.findProjectFilePath = exports.paths = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const fs_extra_1 = require("fs-extra");
const path_2 = require("./path");
exports.paths = {
root: (0, path_2.resolveRoot)('.'),
packageJson: (0, path_2.resolveRoot)('package.json'),
babelConfigJS: (0, path_2.resolveRoot)('babel.config.js'),
};
const findProjectFilePath = (mayFiles, extensions = ['js', 'ts'], root = exports.paths.root) => {
const mayFilePaths = mayFiles.reduce((all, filePath) => {
return all.concat(extensions.map(ext => (filePath.endsWith(ext) ? filePath : `${filePath}${ext.startsWith('.') ? '' : '.'}${ext}`)));
}, []);
const filePath = mayFilePaths.map(filename => (0, path_1.join)(root, filename)).find(fs_extra_1.pathExistsSync);
return filePath;
};
exports.findProjectFilePath = findProjectFilePath;
const getPackageAbsolutePath = (pkgName) => {
return (0, path_1.isAbsolute)(pkgName) ? pkgName : (0, path_2.resolveRoot)('packages', pkgName);
};
exports.getPackageAbsolutePath = getPackageAbsolutePath;
function findLocalPackageIndexFilePath(indexFileExtensions = ['.ts', '.js', '.tsx', '.jsx']) {
const localPackages = (0, path_2.findPackages)(exports.paths.root);
const indexFiles = localPackages.reduce((files, pkgDir) => {
const existSrc = (0, fs_1.existsSync)((0, path_1.join)(pkgDir, 'src'));
const pkgName = (0, path_2.readPackageInfo)(pkgDir, 'name');
if (existSrc && pkgName) {
const key = pkgName;
const filePath = (0, exports.findProjectFilePath)(['src/index', 'index'], indexFileExtensions, pkgDir);
if (key && filePath && (0, fs_1.existsSync)(filePath)) {
files[key] = filePath;
}
}
return files;
}, {});
return indexFiles;
}
exports.findLocalPackageIndexFilePath = findLocalPackageIndexFilePath;