diff --git a/schematics/scully/README.md b/schematics/scully/README.md index 852b459b1..e471c3575 100644 --- a/schematics/scully/README.md +++ b/schematics/scully/README.md @@ -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 ``` diff --git a/schematics/scully/package-lock.json b/schematics/scully/package-lock.json index 42a66f9ac..4be72152d 100644 --- a/schematics/scully/package-lock.json +++ b/schematics/scully/package-lock.json @@ -1,6 +1,6 @@ { "name": "@scullyio/init", - "version": "0.0.6", + "version": "0.0.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/schematics/scully/package.json b/schematics/scully/package.json index fd9bee929..1633f64dc 100644 --- a/schematics/scully/package.json +++ b/schematics/scully/package.json @@ -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", diff --git a/schematics/scully/scullyio-init-0.0.7.tgz b/schematics/scully/scullyio-init-0.0.7.tgz new file mode 100644 index 000000000..71b69a74f Binary files /dev/null and b/schematics/scully/scullyio-init-0.0.7.tgz differ diff --git a/schematics/scully/src/add-blog/index.ts b/schematics/scully/src/add-blog/index.ts index 9f48e99c9..9dcc340d7 100644 --- a/schematics/scully/src/add-blog/index.ts +++ b/schematics/scully/src/add-blog/index.ts @@ -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), []); }; } diff --git a/schematics/scully/src/add-markdown/index.ts b/schematics/scully/src/add-markdown/index.ts deleted file mode 100644 index 720b4dc2a..000000000 --- a/schematics/scully/src/add-markdown/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { - Rule, Tree, SchematicContext, externalSchematic, chain, -} from '@angular-devkit/schematics'; -import {RunSchematicTask} from '@angular-devkit/schematics/tasks'; -import { Schema as MyServiceSchema } from './schema'; -// @ts-ignore -export default function(options: MyServiceSchema): Rule { - return (host: Tree, context: SchematicContext) => { - return chain([ - externalSchematic('@schematics/angular', 'module', { - name: options.name, - route: options.name, - routing: true, - module: 'app.module.ts' - }), - // tslint:disable-next-line:no-shadowed-variable - (host: Tree, context: SchematicContext) => { - context.addTask(new RunSchematicTask('create-markdown', options), []); - }]); - }; -} - diff --git a/schematics/scully/src/add-markdown/schema.json b/schematics/scully/src/add-markdown/schema.json deleted file mode 100644 index df10f23cc..000000000 --- a/schematics/scully/src/add-markdown/schema.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "$schema": "http://json-schema.org/schema", - "id": "add-markdown", - "title": "Scully add markdown schematic", - "type": "object", - "properties": { - "routingScope": { - "enum": ["Child", "Root"], - "type": "string", - "description": "The scope for the new routing module.", - "default": "Child" - }, - "name": { - "type": "string", - "description": "add the name for the folder and module" - }, - "slug": { - "type": "string", - "description": "add the name for the :${slug}" - } - }, - "required": [] -} diff --git a/schematics/scully/src/add-markdown/schema.ts b/schematics/scully/src/add-markdown/schema.ts deleted file mode 100644 index 2a8aa79f4..000000000 --- a/schematics/scully/src/add-markdown/schema.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface Schema { - name: string; - slug?: string; -} diff --git a/schematics/scully/src/collection.json b/schematics/scully/src/collection.json index 2fdc500c8..73c41df6c 100644 --- a/schematics/scully/src/collection.json +++ b/schematics/scully/src/collection.json @@ -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"] } } } diff --git a/schematics/scully/src/create-blog/index.ts b/schematics/scully/src/create-blog/index.ts deleted file mode 100644 index 7e5e0b479..000000000 --- a/schematics/scully/src/create-blog/index.ts +++ /dev/null @@ -1,82 +0,0 @@ -import {applyTemplates, chain, move, Rule, SchematicContext, Tree, url} from '@angular-devkit/schematics'; -import {addRouteToScullyConfig, applyWithOverwrite} from '../utils/utils'; -import {normalize, strings} from '@angular-devkit/core'; -// @ts-ignore -export default function(options: any): Rule { - return (host: Tree, context: SchematicContext) => { - try { - const name = options.title ? options.title : 'blog-X'; - const date = new Date(); - const fullDay = `${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear()}`; - if (!host.exists(`./blog/${fullDay}-${name}.md`)) { - host.create(`./blog/${fullDay}-${name}.md`, - `--- -title: This is the blog title -description: blog description -publish: false ---- - -# Page X example -`); - context.logger.info(`✅️Blog ${fullDay}-${name} file created`); - } - - let scullyJson; - try { - scullyJson = (host.read('/scully.config.js')).toString(); - const newScullyJson = addRouteToScullyConfig(scullyJson, {name: 'blog', slug: 'slug', type: 'contentFolder'}); - host.overwrite(`/scully.config.js`, newScullyJson); - context.logger.info('✅️ Update scully.config.js'); - } catch (e) { - // for test in schematics - scullyJson = `exports.config = { - routes: { - '/demo/:id': { - type: 'fake', - numberOfPages: 100 - }, - }, -};`; - const newScullyJson = addRouteToScullyConfig(scullyJson, {name: 'blog', slug: 'slug', type: 'contentFolder'}); - console.log(newScullyJson); - context.logger.info('✅️ Update scully.config.js'); - } - - const templateSource = applyWithOverwrite(url('../files/blog-module'), [ - applyTemplates({ - classify: strings.classify, - dasherize: strings.dasherize, - name: options.name, - slug: 'slug' - }), - move(normalize('./src/app/blog/')) - ]); - - return chain([ - templateSource - ]); - -/* - // test schematics - let path = './src/files/blog-module/'; - if (!host.getDir('./src').subdirs.find(x => x === 'add-blog')) { - // prod - path = './node_modules/@scullyio/init/src/files/blog-module/'; - } - - // create blog module and files - const files = host.getDir(path).subfiles; - // read file and create - files.forEach((fileName) => { - try { - const src = `${path}${fileName}`; - const file = (host.read(src)).toString(); - host.overwrite(`./src/app/blog/${fileName}`, file); - } catch (e) { - console.log(`The file ${fileName} does not exist. If you are testing schematics, this is ok.`); - } - }); - -*/} catch (e) { } - }; -} diff --git a/schematics/scully/src/create-blog/schema.json b/schematics/scully/src/create-blog/schema.json deleted file mode 100644 index 916d0fe43..000000000 --- a/schematics/scully/src/create-blog/schema.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "$schema": "http://json-schema.org/schema", - "id": "add-component", - "title": "Scully add component schematic", - "type": "object", - "properties": { - "routingScope": { - "enum": ["Child", "Root"], - "type": "string", - "description": "The scope for the new routing module.", - "default": "Child" - } - }, - "required": [] -} diff --git a/schematics/scully/src/create-markdown/index.ts b/schematics/scully/src/create-markdown/index.ts index 633be8357..afbb500d0 100644 --- a/schematics/scully/src/create-markdown/index.ts +++ b/schematics/scully/src/create-markdown/index.ts @@ -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) => { @@ -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)) ]); diff --git a/schematics/scully/src/files/markdown-module/__name@dasherize__.component.ts.template b/schematics/scully/src/files/markdown-module/__name@dasherize__.component.ts.template index 7fa770c4c..b285236b9 100644 --- a/schematics/scully/src/files/markdown-module/__name@dasherize__.component.ts.template +++ b/schematics/scully/src/files/markdown-module/__name@dasherize__.component.ts.template @@ -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, diff --git a/schematics/scully/src/utils/utils.ts b/schematics/scully/src/utils/utils.ts index e7469a4f1..bd5648c46 100644 --- a/schematics/scully/src/utils/utils.ts +++ b/schematics/scully/src/utils/utils.ts @@ -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; @@ -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); +}