Skip to content

Instantly share code, notes, and snippets.

@MSakamaki
Last active August 24, 2019 08:32
Show Gist options
  • Save MSakamaki/2748b6ff8d245c88c27b7cbf41e51a54 to your computer and use it in GitHub Desktop.
Save MSakamaki/2748b6ff8d245c88c27b7cbf41e51a54 to your computer and use it in GitHub Desktop.
nest library workspace-schematic for nrwl

nest library workspace-schematic for nrwl

Note: Temporary code

1. Install nest cli

npm install -D @nestjs/cli

2. Generate workspace-schematic

npx ng g workspace-schematic nest-lib

2. Edit the generated file.

edit tools/schematics/nest-lib/index.ts and tools/schematics/nest-lib/schema.json

3. Run workspace-schematic

# npx nx workspace-schematic nest-lib [name] -directory=[directory] --tags=[tags]

npx nx workspace-schematic nest-lib test --directory=sample --tags=lib:sample,lib:api

Related issues

nrwl/nx#1658

nrwl/nx#1010

import {
chain,
externalSchematic,
Rule,
Tree,
SchematicContext,
move
} from '@nrwl/workspace/node_modules/@angular-devkit/schematics';
import { Linter } from '@nrwl/workspace/src/utils/lint';
import { toFileName, formatFiles } from '@nrwl/workspace';
import { SchematicsException } from '@angular-devkit/schematics';
import { strings } from '@angular-devkit/core';
export interface Schema {
name: string;
directory?: string;
skipTsConfig: boolean;
skipFormat: boolean;
tags?: string;
simpleModuleName: boolean;
unitTestRunner: 'jest' | 'none';
linter: Linter;
}
export interface NormalizedSchema extends Schema {
name: string;
fileName: string;
projectRoot: string;
projectDirectory: string;
parsedTags: string[];
}
export const nestjsModule = (
name: string
) => `import { Module } from '@nestjs/common';
@Module({
imports: [],
controllers: [],
providers: []
})
export class ${strings.classify(name)}Module {}
`;
export default function(schema: Schema): Rule {
return (_host: Tree, _context: SchematicContext) => {
const options = normalizeOptions(schema);
return chain([
externalSchematic('@nrwl/workspace', 'library', schema),
addNestModule(options),
move(
`${options.projectRoot}/src/lib/${options.name}`,
`${options.projectRoot}/src/lib`
),
formatFiles(options)
]);
};
}
function addNestModule(schema: NormalizedSchema) {
const modulePath = `${schema.projectRoot}/src/lib/${schema.name}.module`;
return (host: Tree) => {
host.rename(
`${schema.projectRoot}/src/lib/${schema.name}.ts`,
`${modulePath}.ts`
);
host.overwrite(
`${schema.projectRoot}/src/index.ts`,
`export * from './lib/${schema.name}.module';`
);
host.overwrite(`${modulePath}.ts`, nestjsModule(schema.name));
return host;
};
}
function normalizeOptions(options: Schema): NormalizedSchema {
const name = toFileName(options.name);
const projectDirectory = options.directory
? `${toFileName(options.directory)}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = options.simpleModuleName ? name : projectName;
const projectRoot = `libs/${projectDirectory}`;
const parsedTags = options.tags
? options.tags.split(',').map(s => s.trim())
: [];
return {
...options,
fileName,
name: projectName,
projectRoot,
projectDirectory,
parsedTags
};
}
export const fileReadText = (host: Tree, path: string): string => {
const source = host.read(path);
if (source === null) {
throw new SchematicsException(`File ${path} does not exist.`);
}
return source.toString('utf-8');
};
{
"$schema": "http://json-schema.org/schema",
"id": "nest-lib",
"type": "object",
"examples": [
{
"command": "g lib mylib --directory=myapp",
"description": "Generate libs/myapp/mylib"
}
],
"properties": {
"name": {
"type": "string",
"description": "Library name",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use for the library?"
},
"directory": {
"type": "string",
"description": "A directory where the app is placed"
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "tslint"],
"default": "tslint"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests",
"default": "jest"
},
"tags": {
"type": "string",
"description": "Add tags to the library (used for linting)"
},
"skipFormat": {
"description": "Skip formatting files",
"type": "boolean",
"default": false
},
"skipTsConfig": {
"type": "boolean",
"default": false,
"description": "Do not update tsconfig.json for development experience."
}
},
"required": ["name"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment