Skip to content

Commit

Permalink
feat: external incompatibility with Yarn 2+; conditionally set packag…
Browse files Browse the repository at this point in the history
…er args (#436)

* feat: add method to getVersion for yarn packager

* fix: dynamically set yarn install parameters for compatibility with yarn 2+

* feat: add ignoreLockfile packager option for Yarn
  • Loading branch information
track0x1 committed Mar 24, 2023
1 parent bac2356 commit af53cd0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ The following `esbuild` options are automatically set.

#### Packager Options

| Option | Description | Default |
| --------- | ----------------------------------------------------------------------------------------------------- | ----------- |
| `scripts` | A string or array of scripts to be executed, currently only supports 'scripts' for npm, pnpm and yarn | `undefined` |
| `noInstall` | [Yarn only] A boolean that deactivates the install step | `false` |
| Option | Description | Default |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| `scripts` | A string or array of scripts to be executed, currently only supports 'scripts' for npm, pnpm and yarn | `undefined` |
| `noInstall` | [Yarn only] A boolean that deactivates the install step | `false` |
| `ignoreLockfile` | [Yarn only] A boolean to bypass lockfile validation, typically paired with `external` dependencies because we generate a new package.json with only the externalized dependencies. | `false` |

#### Watch Options

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
packager: 'npm',
packagerOptions: {
noInstall: false,
ignoreLockfile: false,
},
installExtraArgs: [],
watch: {
Expand Down
22 changes: 18 additions & 4 deletions src/packagers/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ export class Yarn implements Packager {
return false;
}

async getVersion(cwd: string) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['-v'];

const output = await spawnProcess(command, args, { cwd });

return {
version: output.stdout,
isBerry: parseInt(output.stdout.charAt(0), 10) > 1,
};
}

async getProdDependencies(cwd: string, depth?: number): Promise<DependenciesResult> {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['list', depth ? `--depth=${depth}` : null, '--json', '--production'].filter(isString);
Expand Down Expand Up @@ -225,16 +237,18 @@ export class Yarn implements Packager {
);
}

async install(cwd: string, extraArgs: Array<string>, useLockfile = true) {
async install(cwd: string, extraArgs: Array<string>, hasLockfile = true) {
if (this.packagerOptions.noInstall) {
return;
}

const version = await this.getVersion(cwd);
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';

const args = useLockfile
? ['install', '--frozen-lockfile', '--non-interactive', ...extraArgs]
: ['install', '--non-interactive', ...extraArgs];
const args =
!this.packagerOptions.ignoreLockfile && hasLockfile
? ['install', ...(version.isBerry ? ['--immutable'] : ['--frozen-lockfile', '--non-interactive']), ...extraArgs]
: ['install', ...(version.isBerry ? [] : ['--non-interactive']), ...extraArgs];

await spawnProcess(command, args, { cwd });
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface WatchConfiguration {
export interface PackagerOptions {
scripts?: string[] | string;
noInstall?: boolean;
ignoreLockfile?: boolean;
}

interface NodeExternalsOptions {
Expand Down

0 comments on commit af53cd0

Please sign in to comment.