Example #1
0
 return function (host) {
     if (!options.module) {
         return host;
     }
     var modulePath = options.module;
     if (!host.exists(options.module)) {
         throw new Error("Specified module path " + modulePath + " does not exist");
     }
     var text = host.read(modulePath);
     if (text === null) {
         throw new schematics_1.SchematicsException("File " + modulePath + " does not exist.");
     }
     var sourceText = text.toString('utf-8');
     var source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
     var commonImports = [
         route_utils_1.insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
     ];
     var reducerPath = "/" + options.path + "/" +
         (options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
         (options.group ? 'reducers/' : '') +
         stringUtils.dasherize(options.name) +
         '.reducer';
     var relativePath = find_module_1.buildRelativePath(modulePath, reducerPath);
     var reducerImport = route_utils_1.insertImport(source, modulePath, "* as from" + stringUtils.classify(options.name), relativePath, true);
     var _a = __read(ast_utils_1.addImportToModule(source, modulePath, "StoreModule.forFeature('" + stringUtils.camelize(options.name) + "', from" + stringUtils.classify(options.name) + ".reducer)", relativePath), 1), storeNgModuleImport = _a[0];
     var changes = __spread(commonImports, [reducerImport, storeNgModuleImport]);
     var recorder = host.beginUpdate(modulePath);
     try {
         for (var changes_2 = __values(changes), changes_2_1 = changes_2.next(); !changes_2_1.done; changes_2_1 = changes_2.next()) {
             var change = changes_2_1.value;
             if (change instanceof change_1.InsertChange) {
                 recorder.insertLeft(change.pos, change.toAdd);
             }
         }
     }
     catch (e_2_1) { e_2 = { error: e_2_1 }; }
     finally {
         try {
             if (changes_2_1 && !changes_2_1.done && (_b = changes_2.return)) _b.call(changes_2);
         }
         finally { if (e_2) throw e_2.error; }
     }
     host.commitUpdate(recorder);
     return host;
     var e_2, _b;
 };
Example #2
0
 return function (host) {
     if (!options.reducers) {
         return host;
     }
     var reducersPath = core_1.normalize("/" + options.path + "/" + options.reducers);
     if (!host.exists(reducersPath)) {
         throw new Error("Specified reducers path " + reducersPath + " does not exist");
     }
     var text = host.read(reducersPath);
     if (text === null) {
         throw new schematics_1.SchematicsException("File " + reducersPath + " does not exist.");
     }
     var sourceText = text.toString('utf-8');
     var source = ts.createSourceFile(reducersPath, sourceText, ts.ScriptTarget.Latest, true);
     var reducerPath = "/" + options.path + "/" +
         (options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
         (options.group ? 'reducers/' : '') +
         stringUtils.dasherize(options.name) +
         '.reducer';
     var relativePath = find_module_1.buildRelativePath(reducersPath, reducerPath);
     var reducerImport = route_utils_1.insertImport(source, reducersPath, "* as from" + stringUtils.classify(options.name), relativePath, true);
     var stateInterfaceInsert = addReducerToStateInterface(source, reducersPath, options);
     var reducerMapInsert = addReducerToActionReducerMap(source, reducersPath, options);
     var changes = [reducerImport, stateInterfaceInsert, reducerMapInsert];
     var recorder = host.beginUpdate(reducersPath);
     try {
         for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
             var change = changes_1_1.value;
             if (change instanceof change_1.InsertChange) {
                 recorder.insertLeft(change.pos, change.toAdd);
             }
         }
     }
     catch (e_1_1) { e_1 = { error: e_1_1 }; }
     finally {
         try {
             if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
         }
         finally { if (e_1) throw e_1.error; }
     }
     host.commitUpdate(recorder);
     return host;
     var e_1, _a;
 };