'use strict'; const pathHelper = require('./shared/node-tools.a4003652.cjs'); const path = require('path'); const fs = require('fs-extra'); const lodashEs = require('lodash-es'); const checkbox = require('@inquirer/checkbox'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const path__default = /*#__PURE__*/_interopDefaultCompat(path); const fs__default = /*#__PURE__*/_interopDefaultCompat(fs); const checkbox__default = /*#__PURE__*/_interopDefaultCompat(checkbox); function scanFilesByConditions(dirPath, modifier, filter) { const fileList = []; const files = fs__default.readdirSync(dirPath, { withFileTypes: true }); files.forEach((file) => { const filePath = path__default.join(dirPath, file.name); if (file.isDirectory()) { fileList.push(...scanFilesByConditions(filePath, modifier, filter)); } else if (file.isFile()) { const shouldInclude = !filter || filter(file, filePath, dirPath); if (shouldInclude) { const modifiedObj = typeof modifier === "function" ? modifier(file, filePath, dirPath) : filePath; fileList.push(modifiedObj); } } }); return fileList; } function scanFiles(dirPath, modifier) { return scanFilesByConditions(dirPath, modifier); } function scanFilesByFilter(dirPath, filter) { const modifier = (f, fp, p) => { return fp; }; return scanFilesByConditions(dirPath, modifier, filter); } function scanAllComponents(packages) { const comPaths = {}; if (Array.isArray(packages) && packages.length) { packages.forEach((pkg) => { if (pkg === "/@/") { const srcPath = path__default.resolve("src"); const filter = (f, filePath, parentPath) => { return filePath.endsWith(".vue"); }; const allVues = scanFilesByFilter(srcPath, filter); if (Array.isArray(allVues)) { allVues.forEach((f) => { const d = pathHelper.normalizePath(f).split("/src/")[1]; const key = `/@/${d}`; comPaths[key] = key; }); } return; } const packagePath = path__default.resolve("node_modules", pkg); if (fs__default.existsSync(packagePath)) { const componentsPath = path__default.join(packagePath, "components.json"); if (fs__default.existsSync(componentsPath)) { const componentsJson = JSON.parse(fs__default.readFileSync(componentsPath, "utf8")); componentsJson.forEach((component) => { const { basedir, importPath, outputFileName } = component; let comPath; const dir = basedir.split("/src/")[1]; let impName; if (fs__default.existsSync(path__default.join(packagePath, "src"))) { comPath = path__default.join("./node_modules", pkg, "src/", dir, importPath); impName = path__default.basename(importPath, ".vue"); } else if (fs__default.existsSync(path__default.join(packagePath, "dist"))) { if (outputFileName) { comPath = path__default.join("./node_modules", pkg, "dist/", dir, outputFileName); impName = outputFileName; } else { comPath = path__default.join("./node_modules", pkg, "dist/", dir, importPath); impName = path__default.basename(importPath); } } if (comPath && impName) { let importer = `${pkg}/${dir}/${impName}`; comPath = pathHelper.normalizePath(comPath); if (fs__default.existsSync(comPath)) { comPaths[importer] = comPath; } } }); } } else { console.error(`${pkg} hasn't installed in this project!`); } }); } return comPaths; } function loadJsonFile(filePath) { if (filePath) { const content = fs__default.readFileSync(filePath, "utf-8"); if (content) { try { return JSON.parse(content); } catch (err) { throw new err(); } } } return null; } function loadPackages(baseDir, excludes) { if (!baseDir) { throw new Error("baseDir is empty!"); } if (!fs__default.existsSync(baseDir)) { throw new Error("baseDir is not exist!"); } const alias = []; const dirs = fs__default.readdirSync(baseDir, { withFileTypes: true }); if (Array.isArray(dirs)) { const excludeFilter = (targetP) => { const fN = targetP.name; if (Array.isArray(excludes)) { for (let i = 0; i < excludes.length; i++) { const exclude = excludes[i]; if (typeof exclude === "string" && fN === exclude) { return false; } else if (exclude instanceof RegExp && exclude.test(fN)) { return false; } } } return true; }; dirs.forEach((dir) => { if (dir.isDirectory()) { const pkgRoot = path__default.join(baseDir, dir.name); const pkgPath = path__default.join(pkgRoot, "package.json"); if (fs__default.existsSync(pkgPath) && excludeFilter(dir)) { const pkgJson = loadJsonFile(pkgPath); if (pkgJson.name) { alias.push({ packageName: pkgJson.name, packagePath: pkgRoot }); } } } }); } return alias; } function getAllDependencies(dependencies) { const deps = []; if (dependencies && Object.keys(dependencies).length) { for (let dep in dependencies) { deps.push(dep); } } return deps; } function mergeBaseTsConfigAlias(baseRoot, baseConfigPath, pathAlias) { if (!baseConfigPath || !fs__default.existsSync(baseConfigPath)) { throw new Error("baseConfigPath is empty or not exist!"); } const contentStr = fs__default.readFileSync(baseConfigPath, "utf8"); if (!contentStr) { throw new Error("Unable to read the base tsconfig file!"); } try { const baseConfig = JSON.parse(contentStr); const newContent = lodashEs.cloneDeep(baseConfig); const tsPaths = newContent.compilerOptions && newContent.compilerOptions.paths ? lodashEs.cloneDeep(newContent.compilerOptions.paths) : {}; let pathsUpdated = false; for (const [alias, targetPath] of Object.entries(pathAlias)) { const formattedAlias = `${alias}/*`; const formattedPath = `${targetPath.replace(/\\/g, "/")}/*`; if (!tsPaths[formattedAlias] || !lodashEs.isEqual(tsPaths[formattedAlias], [formattedPath])) { tsPaths[formattedAlias] = [formattedPath]; pathsUpdated = true; } } if (pathsUpdated) { console.log("Updating tsconfig.json with new paths."); newContent.compilerOptions = newContent.compilerOptions || {}; newContent.compilerOptions.paths = tsPaths; fs__default.writeFileSync(baseConfigPath, JSON.stringify(newContent, null, 2)); } else { console.log("No changes to tsconfig.json paths required."); } } catch (error) { throw new Error(`Error parsing or updating tsconfig: ${error.message}`); } } function selectFiles(options) { const { basePath, modifier, filter, tipInfo } = options; if (!basePath) { throw new Error("basePath is empty"); } let localModifier = modifier ? modifier : (file, filePath, parentPath) => { }; let localFilter = filter ? filter : (file, filePath, parentPath) => true; const choiseList = scanFilesByConditions(basePath, localModifier, localFilter); const question = { message: tipInfo || "please choose your needed file!", pageSize: 15, choices: choiseList }; return new Promise(async (resolve, reject) => { try { const selected = await checkbox__default(question); resolve(selected); } catch (error) { reject(error); } }); } function selectSfc(basePath, isTS = false) { const scanPath = pathHelper.normalizePath(path__default.join(basePath, "src")); const modifier = (file, filePath, parentPath) => { const fp = pathHelper.normalizePath(filePath); const name = fp.replace(scanPath, ""); return { name, value: fp }; }; const filter = (file, filePath, parentPath) => { if (".vue" === path__default.extname(filePath) || (isTS ? "index.ts" === path__default.basename(filePath) : "index.js" === path__default.basename(filePath))) { return true; } return false; }; return selectFiles({ basePath: scanPath, tipInfo: "\u8BF7\u9009\u62E9\u9700\u8981\u5355\u72EC\u6253\u5305\u7684\u7EC4\u4EF6\uFF08\u5373\u9700\u8981\u751F\u6210install\u5165\u53E3\u6587\u4EF6\u7684\u7EC4\u4EF6\uFF09:", modifier, filter }); } exports.normalizePath = pathHelper.normalizePath; exports.removeFileExt = pathHelper.removeFileExt; exports.getAllDependencies = getAllDependencies; exports.loadJsonFile = loadJsonFile; exports.loadPackages = loadPackages; exports.mergeBaseTsConfigAlias = mergeBaseTsConfigAlias; exports.scanAllComponents = scanAllComponents; exports.scanFiles = scanFiles; exports.scanFilesByConditions = scanFilesByConditions; exports.scanFilesByFilter = scanFilesByFilter; exports.selectFiles = selectFiles; exports.selectSfc = selectSfc;