Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function normalizeOptions(
host: Tree,
options: Schema
): Promise {
let appProjectSourcePath: Path;
let appMainFilePath: string;
const extraNames = names(options.name);
const { sourceRoot } = getProjectConfig(host, options.project);
const workspace = await getWorkspace(host);
const projectType = workspace.projects.get(options.project).extensions
.projectType as string;
const tsConfigJson = readJsonInTree(host, 'tsconfig.json');
const tsPaths: { [module: string]: string[] } = tsConfigJson.compilerOptions
? tsConfigJson.compilerOptions.paths || {}
: {};
const modulePath =
projectType === 'application'
? `./app/${extraNames.fileName}.slice`
: Object.keys(tsPaths).find(k =>
tsPaths[k].some(s => s.includes(sourceRoot))
);
// If --project is set to an app, automatically configure store
// for it without needing to specify --appProject.
const className = `${toClassName(options.name)}`;
const exportBarrels = options.barrels === true;
const buffer = host.read(indexFilePath);
if (!!buffer) {
// AST to 'index.ts' barrel for the public API
const indexSource = buffer!.toString('utf-8');
const indexSourceFile = ts.createSourceFile(
indexFilePath,
indexSource,
ts.ScriptTarget.Latest,
true
);
// Public API for the feature interfaces, selectors, and facade
const { fileName } = names(options.name);
const statePath = `./lib/${options.directory}/${fileName}`;
insert(host, indexFilePath, [
...addGlobal(
indexSourceFile,
indexFilePath,
exportBarrels
? `import * as ${className}Actions from '${statePath}.actions';`
: `export * from '${statePath}.actions';`
),
...addGlobal(
indexSourceFile,
indexFilePath,
exportBarrels
? `import * as ${className}Feature from '${statePath}.reducer';`
: `export * from '${statePath}.reducer';`
function normalizeOptions(
host: Tree,
options: Schema,
context: SchematicContext
): NormalizedSchema {
const { className, fileName } = names(options.name);
const componentFileName = options.pascalCaseFiles ? className : fileName;
const { sourceRoot: projectSourceRoot, projectType } = getProjectConfig(
host,
options.project
);
const styledModule = /^(css|scss|less|styl)$/.test(options.style)
? null
: options.style;
assertValidStyle(options.style);
if (options.export && projectType === 'application') {
context.logger.warn(
`The "--export" option should not be used with applications and will do nothing.`
);
function createFiles(options: NormalizedSchema): Rule {
return mergeWith(
apply(url(`./files/lib`), [
template({
...options,
...names(options.name),
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot)
}),
move(options.projectRoot),
options.publishable
? noop()
: filter(file => !file.endsWith('package.json'))
])
);
}
function createApplicationFiles(options: NormalizedSchema): Rule {
return mergeWith(
apply(url(`./files`), [
template({
...names(options.name),
...options,
tmpl: '',
offsetFromRoot: offsetFromRoot(options.appProjectRoot)
}),
options.styledModule
? filter(file => !file.endsWith(`.${options.style}`))
: noop(),
options.unitTestRunner === 'none'
? filter(file => file !== `/specs/index.spec.tsx`)
: noop(),
move(options.appProjectRoot)
])
);
}
function createFiles(options: NormalizedSchema): Rule {
return mergeWith(
apply(url(`./files/lib`), [
template({
...options,
...names(options.name),
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot)
}),
move(options.projectRoot),
options.unitTestRunner === 'none'
? filter(file => !file.endsWith('spec.ts'))
: noop(),
options.publishable
? noop()
: filter(file => !file.endsWith('package.json'))
]),
MergeStrategy.Overwrite
);
}
function createApplicationFiles(options: NormalizedSchema): Rule {
return mergeWith(
apply(url(`./files/app`), [
template({
...names(options.name),
...options,
tmpl: '',
offsetFromRoot: offsetFromRoot(options.appProjectRoot)
}),
options.styledModule
? filter(file => !file.endsWith(`.${options.style}`))
: noop(),
options.unitTestRunner === 'none'
? filter(file => file !== `/src/app/${options.fileName}.spec.tsx`)
: noop(),
move(options.appProjectRoot)
])
);
}
function createApplicationFiles(options: NormalizedSchema): Rule {
return mergeWith(
apply(url(`./files/app`), [
template({
...options,
...names(options.name),
tmpl: '',
offsetFromRoot: offsetFromRoot(options.appProjectRoot)
}),
options.unitTestRunner === 'none'
? filter(file => file !== '/src/app/app.spec.ts')
: noop(),
move(options.appProjectRoot)
])
);
}
function generateNgrxFilesFromTemplates(options: Schema) {
const name = options.name;
const moduleDir = path.dirname(options.module);
const excludeFacade = path => path.match(/^((?!facade).)*$/);
const templateSource = apply(
url(options.syntax === 'creators' ? './creator-files' : './files'),
[
!options.facade ? filter(excludeFacade) : noop(),
template({ ...options, tmpl: '', ...names(name) }),
move(moduleDir)
]
);
return mergeWith(templateSource);
}