Skip to content

Commit

Permalink
Remove child-workspace-package-names-as-json script in favour of `y…
Browse files Browse the repository at this point in the history
…arn workspaces filter` command (MetaMask#1491)

* Remove `child-workspace-package-names-as-json` script in favour of `yarn workspaces filter` command

* Revert changes to Yarn binary

* Load minimatch from execute

* Update LavaMoat policies

* Use await import

* Return full name instead of workspace folder

* Use older version of minimatch and patch @types/glob

* Exclude root from child workspaces
  • Loading branch information
Mrtenz authored Jun 12, 2023
1 parent 53a3b9d commit 88a0b2c
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 76 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
outputs:
child-workspace-package-names: ${{ steps.workspace-package-names.outputs.child-workspace-package-names }}
all-child-workspace-package-names: ${{ steps.all-workspace-package-names.outputs.all-child-workspace-package-names }}
all-workspace-package-names: ${{ steps.workspace-package-names.outputs.all-workspace-package-names }}
strategy:
fail-fast: false
matrix:
Expand All @@ -35,12 +35,8 @@ jobs:
- name: Fetch workspace package names
id: workspace-package-names
run: |
echo "child-workspace-package-names=$(yarn child-workspace-package-names-as-json)" >> "$GITHUB_OUTPUT"
shell: bash
- name: Fetch all workspace package names
id: all-workspace-package-names
run: |
echo "all-child-workspace-package-names=$(yarn child-workspace-package-names-as-json --all)" >> "$GITHUB_OUTPUT"
echo "child-workspace-package-names=$(yarn workspaces filter --include 'packages/*' --exclude 'packages/examples' --json)" >> "$GITHUB_OUTPUT"
echo "all-workspace-package-names=$(yarn workspaces filter --include '{.,packages/**}' --json)" >> "$GITHUB_OUTPUT"
shell: bash

build:
Expand Down Expand Up @@ -134,7 +130,7 @@ jobs:
strategy:
fail-fast: false
matrix:
package-name: ${{ fromJson(needs.prepare.outputs.all-child-workspace-package-names) }}
package-name: ${{ fromJson(needs.prepare.outputs.all-workspace-package-names) }}
steps:
- uses: actions/checkout@v3
- name: Setup Node
Expand Down
22 changes: 22 additions & 0 deletions .yarn/patches/@types-glob-npm-7.1.4-d45247eaa2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/index.d.ts b/index.d.ts
index 8213c43da1204e92db61cd9af6018362986f0ad9..d9fa158b4fa858c8b9cf9d5cc75f5e7b2c6bcbb8 100755
--- a/index.d.ts
+++ b/index.d.ts
@@ -25,7 +25,7 @@ declare namespace G {
let Glob: IGlobStatic;
let GlobSync: IGlobSyncStatic;

- interface IOptions extends minimatch.IOptions {
+ interface IOptions extends minimatch.MinimatchOptions {
cwd?: string | undefined;
root?: string | undefined;
dot?: boolean | undefined;
@@ -69,7 +69,7 @@ declare namespace G {
}

interface IGlobBase {
- minimatch: minimatch.IMinimatch;
+ minimatch: minimatch.MinimatchOptions;
options: IOptions;
aborted: boolean;
cache: { [path: string]: boolean | 'DIR' | 'FILE' | ReadonlyArray<string> };
97 changes: 97 additions & 0 deletions .yarn/plugins/local/plugin-workspaces-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module.exports = {
name: `plugin-workspaces-filter`,
factory: (require) => {
const { BaseCommand } = require('@yarnpkg/cli');
const { Configuration, Project, StreamReport } = require('@yarnpkg/core');
const { Command, Option, UsageError } = require('clipanion');
const { isString, isBoolean } = require('typanion');

class FilterCommand extends BaseCommand {
static paths = [['workspaces', 'filter']];

static usage = Command.Usage({
description: 'Filter workspaces',
details: `
This command will filter workspaces based on the given criteria. It's
like \`yarn workspaces list\` but on steroids.
`,
examples: [
[
`List workspaces based on a glob pattern`,
`yarn workspaces filter --include "packages/*"`,
],
[
'Exclude workspaces based on a glob pattern',
`yarn workspaces filter --exclude "packages/*/foo"`,
],
],
});

include = Option.String('--include', {
description: `List workspaces based on a glob pattern`,
validator: isString,
});

exclude = Option.String('--exclude', {
description: `Exclude workspaces based on a glob pattern`,
validator: isString,
});

json = Option.Boolean(`--json`, false, {
description: `Format the output as a JSON array`,
validator: isBoolean,
});

async execute() {
// Note: We have to import `minimatch` here, because Yarn will always
// load the plugin, even if the command is not used, and `minimatch`
// may not be installed.
const { minimatch } = await import('minimatch');

const configuration = await Configuration.find(
this.context.cwd,
this.context.plugins,
);
const { project } = await Project.find(configuration, this.context.cwd);
const { workspaces } = project;

if (!this.include && !this.exclude) {
throw new UsageError(
`This command requires at least one of --include or --exclude to be specified.`,
);
}

const report = await StreamReport.start(
{
configuration,
json: this.json,
stdout: this.context.stdout,
},
async (report) => {
const filteredWorkspaces = workspaces.filter((workspace) => {
return (
(!this.include ||
minimatch(workspace.relativeCwd, this.include)) &&
(!this.exclude ||
!minimatch(workspace.relativeCwd, this.exclude))
);
});

for (const workspace of filteredWorkspaces) {
report.reportInfo(null, workspace.relativeCwd);
}

const result = filteredWorkspaces.map((workspace) => workspace.manifest.raw.name);
report.reportJson(result);
},
);

return report.exitCode();
}
}

return {
commands: [FilterCommand],
};
},
};
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ plugins:
spec: 'https://raw.githubusercontent.com/LavaMoat/LavaMoat/main/packages/yarn-plugin-allow-scripts/bundles/@yarnpkg/plugin-allow-scripts.js'
- path: .yarn/plugins/@yarnpkg/plugin-constraints.cjs
spec: '@yarnpkg/plugin-constraints'
- path: .yarn/plugins/local/plugin-workspaces-filter.js

yarnPath: .yarn/releases/yarn-3.4.1.cjs

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"resolutions": {
"@babel/core": "patch:@babel/core@npm%3A7.21.0#./.yarn/patches/@babel-core-npm-7.21.0-fb3817b0e5.patch",
"@lavamoat/lavapack@^5.1.2": "patch:@lavamoat/lavapack@npm%3A5.1.2#./.yarn/patches/@lavamoat-lavapack-npm-5.1.2-67a55c51e2.patch",
"@types/glob@*": "patch:@types/glob@npm%3A7.1.4#./.yarn/patches/@types-glob-npm-7.1.4-d45247eaa2.patch",
"@types/glob@^7.1.1": "patch:@types/glob@npm%3A7.1.4#./.yarn/patches/@types-glob-npm-7.1.4-d45247eaa2.patch",
"@types/mocha@^10.0.1": "patch:@types/mocha@npm:10.0.1#.yarn/patches/@types-mocha-npm-10.0.1-7c94e9e170.patch",
"clet@^1.0.1": "patch:clet@npm%3A1.0.1#./.yarn/patches/clet-npm-1.0.1-8523231bdc.patch",
"inline-source-map@~0.6.0": "patch:inline-source-map@npm%3A0.6.2#./.yarn/patches/inline-source-map-npm-0.6.2-96902459a0.patch",
Expand Down Expand Up @@ -79,6 +81,7 @@
"geckodriver": "^3.2.0",
"jest": "^29.0.2",
"lint-staged": "^12.4.1",
"minimatch": "^7.4.1",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^4.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1438,9 +1438,9 @@
"packages": {
"browserify>glob>fs.realpath": true,
"browserify>glob>inflight": true,
"browserify>glob>minimatch": true,
"browserify>glob>path-is-absolute": true,
"browserify>inherits": true,
"eslint>minimatch": true,
"pump>once": true
}
},
Expand Down Expand Up @@ -1478,6 +1478,23 @@
"pump>once>wrappy": true
}
},
"browserify>glob>minimatch": {
"builtin": {
"path": true
},
"globals": {
"console": true
},
"packages": {
"browserify>glob>minimatch>brace-expansion": true
}
},
"browserify>glob>minimatch>brace-expansion": {
"packages": {
"eslint>minimatch>brace-expansion>balanced-match": true,
"eslint>minimatch>brace-expansion>concat-map": true
}
},
"browserify>glob>path-is-absolute": {
"globals": {
"process.platform": true
Expand Down Expand Up @@ -1960,23 +1977,6 @@
"eslint>debug>ms": true
}
},
"eslint>minimatch": {
"builtin": {
"path": true
},
"globals": {
"console": true
},
"packages": {
"eslint>minimatch>brace-expansion": true
}
},
"eslint>minimatch>brace-expansion": {
"packages": {
"eslint>minimatch>brace-expansion>balanced-match": true,
"eslint>minimatch>brace-expansion>concat-map": true
}
},
"eslint>strip-ansi": {
"packages": {
"eslint>strip-ansi>ansi-regex": true
Expand Down
45 changes: 0 additions & 45 deletions scripts/child-workspace-package-names-as-json.ts

This file was deleted.

21 changes: 16 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5857,7 +5857,7 @@ __metadata:
languageName: node
linkType: hard

"@types/glob@npm:*, @types/glob@npm:^7.1.1":
"@types/glob@npm:7.1.4":
version: 7.1.4
resolution: "@types/glob@npm:7.1.4"
dependencies:
Expand All @@ -5867,6 +5867,16 @@ __metadata:
languageName: node
linkType: hard

"@types/glob@patch:@types/glob@npm%3A7.1.4#./.yarn/patches/@types-glob-npm-7.1.4-d45247eaa2.patch::locator=root%40workspace%3A.":
version: 7.1.4
resolution: "@types/glob@patch:@types/glob@npm%3A7.1.4#./.yarn/patches/@types-glob-npm-7.1.4-d45247eaa2.patch::version=7.1.4&hash=5dccb7&locator=root%40workspace%3A."
dependencies:
"@types/minimatch": "*"
"@types/node": "*"
checksum: a7c3ddf075353c407ba3d9142b914756088f2b7ef213f2f5f10553151f8cfb7769cf9b9c513b75f2dcf67f1c66a05b39a6676f3fee40978b524c19795daf250e
languageName: node
linkType: hard

"@types/graceful-fs@npm:^4.1.3":
version: 4.1.5
resolution: "@types/graceful-fs@npm:4.1.5"
Expand Down Expand Up @@ -17263,12 +17273,12 @@ __metadata:
languageName: node
linkType: hard

"minimatch@npm:^7.0.0":
version: 7.4.1
resolution: "minimatch@npm:7.4.1"
"minimatch@npm:^7.0.0, minimatch@npm:^7.4.1":
version: 7.4.6
resolution: "minimatch@npm:7.4.6"
dependencies:
brace-expansion: ^2.0.1
checksum: fd930530878ba08e5a57eacc0e69fbc980d4c1dfeb98c6c5eba3a05a10b1f6d8dd4ecca59442a1909107b7ef891b43c005720c1a8ed1a02db962fd0327bf9557
checksum: 1a6c8d22618df9d2a88aabeef1de5622eb7b558e9f8010be791cb6b0fa6e102d39b11c28d75b855a1e377b12edc7db8ff12a99c20353441caa6a05e78deb5da9
languageName: node
linkType: hard

Expand Down Expand Up @@ -20652,6 +20662,7 @@ __metadata:
geckodriver: ^3.2.0
jest: ^29.0.2
lint-staged: ^12.4.1
minimatch: ^7.4.1
prettier: ^2.7.1
prettier-plugin-packagejson: ^2.2.11
rimraf: ^4.1.2
Expand Down

0 comments on commit 88a0b2c

Please sign in to comment.