From 2285b4ecc233ad05ebacecabf32dc13b38f683eb Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 2 Mar 2023 14:03:38 +0000 Subject: [PATCH] fix: remove it-glob dependency (#1200) it-glob uses aegir to release so we can't depend on it here, otherwise the aegir dependency on it gets symlinked to the package in the it monorepo which isn't built at the time aegir tries to build it so building fails. --- package.json | 2 +- src/check-project/index.js | 4 +-- src/release.js | 3 +- src/test-dependant/index.js | 4 +-- src/utils.js | 64 ++++++++++++++++++++++++++++++++++++- 5 files changed, 69 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 8245a4e55..fd47cb9c9 100644 --- a/package.json +++ b/package.json @@ -267,7 +267,6 @@ "fs-extra": "^11.1.0", "gh-pages": "^5.0.0", "globby": "^13.1.1", - "it-glob": "^2.0.0", "kleur": "^4.1.4", "lilconfig": "^2.0.5", "listr": "~0.14.2", @@ -285,6 +284,7 @@ "micromark-extension-gfm-strikethrough": "^1.0.4", "micromark-extension-gfm-table": "^1.0.5", "micromark-extension-gfm-task-list-item": "^1.0.3", + "minimatch": "^5.1.0", "mocha": "^10.0.0", "npm-package-json-lint": "^6.3.0", "nyc": "^15.1.0", diff --git a/src/check-project/index.js b/src/check-project/index.js index 03d1d2c7e..e0d677144 100755 --- a/src/check-project/index.js +++ b/src/check-project/index.js @@ -4,7 +4,6 @@ import fs from 'fs-extra' import path from 'path' import { execa } from 'execa' import prompt from 'prompt' -import glob from 'it-glob' import { monorepoManifest } from './manifests/monorepo.js' import { typedESMManifest } from './manifests/typed-esm.js' import { typescriptManifest } from './manifests/typescript.js' @@ -25,7 +24,8 @@ import Listr from 'listr' import yargsParser from 'yargs-parser' import { fileURLToPath } from 'url' import { - isMonorepoProject + isMonorepoProject, + glob } from '../utils.js' const __dirname = path.dirname(fileURLToPath(import.meta.url)) diff --git a/src/release.js b/src/release.js index edf08193e..08904fd89 100644 --- a/src/release.js +++ b/src/release.js @@ -2,10 +2,9 @@ import Listr from 'listr' import { execa } from 'execa' -import { isMonorepoProject, hasDocs } from './utils.js' +import { isMonorepoProject, hasDocs, glob } from './utils.js' import fs from 'fs-extra' import path from 'path' -import glob from 'it-glob' import { calculateSiblingVersion } from './check-project/utils.js' /** diff --git a/src/test-dependant/index.js b/src/test-dependant/index.js index e54557336..94cee1569 100644 --- a/src/test-dependant/index.js +++ b/src/test-dependant/index.js @@ -3,10 +3,10 @@ import path from 'path' import os from 'os' import { - exec + exec, + glob } from '../utils.js' import fs from 'fs-extra' -import glob from 'it-glob' /** * @param {string} name diff --git a/src/utils.js b/src/utils.js index 263993409..fb9dab8df 100644 --- a/src/utils.js +++ b/src/utils.js @@ -20,7 +20,7 @@ import envPaths from 'env-paths' import lockfile from 'proper-lockfile' import { fileURLToPath } from 'url' import Listr from 'listr' -import glob from 'it-glob' +import minimatch from 'minimatch' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const EnvPaths = envPaths('aegir', { suffix: '' }) @@ -451,3 +451,65 @@ function checkForCircularDependencies (projects) { } } } + +/** + * @typedef {object} GlobOptions + * @property {string} [cwd] The current working directory + * @property {boolean} [absolute] If true produces absolute paths (default: false) + * @property {boolean} [nodir] If true yields file paths and skip directories (default: false) + * + * Async iterable filename pattern matcher + * + * @param {string} dir + * @param {string} pattern + * @param {GlobOptions & import('minimatch').IOptions} [options] + * @returns {AsyncGenerator} + */ +export async function * glob (dir, pattern, options = {}) { + const absoluteDir = path.resolve(dir) + const relativeDir = path.relative(options.cwd ?? process.cwd(), dir) + + const stats = await fs.stat(absoluteDir) + + if (stats.isDirectory()) { + for await (const entry of _glob(absoluteDir, '', pattern, options)) { + yield entry + } + + return + } + + if (minimatch(relativeDir, pattern, options)) { + yield options.absolute === true ? absoluteDir : relativeDir + } +} + +/** + * @param {string} base + * @param {string} dir + * @param {string} pattern + * @param {GlobOptions & import('minimatch').IOptions} options + * @returns {AsyncGenerator} + */ +async function * _glob (base, dir, pattern, options) { + for await (const entry of await fs.opendir(path.join(base, dir))) { + const relativeEntryPath = path.join(dir, entry.name) + const absoluteEntryPath = path.join(base, dir, entry.name) + + let match = minimatch(relativeEntryPath, pattern, options) + + const isDirectory = entry.isDirectory() + + if (isDirectory && options.nodir === true) { + match = false + } + + if (match) { + yield options.absolute === true ? absoluteEntryPath : relativeEntryPath + } + + if (isDirectory) { + yield * _glob(base, relativeEntryPath, pattern, options) + } + } +}