Skip to content

Commit

Permalink
Harmony/merge extension simple config (#2552)
Browse files Browse the repository at this point in the history
* write configs added by extensions into package.json in the component capsule
* add stub tests for configs
* export component factory extension from api
* related to #2456
* related to #2537
  • Loading branch information
GiladShoham authored Apr 5, 2020
1 parent 5af208b commit 566219f
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./simple-config.manifest');
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const ComponentFactoryExt = require('bit-bin').ComponentFactoryExt;

module.exports = {
name: 'simple config',
dependencies: [ComponentFactoryExt],
provider: async ([component]) => {
console.log('simple config runs');
component.registerAddConfig('simple-config', config => {
console.log('config registration hook is running');
return {
'my-custom-key': 'my-custom-val'
};
});
}
};
269 changes: 154 additions & 115 deletions e2e/harmony/extensions-config.e2e.3.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Extension from './legacy-extensions/extension';
import HooksManager from './hooks';
import { BaseLoadArgsProps } from './legacy-extensions/base-extension';

export { ComponentFactoryExt, ComponentFactory } from './extensions/component';

HooksManager.init();

export * from '@teambit/harmony';
Expand Down
1 change: 1 addition & 0 deletions src/api/consumer/lib/isolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default (async function isolate(
});

async function isolateUsingCapsule(componentId: string, opts: IsolateOptions) {
opts.applyExtensionsAddedConfig = true;
const consumer = await loadConsumer();
const bitId = consumer.getParsedId(componentId);
const isolator: Isolator = await Isolator.getInstance('fs', consumer.scope, consumer, opts.writeToPath);
Expand Down
20 changes: 19 additions & 1 deletion src/consumer/component-ops/component-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type ComponentWriterProps = {
existingComponentMap?: ComponentMap;
excludeRegistryPrefix?: boolean;
capsulePaths?: CapsulePaths;
applyExtensionsAddedConfig?: boolean;
};

export default class ComponentWriter {
Expand All @@ -53,6 +54,7 @@ export default class ComponentWriter {
existingComponentMap: ComponentMap | undefined;
excludeRegistryPrefix: boolean;
capsulePaths?: CapsulePaths;
applyExtensionsAddedConfig?: boolean;
constructor({
component,
writeToPath,
Expand All @@ -67,7 +69,8 @@ export default class ComponentWriter {
deleteBitDirContent,
existingComponentMap,
capsulePaths,
excludeRegistryPrefix = false
excludeRegistryPrefix = false,
applyExtensionsAddedConfig = false
}: ComponentWriterProps) {
this.component = component;
this.writeToPath = writeToPath;
Expand All @@ -83,6 +86,7 @@ export default class ComponentWriter {
this.existingComponentMap = existingComponentMap;
this.excludeRegistryPrefix = excludeRegistryPrefix;
this.capsulePaths = capsulePaths;
this.applyExtensionsAddedConfig = applyExtensionsAddedConfig;
}

static getInstance(componentWriterProps: ComponentWriterProps): ComponentWriter {
Expand Down Expand Up @@ -168,6 +172,9 @@ export default class ComponentWriter {
componentConfig.compiler = this.component.compiler ? this.component.compiler.toBitJsonObject() : {};
componentConfig.tester = this.component.tester ? this.component.tester.toBitJsonObject() : {};
packageJson.addOrUpdateProperty('bit', componentConfig.toPlainObject());
if (this.applyExtensionsAddedConfig) {
this._mergePackageJsonPropsFromExtensions(packageJson);
}
this._mergeChangedPackageJsonProps(packageJson);
this._mergePackageJsonPropsFromOverrides(packageJson);
this.component.dataToPersist.addFile(packageJson.toVinylFile());
Expand Down Expand Up @@ -205,6 +212,17 @@ export default class ComponentWriter {
});
}

/**
* these changes were added by extensions
*/
_mergePackageJsonPropsFromExtensions(packageJson: PackageJsonFile) {
// The special keys will be merged in other place
const specialKeys = ['extensions', 'dependencies', 'devDependencies', 'peerDependencies'];
if (!this.component.extensionsAddedConfig || R.isEmpty(this.component.extensionsAddedConfig)) return;
const valuesToMerge = R.omit(specialKeys, this.component.extensionsAddedConfig);
packageJson.mergePackageJsonObject(valuesToMerge);
}

/**
* these changes were entered manually by a user via `overrides` key
*/
Expand Down
5 changes: 5 additions & 0 deletions src/consumer/component-ops/many-components-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface ManyComponentsWriterParams {
installProdPackagesOnly?: boolean;
excludeRegistryPrefix?: boolean;
capsulePaths?: CapsulePaths;
applyExtensionsAddedConfig?: boolean;
}

/**
Expand Down Expand Up @@ -83,6 +84,8 @@ export default class ManyComponentsWriter {
basePath?: string;
capsulePaths?: CapsulePaths;
pacackgeManager?: string;
// Apply config added by extensions
applyExtensionsAddedConfig?: boolean;

constructor(params: ManyComponentsWriterParams) {
this.consumer = params.consumer;
Expand All @@ -107,6 +110,7 @@ export default class ManyComponentsWriter {
this.bitMap = this.consumer ? this.consumer.bitMap : new BitMap();
this.capsulePaths = params.capsulePaths;
this.pacackgeManager = params.packageManager;
this.applyExtensionsAddedConfig = params.applyExtensionsAddedConfig;
if (this.consumer && !this.isolated) this.basePath = this.consumer.getPath();
}
_setBooleanDefault(field: boolean | null | undefined, defaultValue: boolean): boolean {
Expand Down Expand Up @@ -224,6 +228,7 @@ export default class ManyComponentsWriter {
consumer: this.consumer,
bitMap: this.bitMap,
isolated: this.isolated,
applyExtensionsAddedConfig: this.applyExtensionsAddedConfig,
excludeRegistryPrefix: this.excludeRegistryPrefix
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/environment/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export default class Environment {
installProdPackagesOnly: opts.installProdPackagesOnly,
verbose: opts.verbose,
excludeRegistryPrefix: !!opts.excludeRegistryPrefix,
silentPackageManagerResult: opts.silentPackageManagerResult
silentPackageManagerResult: opts.silentPackageManagerResult,
applyExtensionsAddedConfig: opts.applyExtensionsAddedConfig
};
const manyComponentsWriter = new ManyComponentsWriter(concreteOpts);
await manyComponentsWriter.writeAll();
Expand Down
2 changes: 2 additions & 0 deletions src/environment/isolator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface IsolateOptions {
verbose?: boolean; // Print more logs
excludeRegistryPrefix?: boolean; // exclude the registry prefix from the component's name in the package.json
silentPackageManagerResult?: boolean; // Print environment install result
applyExtensionsAddedConfig?: boolean; // apply configs added by extension in the package.json
}

export default class Isolator {
Expand Down Expand Up @@ -119,6 +120,7 @@ export default class Isolator {
verbose: opts.verbose,
excludeRegistryPrefix: !!opts.excludeRegistryPrefix,
silentPackageManagerResult: opts.silentPackageManagerResult,
applyExtensionsAddedConfig: opts.applyExtensionsAddedConfig,
isolated: true
};
this.componentWithDependencies = componentWithDependencies;
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/isolator/write-components-to-capsules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export default async function writeComponentsToCapsules(
silentPackageManagerResult: false,
isolated: true,
capsulePaths,
packageManager
packageManager,
applyExtensionsAddedConfig: true
};
componentsWithDependencies.map(cmp => normalizeComponentDir(cmp));
const manyComponentsWriter = new ManyComponentsWriter(concreteOpts);
Expand Down

0 comments on commit 566219f

Please sign in to comment.