diff --git a/schematics/scully/README.md b/schematics/scully/README.md index 6dcb35aa5..7c3a1fa4a 100644 --- a/schematics/scully/README.md +++ b/schematics/scully/README.md @@ -19,9 +19,9 @@ npm run schematics cd schematics/scully npm run build npm pack -cp -r scullyio-init-0.0.4.tgz {{project_folder}} +cp -r scullyio-init-0.0.(tab) {{project_folder}} cd {{project_folder}} -npm i --save scullyio-init-0.0.4.tgz +npm i --save scullyio-init-0.0. ng g .\node_modules\@scullyio\init\src\collection.json:ng-add ng g .\node_modules\@scullyio\init\src\collection.json:blog ng g .\node_modules\@scullyio\init\src\collection.json:post --name="This is my post" diff --git a/schematics/scully/scullyio-init-0.0.4.tgz b/schematics/scully/scullyio-init-0.0.4.tgz deleted file mode 100644 index 83abfc6da..000000000 Binary files a/schematics/scully/scullyio-init-0.0.4.tgz and /dev/null differ diff --git a/schematics/scully/scullyio-init-0.0.5.tgz b/schematics/scully/scullyio-init-0.0.5.tgz deleted file mode 100644 index f59e468d3..000000000 Binary files a/schematics/scully/scullyio-init-0.0.5.tgz and /dev/null differ diff --git a/schematics/scully/scullyio-init-0.0.7.tgz b/schematics/scully/scullyio-init-0.0.7.tgz deleted file mode 100644 index f542e91d3..000000000 Binary files a/schematics/scully/scullyio-init-0.0.7.tgz and /dev/null differ diff --git a/schematics/scully/src/ng-add/index.ts b/schematics/scully/src/ng-add/index.ts index ade4f4123..f8506eecb 100644 --- a/schematics/scully/src/ng-add/index.ts +++ b/schematics/scully/src/ng-add/index.ts @@ -1,14 +1,17 @@ -import {chain, Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; +import {chain, Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics'; import {addPackageToPackageJson} from './package-config'; import {Schema} from './schema'; import {scullyVersion, scullyComponentVersion} from './version-names'; -import {addModuleImportToRootModule, getProjectFromWorkspace, getWorkspace} from 'schematics-utilities'; import {NodePackageInstallTask, RunSchematicTask} from '@angular-devkit/schematics/tasks'; -import {getSrc} from '../utils/utils'; +import {getSourceFile, getSrc} from '../utils/utils'; +import {addImportToModule, insertImport} from '@schematics/angular/utility/ast-utils'; +import {InsertChange} from '@schematics/angular/utility/change'; +import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript'; export default (options: Schema): Rule => { return chain([ addDependencies(options), + importHttpClientModule(options), addHttpClientModule(options), addPolyfill(options), injectIdleService(options), @@ -22,14 +25,41 @@ const addDependencies = (options: Schema) => (tree: Tree, context: SchematicCont context.logger.info('✅️ Added dependency'); }; -const addHttpClientModule = (options: Schema) => (tree: Tree, context: SchematicContext) => { +const importHttpClientModule = (options: Schema) => (tree: Tree, context: SchematicContext) => { try { - const workspace = getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); - // import the httpClient we need for the plugins - addModuleImportToRootModule(tree, 'HttpClientModule', '@angular/common/http', project); - context.logger.info('✅️ Import HttpClientModule into root module'); - } catch (e) {} + const mainFilePath = `./${getSrc(tree)}/app/app.module.ts`; + const recorder = tree.beginUpdate(mainFilePath); + + const mainFileSource = getSourceFile(tree, mainFilePath); + const importChange = insertImport(mainFileSource, mainFilePath, 'HttpClientModule', '@angular/common/http') as InsertChange; + if (importChange.toAdd) { + recorder.insertLeft(importChange.pos, importChange.toAdd); + } + tree.commitUpdate(recorder); + return tree; + + } catch (e) { + console.log('error into import httpclient', e); + } +}; + +const addHttpClientModule = (options: Schema) => (tree: Tree, context: SchematicContext) => { + const mainFilePath = `./${getSrc(tree)}/app/app.module.ts`; + const text = tree.read(mainFilePath); + if (text === null) { + throw new SchematicsException(`File ${mainFilePath} does not exist.`); + } + const sourceText = text.toString(); + const source = ts.createSourceFile(mainFilePath, sourceText, ts.ScriptTarget.Latest, true); + const changes = addImportToModule(source, mainFilePath, 'HttpClientModule', '@angular/common/http'); + const recorder = tree.beginUpdate(mainFilePath); + for (const change of changes) { + if (change instanceof InsertChange) { + recorder.insertLeft(change.pos, change.toAdd); + } + } + tree.commitUpdate(recorder); + return tree; }; const addPolyfill = (options: Schema) => (tree: Tree, context: SchematicContext) => { diff --git a/schematics/scully/src/utils/utils.ts b/schematics/scully/src/utils/utils.ts index 738206802..f0bcbf52d 100644 --- a/schematics/scully/src/utils/utils.ts +++ b/schematics/scully/src/utils/utils.ts @@ -200,3 +200,14 @@ export const overwritePackageJson = ( tree.overwrite(url, JSON.stringify(content, null, 2)); return tree; }; + +export function getSourceFile(host: Tree, path: string): ts.SourceFile { + const buffer = host.read(path); + if (!buffer) { + throw new SchematicsException(`Could not find ${path}.`); + } + const content = buffer.toString(); + const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); + + return source; +}