Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pnpm #3351

Closed
wants to merge 14 commits into from
Closed
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ commands:
- run: git config --global core.autocrlf input
- node/install:
node-version: '18.15.0'
- run:
name: Install pnpm package manager
command: |
corepack enable
corepack prepare pnpm@latest-8 --activate
- checkout
run-lint-and-build:
steps:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"electron-windows-store": "^2.1.0",
"electron-winstaller": "^5.0.0",
"electron-wix-msi": "^5.0.0",
"exe-icon-extractor": "npm:@bitdisaster/exe-icon-extractor",
makeryi marked this conversation as resolved.
Show resolved Hide resolved
"macos-alias": "^0.2.11"
},
"peerDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions packages/api/cli/src/util/check-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ function warnIfPackageManagerIsntAKnownGoodVersion(packageManager: string, versi
}

async function checkPackageManagerVersion() {
const version = await forgeUtils.yarnOrNpmSpawn(['--version']);
const version = await forgeUtils.packageManagerSpawn(['--version']);
const versionString = version.toString().trim();
if (forgeUtils.hasYarn()) {
if (forgeUtils.isYarn()) {
warnIfPackageManagerIsntAKnownGoodVersion('Yarn', versionString, YARN_ALLOWLISTED_VERSIONS);
return `yarn@${versionString}`;
} else {
} else if (forgeUtils.isNpm()) {
warnIfPackageManagerIsntAKnownGoodVersion('NPM', versionString, NPM_ALLOWLISTED_VERSIONS);
return `npm@${versionString}`;
} else {
// I think we don't need to check version of pnpm since 2023
return `${forgeUtils.getPackageManager()}@${versionString}`;
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/api/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@
"semver": "^7.2.1",
"source-map-support": "^0.5.13",
"sudo-prompt": "^9.1.1",
"username": "^5.1.0",
"yarn-or-npm": "^3.0.1"
"username": "^5.1.0"
},
"engines": {
"node": ">= 14.17.5"
Expand Down
12 changes: 6 additions & 6 deletions packages/api/core/src/api/import.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { safeYarnOrNpm, updateElectronDependency } from '@electron-forge/core-utils';
import { getPackageManager, updateElectronDependency } from '@electron-forge/core-utils';
import baseTemplate from '@electron-forge/template-base';
import chalk from 'chalk';
import debug from 'debug';
Expand Down Expand Up @@ -194,23 +194,23 @@ export default async ({
{
title: 'Installing dependencies',
task: async (_, task) => {
const packageManager = safeYarnOrNpm();
const packageManager = getPackageManager();
await writeChanges();

d('deleting old dependencies forcefully');
await fs.remove(path.resolve(dir, 'node_modules/.bin/electron'));
await fs.remove(path.resolve(dir, 'node_modules/.bin/electron.cmd'));

d('installing dependencies');
task.output = `${packageManager} install ${importDeps.join(' ')}`;
task.output = `${packageManager} add ${importDeps.join(' ')}`;
await installDepList(dir, importDeps);

d('installing devDependencies');
task.output = `${packageManager} install --dev ${importDevDeps.join(' ')}`;
task.output = `${packageManager} add -D ${importDevDeps.join(' ')}`;
await installDepList(dir, importDevDeps, DepType.DEV);

d('installing exactDevDependencies');
task.output = `${packageManager} install --dev --exact ${importExactDevDeps.join(' ')}`;
task.output = `${packageManager} add -D -E ${importExactDevDeps.join(' ')}`;
await installDepList(dir, importExactDevDeps, DepType.DEV, DepVersionRestriction.EXACT);
},
},
Expand Down Expand Up @@ -258,7 +258,7 @@ export default async ({
},
task: (_, task) => {
task.output = `We have attempted to convert your app to be in a format that Electron Forge understands.

Thanks for using ${chalk.green('Electron Forge')}!`;
},
},
Expand Down
21 changes: 15 additions & 6 deletions packages/api/core/src/api/init-scripts/init-link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { safeYarnOrNpm, yarnOrNpmSpawn } from '@electron-forge/core-utils';
import { getPackageManager, isPnpm, packageManagerSpawn } from '@electron-forge/core-utils';
import { ForgeListrTask } from '@electron-forge/shared-types';
import debug from 'debug';

Expand All @@ -22,14 +22,23 @@ export async function initLink<T>(dir: string, task?: ForgeListrTask<T>) {
if (shouldLink) {
d('Linking forge dependencies');
const packageJson = await readRawPackageJson(dir);
const packageManager = safeYarnOrNpm();
const packageManager = getPackageManager();
const linkFolder = path.resolve(__dirname, '..', '..', '..', '..', '..', '..', '.links');
for (const packageName of Object.keys(packageJson.devDependencies)) {
if (packageName.startsWith('@electron-forge/')) {
if (task) task.output = `${packageManager} link --link-folder ${linkFolder} ${packageName}`;
await yarnOrNpmSpawn(['link', '--link-folder', linkFolder, packageName], {
cwd: dir,
});
if (task) {
if (isPnpm()) {
task.output = `${packageManager} link ${linkFolder}/${packageName}`;
await packageManagerSpawn(['link', `${linkFolder}/${packageName}`], {
cwd: dir,
});
} else {
task.output = `${packageManager} link --link-folder ${linkFolder} ${packageName}`;
await packageManagerSpawn(['link', '--link-folder', linkFolder, packageName], {
cwd: dir,
});
}
}
}
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions packages/api/core/src/api/init-scripts/init-npm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { safeYarnOrNpm } from '@electron-forge/core-utils';
import { getPackageManager } from '@electron-forge/core-utils';
import { ForgeListrTask } from '@electron-forge/shared-types';
import debug from 'debug';
import fs from 'fs-extra';
Expand All @@ -27,17 +27,17 @@ export const exactDevDeps = ['electron'];

export const initNPM = async <T>(dir: string, task: ForgeListrTask<T>): Promise<void> => {
d('installing dependencies');
const packageManager = safeYarnOrNpm();
task.output = `${packageManager} install ${deps.join(' ')}`;
const packageManager = getPackageManager();
task.output = `${packageManager} add ${deps.join(' ')}`;
await installDepList(dir, deps);

d('installing devDependencies');
task.output = `${packageManager} install --dev ${deps.join(' ')}`;
task.output = `${packageManager} add -D ${deps.join(' ')}`;
await installDepList(dir, devDeps, DepType.DEV);

d('installing exact devDependencies');
for (const packageName of exactDevDeps) {
task.output = `${packageManager} install --dev --exact ${packageName}`;
task.output = `${packageManager} add -D -E ${packageName}`;
await installDepList(dir, [packageName], DepType.DEV, DepVersionRestriction.EXACT);
}
};
8 changes: 4 additions & 4 deletions packages/api/core/src/api/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { safeYarnOrNpm } from '@electron-forge/core-utils';
import { getPackageManager } from '@electron-forge/core-utils';
import { ForgeTemplate } from '@electron-forge/shared-types';
import debug from 'debug';
import { Listr } from 'listr2';
Expand Down Expand Up @@ -56,7 +56,7 @@ async function validateTemplate(template: string, templateModule: ForgeTemplate)
export default async ({ dir = process.cwd(), interactive = false, copyCIFiles = false, force = false, template = 'base' }: InitOptions): Promise<void> => {
d(`Initializing in: ${dir}`);

const packageManager = safeYarnOrNpm();
const packageManager = getPackageManager();

const runner = new Listr<{
templateModule: ForgeTemplate;
Expand Down Expand Up @@ -105,7 +105,7 @@ export default async ({ dir = process.cwd(), interactive = false, copyCIFiles =
task: async (_, task) => {
d('installing dependencies');
if (templateModule.dependencies?.length) {
task.output = `${packageManager} install ${templateModule.dependencies.join(' ')}`;
task.output = `${packageManager} add ${templateModule.dependencies.join(' ')}`;
}
return await installDepList(dir, templateModule.dependencies || [], DepType.PROD, DepVersionRestriction.RANGE);
},
Expand All @@ -116,7 +116,7 @@ export default async ({ dir = process.cwd(), interactive = false, copyCIFiles =
task: async (_, task) => {
d('installing devDependencies');
if (templateModule.devDependencies?.length) {
task.output = `${packageManager} install --dev ${templateModule.devDependencies.join(' ')}`;
task.output = `${packageManager} add -D ${templateModule.devDependencies.join(' ')}`;
}
await installDepList(dir, templateModule.devDependencies || [], DepType.DEV);
},
Expand Down
12 changes: 9 additions & 3 deletions packages/api/core/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getElectronVersion, hasYarn, yarnOrNpmSpawn } from '@electron-forge/core-utils';
import { getElectronVersion, getPackageManager, isNpm, isPnpm, isYarn, packageManagerSpawn } from '@electron-forge/core-utils';

import { BuildIdentifierConfig, BuildIdentifierMap, fromBuildIdentifier } from './forge-config';

Expand All @@ -16,7 +16,13 @@ export default class ForgeUtils {

getElectronVersion = getElectronVersion;

hasYarn = hasYarn;
getPackageManager = getPackageManager;

yarnOrNpmSpawn = yarnOrNpmSpawn;
packageManagerSpawn = packageManagerSpawn;

isNpm = isNpm;

isYarn = isYarn;

isPnpm = isPnpm;
}
31 changes: 18 additions & 13 deletions packages/api/core/src/util/install-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasYarn, yarnOrNpmSpawn } from '@electron-forge/core-utils';
import { isNpm, isPnpm, isYarn, packageManagerSpawn } from '@electron-forge/core-utils';
import { ExitError } from '@malept/cross-spawn-promise';
import debug from 'debug';

Expand All @@ -15,24 +15,29 @@ export enum DepVersionRestriction {
}

export default async (dir: string, deps: string[], depType = DepType.PROD, versionRestriction = DepVersionRestriction.RANGE): Promise<void> => {
d('installing', JSON.stringify(deps), 'in:', dir, `depType=${depType},versionRestriction=${versionRestriction},withYarn=${hasYarn()}`);
d(
'installing',
JSON.stringify(deps),
'in:',
dir,
`depType=${depType},versionRestriction=${versionRestriction}},withYarn=${isYarn()},withNpm=${isNpm()},withPnpm=${isPnpm()}`
);
if (deps.length === 0) {
d('nothing to install, stopping immediately');
return Promise.resolve();
}
let cmd = ['install'].concat(deps);
if (hasYarn()) {
cmd = ['add'].concat(deps);
if (depType === DepType.DEV) cmd.push('--dev');
if (versionRestriction === DepVersionRestriction.EXACT) cmd.push('--exact');
} else {
if (versionRestriction === DepVersionRestriction.EXACT) cmd.push('--save-exact');
if (depType === DepType.DEV) cmd.push('--save-dev');
if (depType === DepType.PROD) cmd.push('--save');
}
/**
* To install the specified packages as dependencies
* yarn and pnpm use `add` command
* npm use `add` as an alias command of `install`
* for consistency, we use `add` command here
*/
const cmd = ['add'].concat(deps);
if (depType === DepType.DEV) cmd.push('-D');
if (versionRestriction === DepVersionRestriction.EXACT) cmd.push('-E');
d('executing', JSON.stringify(cmd), 'in:', dir);
try {
await yarnOrNpmSpawn(cmd, {
await packageManagerSpawn(cmd, {
cwd: dir,
stdio: 'pipe',
});
Expand Down
59 changes: 46 additions & 13 deletions packages/api/core/test/fast/install-dependencies_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import installDependencies, { DepType, DepVersionRestriction } from '../../src/u
describe('Install dependencies', () => {
let install: typeof installDependencies;
let spawnSpy: SinonStub;
let hasYarnSpy: SinonStub;
let isNpmSpy: SinonStub;
let isYarnSpy: SinonStub;
let isPnpmSpy: SinonStub;
let spawnPromise: Promise<void>;
let spawnPromiseResolve: () => void;
let spawnPromiseReject: () => void;
Expand All @@ -19,11 +21,16 @@ describe('Install dependencies', () => {
spawnPromiseReject = reject;
});
spawnSpy.returns(spawnPromise);
hasYarnSpy = stub();
isNpmSpy = stub();
isYarnSpy = stub();
isPnpmSpy = stub();

install = proxyquire.noCallThru().load('../../src/util/install-dependencies', {
'@electron-forge/core-utils': {
yarnOrNpmSpawn: spawnSpy,
hasYarn: hasYarnSpy,
packageManagerSpawn: spawnSpy,
isNpm: isNpmSpy,
isYarn: isYarnSpy,
isPnpm: isPnpmSpy,
},
}).default;
});
Expand All @@ -47,7 +54,7 @@ describe('Install dependencies', () => {

describe('with yarn', () => {
beforeEach(() => {
hasYarnSpy.returns(true);
isYarnSpy.returns(true);
});

it('should install prod deps', () => {
Expand All @@ -57,43 +64,69 @@ describe('Install dependencies', () => {

it('should install dev deps', () => {
install('mydir', ['eslint'], DepType.DEV);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'eslint', '--dev']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'eslint', '-D']);
});

it('should install exact deps', () => {
install('mydir', ['react-dom'], DepType.PROD, DepVersionRestriction.EXACT);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react-dom', '--exact']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react-dom', '-E']);
});

it('should install exact dev deps', () => {
install('mydir', ['mocha'], DepType.DEV, DepVersionRestriction.EXACT);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'mocha', '--dev', '--exact']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'mocha', '-D', '-E']);
});
});

describe('with npm', () => {
beforeEach(() => {
hasYarnSpy.returns(false);
isNpmSpy.returns(true);
});

it('should install prod deps', () => {
install('mydir', ['react']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'react', '--save']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react']);
});

it('should install dev deps', () => {
install('mydir', ['eslint'], DepType.DEV);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'eslint', '-D']);
});

it('should install exact deps', () => {
install('mydir', ['react-dom'], DepType.PROD, DepVersionRestriction.EXACT);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react-dom', '-E']);
});

it('should install exact dev deps', () => {
install('mydir', ['mocha'], DepType.DEV, DepVersionRestriction.EXACT);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'mocha', '-D', '-E']);
});
});

describe('with pnpm', () => {
beforeEach(() => {
isPnpmSpy.returns(true);
});

it('should install prod deps', () => {
install('mydir', ['react']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react']);
});

it('should install dev deps', () => {
install('mydir', ['eslint'], DepType.DEV);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'eslint', '--save-dev']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'eslint', '-D']);
});

it('should install exact deps', () => {
install('mydir', ['react-dom'], DepType.PROD, DepVersionRestriction.EXACT);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'react-dom', '--save-exact', '--save']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react-dom', '-E']);
});

it('should install exact dev deps', () => {
install('mydir', ['mocha'], DepType.DEV, DepVersionRestriction.EXACT);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'mocha', '--save-exact', '--save-dev']);
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'mocha', '-D', '-E']);
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading