Skip to content

Commit

Permalink
refactor(migrations): reduce usage of @angular-devkit/core package
Browse files Browse the repository at this point in the history
Many direct usages of the `@angular-devkit/core` package are no longer
necessary as the Angular schematics package exposes relevant helper
types and functions. Also, builtin Node.js functionality can be used in
unit-tests instead of custom functionality that may be removed in the future.
  • Loading branch information
clydin committed Sep 13, 2024
1 parent 8a5f319 commit dfb6dfd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 44 deletions.
4 changes: 2 additions & 2 deletions packages/core/schematics/test/all-migrations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core';
import {getSystemPath, normalize} from '@angular-devkit/core';
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('all migrations', () => {
});

function writeFile(filePath: string, contents: string) {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
host.sync.write(normalize(filePath), Buffer.from(contents));
}

async function runMigration(migrationName: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {getSystemPath, logging, normalize, virtualFs} from '@angular-devkit/core';
import {getSystemPath, logging, normalize} from '@angular-devkit/core';
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
Expand All @@ -23,7 +23,7 @@ describe('control flow migration', () => {
let warnOutput: string[] = [];

function writeFile(filePath: string, contents: string) {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
host.sync.write(normalize(filePath), Buffer.from(contents));
}

function runMigration(path: string | undefined = undefined, format: boolean = true) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/schematics/test/standalone_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core';
import {getSystemPath, normalize} from '@angular-devkit/core';
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
Expand All @@ -21,7 +21,7 @@ describe('standalone migration', () => {
let previousWorkingDir: string;

function writeFile(filePath: string, contents: string) {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
host.sync.write(normalize(filePath), Buffer.from(contents));
}

function runMigration(mode: string, path = './') {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/schematics/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ ts_library(
deps = [
"//packages/compiler",
"//packages/compiler-cli/private",
"@npm//@angular-devkit/core",
"@npm//@angular-devkit/schematics",
"@npm//@schematics/angular",
"@npm//@types/node",
"@npm//typescript",
],
Expand Down
42 changes: 5 additions & 37 deletions packages/core/schematics/utils/project_tsconfig_paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

import {json, normalize, virtualFs, workspaces} from '@angular-devkit/core';
import {Tree} from '@angular-devkit/schematics';
import {readWorkspace, TargetDefinition} from '@schematics/angular/utility';
import {normalize} from 'node:path/posix';

/**
* Gets all tsconfig paths from a CLI project by reading the workspace configuration
Expand All @@ -21,7 +22,7 @@ export async function getProjectTsConfigPaths(
const buildPaths = new Set<string>();
const testPaths = new Set<string>();

const workspace = await getWorkspace(tree);
const workspace = await readWorkspace(tree, '/');
for (const [, project] of workspace.projects) {
for (const [name, target] of project.targets) {
if (name !== 'build' && name !== 'test') {
Expand Down Expand Up @@ -52,8 +53,8 @@ export async function getProjectTsConfigPaths(

/** Get options for all configurations for the passed builder target. */
function* allTargetOptions(
target: workspaces.TargetDefinition,
): Iterable<[string | undefined, Record<string, json.JsonValue | undefined>]> {
target: TargetDefinition,
): Iterable<[string | undefined, Record<string, unknown>]> {
if (target.options) {
yield [undefined, target.options];
}
Expand All @@ -68,36 +69,3 @@ function* allTargetOptions(
}
}
}

function createHost(tree: Tree): workspaces.WorkspaceHost {
return {
async readFile(path: string): Promise<string> {
const data = tree.read(path);
if (!data) {
throw new Error('File not found.');
}

return virtualFs.fileBufferToString(data);
},
async writeFile(path: string, data: string): Promise<void> {
return tree.overwrite(path, data);
},
async isDirectory(path: string): Promise<boolean> {
// Approximate a directory check.
// We don't need to consider empty directories and hence this is a good enough approach.
// This is also per documentation, see:
// https://angular.dev/tools/cli/schematics-for-libraries#get-the-project-configuration
return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
},
async isFile(path: string): Promise<boolean> {
return tree.exists(path);
},
};
}

async function getWorkspace(tree: Tree): Promise<workspaces.WorkspaceDefinition> {
const host = createHost(tree);
const {workspace} = await workspaces.readWorkspace('/', host);

return workspace;
}

0 comments on commit dfb6dfd

Please sign in to comment.