1 | {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../modules/schematics/src/component-store/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yDAcoC;AACpC,+BAAiC;AAEjC,yDAW+B;AAO/B,SAAS,sBAAsB,CAC7B,OAA8D,EAC9D,aAAqB;IAErB,IAAM,kBAAkB,GAAG,UAAG,6BAAW,CAAC,QAAQ,CAAC,UAAG,OAAO,CAAC,IAAI,UAAO,CAAC,CAAE,CAAC;IAC7E,IAAM,kBAAkB,GACtB,WAAI,OAAO,CAAC,IAAI,MAAG;QACnB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,6BAAW,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC;QAC/D,6BAAW,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;QACnC,QAAQ,CAAC;IACX,IAAM,0BAA0B,GAAG,IAAA,mCAAiB,EAClD,aAAa,EACb,kBAAkB,CACnB,CAAC;IAEF,OAAO;QACL,0BAA0B,4BAAA;QAC1B,kBAAkB,oBAAA;KACnB,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAgB,mCAAmC,CACjD,OAA8B;IAE9B,OAAO,UAAC,IAAU;;QAChB,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;QAClC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,gCAAyB,UAAU,oBAAiB,CAAC,CAAC;QACxE,CAAC;QAED,IAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACnC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YAClB,MAA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AC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAvB,CAAuB,KAC9C,OAAO,EACV;YACF,IAAA,iBAAI,EAAC,UAAU,CAAC,IAAI,CAAC;SACtB,CAAC,CAAC;QAEH,OAAO,IAAA,kBAAK,EAAC;YACX,IAAA,2BAAc,EACZ,IAAA,kBAAK,EAAC;gBACJ,mCAAmC,CAAC,OAAO,CAAC;gBAC5C,oCAAoC,CAAC,OAAO,CAAC;gBAC7C,IAAA,sBAAS,EAAC,cAAc,CAAC;aAC1B,CAAC,CACH;SACF,CAAC,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC;AAtCD,4BAsCC","sourcesContent":["import {\n apply,\n applyTemplates,\n branchAndMerge,\n chain,\n filter,\n mergeWith,\n move,\n noop,\n Rule,\n SchematicContext,\n SchematicsException,\n Tree,\n url,\n} from '@angular-devkit/schematics';\nimport * as ts from 'typescript';\nimport { Schema as ComponentStoreOptions } from './schema';\nimport {\n addProviderToComponent,\n addProviderToModule,\n buildRelativePath,\n findComponentFromOptions,\n findModuleFromOptions,\n getProjectPath,\n InsertChange,\n parseName,\n stringUtils,\n insertImport,\n} from '../../schematics-core';\n\ninterface AddProviderContext {\n componentStoreRelativePath: string;\n componentStoreName: string;\n}\n\nfunction createProvidingContext(\n options: Pick<ComponentStoreOptions, 'name' | 'flat' | 'path'>,\n providingPath: string\n): AddProviderContext {\n const componentStoreName = `${stringUtils.classify(`${options.name}Store`)}`;\n const componentStorePath =\n `/${options.path}/` +\n (options.flat ? '' : stringUtils.dasherize(options.name) + '/') +\n stringUtils.dasherize(options.name) +\n '.store';\n const componentStoreRelativePath = buildRelativePath(\n providingPath,\n componentStorePath\n );\n\n return {\n componentStoreRelativePath,\n componentStoreName,\n };\n}\n\n/**\n * Add component store to NgModule\n */\nexport function addComponentStoreProviderToNgModule(\n options: ComponentStoreOptions\n): Rule {\n return (host: Tree) => {\n if (!options.module) {\n return host;\n }\n\n const modulePath = options.module;\n if (!host.exists(options.module)) {\n throw new Error(`Specified module path ${modulePath} does not exist`);\n }\n\n const text = host.read(modulePath);\n if (text === null) {\n throw new SchematicsException(`File ${modulePath} does not exist.`);\n }\n const sourceText = text.toString('utf-8');\n\n const source = ts.createSourceFile(\n modulePath,\n sourceText,\n ts.ScriptTarget.Latest,\n true\n );\n\n const context = createProvidingContext(options, options.module);\n\n const componentStore = insertImport(\n source,\n modulePath,\n context.componentStoreName,\n context.componentStoreRelativePath\n );\n const [storeNgModuleProvider] = addProviderToModule(\n source,\n modulePath,\n context.componentStoreName,\n context.componentStoreRelativePath\n );\n const changes = [componentStore, storeNgModuleProvider];\n const recorder = host.beginUpdate(modulePath);\n for (const change of changes) {\n if (change instanceof InsertChange) {\n recorder.insertLeft(change.pos, change.toAdd);\n }\n }\n host.commitUpdate(recorder);\n\n return host;\n };\n}\n\n/**\n * Add component store to Component\n */\nexport function addComponentStoreProviderToComponent(\n options: ComponentStoreOptions\n): Rule {\n return (host: Tree) => {\n if (!options.component) {\n return host;\n }\n\n const componentPath = options.component;\n if (!host.exists(options.component)) {\n throw new Error(\n `Specified component path ${componentPath} does not exist`\n );\n }\n\n const text = host.read(componentPath);\n if (text === null) {\n throw new SchematicsException(`File ${componentPath} does not exist.`);\n }\n const sourceText = text.toString('utf-8');\n\n const source = ts.createSourceFile(\n componentPath,\n sourceText,\n ts.ScriptTarget.Latest,\n true\n );\n\n const context = createProvidingContext(options, options.component);\n\n const componentStore = insertImport(\n source,\n componentPath,\n context.componentStoreName,\n context.componentStoreRelativePath\n );\n const [storeNgModuleProvider] = addProviderToComponent(\n source,\n componentPath,\n context.componentStoreName,\n context.componentStoreRelativePath\n );\n const changes = [componentStore, storeNgModuleProvider];\n const recorder = host.beginUpdate(componentPath);\n for (const change of changes) {\n if (change instanceof InsertChange) {\n recorder.insertLeft(change.pos, change.toAdd);\n }\n }\n host.commitUpdate(recorder);\n\n return host;\n };\n}\n\nexport default function (options: ComponentStoreOptions): Rule {\n return (host: Tree, context: SchematicContext) => {\n options.path = getProjectPath(host, options);\n\n if (options.module) {\n options.module = findModuleFromOptions(host, options);\n }\n\n if (options.component) {\n options.component = findComponentFromOptions(host, options);\n }\n\n const parsedPath = parseName(options.path, options.name);\n options.name = parsedPath.name;\n options.path = parsedPath.path;\n\n const templateSource = apply(url('./files'), [\n options.skipTests\n ? filter((path) => !path.endsWith('.spec.ts.template'))\n : noop(),\n applyTemplates({\n ...stringUtils,\n 'if-flat': (s: string) => (options.flat ? '' : s),\n ...options,\n }),\n move(parsedPath.path),\n ]);\n\n return chain([\n branchAndMerge(\n chain([\n addComponentStoreProviderToNgModule(options),\n addComponentStoreProviderToComponent(options),\n mergeWith(templateSource),\n ])\n ),\n ])(host, context);\n };\n}\n"]} |