From 84a4a8b4d6fb172e1c45c3cf26123b2ca683ffc0 Mon Sep 17 00:00:00 2001 From: ncqwer <1732031236@qq.com> Date: Wed, 20 Mar 2024 13:17:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0library=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E5=AF=BB=E6=89=BE=E7=9A=84=E6=96=B9=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/utils/getBranchInfo.js | 2 +- scripts/utils/isPackageExist.js | 37 ++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/scripts/utils/getBranchInfo.js b/scripts/utils/getBranchInfo.js index 9b2d71a7..26c98fd4 100644 --- a/scripts/utils/getBranchInfo.js +++ b/scripts/utils/getBranchInfo.js @@ -1,7 +1,7 @@ const { execCommand } = require('./execCommand'); const branchReg = - /^task(?:\(([a-zA-Z\-\_\d]+)\))?\-([a-zA-Z\_\d]+)\-([a-zA-Z\_\d]+)\-/i; + /^task(?:\(([a-zA-Z\-\_\d]+)\))?\-([a-zA-Z\_\d]+)\-([a-zA-Z\_\d]+)\-?/i; module.exports.getBranchInfo = async () => { const branchName = await execCommand('git branch --show-current'); diff --git a/scripts/utils/isPackageExist.js b/scripts/utils/isPackageExist.js index 9b8f0d12..b037e39c 100644 --- a/scripts/utils/isPackageExist.js +++ b/scripts/utils/isPackageExist.js @@ -1,17 +1,37 @@ -const fsp = require('fs').promises; +const fs = require('fs'); +const fsp = fs.promises; const path = require('path'); +const LIBRARY_INFO = { + ...getAllSubdirectories('./packages/cw'), + ...getAllSubdirectories('./packages/lcap'), +}; + const isPackageExist = async (libraryName) => { + if (LIBRARY_INFO[libraryName]) return LIBRARY_INFO[libraryName]; + + console.warn('leagacy mode, should not be here'); const [prefix] = libraryName.split(/[\-\_]/); - let targetPackagesDir = path.join('.', 'packages', prefix.length === libraryName.length ? '' : prefix, libraryName); + let targetPackagesDir = path.join( + '.', + 'packages', + prefix.length === libraryName.length ? '' : prefix, + libraryName + ); try { - await fsp.access(targetPackagesDir, fsp.constants.R_OK | fsp.constants.W_OK); + await fsp.access( + targetPackagesDir, + fsp.constants.R_OK | fsp.constants.W_OK + ); return targetPackagesDir; } catch { try { targetPackagesDir = path.join('.', libraryName); - await fsp.access(targetPackagesDir, fsp.constants.R_OK | fsp.constants.W_OK); + await fsp.access( + targetPackagesDir, + fsp.constants.R_OK | fsp.constants.W_OK + ); return targetPackagesDir; } catch { return null; @@ -19,6 +39,13 @@ const isPackageExist = async (libraryName) => { } }; +function getAllSubdirectories(directoryPath) { + const files = fs.readdirSync(directoryPath); + return Object.fromEntries( + files.map((file) => [file, path.resolve(directoryPath, file)]) + ); +} + module.exports.isPackageExist = isPackageExist; -// isPackageExist('noexist_library').then((v) => console.log(v)); +// console.log('🚀 ~ file: isPackageExist.js:6 ~ LIBRARY_INFO:', LIBRARY_INFO);