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

refactor, move "bit init" command from legacy to a new aspect "teambit.harmony/host-initializer" #8914

Merged
merged 13 commits into from
May 28, 2024
14 changes: 14 additions & 0 deletions .bitmap
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,20 @@
"mainFile": "index.ts",
"rootDir": "components/hooks/use-viewed-lane-from-url_1"
},
"host-initializer": {
"name": "host-initializer",
"scope": "",
"version": "",
"defaultScope": "teambit.harmony",
"mainFile": "index.ts",
"rootDir": "scopes/harmony/host-initializer",
"config": {
"teambit.harmony/aspect": {},
"teambit.envs/envs": {
"env": "teambit.harmony/aspect"
}
}
},
"html": {
"name": "html",
"scope": "teambit.html",
Expand Down
3 changes: 2 additions & 1 deletion e2e/commands/init.e2e.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ describe('run bit init', function () {
});
});
});
describe('when there is .bitmap, bit.json but not .bit dir', () => {
// no good reason to have it. it's ok to throw an error and telling the user to run "bit init"
describe.skip('when there is .bitmap, bit.json but not .bit dir', () => {
describe('when .bit located directly on workspace root', () => {
before(() => {
helper.scopeHelper.reInitLocalScope();
Expand Down
1 change: 1 addition & 0 deletions e2e/flows/out-of-sync-componets.e2e.3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ describe('components that are not synced between the scope and the consumer', fu
helper.fixtures.tagComponentBarFoo();
helper.command.export();
helper.fs.deletePath('.bit');
helper.command.init();
scopeOutOfSync = helper.scopeHelper.cloneLocalScope();
});
describe('bit tag', () => {
Expand Down
1 change: 1 addition & 0 deletions e2e/harmony/install-and-compile.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ describe('do not fail on environment loading files from a location inside node_m
before(() => {
helper = new Helper();
helper.fixtures.copyFixtureDir('workspace-with-tsconfig-issue', helper.scopes.localPath);
helper.command.init();
});
it('should not fail', () => {
helper.command.install();
Expand Down
2 changes: 2 additions & 0 deletions e2e/harmony/lanes/bit-import-on-lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ describe('bit lane command', function () {
helper.command.snapAllComponentsWithoutBuild('--unmodified');
helper.command.export();
helper.fs.deletePath('.bit');
helper.command.init();
helper.scopeHelper.addRemoteScope();
});
// previously, it was throwing the following error:
Expand All @@ -195,6 +196,7 @@ describe('bit lane command', function () {
helper.command.export();
helper.command.switchLocalLane('dev', '--skip-dependency-installation');
helper.fs.deletePath('.bit');
helper.command.init();
helper.scopeHelper.addRemoteScope();
helper.command.import();
localScope = helper.scopeHelper.cloneLocalScope();
Expand Down
2 changes: 2 additions & 0 deletions e2e/harmony/lanes/lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,7 @@ describe('bit lane command', function () {
helper.command.snapAllComponentsWithoutBuild();
helper.command.exportLane();
helper.fs.deletePath('.bit');
helper.command.init();
helper.scopeHelper.addRemoteScope();
});
it('should re-create scope.json with checkout to the lane specified in the .bitmap file', () => {
Expand Down Expand Up @@ -1349,6 +1350,7 @@ describe('bit lane command', function () {
helper.command.export();
helper.command.snapAllComponentsWithoutBuild('--unmodified');
helper.fs.deletePath('.bit');
helper.command.init();
helper.scopeHelper.addRemoteScope();
});
it('bit status should not throw', () => {
Expand Down
3 changes: 3 additions & 0 deletions e2e/harmony/out-of-sync-components-harmony.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('components that are not synced between the scope and the consumer', fu
helper.fixtures.addComponentBarFoo();
helper.command.tagAllWithoutBuild();
helper.fs.deletePath('.bit');
helper.command.init();
scopeOutOfSync = helper.scopeHelper.cloneLocalScope();
});
describe('bit build', () => {
Expand Down Expand Up @@ -91,6 +92,7 @@ describe('components that are not synced between the scope and the consumer', fu
helper.command.export();
helper.command.tagAllWithoutBuild('--unmodified'); // 0.0.2
helper.fs.deletePath('.bit');
helper.command.init();
helper.scopeHelper.addRemoteScope();
});
it('bit import should not throw', () => {
Expand All @@ -110,6 +112,7 @@ describe('components that are not synced between the scope and the consumer', fu
helper.command.createLane();
helper.command.snapAllComponentsWithoutBuild();
helper.fs.deletePath('.bit');
helper.command.init();
helper.scopeHelper.addRemoteScope();
});
it('should recrate the lane', () => {
Expand Down
15 changes: 13 additions & 2 deletions scopes/generator/generator/workspace-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { UIAspect, UiMain } from '@teambit/ui';
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
import { WorkspaceAspect, Workspace } from '@teambit/workspace';
import { ForkingAspect, ForkingMain } from '@teambit/forking';
import { init } from '@teambit/legacy/dist/api/consumer';
import { ImporterAspect, ImporterMain } from '@teambit/importer';
import { CompilerAspect, CompilerMain } from '@teambit/compiler';
import getGitExecutablePath from '@teambit/legacy/dist/utils/git/git-executable';
Expand All @@ -17,6 +16,7 @@ import { join } from 'path';
import { ComponentID } from '@teambit/component-id';
import { GitAspect, GitMain } from '@teambit/git';
import { InstallAspect, InstallMain } from '@teambit/install';
import { HostInitializerMain } from '@teambit/host-initializer';
import { WorkspaceConfigFilesAspect, WorkspaceConfigFilesMain } from '@teambit/workspace-config-files';
// import { ComponentGenerator } from './component-generator';
import { WorkspaceTemplate, WorkspaceContext } from './workspace-template';
Expand Down Expand Up @@ -50,7 +50,18 @@ export class WorkspaceGenerator {
try {
process.chdir(this.workspacePath);
await this.initGit();
await init(this.workspacePath, this.options.skipGit, false, false, false, false, false, false, false, {});
await HostInitializerMain.init(
this.workspacePath,
this.options.skipGit,
false,
false,
false,
false,
false,
false,
false,
{}
);
await this.writeWorkspaceFiles();
await this.reloadBitInWorkspaceDir();
// Setting the workspace to be in install context to prevent errors during the workspace generation
Expand Down
7 changes: 0 additions & 7 deletions scopes/harmony/bit/load-bit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ function getMainAspect() {
function shouldLoadInSafeMode() {
const currentCommand = process.argv[2];
const commandsToAlwaysRunInSafeMode = [
'init',
'cat-scope',
'cat-object',
'cat-component',
Expand Down Expand Up @@ -352,12 +351,6 @@ export function takeLegacyGlobalsSnapshot(): LegacyGlobal[] {
value: ExtensionDataList.toModelObjectsHook,
empty: [],
},
{
classInstance: WorkspaceConfig,
methodName: 'workspaceConfigEnsuringRegistry',
value: WorkspaceConfig.workspaceConfigEnsuringRegistry,
empty: undefined,
},
{
classInstance: WorkspaceConfig,
methodName: 'workspaceConfigLoadingRegistry',
Expand Down
2 changes: 2 additions & 0 deletions scopes/harmony/bit/manifests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import { GitAspect } from '@teambit/git';
import { IpcEventsAspect } from '@teambit/ipc-events';
import { ConfigMergerAspect } from '@teambit/config-merger';
import { VersionHistoryAspect } from '@teambit/version-history';
import { HostInitializerAspect } from '@teambit/host-initializer';
import { BitAspect } from './bit.aspect';

export const manifestsMap = {
Expand Down Expand Up @@ -208,6 +209,7 @@ export const manifestsMap = {
[IpcEventsAspect.id]: IpcEventsAspect,
[ConfigMergerAspect.id]: ConfigMergerAspect,
[VersionHistoryAspect.id]: VersionHistoryAspect,
[HostInitializerAspect.id]: HostInitializerAspect,
};

export function isCoreAspect(id: string) {
Expand Down
47 changes: 17 additions & 30 deletions scopes/harmony/config/config.main.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { getConsumerInfo } from '@teambit/legacy/dist/consumer';
import {
ExtensionDataEntry,
ExtensionDataList,
ILegacyWorkspaceConfig,
LegacyWorkspaceConfigProps,
} from '@teambit/legacy/dist/consumer/config';
import { ExtensionDataEntry, ExtensionDataList, ILegacyWorkspaceConfig } from '@teambit/legacy/dist/consumer/config';
import LegacyWorkspaceConfig, {
WorkspaceConfigEnsureFunction,
WorkspaceConfigLoadFunction,
} from '@teambit/legacy/dist/consumer/config/workspace-config';
import { PathOsBased, PathOsBasedAbsolute } from '@teambit/legacy/dist/utils/path';
import { findScopePath } from '@teambit/scope.modules.find-scope-path';
import { MainRuntime } from '@teambit/cli';
import { GlobalConfig, Harmony } from '@teambit/harmony';
import path from 'path';
import { transformLegacyPropsToExtensions, WorkspaceConfig, WorkspaceConfigFileProps } from './workspace-config';
import { WorkspaceConfig, WorkspaceConfigFileProps, WorkspaceExtensionProps } from './workspace-config';
import { ConfigType, HostConfig } from './types';
import { ConfigAspect } from './config.aspect';

Expand Down Expand Up @@ -94,12 +88,26 @@ export class ConfigMain {
return config;
}

static async workspaceEnsureLegacy(
workspacePath: string,
scopePath: string,
workspaceExtensionProps?: WorkspaceExtensionProps
) {
let workspaceConfigProps;
if (workspaceExtensionProps) {
workspaceConfigProps = { 'teambit.workspace/workspace': workspaceExtensionProps };
}
const config = await ConfigMain.ensureWorkspace(workspacePath, scopePath, workspaceConfigProps);
const workspaceConfig = config.config;
return (workspaceConfig as WorkspaceConfig).toLegacy();
}

static runtime = MainRuntime;
static slots = [];
static dependencies = [];
static config = {};
static async provider(_deps, _config, _slots, harmony: Harmony) {
LegacyWorkspaceConfig.registerOnWorkspaceConfigEnsuring(onLegacyWorkspaceEnsure());
// LegacyWorkspaceConfig.registerOnWorkspaceConfigEnsuring(onLegacyWorkspaceEnsure());

let configMain: ConfigMain | any;
const bitConfig = harmony.config.raw.get('teambit.harmony/bit') as any;
Expand All @@ -111,9 +119,6 @@ export class ConfigMain {
configMain = {};
}
LegacyWorkspaceConfig.registerOnWorkspaceConfigLoading(onLegacyWorkspaceLoad(configMain));
LegacyWorkspaceConfig.registerOnWorkspaceConfigReset((dirPath, resetHard) =>
WorkspaceConfig.reset(dirPath, resetHard)
);
return configMain;
}
}
Expand All @@ -140,21 +145,3 @@ function onLegacyWorkspaceLoad(config?: ConfigMain): WorkspaceConfigLoadFunction
return undefined;
};
}

function onLegacyWorkspaceEnsure(): WorkspaceConfigEnsureFunction {
const func: WorkspaceConfigEnsureFunction = async (
workspacePath: string,
scopePath: string,
standAlone,
legacyWorkspaceConfigProps?: LegacyWorkspaceConfigProps
) => {
let workspaceConfigProps;
if (legacyWorkspaceConfigProps) {
workspaceConfigProps = transformLegacyPropsToExtensions(legacyWorkspaceConfigProps);
}
const config = await ConfigMain.ensureWorkspace(workspacePath, scopePath, workspaceConfigProps);
const workspaceConfig = config.config;
return (workspaceConfig as WorkspaceConfig).toLegacy();
};
return func;
}
4 changes: 2 additions & 2 deletions scopes/harmony/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ConfigMain } from './config.main.runtime';
export { ConfigAspect, ConfigRuntime } from './config.aspect';
export type { ComponentScopeDirMap, WorkspaceConfig } from './workspace-config';
export { getWorkspaceConfigTemplateParsed, stringifyWorkspaceConfig } from './workspace-config';
export type { ComponentScopeDirMap, WorkspaceExtensionProps, WorkspaceConfigFileProps } from './workspace-config';
export { getWorkspaceConfigTemplateParsed, stringifyWorkspaceConfig, WorkspaceConfig } from './workspace-config';
46 changes: 3 additions & 43 deletions scopes/harmony/config/workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import { DEFAULT_LANGUAGE, WORKSPACE_JSONC } from '@teambit/legacy/dist/constant
import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist';
import { ExtensionDataList, ILegacyWorkspaceConfig } from '@teambit/legacy/dist/consumer/config';
import LegacyWorkspaceConfig, {
WorkspaceConfigProps as LegacyWorkspaceConfigProps,
} from '@teambit/legacy/dist/consumer/config/workspace-config';
import LegacyWorkspaceConfig from '@teambit/legacy/dist/consumer/config/workspace-config';
import logger from '@teambit/legacy/dist/logger/logger';
import { PathOsBased, PathOsBasedAbsolute } from '@teambit/legacy/dist/utils/path';
import { currentDateAndTimeToFileName } from '@teambit/legacy/dist/consumer/consumer';
import { assign, parse, stringify, CommentJSONValue } from 'comment-json';
import * as fs from 'fs-extra';
import * as path from 'path';
import { isEmpty, omit } from 'lodash';
import { omit } from 'lodash';
import { WorkspaceAspect } from '@teambit/workspace';
import { SetExtensionOptions } from './config.main.runtime';
import { ExtensionAlreadyConfigured } from './exceptions';
Expand All @@ -29,7 +27,7 @@ export type WorkspaceConfigFileProps = {
// TODO: make it no optional
$schema?: string;
$schemaVersion?: string;
} & ExtensionsDefs;
} & WorkspaceSettingsNewProps;

export type ComponentScopeDirMapEntry = {
defaultScope?: string;
Expand Down Expand Up @@ -60,8 +58,6 @@ export type WorkspaceSettingsNewProps = {
'teambit.dependencies/dependency-resolver': DependencyResolverExtensionProps;
};

export type ExtensionsDefs = WorkspaceSettingsNewProps;

export class WorkspaceConfig implements HostConfig {
raw?: any;
_extensions: ExtensionDataList;
Expand Down Expand Up @@ -370,42 +366,6 @@ export class WorkspaceConfig implements HostConfig {
}
}

export function transformLegacyPropsToExtensions(
legacyConfig: LegacyWorkspaceConfig | LegacyWorkspaceConfigProps
): ExtensionsDefs {
// TODO: move to utils
const removeUndefined = (obj) => {
// const res = omit(mapObjIndexed((val) => val === undefined))(obj);
// return res;
Object.entries(obj).forEach((e) => {
if (e[1] === undefined) delete obj[e[0]];
});
return obj;
};

const workspace = removeUndefined({
defaultScope: legacyConfig.defaultScope,
defaultDirectory: legacyConfig.componentsDefaultDirectory,
});
const dependencyResolver = removeUndefined({
packageManager: legacyConfig.packageManager,
// strictPeerDependencies: false,
extraArgs: legacyConfig.packageManagerArgs,
packageManagerProcessOptions: legacyConfig.packageManagerProcessOptions,
manageWorkspaces: legacyConfig.manageWorkspaces,
useWorkspaces: legacyConfig.useWorkspaces,
});
const data = {};
if (workspace && !isEmpty(workspace)) {
data['teambit.workspace/workspace'] = workspace;
}
if (dependencyResolver && !isEmpty(dependencyResolver)) {
data['teambit.dependencies/dependency-resolver'] = dependencyResolver;
}
// @ts-ignore
return data;
}

export async function getWorkspaceConfigTemplateParsed(): Promise<CommentJSONValue> {
let fileContent: Buffer;
try {
Expand Down
Loading