|
| 1 | +import { |
| 2 | + addProjectConfiguration, |
| 3 | + formatFiles, |
| 4 | + generateFiles, |
| 5 | + getWorkspaceLayout, |
| 6 | + names, |
| 7 | + offsetFromRoot, |
| 8 | + Tree, |
| 9 | +} from '@nrwl/devkit'; |
| 10 | +import * as path from 'path'; |
| 11 | +import { SyncGeneratorSchema } from './schema'; |
| 12 | + |
| 13 | +interface NormalizedSchema extends SyncGeneratorSchema { |
| 14 | + projectName: string; |
| 15 | + projectRoot: string; |
| 16 | + projectDirectory: string; |
| 17 | + parsedTags: string[]; |
| 18 | +} |
| 19 | + |
| 20 | +function normalizeOptions( |
| 21 | + host: Tree, |
| 22 | + options: SyncGeneratorSchema |
| 23 | +): NormalizedSchema { |
| 24 | + const name = names(options.name).fileName; |
| 25 | + const projectDirectory = options.directory |
| 26 | + ? `${names(options.directory).fileName}/${name}` |
| 27 | + : name; |
| 28 | + const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-'); |
| 29 | + const projectRoot = `${getWorkspaceLayout(host).libsDir}/${projectDirectory}`; |
| 30 | + const parsedTags = options.tags |
| 31 | + ? options.tags.split(',').map((s) => s.trim()) |
| 32 | + : []; |
| 33 | + |
| 34 | + return { |
| 35 | + ...options, |
| 36 | + projectName, |
| 37 | + projectRoot, |
| 38 | + projectDirectory, |
| 39 | + parsedTags, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function addFiles(host: Tree, options: NormalizedSchema) { |
| 44 | + const templateOptions = { |
| 45 | + ...options, |
| 46 | + ...names(options.name), |
| 47 | + offsetFromRoot: offsetFromRoot(options.projectRoot), |
| 48 | + template: '', |
| 49 | + }; |
| 50 | + generateFiles( |
| 51 | + host, |
| 52 | + path.join(__dirname, 'files'), |
| 53 | + options.projectRoot, |
| 54 | + templateOptions |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +export default async function (host: Tree, options: SyncGeneratorSchema) { |
| 59 | + const normalizedOptions = normalizeOptions(host, options); |
| 60 | + addProjectConfiguration(host, normalizedOptions.projectName, { |
| 61 | + root: normalizedOptions.projectRoot, |
| 62 | + projectType: 'library', |
| 63 | + sourceRoot: `${normalizedOptions.projectRoot}/src`, |
| 64 | + targets: { |
| 65 | + build: { |
| 66 | + executor: '@nx-dotnet/core:build', |
| 67 | + }, |
| 68 | + }, |
| 69 | + tags: normalizedOptions.parsedTags, |
| 70 | + }); |
| 71 | + addFiles(host, normalizedOptions); |
| 72 | + await formatFiles(host); |
| 73 | +} |
0 commit comments