Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
const opts = ['state', 'stateInterface'].reduce(
(current: Partial, key) => {
return omit(current, key as any);
},
options
);
const templateSource = apply(
url(options.testDepth === 'unit' ? './files' : './integration-files'),
[
options.spec
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
const opts = ['state', 'stateInterface'].reduce(
(current: Partial, key) => {
return omit(current, key as any);
},
options
);
const templateSource = apply(
url(options.testDepth === 'unit' ? './files' : './integration-files'),
[
options.spec
? noop()
: filter(path => !path.endsWith('.spec.ts.template')),
const stateImportPath = buildRelativePath(componentPath, statePath);
const storeImport = insertImport(
source,
componentPath,
'Store',
'@ngrx/store'
);
const stateImport = options.state
? insertImport(
source,
componentPath,
`* as fromStore`,
stateImportPath,
true
)
: new NoopChange();
const componentClass = source.statements.find(
stm => stm.kind === ts.SyntaxKind.ClassDeclaration
);
const component = componentClass as ts.ClassDeclaration;
const componentConstructor = component.members.find(
member => member.kind === ts.SyntaxKind.Constructor
);
const cmpCtr = componentConstructor as ts.ConstructorDeclaration;
const { pos } = cmpCtr;
const stateType = options.state
? `fromStore.${options.stateInterface}`
: 'any';
const constructorText = cmpCtr.getText();
const [start, end] = constructorText.split('()');
const storeText = `private store: Store<${stateType}>`;
stm => stm.kind === ts.SyntaxKind.ClassDeclaration
);
const component = componentClass as ts.ClassDeclaration;
const componentConstructor = component.members.find(
member => member.kind === ts.SyntaxKind.Constructor
);
const cmpCtr = componentConstructor as ts.ConstructorDeclaration;
const { pos } = cmpCtr;
const stateType = options.state
? `fromStore.${options.stateInterface}`
: 'any';
const constructorText = cmpCtr.getText();
const [start, end] = constructorText.split('()');
const storeText = `private store: Store<${stateType}>`;
const storeConstructor = [start, `(${storeText})`, end].join('');
const constructorUpdate = new ReplaceChange(
componentPath,
pos,
` ${constructorText}\n\n`,
`\n\n ${storeConstructor}`
);
const changes = [storeImport, stateImport, constructorUpdate];
const recorder = host.beginUpdate(componentPath);
for (const change of changes) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
} else if (change instanceof ReplaceChange) {
recorder.remove(pos, change.oldText.length);
recorder.insertLeft(change.order, change.newText);
}
(current: Partial, key) => {
return omit(current, key as any);
},
options
const effectsModuleImport = insertImport(
source,
modulePath,
'EffectsModule',
'@ngrx/effects'
);
const effectsPath =
`/${options.path}/` +
(options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
(options.group ? 'effects/' : '') +
stringUtils.dasherize(options.name) +
'.effects';
const relativePath = buildRelativePath(modulePath, effectsPath);
const effectsImport = insertImport(
source,
modulePath,
effectsName,
relativePath
);
const effectsSetup =
options.root && options.minimal ? `[]` : `[${effectsName}]`;
const [effectsNgModuleImport] = addImportToModule(
source,
modulePath,
`EffectsModule.for${options.root ? 'Root' : 'Feature'}(${effectsSetup})`,
relativePath
);
let changes = [effectsModuleImport, effectsNgModuleImport];
const text = host.read(modulePath);
if (text === null) {
throw new SchematicsException(`File ${modulePath} does not exist.`);
}
const sourceText = text.toString('utf-8');
const source = ts.createSourceFile(
modulePath,
sourceText,
ts.ScriptTarget.Latest,
true
);
const effectsName = `${stringUtils.classify(`${options.name}Effects`)}`;
const effectsModuleImport = insertImport(
source,
modulePath,
'EffectsModule',
'@ngrx/effects'
);
const effectsPath =
`/${options.path}/` +
(options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
(options.group ? 'effects/' : '') +
stringUtils.dasherize(options.name) +
'.effects';
const relativePath = buildRelativePath(modulePath, effectsPath);
const effectsImport = insertImport(
source,
modulePath,
if (options.root) {
const storeDevtoolsNgModuleImport = addImportToModule(
source,
modulePath,
`!environment.production ? StoreDevtoolsModule.instrument() : []`,
relativePath
).shift();
rootImports = rootImports.concat([
insertImport(
source,
modulePath,
'StoreDevtoolsModule',
'@ngrx/store-devtools'
),
insertImport(source, modulePath, 'environment', environmentsPath),
storeDevtoolsNgModuleImport,
]);
}
const changes = [...commonImports, ...rootImports];
const recorder = host.beginUpdate(modulePath);
for (const change of changes) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(recorder);
return host;
};
}
options.root
? `StoreModule.forRoot(${rootStoreReducers}, ${rootStoreConfig})`
: `StoreModule.forFeature(from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(
options.name
)}FeatureKey, from${stringUtils.classify(
options.name
)}.reducers, { metaReducers: from${stringUtils.classify(
options.name
)}.metaReducers })`,
relativePath
).shift();
let commonImports = [
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
storeNgModuleImport,
];
if (options.root && !options.minimal) {
commonImports = commonImports.concat([
insertImport(
source,
modulePath,
'reducers, metaReducers',
relativePath
),
]);
} else if (!options.root) {
commonImports = commonImports.concat([
insertImport(
source,
)}FeatureKey, from${stringUtils.classify(
options.name
)}.reducers, { metaReducers: from${stringUtils.classify(
options.name
)}.metaReducers })`,
relativePath
).shift();
let commonImports = [
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
storeNgModuleImport,
];
if (options.root && !options.minimal) {
commonImports = commonImports.concat([
insertImport(
source,
modulePath,
'reducers, metaReducers',
relativePath
),
]);
} else if (!options.root) {
commonImports = commonImports.concat([
insertImport(
source,
modulePath,
`* as from${stringUtils.classify(options.name)}`,
relativePath,
true
),
]);