Skip to content

Instantly share code, notes, and snippets.

@buildmotion
Created November 24, 2018 01:41
Show Gist options
  • Save buildmotion/5e40d45cf32cfa0de0160f127e5f6a34 to your computer and use it in GitHub Desktop.
Save buildmotion/5e40d45cf32cfa0de0160f127e5f6a34 to your computer and use it in GitHub Desktop.
schematic default function
import {
Rule,
SchematicContext,
SchematicsException,
Tree,
apply,
filter,
mergeWith,
move,
noop,
template,
url,
branchAndMerge,
} from '@angular-devkit/schematics';
import { strings } from '@angular-devkit/core';
import { parseName } from '@schematics/angular/utility/parse-name'
import { getWorkspace } from '@schematics/angular/utility/config'
import { buildDefaultPath } from '@schematics/angular/utility/project'
import { WorkspaceProject } from '@schematics/angular/utility/workspace-models';
/**
* Use to setup the target path using the specified options [project].
* @param host the current [Tree]
* @param options the current [options]
* @param context the [SchematicContext]
*/
export function setupOptions(host: Tree, options: any, context: SchematicContext) {
const workspace = getWorkspace(host);
if (!options.project) {
context.logger.error(`The [project] option is missing.`);
throw new SchematicsException('Option (project) is required.');
}
context.logger.info (`Preparing to retrieve the project using: ${options.project}`);
const project = <WorkspaceProject>workspace.projects[options.project];
if (options.path === undefined) {
context.logger.info(`Preparing to determine the target path.`);
options.path = buildDefaultPath(project);
context.logger.info(`The target path: ${options.path}`);
}
options.type = !!options.type ? `.${options.type}` : '';
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
context.logger.info(`Finished options setup.`);
return host;
}
export default function (options: any): Rule {
return (host: Tree, context: SchematicContext) => {
setupOptions(host, options, context);
// setup a variable [currentDateTime] programmatically --> used in template;
options.currentDateTime = new Date(Date.now()).toUTCString();
const templateSource = apply(url('./files'), [
options.spec ? noop() : filter(path => !path.endsWith('.spec.ts')),
template({
...strings,
...options,
}),
move(options.path),
]);
return branchAndMerge(mergeWith(templateSource));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment