Skip to content

feat(pkg), add a new API "modifyPackageJson" to the env #9530

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

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion scopes/envs/envs/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Bundler, BundlerContext, DevServer, DevServerContext } from '@team
import type { BuildTask } from '@teambit/builder';
import type { SchemaExtractor } from '@teambit/schema';
import type { WebpackConfigTransformer } from '@teambit/webpack';
import type { PackageJsonProps } from '@teambit/pkg';
import type { ModifyPackageJsonFunc, PackageJsonProps } from '@teambit/pkg';
import type { DependencyDetector, EnvPolicyConfigObject } from '@teambit/dependency-resolver';
import type { Capsule } from '@teambit/isolator';
import type { Component } from '@teambit/component';
Expand Down Expand Up @@ -119,6 +119,12 @@ export interface PackageEnv extends Environment {
* return `.npmignore` entries to be written before packing the component
*/
getNpmIgnore?: (npmIgnoreContext?: GetNpmIgnoreContext) => string[];

/**
* in case the static "getPackageJsonProps" is not enough, and the package.json props need to be modified
* per component, use this function.
*/
modifyPackageJson?: ModifyPackageJsonFunc;
}

export interface LinterEnv extends Environment {
Expand Down
2 changes: 1 addition & 1 deletion scopes/pkg/pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export type {
} from './pkg.main.runtime';
export type { PackageDependency, PackageDependencyFactory } from './package-dependency';
export type { PackageEnv } from './package-env-type';
export { PackageGenerator } from './package-generator';
export { PackageGenerator, ModifyPackageJsonFunc } from './package-generator';
export { PkgAspect as default, PkgAspect } from './pkg.aspect';
export { PkgUI } from './pkg.ui.runtime';
14 changes: 12 additions & 2 deletions scopes/pkg/pkg/package-generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { EnvContext, EnvHandler } from '@teambit/envs';
import { Component } from '@teambit/component';
import { PackageJsonProps } from './pkg.main.runtime';

export type ModifyPackageJsonFunc = (
component: Component, packageJsonObject: PackageJsonProps) => Promise<PackageJsonProps>;

export type PackageGeneratorOptions = {
packageJson: PackageJsonProps;
npmIgnore?: string[];
modifyPackageJson?: ModifyPackageJsonFunc;
};

/**
Expand All @@ -12,9 +17,10 @@ export type PackageGeneratorOptions = {
*/
export class PackageGenerator {
constructor(
private context: EnvContext,
private _packageJson: PackageJsonProps = {},
private _npmIgnore: string[] = [],
private context: EnvContext
private _modifyPackageJson?: ModifyPackageJsonFunc,
) {}

get packageJsonProps() {
Expand All @@ -25,9 +31,13 @@ export class PackageGenerator {
return this._npmIgnore;
}

get modifyPackageJson(): ModifyPackageJsonFunc | undefined {
return this._modifyPackageJson;
}

static from(options: PackageGeneratorOptions): EnvHandler<PackageGenerator> {
return (context: EnvContext) => {
return new PackageGenerator(options.packageJson, options.npmIgnore, context);
return new PackageGenerator(context, options.packageJson, options.npmIgnore, options.modifyPackageJson);
};
}
}
7 changes: 6 additions & 1 deletion scopes/pkg/pkg/pkg.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,12 @@ export class PkgMain {
packageJsonObject: Record<string, any>
): Promise<Record<string, any>> {
const newProps = this.getPackageJsonModifications(component);
return Object.assign(packageJsonObject, newProps);
const pkgJsonObj = Object.assign(packageJsonObject, newProps);
const env = this.envs.getEnv(component).env;
if (env.modifyPackageJson) {
return env.modifyPackageJson(component, pkgJsonObj);
}
return pkgJsonObj;
}
}

Expand Down
1 change: 1 addition & 0 deletions scopes/pkg/pkg/pkg.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class PkgService implements EnvService<{}, PkgDescriptor> {
getPackageJsonProps: () => packageGenerator.packageJsonProps,
// TODO: somehow handle context here? used in the aspect env
getNpmIgnore: () => packageGenerator.npmIgnore,
modifyPackageJson: packageGenerator.modifyPackageJson,
};
}

Expand Down