-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(registries): allowed-hosts are updated for lockfile lint with li…
…fting registries
- Loading branch information
Showing
7 changed files
with
146 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export default function ({packageManager, registries}) { | ||
export default function ({packageManager, registries = {}}) { | ||
return [ | ||
...(!registries || (registries && !registries.registry)) ? [packageManager] : [], | ||
...!registries.registry ? [packageManager] : [], | ||
...Object.values(Object.fromEntries(Object.entries(registries).filter(([scope]) => 'publish' !== scope))) | ||
]; | ||
} |
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,16 +1,43 @@ | ||
import { | ||
scaffold as scaffoldLockfileLint, | ||
test as lockfileLintIsAlreadyConfigured, | ||
read as readLockfileLintConfig, | ||
write as writeLockfileLintConfig | ||
} from '../lockfile-lint/index.js'; | ||
import {read as readNpmConfig, write as writeNpmConfig} from '../npm-config/index.js'; | ||
import buildRegistriesConfig from './npm-config/list-builder.js'; | ||
import buildAllowedHostsList from '../lockfile-lint/allowed-hosts-builder.js'; | ||
|
||
export default async function ({projectRoot, configs}) { | ||
const registries = buildRegistriesConfig(configs.registries); | ||
async function updateRegistriesInNpmConfig(registries, projectRoot) { | ||
const registriesForNpmConfig = buildRegistriesConfig(registries); | ||
|
||
await writeNpmConfig({ | ||
projectRoot, | ||
config: { | ||
...(await readNpmConfig({projectRoot})), | ||
...registries | ||
...registriesForNpmConfig | ||
} | ||
}); | ||
} | ||
|
||
async function updateRegistriesInLockfileLintConfig(projectRoot, packageManager, registries) { | ||
await writeLockfileLintConfig({ | ||
projectRoot, | ||
config: { | ||
...await readLockfileLintConfig(), | ||
'allowed-hosts': buildAllowedHostsList({packageManager, registries}) | ||
} | ||
}); | ||
} | ||
|
||
export default async function ({projectRoot, packageManager, configs: {registries}}) { | ||
await updateRegistriesInNpmConfig(registries, projectRoot); | ||
|
||
if (!(await lockfileLintIsAlreadyConfigured({projectRoot}))) { | ||
return scaffoldLockfileLint({projectRoot, packageManager, registries}); | ||
} | ||
|
||
await updateRegistriesInLockfileLintConfig(projectRoot, packageManager, registries); | ||
|
||
return {}; | ||
} |
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,29 +1,63 @@ | ||
import any from '@travi/any'; | ||
import {describe, expect, it, vi} from 'vitest'; | ||
import {beforeEach, describe, expect, it, vi} from 'vitest'; | ||
import {when} from 'jest-when'; | ||
|
||
import buildAllowedHostsList from '../lockfile-lint/allowed-hosts-builder.js'; | ||
import { | ||
scaffold as scaffoldLockfileLint, | ||
test as lockfileLintIsAlreadyConfigured, | ||
read as readLockfileLintConfig, | ||
write as writeLockfileLintConfig | ||
} from '../lockfile-lint/index.js'; | ||
import {read as readNpmConfig, write as writeNpmConfig} from '../npm-config/index.js'; | ||
import buildRegistriesConfig from './npm-config/list-builder.js'; | ||
import liftRegistries from './lifter.js'; | ||
|
||
vi.mock('../lockfile-lint/allowed-hosts-builder.js'); | ||
vi.mock('../lockfile-lint/index.js'); | ||
vi.mock('../registries/npm-config/list-builder.js'); | ||
vi.mock('../npm-config/index.js'); | ||
|
||
describe('registries lifter', () => { | ||
it('should define the registries in the npmrc and lockfile-lint configs', async () => { | ||
const projectRoot = any.string(); | ||
const registries = any.simpleObject(); | ||
const configs = {...any.simpleObject(), registries}; | ||
const processedRegistryDetails = any.simpleObject(); | ||
const existingNpmConfig = any.simpleObject(); | ||
const projectRoot = any.string(); | ||
const packageManager = any.word(); | ||
const registries = any.simpleObject(); | ||
const configs = {...any.simpleObject(), registries}; | ||
const processedRegistryDetails = any.simpleObject(); | ||
const existingNpmConfig = any.simpleObject(); | ||
|
||
beforeEach(() => { | ||
when(readNpmConfig).calledWith({projectRoot}).mockResolvedValue(existingNpmConfig); | ||
when(buildRegistriesConfig).calledWith(registries).mockReturnValue(processedRegistryDetails); | ||
}); | ||
|
||
it('should define the registries in the npmrc and lockfile-lint configs', async () => { | ||
const existingLockfileLintConfig = any.simpleObject(); | ||
const allowedHosts = any.listOf(any.url); | ||
when(lockfileLintIsAlreadyConfigured).calledWith({projectRoot}).mockResolvedValue(true); | ||
readLockfileLintConfig.mockResolvedValue(existingLockfileLintConfig); | ||
when(buildAllowedHostsList).calledWith({packageManager, registries}).mockReturnValue(allowedHosts); | ||
|
||
expect(await liftRegistries({projectRoot, configs})).toEqual({}); | ||
expect(await liftRegistries({projectRoot, packageManager, configs})).toEqual({}); | ||
|
||
expect(writeNpmConfig).toHaveBeenCalledWith({ | ||
projectRoot, | ||
config: {...existingNpmConfig, ...processedRegistryDetails} | ||
}); | ||
expect(writeLockfileLintConfig).toHaveBeenCalledWith({ | ||
projectRoot, | ||
config: {...existingLockfileLintConfig, 'allowed-hosts': allowedHosts} | ||
}); | ||
}); | ||
|
||
it('should scaffold lockfile-lint if not already present', async () => { | ||
const lockfileLintResults = any.simpleObject(); | ||
when(lockfileLintIsAlreadyConfigured).calledWith({projectRoot}).mockResolvedValue(false); | ||
when(scaffoldLockfileLint) | ||
.calledWith({projectRoot, packageManager, registries}) | ||
.mockResolvedValue(lockfileLintResults); | ||
|
||
expect(await liftRegistries({projectRoot, packageManager, configs})) | ||
.toEqual(lockfileLintResults); | ||
}); | ||
}); |
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
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