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(ui-devkit): Support pnpm to build UI extensions #2877

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## compileUiExtensions

<GenerationInfo sourceFile="packages/ui-devkit/src/compiler/compile.ts" sourceLine="35" packageName="@vendure/ui-devkit" />
<GenerationInfo sourceFile="packages/ui-devkit/src/compiler/compile.ts" sourceLine="36" packageName="@vendure/ui-devkit" />

Compiles the Admin UI app with the specified extensions.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: "UiExtensionBuildCommand"
isDefaultIndex: false
generated: true
---
<!-- This file was generated from the Vendure source. Do not modify. Instead, re-run the "docs:build" script -->
import MemberInfo from '@site/src/components/MemberInfo';
import GenerationInfo from '@site/src/components/GenerationInfo';
import MemberDescription from '@site/src/components/MemberDescription';


## UiExtensionBuildCommand

<GenerationInfo sourceFile="packages/ui-devkit/src/compiler/types.ts" sourceLine="356" packageName="@vendure/ui-devkit" />

The package manager to use when invoking the Angular CLI to build UI extensions.

```ts title="Signature"
type UiExtensionBuildCommand = 'npm' | 'yarn' | 'pnpm'
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## UiExtensionCompilerOptions

<GenerationInfo sourceFile="packages/ui-devkit/src/compiler/types.ts" sourceLine="356" packageName="@vendure/ui-devkit" />
<GenerationInfo sourceFile="packages/ui-devkit/src/compiler/types.ts" sourceLine="364" packageName="@vendure/ui-devkit" />

Options to configure how the Admin UI should be compiled.

Expand All @@ -23,7 +23,7 @@ interface UiExtensionCompilerOptions {
devMode?: boolean;
baseHref?: string;
watchPort?: number;
command?: 'yarn' | 'npm';
command?: UiExtensionBuildCommand;
additionalProcessArguments?: UiExtensionCompilerProcessArgument[];
}
```
Expand Down Expand Up @@ -102,11 +102,11 @@ In watch mode, allows the port of the dev server to be specified. Defaults to th
of `4200`.
### command

<MemberInfo kind="property" type={`'yarn' | 'npm'`} since="1.5.0" />
<MemberInfo kind="property" type={`<a href='/reference/admin-ui-api/ui-devkit/ui-extension-build-command#uiextensionbuildcommand'>UiExtensionBuildCommand</a>`} since="1.5.0" />

Internally, the Angular CLI will be invoked as an npm script. By default, the compiler will use Yarn
to run the script if it is detected, otherwise it will use npm. This setting allows you to explicitly
set which command to use, rather than relying on the default behavior.
set which command to use, including pnpm, rather than relying on the default behavior.
### additionalProcessArguments

<MemberInfo kind="property" type={`<a href='/reference/admin-ui-api/ui-devkit/ui-extension-compiler-process-argument#uiextensioncompilerprocessargument'>UiExtensionCompilerProcessArgument</a>[]`} default="undefined" since="1.5.0" />
Expand Down
28 changes: 16 additions & 12 deletions packages/ui-devkit/src/compiler/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import { copyGlobalStyleFile, setBaseHref, setupScaffold } from './scaffold';
import { getAllTranslationFiles, mergeExtensionTranslations } from './translations';
import {
StaticAssetDefinition,
UiExtensionBuildCommand,
UiExtensionCompilerOptions,
UiExtensionCompilerProcessArgument,
} from './types';
import {
copyStaticAsset,
copyUiDevkit,
determinePackageManager,
getStaticAssetPath,
isAdminUiExtension,
isGlobalStylesExtension,
isStaticAssetExtension,
isTranslationExtension,
normalizeExtensions,
shouldUseYarn,
} from './utils';

/**
Expand All @@ -35,18 +36,21 @@ import {
export function compileUiExtensions(
options: UiExtensionCompilerOptions,
): AdminUiAppConfig | AdminUiAppDevModeConfig {
const { devMode, watchPort, command } = options;
const usingYarn = command && command === 'npm' ? false : shouldUseYarn();
const { devMode, watchPort } = options;
const command: UiExtensionBuildCommand =
options.command && ['npm', 'pnpm'].includes(options.command)
? options.command
: determinePackageManager();
if (devMode) {
return runWatchMode({
watchPort: watchPort || 4200,
usingYarn,
...options,
command,
});
} else {
return runCompileMode({
usingYarn,
...options,
command,
});
}
}
Expand All @@ -55,24 +59,24 @@ function runCompileMode({
outputPath,
baseHref,
extensions,
usingYarn,
command,
additionalProcessArguments,
ngCompilerPath,
}: UiExtensionCompilerOptions & { usingYarn: boolean }): AdminUiAppConfig {
}: UiExtensionCompilerOptions & { command: UiExtensionBuildCommand }): AdminUiAppConfig {
const distPath = path.join(outputPath, 'dist');

const compile = () =>
new Promise<void>(async (resolve, reject) => {
await setupScaffold(outputPath, extensions);
await setBaseHref(outputPath, baseHref || DEFAULT_BASE_HREF);

let cmd = usingYarn ? 'yarn' : 'npm';
let cmd: UiExtensionBuildCommand | 'node' = command;
let commandArgs = ['run', 'build'];
if (ngCompilerPath) {
cmd = 'node';
commandArgs = [ngCompilerPath, 'build', '--configuration production'];
} else {
if (!usingYarn) {
if (cmd === 'npm') {
// npm requires `--` before any command line args being passed to a script
commandArgs.splice(2, 0, '--');
}
Expand Down Expand Up @@ -109,10 +113,10 @@ function runWatchMode({
baseHref,
watchPort,
extensions,
usingYarn,
command,
additionalProcessArguments,
ngCompilerPath,
}: UiExtensionCompilerOptions & { usingYarn: boolean }): AdminUiAppDevModeConfig {
}: UiExtensionCompilerOptions & { command: UiExtensionBuildCommand }): AdminUiAppDevModeConfig {
const devkitPath = require.resolve('@vendure/ui-devkit');
let buildProcess: ChildProcess;
let watcher: FSWatcher | undefined;
Expand All @@ -128,7 +132,7 @@ function runWatchMode({
const globalStylesExtensions = extensions.filter(isGlobalStylesExtension);
const staticAssetExtensions = extensions.filter(isStaticAssetExtension);
const allTranslationFiles = getAllTranslationFiles(extensions.filter(isTranslationExtension));
let cmd = usingYarn ? 'yarn' : 'npm';
let cmd: UiExtensionBuildCommand | 'node' = command;
let commandArgs = ['run', 'start'];
if (ngCompilerPath) {
cmd = 'node';
Expand Down
12 changes: 10 additions & 2 deletions packages/ui-devkit/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ export interface AdminUiExtensionLazyModule {
*/
export type UiExtensionCompilerProcessArgument = string | [string, any];

/**
* @description
* The package manager to use when invoking the Angular CLI to build UI extensions.
*
* @docsCategory UiDevkit
*/
export type UiExtensionBuildCommand = 'npm' | 'yarn' | 'pnpm';

/**
* @description
* Options to configure how the Admin UI should be compiled.
Expand Down Expand Up @@ -435,11 +443,11 @@ export interface UiExtensionCompilerOptions {
* @description
* Internally, the Angular CLI will be invoked as an npm script. By default, the compiler will use Yarn
* to run the script if it is detected, otherwise it will use npm. This setting allows you to explicitly
* set which command to use, rather than relying on the default behavior.
* set which command to use, including pnpm, rather than relying on the default behavior.
*
* @since 1.5.0
*/
command?: 'yarn' | 'npm';
command?: UiExtensionBuildCommand;

/**
* @description
Expand Down
8 changes: 4 additions & 4 deletions packages/ui-devkit/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export const logger = {
};

/**
* Checks for the global yarn binary and returns true if found.
* Checks for the global yarn binary to determine whether to use yarn or npm.
*/
export function shouldUseYarn(): boolean {
export function determinePackageManager(): 'yarn' | 'npm' {
try {
execSync('yarnpkg --version', { stdio: 'ignore' });
return true;
return 'yarn';
} catch (e: any) {
return false;
return 'npm';
}
}

Expand Down
Loading