Skip to content

Commit

Permalink
fix(nx-plugin): fix plugin generation in ts solution setup
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jan 23, 2025
1 parent a0cfe88 commit eca2921
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
56 changes: 56 additions & 0 deletions e2e/plugin/src/nx-plugin-ts-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,62 @@ describe('Nx Plugin (TS solution)', () => {

afterAll(() => cleanupProject());

it('should be able to generate a Nx Plugin with generators, executors and migrations', async () => {
const plugin = uniq('plugin');
const generator = uniq('generator');
const executor = uniq('executor');
const migrationVersion = '1.0.0';

runCLI(
`generate @nx/plugin:plugin packages/${plugin} --linter=eslint --unitTestRunner=jest --e2eTestRunner=jest --publishable`
);
runCLI(
`generate @nx/plugin:generator packages/${plugin}/src/generators/${generator}/generator --name ${generator}`
);
runCLI(
`generate @nx/plugin:executor packages/${plugin}/src/executors/${executor}/executor --name ${executor} --includeHasher`
);
runCLI(
`generate @nx/plugin:migration packages/${plugin}/src/migrations/update-${migrationVersion}/update-${migrationVersion} --packageVersion=${migrationVersion} --packageJsonUpdates=false`
);

expect(runCLI(`lint ${plugin}`)).toContain(
`Successfully ran target lint for project ${plugin}`
);
expect(runCLI(`typecheck ${plugin}`)).toContain(
`Successfully ran target typecheck for project ${plugin}`
);
expect(runCLI(`build ${plugin}`)).toContain(
`Successfully ran target build for project ${plugin}`
);
checkFilesExist(
// entry point
`packages/${plugin}/dist/index.js`,
`packages/${plugin}/dist/index.d.ts`,
// generator
`packages/${plugin}/dist/generators/${generator}/schema.json`,
`packages/${plugin}/dist/generators/${generator}/schema.d.ts`,
`packages/${plugin}/dist/generators/${generator}/generator.js`,
`packages/${plugin}/dist/generators/${generator}/generator.d.ts`,
// executor
`packages/${plugin}/dist/executors/${executor}/schema.json`,
`packages/${plugin}/dist/executors/${executor}/schema.d.ts`,
`packages/${plugin}/dist/executors/${executor}/executor.js`,
`packages/${plugin}/dist/executors/${executor}/executor.d.ts`,
`packages/${plugin}/dist/executors/${executor}/hasher.js`,
`packages/${plugin}/dist/executors/${executor}/hasher.d.ts`,
// migration
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.js`,
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.d.ts`
);
expect(runCLI(`test ${plugin}`)).toContain(
`Successfully ran target test for project ${plugin}`
);
expect(runCLI(`e2e ${plugin}-e2e`)).toContain(
`Successfully ran target e2e for project ${plugin}-e2e`
);
}, 90000);

it('should be able to infer projects and targets', async () => {
const plugin = uniq('plugin');
runCLI(`generate @nx/plugin:plugin packages/${plugin}`);
Expand Down
34 changes: 23 additions & 11 deletions packages/eslint-plugin/src/rules/nx-plugin-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,27 @@ function normalizeOptions(
let outDir: string;
const base = { ...DEFAULT_OPTIONS, ...options };
let runtimeTsConfig: string;
try {
runtimeTsConfig = require.resolve(
path.join(sourceProject.data.root, base.tsConfig)
);
const tsConfig = readJsonFile(runtimeTsConfig);
rootDir = tsConfig.compilerOptions?.rootDir;
outDir = tsConfig.compilerOptions?.outDir;
} catch {
// nothing

if (sourceProject.data.targets?.build?.executor === '@nx/js:tsc') {
rootDir = sourceProject.data.targets.build.options.rootDir;
outDir = sourceProject.data.targets.build.options.outputPath;
}

if (!rootDir && !outDir) {
try {
runtimeTsConfig = require.resolve(
path.join(workspaceRoot, sourceProject.data.root, base.tsConfig)
);
const tsConfig = readJsonFile(runtimeTsConfig);
rootDir ??= tsConfig.compilerOptions?.rootDir
? path.join(sourceProject.data.root, tsConfig.compilerOptions.rootDir)
: undefined;
outDir ??= tsConfig.compilerOptions?.outDir
? path.join(sourceProject.data.root, tsConfig.compilerOptions.outDir)
: undefined;
} catch {
// nothing
}
}
const pathPrefix =
sourceProject.data.root !== '.' ? `${sourceProject.data.root}/` : '';
Expand All @@ -217,8 +229,8 @@ function normalizeOptions(
packageJson: base.packageJson
? `${pathPrefix}${base.packageJson}`
: undefined,
rootDir: rootDir ? path.join(sourceProject.data.root, rootDir) : undefined,
outDir: outDir ? path.join(sourceProject.data.root, outDir) : undefined,
rootDir,
outDir,
};
}

Expand Down
12 changes: 12 additions & 0 deletions packages/plugin/src/generators/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
readProjectConfiguration,
runTasksInSerial,
Tree,
updateJson,
updateProjectConfiguration,
} from '@nx/devkit';
import { Linter } from '@nx/eslint';
Expand Down Expand Up @@ -102,6 +103,17 @@ export async function pluginGeneratorInternal(host: Tree, schema: Schema) {
})
);

if (options.isTsSolutionSetup) {
updateJson(
host,
joinPathFragments(options.projectRoot, 'package.json'),
(json) => {
delete json.type;
return json;
}
);
}

if (options.bundler === 'tsc') {
tasks.push(addTsLibDependencies(host));
}
Expand Down

0 comments on commit eca2921

Please sign in to comment.