Last active
June 12, 2019 04:15
-
-
Save ricmello/9f079ad8d3c7ed5bfc09828bb0ba8bb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
apply, | |
branchAndMerge, | |
chain, | |
mergeWith, | |
move, | |
renameTemplateFiles, | |
Rule, | |
SchematicContext, | |
template, | |
Tree, | |
url | |
} from '@angular-devkit/schematics'; | |
import { | |
basename, | |
dirname, | |
experimental, | |
normalize, | |
Path, | |
strings | |
} from '@angular-devkit/core'; | |
export function mySchematics(_options: any): Rule { | |
return (tree: Tree, context: SchematicContext) => { | |
const workspaceConfig = tree.read('/angular.json'); | |
if (!workspaceConfig) { | |
throw new Error('Could not find Angular workspace configuration'); | |
} | |
// convert workspace settings to string | |
const workspaceContent = workspaceConfig.toString(); | |
// parse workspace string into JSON object | |
const workspace: experimental.workspace.WorkspaceSchema = JSON.parse( | |
workspaceContent | |
); | |
// get project name | |
if (!_options.project) { | |
_options.project = workspace.defaultProject; | |
} | |
const projectName = _options.project as string; | |
const project = workspace.projects[projectName]; | |
const projectType = project.projectType === 'application' ? 'app' : 'lib'; | |
// Get the path to create files | |
if (_options.path === undefined) { | |
_options.path = `${project.sourceRoot}/${projectType}`; | |
} | |
const parsedPath = parseName(_options.path, _options.name); | |
_options.name = parsedPath.name; | |
_options.path = parsedPath.path; | |
// Parse template files | |
const templateSource = apply(url('./files'), [ | |
renameTemplateFiles(), | |
template({ | |
...strings, | |
..._options, | |
classify: strings.classify, | |
dasherize: strings.dasherize | |
}), | |
move(normalize((_options.path + '/' + _options.name) as string)) | |
]); | |
// Return Rule chain | |
return chain([branchAndMerge(chain([mergeWith(templateSource)]))])( | |
tree, | |
context | |
); | |
}; | |
} | |
export function parseName( | |
path: string, | |
name: string | |
): { name: string; path: Path } { | |
const nameWithoutPath = basename(name as Path); | |
const namePath = dirname((path + '/' + name) as Path); | |
return { | |
name: nameWithoutPath, | |
path: normalize('/' + namePath) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment