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
1 change: 1 addition & 0 deletions schematics/scully/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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"
ng g .\node_modules\@scullyio\init\src\collection.json:markdown --name=test --slug=idid
ng g .\node_modules\@scullyio\init\src\collection.json:c-markdown --name=test --slug=idid
ng build --prod
npm run scully
```
Expand Down
2 changes: 1 addition & 1 deletion schematics/scully/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion schematics/scully/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scullyio/init",
"version": "0.0.6",
"version": "0.0.7",
"description": "Add scully to your angular app",
"repository": {
"type": "GIT",
Expand Down
Binary file added schematics/scully/scullyio-init-0.0.7.tgz
Binary file not shown.
18 changes: 4 additions & 14 deletions schematics/scully/src/add-blog/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { Rule, SchematicContext, Tree, chain, externalSchematic } from '@angular-devkit/schematics';
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import {RunSchematicTask} from '@angular-devkit/schematics/tasks';

export default function(options: any): Rule {
return (tree: Tree, context: SchematicContext) => {
return chain([
externalSchematic('@schematics/angular', 'module', {
name: 'blog',
route: 'blog',
routing: true,
module: 'app.module.ts'
}),
// tslint:disable-next-line:no-shadowed-variable
(tree: Tree, context: SchematicContext) => {
context.addTask(new RunSchematicTask('create-blog', options), []);
},
]);

options.name = 'blog';
options.slug = 'slug';
context.addTask(new RunSchematicTask('create-markdown', options), []);
};
}
22 changes: 0 additions & 22 deletions schematics/scully/src/add-markdown/index.ts

This file was deleted.

23 changes: 0 additions & 23 deletions schematics/scully/src/add-markdown/schema.json

This file was deleted.

4 changes: 0 additions & 4 deletions schematics/scully/src/add-markdown/schema.ts

This file was deleted.

13 changes: 1 addition & 12 deletions schematics/scully/src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,17 @@
"schema": "./add-blog/schema.json",
"aliases": ["blog"]
},
"create-blog": {
"description": "Add scully component to your angular app.",
"factory": "./create-blog/index",
"schema": "./create-blog/schema.json"
},
"add-post": {
"description": "Add new blog input",
"factory": "./add-post/index",
"schema": "./add-post/schema.json",
"aliases": ["post"]
},
"add-markdown": {
"description": "Add new markdown folder and module",
"factory": "./add-markdown/index",
"schema": "./add-markdown/schema.json",
"aliases": ["markdown"]
},
"create-markdown": {
"description": "Add create markdown folder and module",
"factory": "./create-markdown/index",
"schema": "./create-markdown/schema.json",
"aliases": ["c-markdown"]
"aliases": ["c-markdown", "markdown", "md"]
}
}
}
82 changes: 0 additions & 82 deletions schematics/scully/src/create-blog/index.ts

This file was deleted.

15 changes: 0 additions & 15 deletions schematics/scully/src/create-blog/schema.json

This file was deleted.

10 changes: 8 additions & 2 deletions schematics/scully/src/create-markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Rule, Tree, url, applyTemplates, move, chain, SchematicContext } from '@angular-devkit/schematics';
import { strings, normalize } from '@angular-devkit/core';
import {Schema as MyServiceSchema} from './schema';
import {addRouteToScullyConfig, applyWithOverwrite} from '../utils/utils';
import {addRouteToModule, addRouteToScullyConfig, applyWithOverwrite, getPrefix} from '../utils/utils';

export default function(options: MyServiceSchema): Rule {
return (host: Tree, context: SchematicContext) => {
Expand Down Expand Up @@ -45,13 +45,19 @@ publish: false
context.logger.info('✅️ Update scully.config.js');

options.path = options.path ? options.path : strings.dasherize(`./src/app/${name}`);
let prefix = 'app';
if (host.exists('./angular.json')) {
prefix = getPrefix(host.read('./angular.json').toString());
addRouteToModule(host, options);
}

const templateSource = applyWithOverwrite(url('../files/markdown-module'), [
applyTemplates({
classify: strings.classify,
dasherize: strings.dasherize,
name: options.name,
slug: options.slug
slug: options.slug,
prefix
}),
move(normalize(options.path as string))
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ActivatedRoute, Router, ROUTES} from '@angular/router';
declare var ng: any;

@Component({
selector: 'app-<%= dasherize(name) %>',
selector: '<%= prefix %>-<%= dasherize(name) %>',
templateUrl: './<%= dasherize(name) %>.component.html',
styleUrls: ['./<%= dasherize(name) %>.component.css'],
preserveWhitespaces: true,
Expand Down
66 changes: 64 additions & 2 deletions schematics/scully/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {apply, forEach, mergeWith, Rule, SchematicContext, Source, Tree} from '@angular-devkit/schematics';
import {strings} from '@angular-devkit/core';
import {normalize, strings} from '@angular-devkit/core';

import { buildRelativePath } from '@schematics/angular/utility/find-module';
import { addRouteDeclarationToModule } from '@schematics/angular/utility/ast-utils';
import ts = require('@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript');
import {InsertChange} from '@schematics/angular/utility/change';
import {ModuleOptions} from '@schematics/angular/utility/find-module';

interface Data {
name: string;
Expand Down Expand Up @@ -63,7 +69,63 @@ export function applyWithOverwrite(source: Source, rules: Rule[]): Rule {

]),
);

return rule(tree, context);
};
}


export function getPrefix(angularjson: string) {
const angularJSON = JSON.parse(angularjson);
const prefixs = [];
// tslint:disable-next-line:forin
for (const project in angularJSON.projects) {
prefixs.push({project, prefix: angularJSON.projects[project].prefix});
}
if (prefixs.length > 1) {
// TODO: ask for prefix we need
return prefixs[0].prefix;
} else if (prefixs.length === 1) {
return prefixs[0].prefix;
}
}

export function addRouteToModule(host: Tree, options: any) {

let path = './src/app/app-routing.module.ts';
if (!host.exists(path)) {
path = './src/app/app.module.ts';
}
const text = host.read(path);
if (!text) {
throw new Error(`Couldn't find the module nor its routing module.`);
}

const sourceText = text.toString();
const addDeclaration = addRouteDeclarationToModule(
ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true),
path,
buildRoute(options, 'app.module'),
) as InsertChange;

const recorder = host.beginUpdate(path);
recorder.insertLeft(addDeclaration.pos, addDeclaration.toAdd);
host.commitUpdate(recorder);

}

function buildRoute(options: ModuleOptions, modulePath: string) {
const relativeModulePath = buildRelativeModulePath(options, modulePath);
const moduleName = `${strings.classify(options.name)}Module`;
const loadChildren = `() => import('${relativeModulePath}').then(m => m.${moduleName})`;
return `{ path: '${options.name}', loadChildren: ${loadChildren} }`;
}

function buildRelativeModulePath(options: ModuleOptions, modulePath: string): string {
// tslint:disable-next-line:no-shadowed-variable
const importModulePath = normalize(`/${options.name}/`
+ strings.dasherize(options.name)
+ '.module',
);

return buildRelativePath(modulePath, importModulePath);
}