diff --git a/packages/nx-plugin/src/generators/executor/executor.spec.ts b/packages/nx-plugin/src/generators/executor/executor.spec.ts index 92a3662f01d9d..29c41dcdfe0bc 100644 --- a/packages/nx-plugin/src/generators/executor/executor.spec.ts +++ b/packages/nx-plugin/src/generators/executor/executor.spec.ts @@ -1,7 +1,8 @@ -import { Tree, readJson } from '@nrwl/devkit'; +import { Tree, readJson, readProjectConfiguration } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { executorGenerator } from './executor'; import { pluginGenerator } from '../plugin/plugin'; +import { libraryGenerator } from '@nrwl/js'; describe('NxPlugin Executor Generator', () => { let tree: Tree; @@ -91,6 +92,25 @@ describe('NxPlugin Executor Generator', () => { ); }); + it('should create executors.json if it is not present', async () => { + await libraryGenerator(tree, { + name: 'test-js-lib', + buildable: true, + }); + const libConfig = readProjectConfiguration(tree, 'test-js-lib'); + await executorGenerator(tree, { + project: 'test-js-lib', + includeHasher: false, + name: 'test-executor', + unitTestRunner: 'jest', + }); + + expect(() => tree.exists(`${libConfig.root}/executors.json`)).not.toThrow(); + expect(readJson(tree, `${libConfig.root}/package.json`).executors).toBe( + 'executors.json' + ); + }); + describe('--unitTestRunner', () => { describe('none', () => { it('should not generate unit test files', async () => { diff --git a/packages/nx-plugin/src/generators/executor/executor.ts b/packages/nx-plugin/src/generators/executor/executor.ts index 5a90663a6e19f..1a9310a4acdd7 100644 --- a/packages/nx-plugin/src/generators/executor/executor.ts +++ b/packages/nx-plugin/src/generators/executor/executor.ts @@ -6,10 +6,14 @@ import { updateJson, getWorkspaceLayout, joinPathFragments, + writeJson, + readJson, + ExecutorsJson, } from '@nrwl/devkit'; import type { Tree } from '@nrwl/devkit'; import type { Schema } from './schema'; import * as path from 'path'; +import { PackageJson } from 'nx/src/utils/package-json'; interface NormalizedSchema extends Schema { fileName: string; @@ -66,15 +70,42 @@ function addHasherFiles(host: Tree, options: NormalizedSchema) { } } +function createExecutorsJson(host: Tree, options: NormalizedSchema) { + updateJson( + host, + joinPathFragments(options.projectRoot, 'package.json'), + (json) => { + json.executors ??= 'executors.json'; + return json; + } + ); + writeJson( + host, + joinPathFragments(options.projectRoot, 'executors.json'), + { + executors: {}, + } + ); +} + function updateExecutorJson(host: Tree, options: NormalizedSchema) { - let executorPath: string; - if (host.exists(path.join(options.projectRoot, 'executors.json'))) { - executorPath = path.join(options.projectRoot, 'executors.json'); - } else { - executorPath = path.join(options.projectRoot, 'builders.json'); + const packageJson = readJson( + host, + joinPathFragments(options.projectRoot, 'package.json') + ); + const packageJsonExecutors = packageJson.executors ?? packageJson.builders; + let executorsPath = packageJsonExecutors + ? joinPathFragments(options.projectRoot, packageJsonExecutors) + : null; + + if (!executorsPath) { + executorsPath = joinPathFragments(options.projectRoot, 'executors.json'); + } + if (!host.exists(executorsPath)) { + createExecutorsJson(host, options); } - return updateJson(host, executorPath, (json) => { + return updateJson(host, executorsPath, (json) => { let executors = json.executors ?? json.builders; executors ||= {}; executors[options.name] = { diff --git a/packages/nx-plugin/src/generators/generator/generator.spec.ts b/packages/nx-plugin/src/generators/generator/generator.spec.ts index e6661c9ec96ed..c412a069cc627 100644 --- a/packages/nx-plugin/src/generators/generator/generator.spec.ts +++ b/packages/nx-plugin/src/generators/generator/generator.spec.ts @@ -1,5 +1,6 @@ -import { readJson, Tree } from '@nrwl/devkit'; +import { readJson, readProjectConfiguration, Tree } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import { libraryGenerator } from '@nrwl/js'; import { pluginGenerator } from '../plugin/plugin'; import { generatorGenerator } from './generator'; @@ -59,6 +60,26 @@ describe('NxPlugin Generator Generator', () => { ); }); + it('should create generators.json if it is not present', async () => { + await libraryGenerator(tree, { + name: 'test-js-lib', + buildable: true, + }); + const libConfig = readProjectConfiguration(tree, 'test-js-lib'); + await generatorGenerator(tree, { + project: 'test-js-lib', + name: 'test-generator', + unitTestRunner: 'jest', + }); + + expect(() => + tree.exists(`${libConfig.root}/generators.json`) + ).not.toThrow(); + expect(readJson(tree, `${libConfig.root}/package.json`).generators).toBe( + 'generators.json' + ); + }); + it('should generate default description', async () => { await generatorGenerator(tree, { project: projectName, diff --git a/packages/nx-plugin/src/generators/generator/generator.ts b/packages/nx-plugin/src/generators/generator/generator.ts index 601c3ab42f1e4..9a06a41b41a0b 100644 --- a/packages/nx-plugin/src/generators/generator/generator.ts +++ b/packages/nx-plugin/src/generators/generator/generator.ts @@ -1,4 +1,9 @@ -import type { Tree } from '@nrwl/devkit'; +import { + GeneratorsJson, + joinPathFragments, + Tree, + writeJson, +} from '@nrwl/devkit'; import { convertNxGenerator, generateFiles, @@ -8,6 +13,7 @@ import { readProjectConfiguration, updateJson, } from '@nrwl/devkit'; +import { PackageJson } from 'nx/src/utils/package-json'; import * as path from 'path'; import type { Schema } from './schema'; @@ -80,15 +86,43 @@ function addFiles(host: Tree, options: NormalizedSchema) { } } +function createGeneratorsJson(host: Tree, options: NormalizedSchema) { + updateJson( + host, + joinPathFragments(options.projectRoot, 'package.json'), + (json) => { + json.generators ??= 'generators.json'; + return json; + } + ); + writeJson( + host, + joinPathFragments(options.projectRoot, 'generators.json'), + { + generators: {}, + } + ); +} + function updateGeneratorJson(host: Tree, options: NormalizedSchema) { - let generatorPath: string; - if (host.exists(path.join(options.projectRoot, 'generators.json'))) { - generatorPath = path.join(options.projectRoot, 'generators.json'); - } else { - generatorPath = path.join(options.projectRoot, 'collection.json'); + const packageJson = readJson( + host, + joinPathFragments(options.projectRoot, 'package.json') + ); + const packageJsonGenerators = + packageJson.generators ?? packageJson.schematics; + let generatorsPath = packageJsonGenerators + ? joinPathFragments(options.projectRoot, packageJsonGenerators) + : null; + + if (!generatorsPath) { + generatorsPath = joinPathFragments(options.projectRoot, 'generators.json'); + } + if (!host.exists(generatorsPath)) { + createGeneratorsJson(host, options); } - return updateJson(host, generatorPath, (json) => { + return updateJson(host, generatorsPath, (json) => { let generators = json.generators ?? json.schematics; generators = generators || {}; generators[options.name] = { diff --git a/packages/nx-plugin/src/generators/migration/migration.ts b/packages/nx-plugin/src/generators/migration/migration.ts index bfc7e2f099015..f3dd275d6b053 100644 --- a/packages/nx-plugin/src/generators/migration/migration.ts +++ b/packages/nx-plugin/src/generators/migration/migration.ts @@ -14,7 +14,7 @@ import type { Schema } from './schema'; import * as path from 'path'; import { addMigrationJsonChecks } from '../lint-checks/generator'; import type { Linter as EsLint } from 'eslint'; -import { PackageJson } from 'nx/src/utils/package-json'; +import { PackageJson, readNxMigrateConfig } from 'nx/src/utils/package-json'; interface NormalizedSchema extends Schema { projectRoot: string; projectSourceRoot: string; @@ -54,7 +54,16 @@ function addFiles(host: Tree, options: NormalizedSchema) { } function updateMigrationsJson(host: Tree, options: NormalizedSchema) { - const migrationsPath = path.join(options.projectRoot, 'migrations.json'); + const configuredMigrationPath = readNxMigrateConfig( + readJson( + host, + joinPathFragments(options.projectRoot, 'package.json') + ) + ).migrations; + const migrationsPath = joinPathFragments( + options.projectRoot, + configuredMigrationPath ?? 'migrations.json' + ); const migrations = host.exists(migrationsPath) ? readJson(host, migrationsPath) : {};