Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nx-plugin): generator and executor generators should work in js libs #10621

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/nx-plugin/src/generators/executor/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down
43 changes: 37 additions & 6 deletions packages/nx-plugin/src/generators/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,15 +70,42 @@ function addHasherFiles(host: Tree, options: NormalizedSchema) {
}
}

function createExecutorsJson(host: Tree, options: NormalizedSchema) {
updateJson<PackageJson>(
host,
joinPathFragments(options.projectRoot, 'package.json'),
(json) => {
json.executors ??= 'executors.json';
return json;
}
);
writeJson<ExecutorsJson>(
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<PackageJson>(
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] = {
Expand Down
23 changes: 22 additions & 1 deletion packages/nx-plugin/src/generators/generator/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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,
Expand Down
48 changes: 41 additions & 7 deletions packages/nx-plugin/src/generators/generator/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { Tree } from '@nrwl/devkit';
import {
GeneratorsJson,
joinPathFragments,
Tree,
writeJson,
} from '@nrwl/devkit';
import {
convertNxGenerator,
generateFiles,
Expand All @@ -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';

Expand Down Expand Up @@ -80,15 +86,43 @@ function addFiles(host: Tree, options: NormalizedSchema) {
}
}

function createGeneratorsJson(host: Tree, options: NormalizedSchema) {
updateJson<PackageJson>(
host,
joinPathFragments(options.projectRoot, 'package.json'),
(json) => {
json.generators ??= 'generators.json';
return json;
}
);
writeJson<GeneratorsJson>(
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<PackageJson>(
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] = {
Expand Down
13 changes: 11 additions & 2 deletions packages/nx-plugin/src/generators/migration/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PackageJson>(
host,
joinPathFragments(options.projectRoot, 'package.json')
)
).migrations;
const migrationsPath = joinPathFragments(
options.projectRoot,
configuredMigrationPath ?? 'migrations.json'
);
const migrations = host.exists(migrationsPath)
? readJson(host, migrationsPath)
: {};
Expand Down