From 169b950fc6d14c5a248ca13382ee44dad119d375 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Wed, 12 Apr 2023 19:54:45 +0100 Subject: [PATCH] cleanup(core): deprecate and remove Rules from workspace (#16112) --- packages/workspace/index.ts | 4 - .../workspace/src/utilities/project-type.ts | 14 --- .../src/utilities/set-default-collection.ts | 23 ----- packages/workspace/src/utils/ast-utils.ts | 1 - packages/workspace/src/utils/fileutils.ts | 20 +++-- packages/workspace/src/utils/project-type.ts | 10 --- .../src/utils/rules/add-install-task.ts | 4 + .../workspace/src/utils/rules/deleteFile.ts | 1 + .../workspace/src/utils/rules/format-files.ts | 3 + packages/workspace/src/utils/rules/ng-add.ts | 14 --- .../utils/rules/visit-not-ignored-files.ts | 4 + .../workspace/src/utils/rules/workspace.ts | 24 ------ packages/workspace/src/utils/testing-utils.ts | 9 ++ packages/workspace/src/utils/testing.ts | 85 ++----------------- packages/workspace/src/utils/workspace.ts | 17 +++- 15 files changed, 58 insertions(+), 175 deletions(-) delete mode 100644 packages/workspace/src/utilities/project-type.ts delete mode 100644 packages/workspace/src/utilities/set-default-collection.ts delete mode 100644 packages/workspace/src/utils/rules/ng-add.ts delete mode 100644 packages/workspace/src/utils/rules/workspace.ts diff --git a/packages/workspace/index.ts b/packages/workspace/index.ts index 80934442ec7b1..cae4276052430 100644 --- a/packages/workspace/index.ts +++ b/packages/workspace/index.ts @@ -16,9 +16,6 @@ export { readPackageJson, } from 'nx/src/project-graph/file-utils'; export { ProjectGraphCache } from 'nx/src/project-graph/nx-deps-cache'; -export { - findNodes, // TODO(v16): remove this -} from './src/utils/ast-utils'; export { getWorkspacePath, @@ -37,7 +34,6 @@ export { Linter } from './src/utils/lint'; export { addInstallTask } from './src/utils/rules/add-install-task'; export { formatFiles } from './src/utils/rules/format-files'; export { deleteFile } from './src/utils/rules/deleteFile'; -export * from './src/utils/rules/ng-add'; export { visitNotIgnoredFiles } from './src/utils/rules/visit-not-ignored-files'; import * as strings from './src/utils/strings'; diff --git a/packages/workspace/src/utilities/project-type.ts b/packages/workspace/src/utilities/project-type.ts deleted file mode 100644 index 73c86ef4b4b20..0000000000000 --- a/packages/workspace/src/utilities/project-type.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { Tree } from '@nrwl/devkit'; -import { joinPathFragments, readProjectConfiguration } from '@nrwl/devkit'; - -export function getProjectRootPath(tree: Tree, projectName: string): string { - const { sourceRoot, projectType } = readProjectConfiguration( - tree, - projectName - ); - - return joinPathFragments( - sourceRoot, - projectType === 'application' ? 'app' : 'lib' - ); -} diff --git a/packages/workspace/src/utilities/set-default-collection.ts b/packages/workspace/src/utilities/set-default-collection.ts deleted file mode 100644 index 4c0ad380c1fbb..0000000000000 --- a/packages/workspace/src/utilities/set-default-collection.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { readNxJson, Tree, updateNxJson } from '@nrwl/devkit'; - -/** - * Sets the default collection within the workspace. - * - * Will only set the defaultCollection if one does not exist or if it is not `@nrwl/workspace` - * - * @deprecated NxJson defaultCollection will be removed - * @param host - * @param collectionName Name of the collection to be set as the default - */ -export function setDefaultCollection(host: Tree, collectionName: string) { - const nxJson = readNxJson(host); - nxJson.cli = nxJson.cli || {}; - - const defaultCollection = nxJson.cli.defaultCollection; - - if (!defaultCollection || defaultCollection === '@nrwl/workspace') { - nxJson.cli.defaultCollection = collectionName; - } - - updateNxJson(host, nxJson); -} diff --git a/packages/workspace/src/utils/ast-utils.ts b/packages/workspace/src/utils/ast-utils.ts index 0c2a65cb3c9e1..df9ff1d6a0b08 100644 --- a/packages/workspace/src/utils/ast-utils.ts +++ b/packages/workspace/src/utils/ast-utils.ts @@ -1,4 +1,3 @@ import { getSourceNodes } from '../utilities/typescript/get-source-nodes'; -export { findNodes } from '../utilities/typescript/find-nodes'; // TODO(v16): Remove this export { getSourceNodes } from '../utilities/typescript/get-source-nodes'; diff --git a/packages/workspace/src/utils/fileutils.ts b/packages/workspace/src/utils/fileutils.ts index f9a2e53f409ae..ba5ce91a8402f 100644 --- a/packages/workspace/src/utils/fileutils.ts +++ b/packages/workspace/src/utils/fileutils.ts @@ -2,28 +2,32 @@ import { createReadStream, createWriteStream, existsSync, - writeFileSync, mkdirSync, renameSync as fsRenameSync, + writeFileSync, } from 'fs'; import { basename, dirname, resolve } from 'path'; import { - readJsonFile, - writeJsonFile, - fileExists, + createDirectory, directoryExists, + fileExists, isRelativePath, - createDirectory, + readJsonFile, + writeJsonFile, } from 'nx/src/utils/fileutils'; export { fileExists, directoryExists, isRelativePath, createDirectory }; +/** + * @deprecated This will be removed in v17. + */ export function writeToFile(filePath: string, str: string) { mkdirSync(dirname(filePath), { recursive: true }); writeFileSync(filePath, str); } /** + * @deprecated This will be removed in v17. * This method is specifically for updating a JSON file using the filesystem * * @remarks @@ -37,6 +41,9 @@ export function updateJsonFile(path: string, callback: (a: any) => any) { writeJsonFile(path, json); } +/** + * @deprecated This will be removed in v17. + */ export function copyFile(file: string, target: string) { const f = basename(file); const source = createReadStream(file); @@ -45,6 +52,9 @@ export function copyFile(file: string, target: string) { source.on('error', (e) => console.error(e)); } +/** + * @deprecated This will be removed in v17. + */ export function renameSync( from: string, to: string, diff --git a/packages/workspace/src/utils/project-type.ts b/packages/workspace/src/utils/project-type.ts index d0b2098071c78..17417ade108b0 100644 --- a/packages/workspace/src/utils/project-type.ts +++ b/packages/workspace/src/utils/project-type.ts @@ -10,13 +10,3 @@ export function projectRootDir(projectType: ProjectType) { return 'libs'; } } - -export function projectDir(projectType: ProjectType) { - if (projectType == ProjectType.Application) { - // apps/test-app/src/app - return 'app'; - } else if (projectType == ProjectType.Library) { - // libs/test-lib/src/lib - return 'lib'; - } -} diff --git a/packages/workspace/src/utils/rules/add-install-task.ts b/packages/workspace/src/utils/rules/add-install-task.ts index 73c157e900363..9c184b9943735 100644 --- a/packages/workspace/src/utils/rules/add-install-task.ts +++ b/packages/workspace/src/utils/rules/add-install-task.ts @@ -1,6 +1,10 @@ import type { Rule } from '@angular-devkit/schematics'; let installAdded = false; + +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'addDependenciesToPackageJson' from @nrwl/devkit. + */ export function addInstallTask( options: { skipInstall: boolean } = { skipInstall: false } ): Rule { diff --git a/packages/workspace/src/utils/rules/deleteFile.ts b/packages/workspace/src/utils/rules/deleteFile.ts index 02c1c8c2b1c09..b0018f1b4e9d1 100644 --- a/packages/workspace/src/utils/rules/deleteFile.ts +++ b/packages/workspace/src/utils/rules/deleteFile.ts @@ -2,6 +2,7 @@ import type { Rule, Tree } from '@angular-devkit/schematics'; /** * Remove a file from the Virtual Schematic Tree + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'Tree.delete' from @nrwl/devkit. */ export function deleteFile(from: string): Rule { return (host: Tree) => host.delete(from); diff --git a/packages/workspace/src/utils/rules/format-files.ts b/packages/workspace/src/utils/rules/format-files.ts index 1b95e1673b8bb..0e7bf2c7efa2c 100644 --- a/packages/workspace/src/utils/rules/format-files.ts +++ b/packages/workspace/src/utils/rules/format-files.ts @@ -11,6 +11,9 @@ import { filter, map, mergeMap } from 'rxjs/operators'; import * as path from 'path'; import { workspaceRoot } from '@nrwl/devkit'; +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'formatFiles' from @nrwl/devkit. + */ export function formatFiles( options: { skipFormat: boolean } = { skipFormat: false }, directory: string = '' diff --git a/packages/workspace/src/utils/rules/ng-add.ts b/packages/workspace/src/utils/rules/ng-add.ts deleted file mode 100644 index c701f53a12454..0000000000000 --- a/packages/workspace/src/utils/rules/ng-add.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { externalSchematic, Rule } from '@angular-devkit/schematics'; - -/** - * Calls init _if_ the package does not already exist - */ -export function addPackageWithInit( - packageName: string, - testRunners: { - unitTestRunner: 'jest' | 'none'; - e2eTestRunner?: 'cypress' | 'none'; - } = { unitTestRunner: 'jest', e2eTestRunner: 'cypress' } -): Rule { - return externalSchematic(packageName, 'init', { ...testRunners }); -} diff --git a/packages/workspace/src/utils/rules/visit-not-ignored-files.ts b/packages/workspace/src/utils/rules/visit-not-ignored-files.ts index 74cbb07981054..0f91d8988af13 100644 --- a/packages/workspace/src/utils/rules/visit-not-ignored-files.ts +++ b/packages/workspace/src/utils/rules/visit-not-ignored-files.ts @@ -7,6 +7,9 @@ import { } from '@angular-devkit/schematics'; import ignore, { Ignore } from 'ignore'; +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'visitNotIgnoredFiles' from @nrwl/devkit. + */ export function visitNotIgnoredFiles( visitor: (file: Path, host: Tree, context: SchematicContext) => void | Rule, dir: Path = normalize('') @@ -17,6 +20,7 @@ export function visitNotIgnoredFiles( ig = ignore(); ig.add(host.read('.gitignore').toString()); } + function visit(_dir: Path) { if (_dir && ig?.ignores(_dir)) { return; diff --git a/packages/workspace/src/utils/rules/workspace.ts b/packages/workspace/src/utils/rules/workspace.ts deleted file mode 100644 index ecdca79a9ca5f..0000000000000 --- a/packages/workspace/src/utils/rules/workspace.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { JsonObject } from '@angular-devkit/core'; -import type { Rule } from '@angular-devkit/schematics'; -import { updateWorkspace } from '../workspace'; - -/** - * Sets the default collection to the provided collection name - * The collection name is only set in case the current defaultCollection is undefined or set to '@nrwl/workspace' - * @param collectionName Name of the collection to be set as defaultCollection - * @deprecated nxJson defaultCollection will be removed - */ -export function setDefaultCollection(collectionName: string): Rule { - return updateWorkspace((workspace) => { - workspace.extensions.cli = workspace.extensions.cli || {}; - - const defaultCollection: string = - workspace.extensions.cli && - ((workspace.extensions.cli as JsonObject).defaultCollection as string); - - if (!defaultCollection || defaultCollection === '@nrwl/workspace') { - (workspace.extensions.cli as JsonObject).defaultCollection = - collectionName; - } - }); -} diff --git a/packages/workspace/src/utils/testing-utils.ts b/packages/workspace/src/utils/testing-utils.ts index 4c9135d300592..9103a265ab542 100644 --- a/packages/workspace/src/utils/testing-utils.ts +++ b/packages/workspace/src/utils/testing-utils.ts @@ -10,6 +10,9 @@ import { json, JsonObject } from '@angular-devkit/core'; import { ScheduleOptions } from '@angular-devkit/architect/src/api'; import { LoggerApi, LogLevel } from '@angular-devkit/core/src/logger'; +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. Use tree.read(filePath, 'utf-8') instead. + */ export function getFileContent(tree: Tree, path: string): string { const fileEntry = tree.get(path); @@ -20,6 +23,9 @@ export function getFileContent(tree: Tree, path: string): string { return fileEntry.content.toString(); } +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. Tests for Generators can use 'createTreeWithEmptyWorkspace()' from @nrwl/devkit/testing. + */ export function createEmptyWorkspace(tree: Tree): Tree { _test_addWorkspaceFile('workspace.json', WorkspaceFormat.JSON); @@ -113,6 +119,7 @@ class NoopLogger implements LoggerApi { /** * Mock context which makes testing builders easier + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. */ export class MockBuilderContext implements BuilderContext { id: 0; @@ -130,9 +137,11 @@ export class MockBuilderContext implements BuilderContext { get currentDirectory() { return this.architectHost.currentDirectory; } + get workspaceRoot() { return this.architectHost.workspaceRoot; } + constructor( private architect: Architect, private architectHost: TestingArchitectHost diff --git a/packages/workspace/src/utils/testing.ts b/packages/workspace/src/utils/testing.ts index 2309af6df7b28..516c164164e70 100644 --- a/packages/workspace/src/utils/testing.ts +++ b/packages/workspace/src/utils/testing.ts @@ -1,12 +1,6 @@ import { join } from 'path'; import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; import type { Rule, Tree } from '@angular-devkit/schematics'; -import { updateWorkspace } from './workspace'; -import { TestingArchitectHost } from '@angular-devkit/architect/testing'; -import { schema } from '@angular-devkit/core'; -import { Architect } from '@angular-devkit/architect'; -import { MockBuilderContext } from './testing-utils'; -import { names } from '@nrwl/devkit'; const testRunner = new SchematicTestRunner( '@nrwl/workspace', @@ -58,22 +52,9 @@ testRunner.registerCollection( join(__dirname, '../../../web/generators.json') ); -const migrationTestRunner = new SchematicTestRunner( - '@nrwl/workspace/migrations', - join(__dirname, '../../migrations.json') -); - -export function runExternalSchematic( - collectionName: string, - schematicName: string, - options: T, - tree: Tree -) { - return testRunner - .runExternalSchematicAsync(collectionName, schematicName, options, tree) - .toPromise(); -} - +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. Generators can be tested by simply calling them in it(). + */ export function runSchematic( schematicName: string, options: T, @@ -82,63 +63,9 @@ export function runSchematic( return testRunner.runSchematicAsync(schematicName, options, tree).toPromise(); } +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. Generators can be tested by simply calling them in it(). + */ export function callRule(rule: Rule, tree: Tree) { return testRunner.callRule(rule, tree).toPromise(); } - -export function runMigration(migrationName: string, options: any, tree: Tree) { - return migrationTestRunner - .runSchematicAsync(migrationName, options, tree) - .toPromise(); -} - -export function createLibWithTests( - tree: Tree, - libName: string, - testBuilder: string, - testSetupFile: string -): Promise { - const { fileName } = names(libName); - - tree.create(`/libs/${fileName}/src/index.ts`, `\n`); - - return callRule( - updateWorkspace((workspace) => { - workspace.projects.add({ - name: fileName, - root: `libs/${fileName}`, - projectType: 'library', - sourceRoot: `libs/${fileName}/src`, - architect: { - test: { - builder: testBuilder, - options: { - setupFile: `libs/${fileName}/src/${testSetupFile}`, - }, - }, - }, - }); - }), - tree - ); -} - -export async function getTestArchitect() { - const architectHost = new TestingArchitectHost('/root', '/root'); - const registry = new schema.CoreSchemaRegistry(); - registry.addPostTransform(schema.transforms.addUndefinedDefaults); - - const architect = new Architect(architectHost, registry); - - await architectHost.addBuilderFromPackage(join(__dirname, '../..')); - - return [architect, architectHost] as [Architect, TestingArchitectHost]; -} - -export async function getMockContext() { - const [architect, architectHost] = await getTestArchitect(); - - const context = new MockBuilderContext(architect, architectHost); - await context.addBuilderFromPackage(join(__dirname, '../..')); - return context; -} diff --git a/packages/workspace/src/utils/workspace.ts b/packages/workspace/src/utils/workspace.ts index fb26a734385af..c00b40f88d0d2 100644 --- a/packages/workspace/src/utils/workspace.ts +++ b/packages/workspace/src/utils/workspace.ts @@ -1,4 +1,4 @@ -import type { Tree, Rule } from '@angular-devkit/schematics'; +import type { Rule, Tree } from '@angular-devkit/schematics'; import { JsonArray, JsonObject, workspaces } from '@angular-devkit/core'; import { ProjectDefinition, @@ -29,6 +29,10 @@ function createHost(tree: Tree): workspaces.WorkspaceHost { }, }; } + +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'getProjects' with Nx Devkit. + */ export async function getWorkspace(tree: Tree, path = '/') { const host = createHost(tree); @@ -36,14 +40,24 @@ export async function getWorkspace(tree: Tree, path = '/') { return workspace; } + +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'updateProjectConfiguration' with Nx Devkit. + */ export function updateWorkspace( updater: ( workspace: workspaces.WorkspaceDefinition ) => void | PromiseLike ): Rule; +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'updateProjectConfiguration' with Nx Devkit. + */ export function updateWorkspace( workspace: workspaces.WorkspaceDefinition ): Rule; +/** + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'updateProjectConfiguration' with Nx Devkit. + */ export function updateWorkspace( updaterOrWorkspace: | workspaces.WorkspaceDefinition @@ -69,6 +83,7 @@ export function updateWorkspace( /** * Updates builder options for options and configurations for given builder names + * @deprecated This will be removed in v17. Prefer writing Nx Generators with @nrwl/devkit. This function can be replaced with 'forEachExecutorOptions' with Nx Devkit. */ export function updateBuilderConfig( updater: (