From 75cfbf1436bf16f06996e4abd68049b1565ffcde Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Tue, 5 Sep 2023 10:44:37 +0200 Subject: [PATCH] fix: handle missing workspace on bare scope for ripple --- .../angular-elements.compiler.ts | 4 ++-- .../compiler/ng-packagr/ng-packagr.compiler.ts | 13 ++++++------- .../compiler/webpack/ng-webpack-bundler.ts | 5 +++-- .../compiler/webpack/ng-webpack-dev-server.ts | 6 +++--- .../generators/angular-templates/ng-env/index.ts | 12 ++++++------ .../common/angular-apps/angular.app-type.ts | 9 +++------ scopes/versions/common/angular-common/utils.ts | 16 +++++++++++++++- 7 files changed, 38 insertions(+), 27 deletions(-) diff --git a/scopes/dev-services/compiler/angular-elements/angular-elements.compiler.ts b/scopes/dev-services/compiler/angular-elements/angular-elements.compiler.ts index 34115e70..be7b5889 100644 --- a/scopes/dev-services/compiler/angular-elements/angular-elements.compiler.ts +++ b/scopes/dev-services/compiler/angular-elements/angular-elements.compiler.ts @@ -1,7 +1,7 @@ import type { AngularCompilerOptions } from '@angular/compiler-cli'; import { componentIsApp } from '@teambit/angular-apps'; import type { AngularEnvOptions } from '@teambit/angular-common'; -import { getNodeModulesPaths } from '@teambit/angular-common'; +import { getNodeModulesPaths, getWorkspace } from '@teambit/angular-common'; import { RollupCompiler } from './rollup/rollup.compiler'; import { ApplicationAspect, ApplicationMain } from '@teambit/application'; import { @@ -200,7 +200,7 @@ Built Angular Compositions return (context: EnvContext) => { const name = options.name || 'angular-elements-compiler'; const logger = context.createLogger(name); - const workspace = context.getAspect(WorkspaceAspect.id); + const workspace = getWorkspace(context); const compositions = context.getAspect(CompositionsAspect.id); const application = context.getAspect(ApplicationAspect.id); const isolator = context.getAspect(IsolatorAspect.id); diff --git a/scopes/dev-services/compiler/ng-packagr/ng-packagr.compiler.ts b/scopes/dev-services/compiler/ng-packagr/ng-packagr.compiler.ts index 9cc28892..6a523174 100644 --- a/scopes/dev-services/compiler/ng-packagr/ng-packagr.compiler.ts +++ b/scopes/dev-services/compiler/ng-packagr/ng-packagr.compiler.ts @@ -1,7 +1,6 @@ import type { AngularCompilerOptions, ParsedConfiguration } from '@angular/compiler-cli'; import { componentIsApp } from '@teambit/angular-apps'; -import type { AngularEnvOptions } from '@teambit/angular-common'; -import { getNodeModulesPaths } from '@teambit/angular-common'; +import { AngularEnvOptions, getNodeModulesPaths, getWorkspace } from '@teambit/angular-common'; import { ApplicationAspect, ApplicationMain } from '@teambit/application'; import { ArtifactDefinition, @@ -21,11 +20,11 @@ import removeFilesAndEmptyDirsRecursively from '@teambit/legacy/dist/utils/fs/remove-files-and-empty-dirs-recursively'; import { Logger } from '@teambit/logger'; import { NgccProcessor } from '@teambit/ngcc'; -import { Workspace, WorkspaceAspect } from '@teambit/workspace'; +import { Workspace } from '@teambit/workspace'; +import chalk from 'chalk'; import { mkdirsSync, outputFileSync } from 'fs-extra'; -import { join, posix, resolve } from 'path'; import type { NgPackageConfig } from 'ng-packagr/ng-package.schema'; -import chalk from 'chalk'; +import { join, posix, resolve } from 'path'; const ViewEngineTemplateError = `Cannot read property 'type' of null`; const NG_PACKAGE_JSON = 'ng-package.json'; @@ -336,7 +335,7 @@ export class NgPackagrCompiler implements Compiler { /** * given a component, returns the path to the source folder to use for the preview, uses the one * in node_modules by default - * used by `bit start` + * used by `bit start` (so workspace is always available) */ getPreviewComponentRootPath(component: Component): string { return this.workspace!.componentDir(component.id, { @@ -362,7 +361,7 @@ export class NgPackagrCompiler implements Compiler { const name = options.name || 'ng-packagr-compiler'; const ngPackagrModulePath = options.ngPackagrModulePath || require.resolve('ng-packagr'); const logger = context.createLogger(name); - const workspace = context.getAspect(WorkspaceAspect.id); + const workspace = getWorkspace(context); const application = context.getAspect(ApplicationAspect.id); const depResolver = context.getAspect(DependencyResolverAspect.id); const isolator = context.getAspect(IsolatorAspect.id); diff --git a/scopes/dev-services/compiler/webpack/ng-webpack-bundler.ts b/scopes/dev-services/compiler/webpack/ng-webpack-bundler.ts index 25027a9f..8803a401 100644 --- a/scopes/dev-services/compiler/webpack/ng-webpack-bundler.ts +++ b/scopes/dev-services/compiler/webpack/ng-webpack-bundler.ts @@ -2,7 +2,8 @@ import { AngularEnvOptions, BrowserOptions, DevServerOptions, - getNodeModulesPaths + getNodeModulesPaths, + getWorkspace } from '@teambit/angular-common'; import { AppBuildContext, ApplicationAspect, ApplicationMain } from '@teambit/application'; import { BundlerContext } from '@teambit/bundler'; @@ -75,7 +76,7 @@ export class NgWebpackBundler { const name = options.name || 'ng-webpack-bundler'; const logger = context.createLogger(name); const bundlerContext = options.bundlerContext; - const workspace = context.getAspect(WorkspaceAspect.id); + const workspace = getWorkspace(context); const pkg = context.getAspect(PkgAspect.id); const application = context.getAspect(ApplicationAspect.id); const isolator = context.getAspect(IsolatorAspect.id); diff --git a/scopes/dev-services/compiler/webpack/ng-webpack-dev-server.ts b/scopes/dev-services/compiler/webpack/ng-webpack-dev-server.ts index 0aa2392e..14f566f1 100644 --- a/scopes/dev-services/compiler/webpack/ng-webpack-dev-server.ts +++ b/scopes/dev-services/compiler/webpack/ng-webpack-dev-server.ts @@ -2,7 +2,8 @@ import { AngularEnvOptions, BrowserOptions, DevServerOptions, - getNodeModulesPaths + getNodeModulesPaths, + getWorkspace } from '@teambit/angular-common'; import { ApplicationAspect, ApplicationMain } from '@teambit/application'; import { DevServer, DevServerContext } from '@teambit/bundler'; @@ -22,7 +23,6 @@ import { WebpackMain } from '@teambit/webpack'; import { generateTransformers, runTransformers } from '@teambit/webpack.webpack-bundler'; -import { Workspace, WorkspaceAspect } from '@teambit/workspace'; import { join, posix } from 'path'; import { Configuration } from 'webpack'; import { @@ -78,7 +78,7 @@ export class NgWebpackDevServer { const name = options.name || 'ng-webpack-dev-server'; const logger = context.createLogger(name); const devServerContext = options.devServerContext; - const workspace = context.getAspect(WorkspaceAspect.id); + const workspace = getWorkspace(context); const webpackMain = context.getAspect(WebpackAspect.id); const pkg = context.getAspect(PkgAspect.id); const application = context.getAspect(ApplicationAspect.id); diff --git a/scopes/generators/angular-templates/ng-env/index.ts b/scopes/generators/angular-templates/ng-env/index.ts index 52a8cb24..9faad359 100644 --- a/scopes/generators/angular-templates/ng-env/index.ts +++ b/scopes/generators/angular-templates/ng-env/index.ts @@ -1,8 +1,9 @@ -import { AngularComponentTemplateOptions } from '@teambit/angular-common'; +import { AngularComponentTemplateOptions, getWorkspace } from '@teambit/angular-common'; +import { ComponentID } from '@teambit/component'; import { EnvContext } from '@teambit/envs'; import { ComponentContext, ComponentTemplate, ConfigContext } from '@teambit/generator'; import { PkgAspect, PkgMain } from '@teambit/pkg'; -import { Workspace, WorkspaceAspect } from '@teambit/workspace'; +import { Workspace } from '@teambit/workspace'; import { eslintConfigFile } from './files/config/eslintrc'; import { jestConfigFile } from './files/config/jest.config'; import { prettierConfigFile } from './files/config/prettier.config'; @@ -13,7 +14,6 @@ import { envJsoncFile } from './files/env-jsonc'; import { indexFile } from './files/index'; import { hostDependenciesFile } from './files/preview/host-dependencies'; import { mounterFile } from './files/preview/mounter'; -import { Component, ComponentID } from '@teambit/component'; export class NgEnvTemplate implements ComponentTemplate { private constructor( @@ -23,12 +23,12 @@ export class NgEnvTemplate implements ComponentTemplate { readonly description = 'create a customized Angular env with your configs and tools', readonly hidden = false, private pkg: PkgMain, - private workspace: Workspace, + private workspace: Workspace | undefined, ) {} async generateFiles(context: ComponentContext) { const aspectId: ComponentID = typeof context.aspectId === 'string' ? ComponentID.fromString(context.aspectId) : context.aspectId; - const envComponent = await this.workspace.get(aspectId); + const envComponent = await this.workspace!.get(aspectId); const envPkgName = this.pkg.getPackageName(envComponent); const envId = envComponent.id.toStringWithoutVersion(); return [ @@ -66,7 +66,7 @@ export class NgEnvTemplate implements ComponentTemplate { static from(options: AngularComponentTemplateOptions & { envName: string; angularVersion: number; }) { return (context: EnvContext) => { const pkg = context.getAspect(PkgAspect.id); - const workspace = context.getAspect(WorkspaceAspect.id); + const workspace = getWorkspace(context); return new NgEnvTemplate( options.envName, options.angularVersion, diff --git a/scopes/versions/common/angular-apps/angular.app-type.ts b/scopes/versions/common/angular-apps/angular.app-type.ts index 11ba603a..cd339982 100644 --- a/scopes/versions/common/angular-apps/angular.app-type.ts +++ b/scopes/versions/common/angular-apps/angular.app-type.ts @@ -1,8 +1,8 @@ -import { GenericAngularEnv } from '@teambit/angular-common'; +import { GenericAngularEnv, getWorkspace } from '@teambit/angular-common'; import { Application, ApplicationType } from '@teambit/application'; import { DependencyResolverAspect, DependencyResolverMain } from '@teambit/dependency-resolver'; import { EnvContext, EnvHandler } from '@teambit/envs'; -import { Workspace, WorkspaceAspect } from '@teambit/workspace'; +import { Workspace } from '@teambit/workspace'; import { AngularAppOptions } from './angular-app-options'; import { AngularApp } from './angular.application'; import { NG_APP_NAME } from './utils'; @@ -29,10 +29,7 @@ export class AngularAppType implements ApplicationType { return (context: EnvContext) => { const name = options.name || NG_APP_NAME; const depsResolver = context.getAspect(DependencyResolverAspect.id); - let workspace: Workspace | undefined; - try { - workspace = context.getAspect(WorkspaceAspect.id); - } catch (err) {} + const workspace = getWorkspace(context); return new AngularAppType(name, options.angularEnv, context, depsResolver, workspace); }; } diff --git a/scopes/versions/common/angular-common/utils.ts b/scopes/versions/common/angular-common/utils.ts index c258255b..8d79c19e 100644 --- a/scopes/versions/common/angular-common/utils.ts +++ b/scopes/versions/common/angular-common/utils.ts @@ -1,8 +1,22 @@ import { ComponentID } from "@teambit/component"; +import { EnvContext } from "@teambit/envs"; import { IsolatorMain } from "@teambit/isolator"; -import { Workspace } from "@teambit/workspace"; +import { Workspace, WorkspaceAspect } from '@teambit/workspace'; import { resolve } from 'path'; +/** + * Returns the workspace instance from the context, if it's available, or undefined otherwise. + */ +export function getWorkspace(context: EnvContext): Workspace | undefined { + // TODO: replace this try catch with context.hasAspect once it's available from harmony + try { + return context.getAspect(WorkspaceAspect.id); + } catch (err) { + // Ignore this. We might be running not from within a workspace, for example, when using bit sign. + } + return undefined; +} + export function getNodeModulesPaths(build: boolean, isolator: IsolatorMain, workspace?: Workspace): string[] { if (!workspace) { return [];