-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from vusion/fix-github_action-commit-hanshijie…
…-240320 fix: 更新library地址寻找的方式。
- Loading branch information
Showing
2 changed files
with
33 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
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; | ||
} | ||
} | ||
}; | ||
|
||
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); |