-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from NullVoxPopuli/support-sub-paths-configs
Support `overrides` for specified paths (as globs) and varying ranges for devDependencies and dependencies, separately
- Loading branch information
Showing
8 changed files
with
307 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
import type { Config } from '../types.js'; | ||
import { normalizeConfig } from '../config.js'; | ||
|
||
export function c(overrides: Partial<Config> = {}): Config { | ||
return { | ||
'write-as': 'pinned' as const, | ||
...overrides, | ||
'update-range': { | ||
'~': [], | ||
'^': [], | ||
...overrides['update-range'], | ||
}, | ||
}; | ||
import type { Config, UserConfig } from '../types.js'; | ||
|
||
export function c(userConfig: UserConfig = {}): Config { | ||
return normalizeConfig(userConfig); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { describe, expect as e, it } from 'vitest'; | ||
|
||
import { c } from './-tests/helpers.ts'; | ||
import { getOverride } from './config.js'; | ||
|
||
const expect = e.soft; | ||
|
||
describe('getOverride', () => { | ||
it('resolves a package glob', () => { | ||
expect( | ||
getOverride( | ||
'packages/ember-repl/addon', | ||
c({ | ||
overrides: [ | ||
{ | ||
path: [ | ||
'packages/*/addon/package.json', | ||
'packages/syntax/*/package.json', | ||
], | ||
dependencies: false, | ||
devDependencies: 'pinned', | ||
}, | ||
], | ||
}), | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"dependencies": false, | ||
"devDependencies": "pinned", | ||
"path": [ | ||
"packages/*/addon/package.json", | ||
"packages/syntax/*/package.json", | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it('returns nothing when no match is found', () => { | ||
expect( | ||
getOverride( | ||
'apps/repl', | ||
c({ | ||
overrides: [ | ||
{ | ||
path: ['packages/*/addon/package.json'], | ||
dependencies: false, | ||
devDependencies: 'pinned', | ||
}, | ||
], | ||
}), | ||
), | ||
).toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { describe, expect as e, it } from 'vitest'; | ||
|
||
import { normalizeConfig } from './config.js'; | ||
|
||
const expect = e.soft; | ||
|
||
describe('normalizeConfig', () => { | ||
it('no passed config', () => { | ||
expect(normalizeConfig()).toMatchInlineSnapshot(` | ||
{ | ||
"overrides": [], | ||
"update-range": { | ||
"^": [], | ||
"~": [], | ||
}, | ||
"write-as": "pinned", | ||
} | ||
`); | ||
}); | ||
|
||
it('overrides "write-as"', () => { | ||
expect(normalizeConfig({ 'write-as': 'minors' })).toMatchInlineSnapshot(` | ||
{ | ||
"overrides": [], | ||
"update-range": { | ||
"^": [], | ||
"~": [], | ||
}, | ||
"write-as": "minors", | ||
} | ||
`); | ||
}); | ||
|
||
it('specifies overrides', () => { | ||
expect( | ||
normalizeConfig({ | ||
overrides: [ | ||
{ | ||
path: 'x/y/z', | ||
devDependencies: false, | ||
dependencies: false, | ||
}, | ||
], | ||
}), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"overrides": [ | ||
{ | ||
"dependencies": false, | ||
"devDependencies": false, | ||
"path": [ | ||
"x/y/z", | ||
], | ||
}, | ||
], | ||
"update-range": { | ||
"^": [], | ||
"~": [], | ||
}, | ||
"write-as": "pinned", | ||
} | ||
`); | ||
}); | ||
|
||
it('specifies update-range', () => { | ||
expect( | ||
normalizeConfig({ | ||
'update-range': { | ||
'~': ['ember-data'], | ||
'^': ['@ember-data/*'], | ||
}, | ||
}), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"overrides": [], | ||
"update-range": { | ||
"^": [ | ||
"@ember-data/*", | ||
], | ||
"~": [ | ||
"ember-data", | ||
], | ||
}, | ||
"write-as": "pinned", | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import path from 'node:path'; | ||
|
||
import { minimatch } from 'minimatch'; | ||
|
||
/** | ||
* @param {Partial<import('./types.ts').UserConfig>} [ userConfig ] | ||
* @return {import('./types.ts').Config} | ||
*/ | ||
export function normalizeConfig(userConfig) { | ||
let config = userConfig || {}; | ||
|
||
let topLevel = { | ||
'write-as': config['write-as'] || 'pinned', | ||
}; | ||
|
||
/** @type {import('./types.ts').Config['overrides'] } */ | ||
const overrides = | ||
config['overrides']?.map((override) => { | ||
const defaultRange = topLevel['write-as']; | ||
const pathsArray = Array.isArray(override.path) | ||
? override.path | ||
: [override.path]; | ||
|
||
return { | ||
devDependencies: override['devDependencies'] ?? defaultRange, | ||
dependencies: override['dependencies'] ?? defaultRange, | ||
path: pathsArray, | ||
}; | ||
}) || []; | ||
|
||
return { | ||
...topLevel, | ||
'update-range': { | ||
'~': [], | ||
'^': [], | ||
...config['update-range'], | ||
}, | ||
overrides, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {string} relativePath the workspace path | ||
* @param {import('./types.ts').Config} config | ||
*/ | ||
export function getOverride(relativePath, config) { | ||
let { overrides } = config; | ||
|
||
let packageJsonPath = path.join(relativePath, 'package.json'); | ||
|
||
let override = overrides.find((override) => { | ||
return override.path.some((match) => minimatch(packageJsonPath, match)); | ||
}); | ||
|
||
return override; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.