A big commit with a bunch of node modules so I could run puppeteer for Walmart. Added some todos and Headway's templates.

This commit is contained in:
Norm Rasmussen
2024-02-28 17:13:10 -05:00
parent dbcdfc8472
commit 1184fe0cd1
1107 changed files with 76526 additions and 8934 deletions

View File

@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.Explorer = void 0;
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const path_type_1 = require("path-type");
const defaults_1 = require("./defaults");
const ExplorerBase_js_1 = require("./ExplorerBase.js");
const loaders_js_1 = require("./loaders.js");
const merge_1 = require("./merge");
const util_js_1 = require("./util.js");
/**
* @internal
@ -33,13 +33,19 @@ class Explorer extends ExplorerBase_js_1.ExplorerBase {
return config;
}
}
const stopDir = path_1.default.resolve(this.config.stopDir);
from = path_1.default.resolve(from);
const dirs = this.#getDirs(from);
const firstDirIter = await dirs.next();
/* istanbul ignore if -- @preserve */
if (firstDirIter.done) {
// this should never happen
throw new Error(`Could not find any folders to iterate through (start from ${from})`);
}
let currentDir = firstDirIter.value;
const search = async () => {
/* istanbul ignore if -- @preserve */
if (await (0, path_type_1.isDirectory)(from)) {
for (const place of this.config.searchPlaces) {
const filepath = path_1.default.join(from, place);
if (await (0, util_js_1.isDirectory)(currentDir.path)) {
for (const filepath of this.getSearchPlacesForDir(currentDir, defaults_1.globalConfigSearchPlaces)) {
try {
const result = await this.#readConfiguration(filepath);
if (result !== null &&
@ -50,18 +56,19 @@ class Explorer extends ExplorerBase_js_1.ExplorerBase {
catch (error) {
if (error.code === 'ENOENT' ||
error.code === 'EISDIR' ||
error.code === 'ENOTDIR') {
error.code === 'ENOTDIR' ||
error.code === 'EACCES') {
continue;
}
throw error;
}
}
}
const dir = path_1.default.dirname(from);
if (from !== stopDir && from !== dir) {
from = dir;
const nextDirIter = await dirs.next();
if (!nextDirIter.done) {
currentDir = nextDirIter.value;
if (this.searchCache) {
return await (0, util_js_1.emplace)(this.searchCache, from, search);
return await (0, util_js_1.emplace)(this.searchCache, currentDir.path, search);
}
return await search();
}
@ -72,31 +79,91 @@ class Explorer extends ExplorerBase_js_1.ExplorerBase {
}
return await search();
}
async #readConfiguration(filepath) {
async #readConfiguration(filepath, importStack = []) {
const contents = await promises_1.default.readFile(filepath, { encoding: 'utf-8' });
return this.toCosmiconfigResult(filepath, await this.#loadConfiguration(filepath, contents));
return this.toCosmiconfigResult(filepath, await this.#loadConfigFileWithImports(filepath, contents, importStack));
}
async #loadConfigFileWithImports(filepath, contents, importStack) {
const loadedContent = await this.#loadConfiguration(filepath, contents);
if (!loadedContent || !(0, merge_1.hasOwn)(loadedContent, '$import')) {
return loadedContent;
}
const fileDirectory = path_1.default.dirname(filepath);
const { $import: imports, ...ownContent } = loadedContent;
const importPaths = Array.isArray(imports) ? imports : [imports];
const newImportStack = [...importStack, filepath];
this.validateImports(filepath, importPaths, newImportStack);
const importedConfigs = await Promise.all(importPaths.map(async (importPath) => {
const fullPath = path_1.default.resolve(fileDirectory, importPath);
const result = await this.#readConfiguration(fullPath, newImportStack);
return result?.config;
}));
return (0, merge_1.mergeAll)([...importedConfigs, ownContent], {
mergeArrays: this.config.mergeImportArrays,
});
}
async #loadConfiguration(filepath, contents) {
if (contents.trim() === '') {
return;
}
if (path_1.default.basename(filepath) === 'package.json') {
return ((0, util_js_1.getPropertyByPath)((0, loaders_js_1.loadJson)(filepath, contents), this.config.packageProp) ?? null);
}
const extension = path_1.default.extname(filepath);
const loader = this.config.loaders[extension || 'noExt'] ??
this.config.loaders['default'];
if (!loader) {
throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`);
}
try {
const loader = this.config.loaders[extension || 'noExt'] ??
this.config.loaders['default'];
if (loader !== undefined) {
// eslint-disable-next-line @typescript-eslint/return-await
return await loader(filepath, contents);
const loadedContents = await loader(filepath, contents);
if (path_1.default.basename(filepath, extension) !== 'package') {
return loadedContents;
}
return ((0, util_js_1.getPropertyByPath)(loadedContents, this.config.packageProp ?? this.config.moduleName) ?? null);
}
catch (error) {
error.filepath = filepath;
throw error;
}
throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`);
}
async #fileExists(path) {
try {
await promises_1.default.stat(path);
return true;
}
catch (e) {
return false;
}
}
async *#getDirs(startDir) {
switch (this.config.searchStrategy) {
case 'none': {
// only check in the passed directory (defaults to working directory)
yield { path: startDir, isGlobalConfig: false };
return;
}
case 'project': {
let currentDir = startDir;
while (true) {
yield { path: currentDir, isGlobalConfig: false };
for (const ext of ['json', 'yaml']) {
const packageFile = path_1.default.join(currentDir, `package.${ext}`);
if (await this.#fileExists(packageFile)) {
break;
}
}
const parentDir = path_1.default.dirname(currentDir);
/* istanbul ignore if -- @preserve */
if (parentDir === currentDir) {
// we're probably at the root of the directory structure
break;
}
currentDir = parentDir;
}
return;
}
case 'global': {
yield* this.getGlobalDirs(startDir);
}
}
}
}
exports.Explorer = Explorer;