This commit is contained in:
parent
7a9bf3199a
commit
c294e2e9ae
5702 changed files with 465039 additions and 34 deletions
45
node_modules/@pkgr/core/lib/helpers.js
generated
vendored
Normal file
45
node_modules/@pkgr/core/lib/helpers.js
generated
vendored
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { CWD, EXTENSIONS, cjsRequire } from './constants.js';
|
||||
export const tryPkg = (pkg) => {
|
||||
try {
|
||||
return cjsRequire.resolve(pkg);
|
||||
}
|
||||
catch { }
|
||||
};
|
||||
export const isPkgAvailable = (pkg) => !!tryPkg(pkg);
|
||||
export const tryFile = (filename, includeDir = false, base = CWD) => {
|
||||
if (typeof filename === 'string') {
|
||||
const filepath = path.resolve(base, filename);
|
||||
return fs.existsSync(filepath) &&
|
||||
(includeDir || fs.statSync(filepath).isFile())
|
||||
? filepath
|
||||
: '';
|
||||
}
|
||||
for (const file of filename ?? []) {
|
||||
const filepath = tryFile(file, includeDir, base);
|
||||
if (filepath) {
|
||||
return filepath;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
};
|
||||
export const tryExtensions = (filepath, extensions = EXTENSIONS) => {
|
||||
const ext = [...extensions, ''].find(ext => tryFile(filepath + ext));
|
||||
return ext == null ? '' : filepath + ext;
|
||||
};
|
||||
export const findUp = (searchEntry, searchFileOrIncludeDir, includeDir) => {
|
||||
const isSearchFile = typeof searchFileOrIncludeDir === 'string';
|
||||
const searchFile = isSearchFile ? searchFileOrIncludeDir : 'package.json';
|
||||
let lastSearchEntry;
|
||||
do {
|
||||
const searched = tryFile(searchFile, isSearchFile && includeDir, searchEntry);
|
||||
if (searched) {
|
||||
return searched;
|
||||
}
|
||||
lastSearchEntry = searchEntry;
|
||||
searchEntry = path.dirname(searchEntry);
|
||||
} while (!lastSearchEntry || lastSearchEntry !== searchEntry);
|
||||
return '';
|
||||
};
|
||||
//# sourceMappingURL=helpers.js.map
|
||||
Loading…
Add table
Add a link
Reference in a new issue