Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions schematics/scully/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Binary file removed schematics/scully/scullyio-init-0.0.4.tgz
Binary file not shown.
Binary file removed schematics/scully/scullyio-init-0.0.5.tgz
Binary file not shown.
Binary file removed schematics/scully/scullyio-init-0.0.7.tgz
Binary file not shown.
50 changes: 40 additions & 10 deletions schematics/scully/src/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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) => {
Expand Down
11 changes: 11 additions & 0 deletions schematics/scully/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}