Skip to content

Commit

Permalink
fix: handle missing workspace on bare scope for ripple
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Sep 5, 2023
1 parent 5ac750e commit 75cfbf1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<Workspace>(WorkspaceAspect.id);
const workspace = getWorkspace(context);
const compositions = context.getAspect<CompositionsMain>(CompositionsAspect.id);
const application = context.getAspect<ApplicationMain>(ApplicationAspect.id);
const isolator = context.getAspect<IsolatorMain>(IsolatorAspect.id);
Expand Down
13 changes: 6 additions & 7 deletions scopes/dev-services/compiler/ng-packagr/ng-packagr.compiler.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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, {
Expand All @@ -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<Workspace>(WorkspaceAspect.id);
const workspace = getWorkspace(context);
const application = context.getAspect<ApplicationMain>(ApplicationAspect.id);
const depResolver = context.getAspect<DependencyResolverMain>(DependencyResolverAspect.id);
const isolator = context.getAspect<IsolatorMain>(IsolatorAspect.id);
Expand Down
5 changes: 3 additions & 2 deletions scopes/dev-services/compiler/webpack/ng-webpack-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<Workspace>(WorkspaceAspect.id);
const workspace = getWorkspace(context);
const pkg = context.getAspect<PkgMain>(PkgAspect.id);
const application = context.getAspect<ApplicationMain>(ApplicationAspect.id);
const isolator = context.getAspect<IsolatorMain>(IsolatorAspect.id);
Expand Down
6 changes: 3 additions & 3 deletions scopes/dev-services/compiler/webpack/ng-webpack-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Workspace>(WorkspaceAspect.id);
const workspace = getWorkspace(context);
const webpackMain = context.getAspect<WebpackMain>(WebpackAspect.id);
const pkg = context.getAspect<PkgMain>(PkgAspect.id);
const application = context.getAspect<ApplicationMain>(ApplicationAspect.id);
Expand Down
12 changes: 6 additions & 6 deletions scopes/generators/angular-templates/ng-env/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(
Expand All @@ -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 [
Expand Down Expand Up @@ -66,7 +66,7 @@ export class NgEnvTemplate implements ComponentTemplate {
static from(options: AngularComponentTemplateOptions & { envName: string; angularVersion: number; }) {
return (context: EnvContext) => {
const pkg = context.getAspect<PkgMain>(PkgAspect.id);
const workspace = context.getAspect<Workspace>(WorkspaceAspect.id);
const workspace = getWorkspace(context);
return new NgEnvTemplate(
options.envName,
options.angularVersion,
Expand Down
9 changes: 3 additions & 6 deletions scopes/versions/common/angular-apps/angular.app-type.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -29,10 +29,7 @@ export class AngularAppType implements ApplicationType<AngularAppOptions> {
return (context: EnvContext) => {
const name = options.name || NG_APP_NAME;
const depsResolver = context.getAspect<DependencyResolverMain>(DependencyResolverAspect.id);
let workspace: Workspace | undefined;
try {
workspace = context.getAspect<Workspace>(WorkspaceAspect.id);
} catch (err) {}
const workspace = getWorkspace(context);
return new AngularAppType(name, options.angularEnv, context, depsResolver, workspace);
};
}
Expand Down
16 changes: 15 additions & 1 deletion scopes/versions/common/angular-common/utils.ts
Original file line number Diff line number Diff line change
@@ -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<Workspace>(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 [];
Expand Down

0 comments on commit 75cfbf1

Please sign in to comment.