From cede407ac38b34f69851c7e0dbbbf61813c7858a Mon Sep 17 00:00:00 2001 From: Maxim-Durand <72691393+Maxim-Durand@users.noreply.github.com> Date: Thu, 7 Mar 2024 06:00:36 -0500 Subject: [PATCH 1/4] added "non_npm" usage for the plugin (updated doc too) --- packages/filter-by-workspace-path/README.md | 4 ++- .../filter-by-workspace-path/src/index.ts | 30 +++++++++++++++---- .../test/index.spec.ts | 2 +- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/filter-by-workspace-path/README.md b/packages/filter-by-workspace-path/README.md index 230d55c..2794ecc 100644 --- a/packages/filter-by-workspace-path/README.md +++ b/packages/filter-by-workspace-path/README.md @@ -11,7 +11,7 @@ To start using this plugin, add it to your `.autorc` config, for example: ```json { "plugins": [ - "@restfulhead/auto-plugin-filter-by-workspace-path", + ["@restfulhead/auto-plugin-filter-by-workspace-path",{"non_npm":false}], "npm", ] } @@ -20,6 +20,8 @@ To start using this plugin, add it to your `.autorc` config, for example: Then, if your project uses NPM workspaces and you run e.g. `auto changelog` not from the root directory, but directly from a workspace directory, then the changelog will only include pull requests that contain files inside the current workspace directory. +If you're using auto without NPM you can specify `"non_npm": true` in the configuration, this way the plugin will filter out changes outside of the current folder `auto` is run from. + However, carefully read the following caveats section. ## Caveats diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index a6a6fd0..11a7919 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -10,6 +10,7 @@ function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: // auto adds the current path to the file paths reported from git, so we need to undo this const fixedFiles = commit.files.map((file) => path.relative(currentDir, file)) + console.log(fixedFiles) const wsDir = path.join(currentWorkspace, path.sep) const atLeastOneFileInCurrentDir = fixedFiles.find((file) => inFolder(wsDir, file)) @@ -28,16 +29,35 @@ function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: } } +export type ProjectFilteringPluginOptions ={ + /** Path from the repo root to project we are filtering on */ + non_npm: boolean, +}; + export default class FilterByWorkspacePathPlugin implements IPlugin { /** The name of the plugin */ name = 'filter-by-workspace-path-plugin' +/** The options of the plugin */ + readonly options: ProjectFilteringPluginOptions; + + /** Initialize the plugin with it's options */ + constructor(options: ProjectFilteringPluginOptions) { + this.options = options + } + + apply(auto: Auto): void { + const currentDir = path.resolve('.') - const npmResult = execSync('npm ls --omit=dev --depth 1 -json', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }) - const workspaceDeps: any = JSON.parse(npmResult).dependencies - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - const currentWorkspace = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved.substring(11) + let currentWorkspace = currentDir + + if (!this.options.non_npm){ + const npmResult = execSync('npm ls --omit=dev --depth 1 -json', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }) + const workspaceDeps: any = JSON.parse(npmResult).dependencies + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + currentWorkspace = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved.substring(11) + } auto.hooks.onCreateLogParse.tap(this.name, (logParse) => { logParse.hooks.omitCommit.tap(this.name, (commit) => @@ -65,4 +85,4 @@ export default class FilterByWorkspacePathPlugin implements IPlugin { } }) } -} +} \ No newline at end of file diff --git a/packages/filter-by-workspace-path/test/index.spec.ts b/packages/filter-by-workspace-path/test/index.spec.ts index 831a445..b6832bb 100644 --- a/packages/filter-by-workspace-path/test/index.spec.ts +++ b/packages/filter-by-workspace-path/test/index.spec.ts @@ -8,7 +8,7 @@ import Release from '@auto-it/core/dist/release' import FilterByPathPlugin from '../src' const setup = () => { - const plugin = new FilterByPathPlugin() + const plugin = new FilterByPathPlugin({"non_npm":false}) const hooks = makeHooks() const logger = createLog() const logParseHooks = makeLogParseHooks() From 9226ecd011f4036ad53bf218b807a7e799eafda6 Mon Sep 17 00:00:00 2001 From: Maxim-Durand <72691393+Maxim-Durand@users.noreply.github.com> Date: Thu, 7 Mar 2024 06:47:57 -0500 Subject: [PATCH 2/4] remove console.log --- packages/filter-by-workspace-path/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index 11a7919..1cc6fd9 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -10,7 +10,6 @@ function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: // auto adds the current path to the file paths reported from git, so we need to undo this const fixedFiles = commit.files.map((file) => path.relative(currentDir, file)) - console.log(fixedFiles) const wsDir = path.join(currentWorkspace, path.sep) const atLeastOneFileInCurrentDir = fixedFiles.find((file) => inFolder(wsDir, file)) From 040b54c3f888b2993886ed100cb387962ce17acb Mon Sep 17 00:00:00 2001 From: Maxim-Durand <72691393+Maxim-Durand@users.noreply.github.com> Date: Mon, 18 Mar 2024 06:27:27 -0400 Subject: [PATCH 3/4] changed config name to "npm" and made it default to "true". Updated Readme with a config table for future changes --- packages/filter-by-workspace-path/README.md | 11 ++++++++--- packages/filter-by-workspace-path/src/index.ts | 6 +++--- packages/filter-by-workspace-path/test/index.spec.ts | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/filter-by-workspace-path/README.md b/packages/filter-by-workspace-path/README.md index 2794ecc..c485ee6 100644 --- a/packages/filter-by-workspace-path/README.md +++ b/packages/filter-by-workspace-path/README.md @@ -11,7 +11,7 @@ To start using this plugin, add it to your `.autorc` config, for example: ```json { "plugins": [ - ["@restfulhead/auto-plugin-filter-by-workspace-path",{"non_npm":false}], + ["@restfulhead/auto-plugin-filter-by-workspace-path",{"npm":true}], "npm", ] } @@ -20,10 +20,15 @@ To start using this plugin, add it to your `.autorc` config, for example: Then, if your project uses NPM workspaces and you run e.g. `auto changelog` not from the root directory, but directly from a workspace directory, then the changelog will only include pull requests that contain files inside the current workspace directory. -If you're using auto without NPM you can specify `"non_npm": true` in the configuration, this way the plugin will filter out changes outside of the current folder `auto` is run from. - However, carefully read the following caveats section. +## Supported Config + +| Parameter | Description | Default | +| ----------------------- | ---------------------------------------------------------- | -------- | +| `npm` | Boolean to use NPM workspaces or not. In case you're using auto without NPM, specify `"npm": false`, this way the plugin will filter out changes outside of the current folder `auto` is run from. | `true` | + + ## Caveats * Note that this plugin also omits commits that diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index 1cc6fd9..406cc60 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -30,7 +30,7 @@ function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: export type ProjectFilteringPluginOptions ={ /** Path from the repo root to project we are filtering on */ - non_npm: boolean, + npm: boolean, }; export default class FilterByWorkspacePathPlugin implements IPlugin { @@ -41,7 +41,7 @@ export default class FilterByWorkspacePathPlugin implements IPlugin { readonly options: ProjectFilteringPluginOptions; /** Initialize the plugin with it's options */ - constructor(options: ProjectFilteringPluginOptions) { + constructor(options: ProjectFilteringPluginOptions = {"npm":true}) { this.options = options } @@ -51,7 +51,7 @@ export default class FilterByWorkspacePathPlugin implements IPlugin { const currentDir = path.resolve('.') let currentWorkspace = currentDir - if (!this.options.non_npm){ + if (this.options.npm){ const npmResult = execSync('npm ls --omit=dev --depth 1 -json', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }) const workspaceDeps: any = JSON.parse(npmResult).dependencies // eslint-disable-next-line @typescript-eslint/no-magic-numbers diff --git a/packages/filter-by-workspace-path/test/index.spec.ts b/packages/filter-by-workspace-path/test/index.spec.ts index b6832bb..831a445 100644 --- a/packages/filter-by-workspace-path/test/index.spec.ts +++ b/packages/filter-by-workspace-path/test/index.spec.ts @@ -8,7 +8,7 @@ import Release from '@auto-it/core/dist/release' import FilterByPathPlugin from '../src' const setup = () => { - const plugin = new FilterByPathPlugin({"non_npm":false}) + const plugin = new FilterByPathPlugin() const hooks = makeHooks() const logger = createLog() const logParseHooks = makeLogParseHooks() From b95107cbffffe129ef0b109d0f197217860b61d9 Mon Sep 17 00:00:00 2001 From: Maxim-Durand <72691393+Maxim-Durand@users.noreply.github.com> Date: Mon, 18 Mar 2024 06:43:47 -0400 Subject: [PATCH 4/4] lint --- .../filter-by-workspace-path/src/index.ts | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index 406cc60..3ad8dd7 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -28,34 +28,32 @@ function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: } } -export type ProjectFilteringPluginOptions ={ +export interface ProjectFilteringPluginOptions { /** Path from the repo root to project we are filtering on */ - npm: boolean, -}; + npm: boolean +} export default class FilterByWorkspacePathPlugin implements IPlugin { /** The name of the plugin */ name = 'filter-by-workspace-path-plugin' -/** The options of the plugin */ - readonly options: ProjectFilteringPluginOptions; + /** The options of the plugin */ + readonly options: ProjectFilteringPluginOptions /** Initialize the plugin with it's options */ - constructor(options: ProjectFilteringPluginOptions = {"npm":true}) { + constructor(options: ProjectFilteringPluginOptions = { npm: true }) { this.options = options } - apply(auto: Auto): void { - const currentDir = path.resolve('.') let currentWorkspace = currentDir - if (this.options.npm){ - const npmResult = execSync('npm ls --omit=dev --depth 1 -json', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }) - const workspaceDeps: any = JSON.parse(npmResult).dependencies - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - currentWorkspace = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved.substring(11) + if (this.options.npm) { + const npmResult = execSync('npm ls --omit=dev --depth 1 -json', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }) + const workspaceDeps: any = JSON.parse(npmResult).dependencies + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + currentWorkspace = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved.substring(11) } auto.hooks.onCreateLogParse.tap(this.name, (logParse) => { @@ -84,4 +82,4 @@ export default class FilterByWorkspacePathPlugin implements IPlugin { } }) } -} \ No newline at end of file +}