Skip to content

Commit

Permalink
feat(angular)!: add new helpers isProjectStandalone and `getProject…
Browse files Browse the repository at this point in the history
…MainPath`
  • Loading branch information
Badisi committed Jan 16, 2024
1 parent b21b0ee commit dafdaa1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
8 changes: 4 additions & 4 deletions projects/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
"build:global": "node ./make.mjs build-global"
},
"peerDependencies": {
"@angular-devkit/core": ">= 14",
"@angular-devkit/schematics": ">= 14",
"@angular/cli": ">= 14",
"@schematics/angular": ">= 14"
"@angular-devkit/core": ">= 17",
"@angular-devkit/schematics": ">= 17",
"@angular/cli": ">= 17",
"@schematics/angular": ">= 17"
},
"dependencies": {
"@badisi/latest-version": "^7.0.1",
Expand Down
24 changes: 21 additions & 3 deletions projects/lib/specs/angular.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { join } from 'path';
import {
addAngularJsonAsset, addAngularJsonScript, addAngularJsonStyle, addDeclarationToNgModule, addExportToNgModule, addImportToNgModule,
addProviderToBootstrapApplication, addProviderToNgModule, addRouteDeclarationToNgModule, ensureIsAngularLibrary, ensureIsAngularProject,
ensureIsAngularWorkspace, getProjectFromWorkspace, getProjectOutputPath, isAngularVersion, removeAngularJsonAsset, removeAngularJsonScript,
removeAngularJsonStyle, removeDeclarationFromNgModule, removeExportFromNgModule, removeImportFromNgModule,
removeProviderFromBootstrapApplication, removeProviderFromNgModule
ensureIsAngularWorkspace, getProjectFromWorkspace, getProjectMainPath, getProjectOutputPath, isAngularVersion, isProjectStandalone,
removeAngularJsonAsset, removeAngularJsonScript, removeAngularJsonStyle, removeDeclarationFromNgModule, removeExportFromNgModule,
removeImportFromNgModule, removeProviderFromBootstrapApplication, removeProviderFromNgModule
} from '../src';
import { appTest1, appTest2, callRule, getCleanAppTree, libTest } from './common.spec';
import { customMatchers } from './jasmine.matchers';
Expand Down Expand Up @@ -629,6 +629,17 @@ const expectAddToNgModule = async (
expect(getProjectOutputPath(tree, libTest.name)).toBeUndefined();
});

it('helper: getProjectMainPath', () => {
if (useWorkspace) {
expect(getProjectMainPath(tree, appTest1.name)).toEqual(`projects/${appTest1.name}/src/main.ts`);
expect(getProjectMainPath(tree, appTest2.name)).toEqual(`projects/${appTest2.name}/src/main.ts`);
} else {
expect(getProjectMainPath(tree, appTest1.name)).toEqual('src/main.ts');
expect(getProjectMainPath(tree, appTest2.name)).toBeUndefined();
}
expect(getProjectMainPath(tree, libTest.name)).toBeUndefined();
});

it('helper: getProjectFromWorkspace', async () => {
await expectAsync(getProjectFromWorkspace(tree, appTest1.name)).toBeResolved();
if (useWorkspace) {
Expand All @@ -642,6 +653,13 @@ const expectAddToNgModule = async (
.toBeRejectedWithError('Project "non-existing-name" was not found in the current workspace.');
});

it('helper: isProjectStandalone', () => {
expect(isProjectStandalone(tree, appTest1.name)).toEqual(useStandalone);
if (useWorkspace) {
expect(isProjectStandalone(tree, appTest2.name)).toEqual(useStandalone);
}
expect(isProjectStandalone(tree, libTest.name)).withContext('Library should not be standalone').toBeFalse();
});
});
});
});
31 changes: 30 additions & 1 deletion projects/lib/src/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { JSONFile } from '@schematics/angular/utility/json-file';
import { findAppConfig } from '@schematics/angular/utility/standalone/app_config';
import { findBootstrapApplicationCall } from '@schematics/angular/utility/standalone/util';
import { getWorkspace } from '@schematics/angular/utility/workspace';
import { ProjectType } from '@schematics/angular/utility/workspace-models';
import { Builders, ProjectType } from '@schematics/angular/utility/workspace-models';
import { join } from 'path';
import { satisfies } from 'semver';

Expand Down Expand Up @@ -361,6 +361,19 @@ export const getProjectOutputPath = (tree: Tree, projectName: string): string =>
return angularJson.get(['projects', projectName, 'architect', 'build', 'options', 'outputPath']) as string;
};

/**
* Gets a project main file path as defined in the `angular.json` file.
* @param {Tree} tree The current schematic's project tree.
* @param {string} projectName The name of the project to look for.
* @returns {string} The default project main file path.
*/
export const getProjectMainPath = (tree: Tree, projectName: string): string => {
ensureProjectIsDefined(projectName);
const angularJson = new JSONFile(tree, 'angular.json');
const buildOptions = angularJson.get(['projects', projectName, 'architect', 'build']) as { builder: string; options: Record<string, string> };
return (buildOptions?.builder === Builders.Application as string) ? buildOptions?.options['browser'] : buildOptions?.options['main'];
};

/**
* Gets a project definition object from the current Angular workspace.
* @async
Expand All @@ -384,6 +397,22 @@ export const getProjectFromWorkspace = async (tree: Tree, projectName: string):
};
};

/**
* Checks if a project if of type standalone.
* @param {Tree} tree The current schematic's project tree.
* @param {string} projectName The name of the project to look for.
* @returns {boolean} Whether or not the project is of type standalone.
*/
export const isProjectStandalone = (tree: Tree, projectName: string): boolean => {
const mainFilePath = getProjectMainPath(tree, projectName);
try {
const bootstrapApplicationCall = findBootstrapApplicationCall(tree, mainFilePath);
return (bootstrapApplicationCall !== null);
} catch {
return false;
}
};

/**
* @internal
*/
Expand Down

0 comments on commit dafdaa1

Please sign in to comment.