-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: asynchronously create packager instance (#352)
- Loading branch information
1 parent
f2d09a3
commit f60b9f5
Showing
7 changed files
with
101 additions
and
44 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
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,44 +1,58 @@ | ||
/** | ||
* Factory for supported packagers. | ||
* | ||
* All packagers must implement the following interface: | ||
* All packagers must extend the Packager class. | ||
* | ||
* interface Packager { | ||
* | ||
* static get lockfileName(): string; | ||
* static get copyPackageSectionNames(): Array<string>; | ||
* static get mustCopyModules(): boolean; | ||
* static getProdDependencies(cwd: string, depth: number = 1): BbPromise<Object>; | ||
* static rebaseLockfile(pathToPackageRoot: string, lockfile: Object): void; | ||
* static install(cwd: string): BbPromise<void>; | ||
* static prune(cwd: string): BbPromise<void>; | ||
* static runScripts(cwd: string, scriptNames): BbPromise<void>; | ||
* | ||
* } | ||
* @see Packager | ||
*/ | ||
import { memoizeWith } from 'ramda'; | ||
|
||
import { isPackagerId } from '../type-predicate'; | ||
|
||
import type EsbuildServerlessPlugin from '../index'; | ||
import type { PackagerId } from '../types'; | ||
import type { Packager } from './packager'; | ||
|
||
const packagerFactories: Record<PackagerId, () => Promise<Packager>> = { | ||
async npm() { | ||
const { NPM } = await import('./npm'); | ||
|
||
return new NPM(); | ||
}, | ||
async pnpm() { | ||
const { Pnpm } = await import('./pnpm'); | ||
|
||
import { Packager } from './packager'; | ||
import { NPM } from './npm'; | ||
import { Pnpm } from './pnpm'; | ||
import { Yarn } from './yarn'; | ||
return new Pnpm(); | ||
}, | ||
async yarn() { | ||
const { Yarn } = await import('./yarn'); | ||
|
||
const registeredPackagers = { | ||
npm: new NPM(), | ||
pnpm: new Pnpm(), | ||
yarn: new Yarn(), | ||
return new Yarn(); | ||
}, | ||
}; | ||
|
||
/** | ||
* Factory method. | ||
* @this ServerlessWebpack - Active plugin instance | ||
* @param {string} packagerId - Well known packager id. | ||
* @returns {Promise<Packager>} - Promised packager to allow packagers be created asynchronously. | ||
* Asynchronously create a Packager instance and memoize it. | ||
* | ||
* @this EsbuildServerlessPlugin - Active plugin instance | ||
* @param {string} packagerId - Well known packager id | ||
* @returns {Promise<Packager>} - The selected Packager | ||
*/ | ||
export function get(packagerId: string): Promise<Packager> { | ||
if (!(packagerId in registeredPackagers)) { | ||
const message = `Could not find packager '${packagerId}'`; | ||
this.log.error(`ERROR: ${message}`); | ||
throw new this.serverless.classes.Error(message); | ||
export const getPackager = memoizeWith( | ||
(packagerId) => packagerId, | ||
async function (this: EsbuildServerlessPlugin, packagerId: PackagerId): Promise<Packager> { | ||
this.log.debug(`Trying to create packager: ${packagerId}`); | ||
|
||
if (!isPackagerId(packagerId)) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore Serverless typings (as of v3.0.2) are incorrect | ||
throw new this.serverless.classes.Error(`Could not find packager '${packagerId}'`); | ||
} | ||
|
||
const packager = await packagerFactories[packagerId](); | ||
|
||
this.log.debug(`Packager created: ${packagerId}`); | ||
|
||
return packager; | ||
} | ||
return registeredPackagers[packagerId]; | ||
} | ||
); |
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,17 @@ | ||
import { getPackager } from '../../packagers'; | ||
|
||
import type EsbuildServerlessPlugin from '../../index'; | ||
|
||
describe('getPackager()', () => { | ||
const mockPlugin = { | ||
log: { | ||
debug: jest.fn(), | ||
}, | ||
} as unknown as EsbuildServerlessPlugin; | ||
|
||
it('Returns a Packager instance', async () => { | ||
const npm = await getPackager.call(mockPlugin, 'npm'); | ||
|
||
expect(npm).toEqual(expect.any(Object)); | ||
}); | ||
}); |
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,15 @@ | ||
import { isPackagerId } from '../type-predicate'; | ||
|
||
describe('isPackagerId()', () => { | ||
it('Returns true for valid input', () => { | ||
['npm', 'pnpm', 'yarn'].forEach((id) => { | ||
expect(isPackagerId(id)).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it('Returns false for invalid input', () => { | ||
['not-a-real-packager-id', false, 123, [], {}].forEach((id) => { | ||
expect(isPackagerId(id)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
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,5 @@ | ||
import type { PackagerId } from './types'; | ||
|
||
export function isPackagerId(input: unknown): input is PackagerId { | ||
return input === 'npm' || input === 'pnpm' || input === 'yarn'; | ||
} |
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