Skip to content

Commit

Permalink
fix(preact): repair missing directory prop and align the code to nx20
Browse files Browse the repository at this point in the history
  • Loading branch information
pawel-twardziak authored and DominikPieper committed Oct 21, 2024
1 parent 9607dc7 commit c1fdefc
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 206 deletions.
23 changes: 0 additions & 23 deletions packages/preact/src/generators/library/files/eslintrc.js.template

This file was deleted.

56 changes: 48 additions & 8 deletions packages/preact/src/generators/library/lib/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import {
ensurePackage,
runTasksInSerial,
NX_VERSION,
updateJson,
} from '@nx/devkit';
import { NormalizedSchema } from '../schema';
import { extraEslintDependencies } from '../../utils/lint';
import { useFlatConfig } from '@nx/eslint/src/utils/flat-config';
import { findEslintFile } from '@nx/eslint/src/generators/utils/eslint-file';

export async function addLinting(host: Tree, options: NormalizedSchema) {
if (options.linter !== 'eslint') {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return () => {};
return () => {
/* empty */
};
}

const { lintProjectGenerator } = ensurePackage<typeof import('@nx/eslint')>(
'@nx/eslint',
NX_VERSION
Expand All @@ -29,13 +34,48 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
skipFormat: true,
});

host.rename(
joinPathFragments(options.projectRoot, 'eslintrc.js'),
joinPathFragments(options.projectRoot, '.eslintrc.js')
);
host.delete(joinPathFragments(options.projectRoot, '.eslintrc.json'));
const eslintFile = findEslintFile(host, options.projectRoot);
const eslintFilePath = joinPathFragments(options.projectRoot, eslintFile);
if (useFlatConfig(host)) {
/**
* TODO: augment flat config once the plugins are ready with the flat
*/
} else {
updateJson(host, eslintFilePath, (json) => {
const overrides = [
{
files: ['*.ts', '*.js', '*.tsx'],
parserOptions: {
project: [
`${joinPathFragments(options.projectRoot, 'tsconfig.*?.json')}`,
],
},
rules: {},
},
...(json.overrides ?? []),
];
const ignorePatterns = [
...(json.ignorePatterns ?? ['!**/*']),
'vite.config.ts',
];
const plugins = [...(json.plugins ?? []), 'preact', '@typescript-eslint'];
const extendsVal = [...(json.extends ?? [])];
delete json.plugins;
delete json.ignorePatterns;
delete json.overrides;
delete json.extends;
return {
extends: extendsVal,
parser: '@typescript-eslint/parser',
plugins,
ignorePatterns,
overrides,
...json,
};
});
}

const installTask = await addDependenciesToPackageJson(
const installTask = addDependenciesToPackageJson(
host,
extraEslintDependencies.dependencies,
extraEslintDependencies.devDependencies
Expand Down
16 changes: 0 additions & 16 deletions packages/preact/src/generators/library/lib/add-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,10 @@ import { NormalizedSchema } from '../schema';
import { addProjectConfiguration, TargetConfiguration, Tree } from '@nx/devkit';

export function addProject(tree: Tree, options: NormalizedSchema) {
const targets: { [key: string]: TargetConfiguration } = {
lint: createLintTarget(options),
};

addProjectConfiguration(tree, options.name, {
root: options.projectRoot,
sourceRoot: `${options.projectRoot}/src`,
projectType: 'library',
tags: options.parsedTags,
targets,
});
}

function createLintTarget(options: NormalizedSchema): TargetConfiguration {
return {
executor: '@nx/eslint:lint',
options: {
linter: 'eslint',
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
exclude: ['**/node_modules/**', `!${options.projectRoot}/**/*`],
},
};
}
20 changes: 18 additions & 2 deletions packages/preact/src/generators/library/lib/add-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ import { NormalizedSchema } from '../schema';

export async function addVite(host: Tree, options: NormalizedSchema) {
await ensurePackage('@nx/vite', NX_VERSION);
const { viteConfigurationGenerator } = await import('@nx/vite');
const { viteConfigurationGenerator, createOrEditViteConfig } = await import(
'@nx/vite'
);

return await viteConfigurationGenerator(host, {
const addViteTask = await viteConfigurationGenerator(host, {
uiFramework: 'none',
project: options.name,
newProject: true,
includeVitest: options.unitTestRunner === 'vitest',
inSourceTests: false,
});
createOrEditViteConfig(
host,
{
project: options.name,
includeLib: false,
includeVitest: options.unitTestRunner === 'vitest',
inSourceTests: false,
rollupOptionsExternal: [],
imports: [`import preact from '@preact/preset-vite'`],
plugins: [`preact()`],
},
false
);
return addViteTask;
}
26 changes: 26 additions & 0 deletions packages/preact/src/generators/library/lib/create-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
generateFiles,
joinPathFragments,
names,
offsetFromRoot,
Tree,
} from '@nx/devkit';
import { NormalizedSchema } from '../schema';
import { useFlatConfig } from '@nx/eslint/src/utils/flat-config';

export function createFiles(host: Tree, options: NormalizedSchema) {
generateFiles(
host,
joinPathFragments(__dirname, '..', './files'),
options.projectRoot,
{
...options,
...names(options.name),
offsetFromRoot: offsetFromRoot(options.projectRoot),
}
);

if (!options.publishable && !options.buildable) {
host.delete(`${options.projectRoot}/package.json`);
}
}
43 changes: 43 additions & 0 deletions packages/preact/src/generators/library/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Tree } from '@nx/devkit';
import { NormalizedSchema, PreactLibrarySchema } from '../schema';
import {
determineProjectNameAndRootOptions,
ensureProjectName
} from '@nx/devkit/src/generators/project-name-and-root-utils';

export async function normalizeOptions(
tree: Tree,
options: PreactLibrarySchema
): Promise<NormalizedSchema> {
await ensureProjectName(tree, options, 'library');
const {
projectName,
names: projectNames,
projectRoot,
importPath,
} = await determineProjectNameAndRootOptions(tree, {
name: options.name,
projectType: 'library',
directory: options.directory,
importPath: options.importPath,
rootProject: false,
});
const fileName =
/* options.simpleName
? projectNames.projectSimpleName
: */ projectNames.projectFileName;

const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];

return {
...options,
projectName,
projectRoot,
parsedTags,
fileName,
projectDirectory: projectRoot,
importPath,
};
}
17 changes: 0 additions & 17 deletions packages/preact/src/generators/library/lib/update-jest-config.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/preact/src/generators/library/lib/update-npm-scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Tree, updateJson } from '@nx/devkit';
import { NormalizedSchema } from '../schema';

export function updateNpmScopeIfBuildableOrPublishable(
host: Tree,
options: NormalizedSchema
) {
if (options.buildable || options.publishable) {
updateLibPackageNpmScope(host, options);
}
}

function updateLibPackageNpmScope(host: Tree, options: NormalizedSchema) {
return updateJson(host, `${options.projectRoot}/package.json`, (json) => {
json.name = options.importPath;
return json;
});
}
28 changes: 0 additions & 28 deletions packages/preact/src/generators/library/lib/update-tsconfig.ts

This file was deleted.

Loading

0 comments on commit c1fdefc

Please sign in to comment.