Skip to content

Commit

Permalink
feat(svelte): support for projectNameAndRootFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikPieper committed Sep 20, 2023
1 parent 9d8b1cb commit 8a8f0d9
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 88 deletions.
32 changes: 32 additions & 0 deletions docs/docs/svelte/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ Type: `number`

Port to listen on.

#### 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 @@ -178,12 +194,28 @@ 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`).

#### publishable

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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"nx-cloud": "16.4.0",
"prettier": "2.7.1",
"pretty-quick": "^3.1.3",
"svelte": "^3.59.1",
"svelte": "^4.2.1",
"svelte-preprocess": "^5.0.4",
"tcp-port-used": "^1.0.2",
"ts-jest": "29.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nxext/svelte",
"version": "16.7.1",
"version": "16.80",
"license": "MIT",
"author": "Dominik Pieper",
"description": "Nx plugin for Svelte",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SvelteApplicationSchema } from './schema';
import { Schema } from './schema';
import { Linter } from '@nx/linter';
import applicationGenerator from './application';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { readJson } from '@nx/devkit';

describe('svelte app generator', () => {
let tree;
const options: SvelteApplicationSchema = {
const options: Schema = {
name: 'my-app',
linter: Linter.EsLint,
unitTestRunner: 'jest',
Expand Down
46 changes: 13 additions & 33 deletions packages/svelte/src/generators/application/application.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {
convertNxGenerator,
formatFiles,
generateFiles,
getWorkspaceLayout,
joinPathFragments,
names,
offsetFromRoot,
Tree,
runTasksInSerial,
GeneratorCallback,
} from '@nx/devkit';
import { NormalizedSchema, SvelteApplicationSchema } 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 @@ -19,39 +18,20 @@ import { updateJestConfig } from './lib/update-jest-config';
import { addVite } from './lib/add-vite';
import { updateViteConfig } from './lib/update-vite-config';
import { createApplicationFiles } from './lib/create-project-files';

function normalizeOptions(
tree: Tree,
options: SvelteApplicationSchema
): 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}`);
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];

return {
...options,
name: projectName,
projectRoot,
parsedTags,
fileName,
projectDirectory,
skipFormat: false,
};
}
import { normalizeOptions } from './lib/normalize-options';

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getWorkspaceLayout, joinPathFragments, names, Tree } from '@nx/devkit';
import { NormalizedSchema, Schema } from '../schema';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';

export async function normalizeOptions(
host: Tree,
options: Schema,
callingGenerator = '@nxext/solid:application'
): Promise<NormalizedSchema> {
const {
projectName: appProjectName,
projectRoot,
projectNameAndRootFormat,
} = await determineProjectNameAndRootOptions(host, {
name: options.name,
projectType: 'application',
directory: options.directory,
projectNameAndRootFormat: options.projectNameAndRootFormat,
rootProject: options.rootProject,
callingGenerator,
});
options.rootProject = projectRoot === '.';
options.projectNameAndRootFormat = projectNameAndRootFormat;

const e2eProjectName = options.rootProject ? 'e2e' : `${appProjectName}-e2e`;
const e2eProjectRoot = options.rootProject ? 'e2e' : `${projectRoot}-e2e`;

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

const fileName = 'App';

return {
...options,
name: names(options.name).fileName,
projectName: appProjectName,
projectRoot,
e2eProjectName,
e2eProjectRoot,
parsedTags,
fileName,
skipFormat: false,
};
}
12 changes: 8 additions & 4 deletions packages/svelte/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 SvelteApplicationSchema {
export interface Schema {
name: string;
tags?: string;

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

export interface NormalizedSchema extends SvelteApplicationSchema {
export interface NormalizedSchema extends Schema {
projectName: string;
projectRoot: string;
projectDirectory: string;
e2eProjectName: string;
e2eProjectRoot: string;
fileName: string;
parsedTags: string[];
skipFormat: boolean;
Expand Down
11 changes: 11 additions & 0 deletions packages/svelte/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"description": "Test runner to use for end to end (e2e) tests.",
"default": "cypress"
},
"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"]
},
"directory": {
"type": "string",
"description": "A directory where the lib is placed.",
Expand All @@ -51,6 +56,12 @@
"description": "Host to listen on.",
"default": "localhost"
},
"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
43 changes: 43 additions & 0 deletions packages/svelte/src/generators/library/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { names, Tree } from '@nx/devkit';
import { NormalizedSchema, SvelteLibrarySchema } from '../schema';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';

export async function normalizeOptions(
host: Tree,
options: SvelteLibrarySchema
): Promise<NormalizedSchema> {
const {
projectName,
names: projectNames,
projectRoot,
importPath,
} = await determineProjectNameAndRootOptions(host, {
name: options.name,
projectType: 'library',
directory: options.directory,
importPath: options.importPath,
projectNameAndRootFormat: options.projectNameAndRootFormat,
callingGenerator: '@nx/react:library',
});
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
: name;
const fileName = options.simpleName
? projectNames.projectSimpleName
: projectNames.projectFileName;

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

return {
...options,
name: projectName,
projectRoot,
parsedTags,
fileName,
projectDirectory,
importPath,
};
}
34 changes: 2 additions & 32 deletions packages/svelte/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { updateTsConfig } from './lib/update-tsconfig';
import {
convertNxGenerator,
formatFiles,
getWorkspaceLayout,
joinPathFragments,
names,
Tree,
updateJson,
runTasksInSerial,
Expand All @@ -19,34 +16,7 @@ import { addVite } from './lib/add-vite';
import { updateViteConfig } from './lib/update-vite-config';
import { createProjectFiles } from './lib/create-project-files';
import { addVitest } from './lib/add-vitest';

function normalizeOptions(
tree: Tree,
options: SvelteLibrarySchema
): NormalizedSchema {
const { libsDir, npmScope } = getWorkspaceLayout(tree);
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName;
const projectRoot = joinPathFragments(`${libsDir}/${projectDirectory}`);
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
const importPath = options.importPath || `@${npmScope}/${projectDirectory}`;

return {
...options,
name: projectName,
projectRoot,
parsedTags,
fileName,
projectDirectory,
importPath,
};
}
import { normalizeOptions } from './lib/normalize-options';

function updateLibPackageNpmScope(host: Tree, options: NormalizedSchema) {
return updateJson(host, `${options.projectRoot}/package.json`, (json) => {
Expand All @@ -59,7 +29,7 @@ export async function libraryGenerator(
host: Tree,
schema: SvelteLibrarySchema
) {
const options = normalizeOptions(host, schema);
const options = await normalizeOptions(host, schema);
if (options.publishable === true && !schema.importPath) {
throw new Error(
`For publishable libs you have to provide a proper "--importPath" which needs to be a valid npm package name (e.g. my-awesome-lib or @myorg/my-lib)`
Expand Down
4 changes: 3 additions & 1 deletion packages/svelte/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Linter } from '@nx/linter';
import { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-and-root-utils';

export interface SvelteLibrarySchema {
name: string;
tags?: string;

projectNameAndRootFormat?: ProjectNameAndRootFormat;
linter: Linter;
unitTestRunner: 'jest' | 'vitest' | 'none';
e2eTestRunner: 'cypress' | 'none';
Expand All @@ -12,6 +13,7 @@ export interface SvelteLibrarySchema {
publishable?: boolean;
importPath?: string;
skipFormat: boolean;
simpleName?: boolean;
}

export interface NormalizedSchema extends SvelteLibrarySchema {
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"description": "A directory where the lib is placed.",
"alias": "d"
},
"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"]
},
"publishable": {
"type": "boolean",
"description": "Create a publishable library."
Expand All @@ -54,6 +59,11 @@
"type": "string",
"description": "The library name used to import it, like @myorg/my-awesome-lib"
},
"simpleName": {
"description": "Don't include the directory in the name of the module of the library.",
"type": "boolean",
"default": false
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
Expand Down
Loading

0 comments on commit 8a8f0d9

Please sign in to comment.