From 0605ea097b78599c84af221478e17a27335f46fb Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Wed, 17 May 2023 11:02:43 +0100 Subject: [PATCH 1/6] Workspace resolver respects .modularignore or .gitignore --- packages/workspace-resolver/package.json | 4 +- .../src/resolve-workspace.ts | 42 ++++++++++++++++--- yarn.lock | 12 ++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/packages/workspace-resolver/package.json b/packages/workspace-resolver/package.json index c5dafe0c9..61a07ad25 100644 --- a/packages/workspace-resolver/package.json +++ b/packages/workspace-resolver/package.json @@ -6,11 +6,13 @@ "dependencies": { "fs-extra": "^10.1.0", "globby": "11.1.0", + "parse-gitignore": "^1.0.1", "semver": "7.3.7" }, "devDependencies": { "@modular-scripts/modular-types": "1.2.0", - "@types/fs-extra": "^9.0.13" + "@types/fs-extra": "^9.0.13", + "@types/parse-gitignore": "^1.0.0" }, "scripts": { "build": "tsc && babel --source-maps --root-mode upward src --out-dir dist-cjs --extensions .ts --ignore **/__tests__", diff --git a/packages/workspace-resolver/src/resolve-workspace.ts b/packages/workspace-resolver/src/resolve-workspace.ts index 2f7b38465..a999c65f7 100644 --- a/packages/workspace-resolver/src/resolve-workspace.ts +++ b/packages/workspace-resolver/src/resolve-workspace.ts @@ -1,7 +1,8 @@ import path, { join } from 'path'; -import { readJson } from 'fs-extra'; +import { existsSync, readFileSync, readJson } from 'fs-extra'; import globby from 'globby'; import semver from 'semver'; +import parse from 'parse-gitignore'; import type { ModularPackageJson, ModularWorkspacePackage, @@ -17,7 +18,7 @@ function packageJsonPath(dir: string) { } function resolveWorkspacesDefinition( - cwd: string, + root: string, def: ModularPackageJson['workspaces'], ): string[] { if (!def) { @@ -30,13 +31,13 @@ function resolveWorkspacesDefinition( [`${path}/package.json`, '!**/node_modules/**/*', '!**/__tests__/**/*'], { absolute: false, - cwd, + cwd: root, }, ); }); } - return resolveWorkspacesDefinition(cwd, def.packages); + return resolveWorkspacesDefinition(root, def.packages); } function readPackageJson( @@ -138,7 +139,38 @@ export async function resolveWorkspace( child && pkg.children.push(child); } - return [collector, pkg]; + // Filter out workspaces covered by .modularignore or .gitignore + const filteredCollector = new Map(); + let ignorePatterns: string[] | undefined; + + const modularIgnorePath = path.join(root, '.modularignore'); + const gitIgnore = path.join(root, '.gitignore'); + + if (existsSync(modularIgnorePath)) { + ignorePatterns = parse(readFileSync(modularIgnorePath)); + } else if (existsSync(gitIgnore)) { + ignorePatterns = parse(readFileSync(gitIgnore)); + } + + if (ignorePatterns) { + collector.forEach((pkg, name) => { + let matches = false; + ignorePatterns?.forEach((pattern) => { + if (pattern.startsWith(path.sep)) { + matches = pkg.location.startsWith(pattern.substring(1)) + ? true + : matches; + } else { + matches = pkg.location.includes(pattern) ? true : matches; + } + }); + if (!matches) { + filteredCollector.set(name, pkg); + } + }); + } + + return [filteredCollector, pkg]; } export function analyzeWorkspaceDependencies( diff --git a/yarn.lock b/yarn.lock index 59b7d3afb..ffa5bc863 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3119,6 +3119,13 @@ "@types/npmlog" "*" "@types/ssri" "*" +"@types/parse-gitignore@^1.0.0": + version "1.0.0" + resolved "https://registry.npmjs.org/@types/parse-gitignore/-/parse-gitignore-1.0.0.tgz#782b2b6179cef4daaf9b8bd312bfe796fdc8aa18" + integrity sha512-GCL7NIxhUzJ1M5bp68T8DtDVTbQdw93MPIIII+YASs3icSozMEG9ZjX7pp7ldRJrVgrRtJBwy+/g+aBzCIttlg== + dependencies: + "@types/node" "*" + "@types/parse-json@^4.0.0": version "4.0.0" resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz" @@ -9489,6 +9496,11 @@ parse-conflict-json@^3.0.0: just-diff "^5.0.1" just-diff-apply "^5.2.0" +parse-gitignore@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/parse-gitignore/-/parse-gitignore-1.0.1.tgz#8b9dc57f17b810d495c5dfa62eb07caffe7758c7" + integrity sha512-UGyowyjtx26n65kdAMWhm6/3uy5uSrpcuH7tt+QEVudiBoVS+eqHxD5kbi9oWVRwj7sCzXqwuM+rUGw7earl6A== + parse-json@^5.0.0, parse-json@^5.2.0: version "5.2.0" resolved "https://registry.npmjs.org/parse-json/-/parse-json-5.2.0.tgz" From 6209e17bbe90eaa0296c291ba26eafebf8a6591f Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Wed, 17 May 2023 11:12:34 +0100 Subject: [PATCH 2/6] Documentation and changeset --- .changeset/eight-bats-double.md | 6 ++++++ docs/configuration.md | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 .changeset/eight-bats-double.md diff --git a/.changeset/eight-bats-double.md b/.changeset/eight-bats-double.md new file mode 100644 index 000000000..6f026939e --- /dev/null +++ b/.changeset/eight-bats-double.md @@ -0,0 +1,6 @@ +--- +'modular-scripts': major +--- + +Modular's workspace resolver now ignores a workspace if covered by a +.modularignore or .gitignore (.modularignore overrides .gitignore) diff --git a/docs/configuration.md b/docs/configuration.md index de0079828..a2ef48f54 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -121,3 +121,9 @@ _e.g._ The `package.json#modular.type` can be `"root"`, `"app"`, `"view"`, `"esm-view"`, `"source"`, `"template"` or `"package"`. Read more about Modular types in [this explainer](/docs/package-types). + +## `.modularignore` & `.gitignore` + +Modular respects `.gitignore` when identifying workspaces in the repository, +ignoring any workspaces covered by the repo's `.gitignore`. This behavior can be +overridden by providing a `.modularignore`. From 114b5d4e96d1125b3ed2ee0d4a4d1ad57222d960 Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Wed, 17 May 2023 11:18:56 +0100 Subject: [PATCH 3/6] Add .modularignore --- .modularignore | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .modularignore diff --git a/.modularignore b/.modularignore new file mode 100644 index 000000000..e69de29bb From 49edc96615212a29f23ee565f507c68b04a9e5a0 Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Wed, 17 May 2023 15:04:47 +0100 Subject: [PATCH 4/6] Simplify with updated globby --- packages/workspace-resolver/package.json | 2 +- .../src/resolve-workspace.ts | 59 +++++++------------ yarn.lock | 16 +++++ 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/packages/workspace-resolver/package.json b/packages/workspace-resolver/package.json index 61a07ad25..32f95f3b3 100644 --- a/packages/workspace-resolver/package.json +++ b/packages/workspace-resolver/package.json @@ -5,7 +5,7 @@ "main": "src/index.ts", "dependencies": { "fs-extra": "^10.1.0", - "globby": "11.1.0", + "@esm2cjs/globby": "13.1.4", "parse-gitignore": "^1.0.1", "semver": "7.3.7" }, diff --git a/packages/workspace-resolver/src/resolve-workspace.ts b/packages/workspace-resolver/src/resolve-workspace.ts index a999c65f7..2a69b82ac 100644 --- a/packages/workspace-resolver/src/resolve-workspace.ts +++ b/packages/workspace-resolver/src/resolve-workspace.ts @@ -1,8 +1,7 @@ import path, { join } from 'path'; -import { existsSync, readFileSync, readJson } from 'fs-extra'; -import globby from 'globby'; +import { existsSync, readJson } from 'fs-extra'; +import { globbySync } from '@esm2cjs/globby'; import semver from 'semver'; -import parse from 'parse-gitignore'; import type { ModularPackageJson, ModularWorkspacePackage, @@ -20,24 +19,25 @@ function packageJsonPath(dir: string) { function resolveWorkspacesDefinition( root: string, def: ModularPackageJson['workspaces'], + ignoreFiles: string[], ): string[] { if (!def) { return []; } - if (Array.isArray(def)) { return def.flatMap((path: string) => { - return globby.sync( + return globbySync( [`${path}/package.json`, '!**/node_modules/**/*', '!**/__tests__/**/*'], { absolute: false, cwd: root, + ignoreFiles: ignoreFiles, }, ); }); } - return resolveWorkspacesDefinition(root, def.packages); + return resolveWorkspacesDefinition(root, def.packages, ignoreFiles); } function readPackageJson( @@ -129,48 +129,33 @@ export async function resolveWorkspace( } } - for (const link of resolveWorkspacesDefinition(root, json.workspaces)) { - const [, child] = await resolveWorkspace( - link, - workingDirToUse, - pkg, - collector, - ); - child && pkg.children.push(child); - } - // Filter out workspaces covered by .modularignore or .gitignore - const filteredCollector = new Map(); - let ignorePatterns: string[] | undefined; + const ignoreFiles: string[] = []; const modularIgnorePath = path.join(root, '.modularignore'); const gitIgnore = path.join(root, '.gitignore'); if (existsSync(modularIgnorePath)) { - ignorePatterns = parse(readFileSync(modularIgnorePath)); + ignoreFiles.push('.modularignore'); } else if (existsSync(gitIgnore)) { - ignorePatterns = parse(readFileSync(gitIgnore)); + ignoreFiles.push('.gitignore'); } - if (ignorePatterns) { - collector.forEach((pkg, name) => { - let matches = false; - ignorePatterns?.forEach((pattern) => { - if (pattern.startsWith(path.sep)) { - matches = pkg.location.startsWith(pattern.substring(1)) - ? true - : matches; - } else { - matches = pkg.location.includes(pattern) ? true : matches; - } - }); - if (!matches) { - filteredCollector.set(name, pkg); - } - }); + for (const link of resolveWorkspacesDefinition( + root, + json.workspaces, + ignoreFiles, + )) { + const [, child] = await resolveWorkspace( + link, + workingDirToUse, + pkg, + collector, + ); + child && pkg.children.push(child); } - return [filteredCollector, pkg]; + return [collector, pkg]; } export function analyzeWorkspaceDependencies( diff --git a/yarn.lock b/yarn.lock index ffa5bc863..073477d33 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1645,6 +1645,22 @@ minimatch "^3.1.2" strip-json-comments "^3.1.1" +"@esm2cjs/globby@13.1.4": + version "13.1.4" + resolved "https://registry.yarnpkg.com/@esm2cjs/globby/-/globby-13.1.4.tgz#38794c6b560654cb94e413b78ab568acf594bd6f" + integrity sha512-JES/VBjQfk78esAY7SSVk2c77v94UKVlOPPTP5HBFRjY5IIr0hjE6B3LzYzY98oaO9ygYVy5CURGdxasDVGv1g== + dependencies: + "@esm2cjs/slash" "^4.0.0" + dir-glob "^3.0.1" + fast-glob "^3.2.11" + ignore "^5.2.0" + merge2 "^1.4.1" + +"@esm2cjs/slash@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@esm2cjs/slash/-/slash-4.0.0.tgz#5101e35a912eeb308c34f448c81cf353048d59b5" + integrity sha512-UUTg4pcAaq1D3R4yj0T+up/eBnZko6nE0ToBq6m6c0rHmpYPIGh/UrNMEeZV2hkLKpXBg6i1Zr9tJgmXq1T2BA== + "@floating-ui/core@^0.7.3": version "0.7.3" resolved "https://registry.yarnpkg.com/@floating-ui/core/-/core-0.7.3.tgz#d274116678ffae87f6b60e90f88cc4083eefab86" From 3212041d4a5e8ec510ac0e31d6955aebe3990794 Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Wed, 17 May 2023 15:31:15 +0100 Subject: [PATCH 5/6] Remove unused packages --- packages/workspace-resolver/package.json | 6 ++---- yarn.lock | 12 ------------ 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/packages/workspace-resolver/package.json b/packages/workspace-resolver/package.json index 32f95f3b3..0e69310a3 100644 --- a/packages/workspace-resolver/package.json +++ b/packages/workspace-resolver/package.json @@ -4,15 +4,13 @@ "license": "Apache-2.0", "main": "src/index.ts", "dependencies": { - "fs-extra": "^10.1.0", "@esm2cjs/globby": "13.1.4", - "parse-gitignore": "^1.0.1", + "fs-extra": "^10.1.0", "semver": "7.3.7" }, "devDependencies": { "@modular-scripts/modular-types": "1.2.0", - "@types/fs-extra": "^9.0.13", - "@types/parse-gitignore": "^1.0.0" + "@types/fs-extra": "^9.0.13" }, "scripts": { "build": "tsc && babel --source-maps --root-mode upward src --out-dir dist-cjs --extensions .ts --ignore **/__tests__", diff --git a/yarn.lock b/yarn.lock index 073477d33..134a3b7d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3135,13 +3135,6 @@ "@types/npmlog" "*" "@types/ssri" "*" -"@types/parse-gitignore@^1.0.0": - version "1.0.0" - resolved "https://registry.npmjs.org/@types/parse-gitignore/-/parse-gitignore-1.0.0.tgz#782b2b6179cef4daaf9b8bd312bfe796fdc8aa18" - integrity sha512-GCL7NIxhUzJ1M5bp68T8DtDVTbQdw93MPIIII+YASs3icSozMEG9ZjX7pp7ldRJrVgrRtJBwy+/g+aBzCIttlg== - dependencies: - "@types/node" "*" - "@types/parse-json@^4.0.0": version "4.0.0" resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz" @@ -9512,11 +9505,6 @@ parse-conflict-json@^3.0.0: just-diff "^5.0.1" just-diff-apply "^5.2.0" -parse-gitignore@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/parse-gitignore/-/parse-gitignore-1.0.1.tgz#8b9dc57f17b810d495c5dfa62eb07caffe7758c7" - integrity sha512-UGyowyjtx26n65kdAMWhm6/3uy5uSrpcuH7tt+QEVudiBoVS+eqHxD5kbi9oWVRwj7sCzXqwuM+rUGw7earl6A== - parse-json@^5.0.0, parse-json@^5.2.0: version "5.2.0" resolved "https://registry.npmjs.org/parse-json/-/parse-json-5.2.0.tgz" From 034cc2976416453f877c9fc2ee126af0c6cfbc33 Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Wed, 17 May 2023 17:04:44 +0100 Subject: [PATCH 6/6] Fix changeset --- .changeset/eight-bats-double.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/eight-bats-double.md b/.changeset/eight-bats-double.md index 6f026939e..f1dbb2adf 100644 --- a/.changeset/eight-bats-double.md +++ b/.changeset/eight-bats-double.md @@ -1,5 +1,6 @@ --- 'modular-scripts': major +'@modular-scripts/workspace-resolver': major --- Modular's workspace resolver now ignores a workspace if covered by a