Skip to content

Commit

Permalink
feat(solid): support for projectNameAndRootFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikPieper committed Sep 20, 2023
1 parent 0b66cc4 commit 9d8b1cb
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 75 deletions.
24 changes: 24 additions & 0 deletions docs/docs/solid/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ Possible values: `eslint`

The tool to use for running lint checks.

#### projectNameAndRootFormat

Type: `string`

Possible values: `as-provided`, `derived`

Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).

#### rootProject (**hidden**)

Default: `false`

Type: `boolean`

Create a application at the root of the workspace

#### skipFormat

Default: `false`
Expand Down Expand Up @@ -168,6 +184,14 @@ Type: `boolean`

Create a publishable library.

#### simpleName

Default: `false`

Type: `boolean`

Don't include the directory in the name of the module of the library.

#### skipFormat

Default: `false`
Expand Down
8 changes: 4 additions & 4 deletions packages/solid/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@nxext/solid",
"version": "16.7.0",
"version": "16.8.0",
"license": "MIT",
"author": "Kristian Mandrup",
"author": "Dominik Pieper",
"description": "Nx plugin for solid",
"homepage": "https://nxext.dev/",
"keywords": [
Expand All @@ -23,10 +23,10 @@
"main": "src/index.js",
"generators": "./generators.json",
"dependencies": {
"tslib": "^2.3.0",
"nx": "^16.8.0",
"@nx/devkit": "^16.8.0",
"typescript": "5.1.6",
"nx": "^16.8.0"
"tslib": "^2.3.0"
},
"peerDependencies": {
"@nx/linter": "^16.8.0",
Expand Down
17 changes: 12 additions & 5 deletions packages/solid/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { SolidApplicationSchema } from './schema';
import { Schema } from './schema';
import { Linter } from '@nx/linter';
import applicationGenerator from './application';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree } from '@nx/devkit';

describe('Solid app generator', () => {
let tree: Tree;
const options: SolidApplicationSchema = {
name: 'test',
const options: Schema = {
name: 'myApp',
linter: Linter.EsLint,
unitTestRunner: 'vitest',
e2eTestRunner: 'cypress',
projectNameAndRootFormat: 'derived',
};

beforeEach(() => {
Expand All @@ -20,8 +21,14 @@ describe('Solid app generator', () => {
describe('Vite bundle', () => {
it('should add vite specific files', async () => {
await applicationGenerator(tree, { ...options });
expect(tree.exists(`apps/${options.name}/public/index.html`)).toBeFalsy();
expect(tree.exists(`apps/${options.name}/index.html`)).toBeTruthy();
expect(tree.exists(`apps/my-app/public/index.html`)).toBeFalsy();
expect(tree.exists(`apps/my-app/index.html`)).toBeTruthy();
});

it('should add vite specific files as rootProject', async () => {
await applicationGenerator(tree, { ...options, rootProject: true });
expect(tree.exists(`public/index.html`)).toBeFalsy();
expect(tree.exists(`index.html`)).toBeTruthy();
});
});
});
65 changes: 43 additions & 22 deletions packages/solid/src/generators/application/application.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {
convertNxGenerator,
formatFiles,
getWorkspaceLayout,
joinPathFragments,
names,
Tree,
runTasksInSerial,
GeneratorCallback,
} from '@nx/devkit';
import { NormalizedSchema, SolidApplicationSchema } from './schema';
import { NormalizedSchema, Schema } from './schema';
import { addProject } from './lib/add-project';
import { initGenerator } from '../init/init';
import { addLinting } from './lib/add-linting';
Expand All @@ -17,39 +16,61 @@ import { updateJestConfig } from './lib/update-jest-config';
import { addVite } from './lib/add-vite';
import { updateViteConfig } from './lib/update-vite-config';
import { createFiles } from './lib/create-project-files';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';

function normalizeOptions(
tree: Tree,
options: SolidApplicationSchema
): NormalizedSchema {
const { appsDir } = getWorkspaceLayout(tree);
const name = names(options.name).fileName;
const projectDirectory = options.directory
? joinPathFragments(`${names(options.directory).fileName}/${name}`)
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName;
const projectRoot = joinPathFragments(`${appsDir}/${projectDirectory}`);
async function normalizeOptions<T extends Schema = Schema>(
host: Tree,
options: Schema,
callingGenerator = '@nxext/solid:application'
): Promise<NormalizedSchema<T>> {
const {
projectName: appProjectName,
projectRoot: appProjectRoot,
projectNameAndRootFormat,
} = await determineProjectNameAndRootOptions(host, {
name: options.name,
projectType: 'application',
directory: options.directory,
projectNameAndRootFormat: options.projectNameAndRootFormat,
rootProject: options.rootProject,
callingGenerator,
});
options.rootProject = appProjectRoot === '.';
options.projectNameAndRootFormat = projectNameAndRootFormat;

const e2eProjectName = options.rootProject ? 'e2e' : `${appProjectName}-e2e`;
const e2eProjectRoot = options.rootProject ? 'e2e' : `${appProjectRoot}-e2e`;
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
//const fileName = options.pascalCaseFiles ? 'App' : 'app';
const fileName = 'App';

return {
...options,
name: projectName,
projectRoot,
name: names(options.name).fileName,
projectName: appProjectName,
appProjectRoot,
e2eProjectName,
e2eProjectRoot,
parsedTags,
fileName,
projectDirectory,
skipFormat: false,
};
}

export async function applicationGenerator(
tree: Tree,
schema: SolidApplicationSchema
) {
const options = normalizeOptions(tree, schema);
host: Tree,
schema: Schema
): Promise<GeneratorCallback> {
return await applicationGeneratorInternal(host, {
projectNameAndRootFormat: 'derived',
...schema,
});
}

export async function applicationGeneratorInternal(tree: Tree, schema: Schema) {
const options = await normalizeOptions(tree, schema);

const initTask = await initGenerator(tree, { ...options, skipFormat: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
{
"files": ["*.ts", "*.js", "*.tsx"],
"parserOptions": {
"project": ["<%= projectRoot %>/tsconfig.*?.json"]
"project": ["<%= appProjectRoot %>/tsconfig.*?.json"]
},
"rules": {}
},
Expand Down
10 changes: 5 additions & 5 deletions packages/solid/src/generators/application/lib/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
linter: options.linter,
project: options.name,
tsConfigPaths: [
joinPathFragments(options.projectRoot, 'tsconfig.app.json'),
joinPathFragments(options.appProjectRoot, 'tsconfig.app.json'),
],
eslintFilePatterns: [`${options.projectRoot}/**/*.{ts,spec.ts,tsx}`],
eslintFilePatterns: [`${options.appProjectRoot}/**/*.{ts,spec.ts,tsx}`],
skipFormat: true,
});

host.rename(
joinPathFragments(options.projectRoot, 'eslintrc.js'),
joinPathFragments(options.projectRoot, '.eslintrc.js')
joinPathFragments(options.appProjectRoot, 'eslintrc.js'),
joinPathFragments(options.appProjectRoot, '.eslintrc.js')
);
host.delete(joinPathFragments(options.projectRoot, '.eslintrc.json'));
host.delete(joinPathFragments(options.appProjectRoot, '.eslintrc.json'));

const installTask = await addDependenciesToPackageJson(
host,
Expand Down
8 changes: 4 additions & 4 deletions packages/solid/src/generators/application/lib/add-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {

export function addProject(tree: Tree, options: NormalizedSchema) {
addProjectConfiguration(tree, options.name, {
root: options.projectRoot,
sourceRoot: `${options.projectRoot}/src`,
root: options.appProjectRoot,
sourceRoot: `${options.appProjectRoot}/src`,
projectType: 'application',
tags: options.parsedTags,
targets: { lint: createLintTarget(options) },
Expand All @@ -21,10 +21,10 @@ function createLintTarget(options: NormalizedSchema): TargetConfiguration {
executor: '@nx/linter:lint',
options: {
linter: 'eslint',
tsConfig: joinPathFragments(options.projectRoot, 'tsconfig.app.json'),
tsConfig: joinPathFragments(options.appProjectRoot, 'tsconfig.app.json'),
exclude: [
'**/node_modules/**',
`!${joinPathFragments(options.projectRoot, '**/*')}`,
`!${joinPathFragments(options.appProjectRoot, '**/*')}`,
],
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export function createFiles(host: Tree, options: NormalizedSchema) {
generateFiles(
host,
joinPathFragments(__dirname, '../files'),
options.projectRoot,
options.appProjectRoot,
{
...options,
...names(options.name),
offsetFromRoot: offsetFromRoot(options.projectRoot),
offsetFromRoot: offsetFromRoot(options.appProjectRoot),
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function updateJestConfig(host: Tree, options: NormalizedSchema) {
return;
}

const jestConfigPath = `${options.projectRoot}/jest.config.ts`;
const jestConfigPath = `${options.appProjectRoot}/jest.config.ts`;
// const svelteConfigPath = `${options.projectRoot}/jest.config.ts`;
const originalContent = host.read(jestConfigPath)?.toString();
const content = updateJestConfigContent(originalContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tree } from '@nx/devkit';
import { NormalizedSchema } from '../schema';

export function updateViteConfig(host: Tree, options: NormalizedSchema) {
const configPath = `${options.projectRoot}/vite.config.ts`;
const configPath = `${options.appProjectRoot}/vite.config.ts`;
const originalContent = host.read(configPath, 'utf-8');
const content = updateViteConfigContent(originalContent);
host.write(configPath, content);
Expand Down
14 changes: 9 additions & 5 deletions packages/solid/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { Linter } from '@nx/linter';
import { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-and-root-utils';

export interface SolidApplicationSchema {
export interface Schema {
name: string;
tags?: string;

linter: Linter;
unitTestRunner: 'jest' | 'vitest' | 'none';
e2eTestRunner: 'cypress' | 'none';
directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
rootProject?: boolean;
host?: string;
port?: number;
}

export interface NormalizedSchema extends SolidApplicationSchema {
projectRoot: string;
projectDirectory: string;
export interface NormalizedSchema<T extends Schema = Schema> extends T {
projectName: string;
appProjectRoot: string;
e2eProjectName: string;
e2eProjectRoot: string;
fileName: string;
parsedTags: string[];
skipFormat: boolean;
Expand Down
11 changes: 11 additions & 0 deletions packages/solid/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"enum": ["eslint"],
"default": "eslint"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
},
"unitTestRunner": {
"type": "string",
"enum": ["vitest", "jest", "none"],
Expand All @@ -41,6 +46,12 @@
"description": "A directory where the lib is placed.",
"alias": "d"
},
"rootProject": {
"description": "Create a application at the root of the workspace",
"type": "boolean",
"default": false,
"hidden": true
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
Expand Down
5 changes: 3 additions & 2 deletions packages/solid/src/generators/library/lib/add-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { ensurePackage, NX_VERSION, Tree } from '@nx/devkit';
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 } = ensurePackage<
typeof import('@nx/vite')
>('@nx/vite', NX_VERSION);

return await viteConfigurationGenerator(host, {
uiFramework: 'none',
Expand Down
12 changes: 6 additions & 6 deletions packages/solid/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { libraryGenerator } from './library';
describe('solid library schematic', () => {
let tree: Tree;
const options: SolidLibrarySchema = {
name: 'test',
name: 'myLib',
linter: Linter.EsLint,
unitTestRunner: 'jest',
e2eTestRunner: 'cypress',
Expand Down Expand Up @@ -42,11 +42,11 @@ describe('solid library schematic', () => {
await libraryGenerator(tree, options);

// expect(tree.exists(`libs/${options.name}/solid.config.cjs`)).toBeTruthy();
expect(tree.exists(`libs/${options.name}/tsconfig.lib.json`)).toBeTruthy();
expect(tree.exists(`libs/${options.name}/tsconfig.spec.json`)).toBeTruthy();
expect(tree.exists(`libs/${options.name}/tsconfig.json`)).toBeTruthy();
expect(tree.exists(`libs/${options.name}/.eslintrc.json`)).toBeFalsy();
expect(tree.exists(`libs/${options.name}/.eslintrc.js`)).toBeTruthy();
expect(tree.exists(`libs/my-lib/tsconfig.lib.json`)).toBeTruthy();
expect(tree.exists(`libs/my-lib/tsconfig.spec.json`)).toBeTruthy();
expect(tree.exists(`libs/my-lib/tsconfig.json`)).toBeTruthy();
expect(tree.exists(`libs/my-lib/.eslintrc.json`)).toBeFalsy();
expect(tree.exists(`libs/my-lib/.eslintrc.js`)).toBeTruthy();
});

it('should fail if no importPath is provided with publishable', async () => {
Expand Down
Loading

0 comments on commit 9d8b1cb

Please sign in to comment.