Skip to content

Commit

Permalink
feat(misc): bump swc-helpers version to match new swc-core requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 18, 2023
1 parent 486ec01 commit 2f146ec
Show file tree
Hide file tree
Showing 21 changed files with 156 additions and 61 deletions.
2 changes: 1 addition & 1 deletion docs/generated/packages/nx-plugin/generators/executor.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
"description": "Do not add an eslint configuration for plugin json files."
}
},
"required": ["project", "name"],
Expand Down
8 changes: 7 additions & 1 deletion docs/generated/packages/nx-plugin/generators/generator.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
"description": "Do not add an eslint configuration for plugin json files."
},
"skipFormat": {
"type": "boolean",
"default": false,
"description": "Do not format files with prettier.",
"x-priority": "internal"
}
},
"required": ["project", "name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"description": "Which project should be the configuration be added to?",
"$default": { "$source": "projectName" },
"x-priority": "important"
},
"skipFormat": {
"type": "boolean",
"x-priority": "internal",
"description": "Skip formatting files with prettier.",
"default": false
}
},
"required": ["projectName"],
Expand Down
15 changes: 0 additions & 15 deletions e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,6 @@ describe('Nx Plugin', () => {
});
}, 90000);

// the test invoke ensureNxProject, which points to @nrwl/workspace collection
// which walks up the directory to find it in the next repo itself, so it
// doesn't use the collection we are building
// we should change it to point to the right collection using relative path
// TODO: Re-enable this to work with pnpm
it(`should run the plugin's e2e tests`, async () => {
const plugin = uniq('plugin-name');
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --e2eTestRunner jest`
);
const e2eResults = runCLI(`e2e ${plugin}-e2e`);
expect(e2eResults).toContain('Successfully ran target e2e');
expect(await killPorts()).toBeTruthy();
}, 250000);

it('should be able to generate a migration', async () => {
const plugin = uniq('plugin');
const version = '1.0.0';
Expand Down
8 changes: 1 addition & 7 deletions e2e/utils/create-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,10 @@ export function runCreateWorkspace(
export function runCreatePlugin(
name: string,
{
pluginName,
packageManager,
extraArgs,
useDetectedPm = false,
}: {
pluginName?: string;
packageManager?: 'npm' | 'yarn' | 'pnpm';
extraArgs?: string;
useDetectedPm?: boolean;
Expand All @@ -232,11 +230,7 @@ export function runCreatePlugin(

let command = `${
pm.runUninstalledPackage
} create-nx-plugin@${getPublishedVersion()} ${name}`;

if (pluginName) {
command += ` --pluginName=${pluginName} --no-nxCloud`;
}
} create-nx-plugin@${getPublishedVersion()} ${name} --no-nxCloud`;

if (packageManager && !useDetectedPm) {
command += ` --package-manager=${packageManager}`;
Expand Down
14 changes: 9 additions & 5 deletions e2e/workspace-create/src/create-nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ describe('create-nx-plugin', () => {

afterEach(() => cleanupProject());

it('should be able to create a plugin repo and run plugin e2e', () => {
const wsName = uniq('ws-plugin');
it('should be able to create a plugin repo build a plugin', () => {
const pluginName = uniq('plugin');

runCreatePlugin(wsName, {
runCreatePlugin(pluginName, {
packageManager,
pluginName,
});

checkFilesExist(
Expand All @@ -31,6 +29,12 @@ describe('create-nx-plugin', () => {
`executors.json`
);

expect(() => runCLI(`e2e e2e`)).not.toThrow();
runCLI(`build ${pluginName}`);

checkFilesExist(
`dist/package.json`,
`dist/generators.json`,
`dist/executors.json`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ describe('<%= pluginName %> e2e', () => {

// Add some tests here to check that your plugin functionality works as expected.
// A sample test is included below to give you some ideas.
//
// it('should be able to build generated projects', async () => {
// const name = uniq('proj')
// await runNxCommandAsync(`generate <%= npmPackageName %>:{my-generator} --name ${name}`);
// expect(() => runNxCommand('build ${proj}')).not.toThrow();
// expect(() => checkFilesExist([`dist/${name}/index.js`])).not.toThrow();
// });
xit('should be able to build generated projects', async () => {
const name = uniq('proj');
const generator = 'PLACEHOLDER';
await runNxCommandAsync(`generate <%= npmPackageName %>:${generator} --name ${name}`);
expect(() => runNxCommand('build ${proj}')).not.toThrow();
expect(() => checkFilesExist([`dist/${name}/index.js`])).not.toThrow();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('NxPlugin Executor Generator', () => {
* you can consume workspace details from the context.
*/
export const myExecutorHasher: CustomHasher = async (task, context) => {
return context.hasher.hashTask(task)
return context.hasher.hashTask(task);
};
export default myExecutorHasher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CustomHasher } from '@nx/devkit';
* you can consume workspace details from the context.
*/
export const <%=propertyName%>Hasher: CustomHasher = async (task, context) => {
return context.hasher.hashTask(task)
return context.hasher.hashTask(task);
};

export default <%=propertyName%>Hasher;
2 changes: 1 addition & 1 deletion packages/nx-plugin/src/generators/executor/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
"description": "Do not add an eslint configuration for plugin json files."
}
},
"required": ["project", "name"],
Expand Down
15 changes: 15 additions & 0 deletions packages/nx-plugin/src/generators/generator/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ describe('NxPlugin Generator Generator', () => {
);
});

it('should throw if recreating an existing generator', async () => {
await generatorGenerator(tree, {
project: projectName,
name: 'my-generator',
unitTestRunner: 'jest',
});
expect(
generatorGenerator(tree, {
project: projectName,
name: 'my-generator',
unitTestRunner: 'jest',
})
).rejects.toThrow('Generator my-generator already exists');
});

it('should update generators.json with the same path as where the generator files folder is located', async () => {
const generatorName = 'myGenerator';
const generatorFileName = 'my-generator';
Expand Down
24 changes: 20 additions & 4 deletions packages/nx-plugin/src/generators/generator/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { GeneratorsJson, joinPathFragments, Tree, writeJson } from '@nx/devkit';
import {
formatFiles,
GeneratorsJson,
joinPathFragments,
Tree,
writeJson,
} from '@nx/devkit';
import {
convertNxGenerator,
generateFiles,
Expand All @@ -10,6 +16,7 @@ import {
} from '@nx/devkit';
import { PackageJson } from 'nx/src/utils/package-json';
import * as path from 'path';
import { hasGenerator } from '../../utils/has-generator';
import pluginLintCheckGenerator from '../lint-checks/generator';
import type { Schema } from './schema';

Expand Down Expand Up @@ -86,7 +93,8 @@ export async function createGeneratorsJson(
host: Tree,
projectRoot: string,
projectName: string,
skipLintChecks?: boolean
skipLintChecks?: boolean,
skipFormat?: boolean
) {
updateJson<PackageJson>(
host,
Expand All @@ -106,6 +114,7 @@ export async function createGeneratorsJson(
if (!skipLintChecks) {
await pluginLintCheckGenerator(host, {
projectName,
skipFormat,
});
}
}
Expand All @@ -129,9 +138,9 @@ async function updateGeneratorJson(host: Tree, options: NormalizedSchema) {
host,
options.projectRoot,
options.project,
options.skipLintChecks
options.skipLintChecks,
options.skipFormat
);
console.log('CREATED GENERATORS.JSON');
}

updateJson<GeneratorsJson>(host, generatorsPath, (json) => {
Expand All @@ -153,10 +162,17 @@ async function updateGeneratorJson(host: Tree, options: NormalizedSchema) {

export async function generatorGenerator(host: Tree, schema: Schema) {
const options = normalizeOptions(host, schema);
if (hasGenerator(host, options.project, options.name)) {
throw new Error(`Generator ${options.name} already exists.`);
}

addFiles(host, options);

await updateGeneratorJson(host, options);

if (!options.skipFormat) {
await formatFiles(host);
}
}

export default generatorGenerator;
Expand Down
1 change: 1 addition & 0 deletions packages/nx-plugin/src/generators/generator/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface Schema {
description?: string;
unitTestRunner: 'jest' | 'none';
skipLintChecks?: boolean;
skipFormat?: boolean;
}
8 changes: 7 additions & 1 deletion packages/nx-plugin/src/generators/generator/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
"description": "Do not add an eslint configuration for plugin json files."
},
"skipFormat": {
"type": "boolean",
"default": false,
"description": "Do not format files with prettier.",
"x-priority": "internal"
}
},
"required": ["project", "name"],
Expand Down
36 changes: 25 additions & 11 deletions packages/nx-plugin/src/generators/lint-checks/generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
addDependenciesToPackageJson,
formatFiles,
joinPathFragments,
logger,
ProjectConfiguration,
Expand Down Expand Up @@ -60,6 +61,11 @@ export default async function pluginLintCheckGenerator(
{},
{ 'jsonc-eslint-parser': jsoncEslintParserVersion }
);

if (!options.skipFormat) {
await formatFiles(host);
}

return () => installTask;
}

Expand Down Expand Up @@ -186,27 +192,35 @@ function updateProjectEslintConfig(
const eslintPath = `${options.root}/.eslintrc.json`;
const eslintConfig = readJson<ESLint.Config>(host, eslintPath);
eslintConfig.overrides ??= [];
if (
!eslintConfig.overrides.some(
let entry: ESLint.ConfigOverride<ESLint.RulesRecord> =
eslintConfig.overrides.find(
(x) =>
Object.keys(x.rules ?? {}).includes('@nx/nx/nx-plugin-checks') ||
Object.keys(x.rules ?? {}).includes('@nrwl/nx/nx-plugin-checks')
)
) {
eslintConfig.overrides.push({
files: [
);
const newentry = !entry;
entry ??= { files: [] };
entry.files = [
...new Set([
...(entry.files ?? []),
...[
'./package.json',
packageJson.generators,
packageJson.executors,
packageJson.schematics,
packageJson.builders,
].filter((f) => !!f),
parser: 'jsonc-eslint-parser',
rules: {
'@nx/nx/nx-plugin-checks': 'error',
},
});
]),
];
entry.parser = 'jsonc-eslint-parser';
entry.rules ??= {
'@nx/nx/nx-plugin-checks': 'error',
};

if (newentry) {
eslintConfig.overrides.push(entry);
}

writeJson(host, eslintPath, eslintConfig);
}

Expand Down
1 change: 1 addition & 0 deletions packages/nx-plugin/src/generators/lint-checks/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface PluginLintChecksGeneratorSchema {
projectName: string;
skipFormat?: boolean;
}
6 changes: 6 additions & 0 deletions packages/nx-plugin/src/generators/lint-checks/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"$source": "projectName"
},
"x-priority": "important"
},
"skipFormat": {
"type": "boolean",
"x-priority": "internal",
"description": "Skip formatting files with prettier.",
"default": false
}
},
"required": ["projectName"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree } from '@nx/devkit';

import update from './<%= name %>';

describe('<%= name %> migration', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace({layout: 'apps-libs'});
});

it('should run successfully', async () => {
await update(tree);
// ... expect changes made
});
});
3 changes: 1 addition & 2 deletions packages/nx-plugin/src/generators/preset/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe('preset generator', () => {
const config = readProjectConfiguration(tree, 'my-plugin');
expect(config).toBeDefined();
const packageJson = readJson<PackageJson>(tree, 'package.json');
expect(packageJson.generators).toEqual('./generators.json');
expect(packageJson.executors).toEqual('./executors.json');
expect(packageJson.dependencies).toHaveProperty('@nx/devkit');
expect(readNxJson(tree).npmScope).not.toBeDefined();
});
});
4 changes: 0 additions & 4 deletions packages/nx-plugin/src/generators/preset/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export default async function (tree: Tree, options: PresetGeneratorSchema) {
e2eTestRunner: 'jest',
});

const { root } = readProjectConfiguration(tree, options.pluginName);
await createExecutorsJson(tree, root, options.pluginName);
await createGeneratorsJson(tree, root, options.pluginName);

removeNpmScope(tree);
moveNxPluginToDevDeps(tree);

Expand Down
Loading

0 comments on commit 2f146ec

Please sign in to comment.