Skip to content

Instantly share code, notes, and snippets.

@MSakamaki
Last active June 13, 2020 07:15
Show Gist options
  • Save MSakamaki/6ee0a7d7c0d6878462a7661c5d848c9f to your computer and use it in GitHub Desktop.
Save MSakamaki/6ee0a7d7c0d6878462a7661c5d848c9f to your computer and use it in GitHub Desktop.
nest/cli workspace-schematic for nrwl

1. Install nest cli

npm install -D @nestjs/cli

2. Generate workspace-schematic

npx ng g workspace-schematic nest

2. Edit the generated file.

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

3. Run workspace-schematic

# npx nx workspace-schematic nest [schematics] [name] [project]

npx nx workspace-schematic nest controller name api

# library (generated but not for nrwl)
npx nx workspace-schematic nest lib firestore 

library

example

Related issues

nrwl/nx#1658

nrwl/nx#1010

import {
chain,
externalSchematic,
Rule,
Tree,
SchematicContext
} from '@angular-devkit/schematics';
import { getWorkspace } from '@schematics/angular/utility/config';
import { Schema as ServiceOptions } from '@schematics/angular/service/schema';
import { NestCollection } from '@nestjs/cli/lib/schematics/nest.collection';
interface NestServiceOptions extends ServiceOptions {
schematic: string;
project: string;
}
const findAliasToName = (name: string) =>
NestCollection.getSchematics().find(
schematic => schematic.alias === name
) || { name };
const getModuleDir = (projectType: 'application' | 'library') =>
projectType === 'application' ? 'app' : 'lib';
export default function(schema: NestServiceOptions): Rule {
return (host: Tree, _context: SchematicContext) => {
const workspace = getWorkspace(host);
const schematicName = findAliasToName(schema.schematic).name;
const generateSchematicOption = () =>
schematicName === 'library'
? {
name: schema.name,
language: 'ts'
}
: {
name: schema.name,
sourceRoot: `${
workspace.projects[schema.project].sourceRoot
}/${getModuleDir(workspace.projects[schema.project].projectType)}`,
language: 'ts'
};
return chain([
externalSchematic(
'@nestjs/schematics',
schematicName,
generateSchematicOption()
)
]);
};
}
{
"$schema": "http://json-schema.org/schema",
"id": "nest",
"type": "object",
"properties": {
"schematic": {
"type": "string",
"description": "schematic type",
"$default": {
"$source": "argv",
"index": 0
}
},
"name": {
"type": "string",
"description": "Library name",
"$default": {
"$source": "argv",
"index": 1
}
},
"project": {
"type": "string",
"description": "target project",
"$default": {
"$source": "argv",
"index": 2
}
}
},
"required": ["schematic", "name"]
}
@andreasasprou
Copy link

Cheers buddy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment