diff --git a/schematics/scully/README.md b/schematics/scully/README.md index 3470a617f..d525e7887 100644 --- a/schematics/scully/README.md +++ b/schematics/scully/README.md @@ -16,16 +16,18 @@ npm run schematics ### Testing in a project devMode ``` -npm run copy:generate -cd schematics/scylly +cd schematics/scully npm run build npm pack -cp -r scullyio-init-0.0.*.tgz {{project_folder}} +cp -r scullyio-init-0.0.4.tgz {{project_folder}} cd {{project_folder}} -npm i --save scullyio-init-0.0.*.tgz +npm i --save scullyio-init-0.0.4.tgz ng g .\node_modules\@scullyio\init\src\collection.json:ng-add -ng serve --prod -npm run scully:generate +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 build --prod +npm run scully ``` #### Testing in a project with npm diff --git a/schematics/scully/package-lock.json b/schematics/scully/package-lock.json index 3469bf662..1ab33ec32 100644 --- a/schematics/scully/package-lock.json +++ b/schematics/scully/package-lock.json @@ -1,6 +1,6 @@ { "name": "@scullyio/init", - "version": "0.0.3", + "version": "0.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/schematics/scully/package.json b/schematics/scully/package.json index 87ea2948d..989d2d1ae 100644 --- a/schematics/scully/package.json +++ b/schematics/scully/package.json @@ -1,6 +1,6 @@ { "name": "@scullyio/init", - "version": "0.0.3", + "version": "0.0.4", "description": "Add scully to your angular app", "repository": { "type": "GIT", diff --git a/schematics/scully/scullyio-init-0.0.4.tgz b/schematics/scully/scullyio-init-0.0.4.tgz new file mode 100644 index 000000000..83abfc6da Binary files /dev/null and b/schematics/scully/scullyio-init-0.0.4.tgz differ diff --git a/schematics/scully/src/add-post/index.ts b/schematics/scully/src/add-post/index.ts index 6c4d25a02..1137eb0d5 100644 --- a/schematics/scully/src/add-post/index.ts +++ b/schematics/scully/src/add-post/index.ts @@ -1,15 +1,14 @@ -import { - Rule, SchematicContext, SchematicsException, Tree - // apply, branchAndMerge, chain, mergeWith, move, Rule, SchematicContext, SchematicsException, Tree, url, -} from '@angular-devkit/schematics'; +import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics'; import {Schema} from './schema'; +import { strings } from '@angular-devkit/core'; export default function(options: Schema): Rule { return (host: Tree, context: SchematicContext) => { - const name = options.title ? options.title : 'blog-X'; - if (!host.exists(`./blog/${name}.md`)) { - host.create(`./blog/${name}.md`, + const name = options.name ? options.name : 'blog-X'; + const namD = options.name ? strings.dasherize(options.name) : 'blog-X'; + if (!host.exists(`./blog/${namD}.md`)) { + host.create(`./blog/${namD}.md`, `--- title: ${name} description: blog description diff --git a/schematics/scully/src/add-post/schema.json b/schematics/scully/src/add-post/schema.json index cb80ac9a8..204a4aa4c 100644 --- a/schematics/scully/src/add-post/schema.json +++ b/schematics/scully/src/add-post/schema.json @@ -4,6 +4,11 @@ "id": "Scully-ng-add-blog", "title": "Scully ng-add-blog schematic", "type": "object", - "properties": { }, + "properties": { + "name": { + "type": "string", + "description": "add the title for the post" + } + }, "required": [] } diff --git a/schematics/scully/src/add-post/schema.ts b/schematics/scully/src/add-post/schema.ts index 1cd01cfdb..6b7dd9502 100644 --- a/schematics/scully/src/add-post/schema.ts +++ b/schematics/scully/src/add-post/schema.ts @@ -1,3 +1,3 @@ export interface Schema { - title: boolean; + name: string; } diff --git a/schematics/scully/src/create-blog/index.ts b/schematics/scully/src/create-blog/index.ts index 5ad11d64b..7e5e0b479 100644 --- a/schematics/scully/src/create-blog/index.ts +++ b/schematics/scully/src/create-blog/index.ts @@ -1,6 +1,6 @@ -import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; -import {addRouteToScullyConfig} from '../utils/utils'; -import {strings} from '@angular-devkit/core'; +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) => { @@ -20,28 +20,13 @@ publish: false `); context.logger.info(`✅️Blog ${fullDay}-${name} file created`); } -/* - // add into scully config - try { - const content: Buffer | null = host.read(`/scully.json`); - let jsonContent; - if (content) { jsonContent = JSON.parse(content.toString()); } - /* tslint:disable:no-string-literal - jsonContent.routes['/blog/:slug'] = { - type: 'contentFolder', - slug: { - folder: './blog' - } - }; - host.overwrite(`/scully.json`, JSON.stringify(jsonContent, undefined, 2)); - context.logger.info('✅️ Update scully.json'); - } catch (e) { - context.logger.error('Cant update scully.json'); - } -*/ + 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 = { @@ -52,29 +37,46 @@ publish: false }, }, };`; + const newScullyJson = addRouteToScullyConfig(scullyJson, {name: 'blog', slug: 'slug', type: 'contentFolder'}); + console.log(newScullyJson); + context.logger.info('✅️ Update scully.config.js'); } - const newScullyJson = addRouteToScullyConfig(scullyJson, {name: 'blog', slug: 'slug', type: 'contentFolder'}); - host.overwrite(`/scully.config.js`, newScullyJson); - context.logger.info('✅️ Update scully.config.js'); - options.path = options.path ? options.path : strings.dasherize(`./src/app/${name}`); + 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-component')) { + if (!host.getDir('./src').subdirs.find(x => x === 'add-blog')) { // prod - path = './node_modules/@herodevs/init/src/files/blog-module/'; + 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) => { - const src = `${path}${fileName}`; - const file = (host.read(src)).toString(); - host.overwrite(`./src/app/blog/${fileName}`, file); + 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) { } + +*/} catch (e) { } }; } - diff --git a/schematics/scully/src/create-markdown/index.ts b/schematics/scully/src/create-markdown/index.ts index cfb543d80..63db1808b 100644 --- a/schematics/scully/src/create-markdown/index.ts +++ b/schematics/scully/src/create-markdown/index.ts @@ -1,12 +1,7 @@ -import { - Rule, Tree, - apply, url, applyTemplates, move, - chain, mergeWith, SchematicContext, forEach, Source, -} from '@angular-devkit/schematics'; - +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} from '../utils/utils'; +import {addRouteToScullyConfig, applyWithOverwrite} from '../utils/utils'; export default function(options: MyServiceSchema): Rule { return (host: Tree, context: SchematicContext) => { @@ -43,8 +38,8 @@ publish: false }, };`; } - const slug = options.slug ? options.slug : 'id'; - const newScullyJson = addRouteToScullyConfig(scullyJson, {name, slug, type: 'contentFolder'}); + options.slug = options.slug ? options.slug : 'id'; + const newScullyJson = addRouteToScullyConfig(scullyJson, {name, slug: options.slug, type: 'contentFolder'}); host.overwrite(`/scully.config.js`, newScullyJson); context.logger.info('✅️ Update scully.config.js'); @@ -54,7 +49,8 @@ publish: false applyTemplates({ classify: strings.classify, dasherize: strings.dasherize, - name: options.name + name: options.name, + slug: options.slug }), move(normalize(options.path as string)) ]); @@ -67,23 +63,3 @@ publish: false }; } -function applyWithOverwrite(source: Source, rules: Rule[]): Rule { - return (tree: Tree, context: SchematicContext) => { - const rule = mergeWith( - apply(source, [ - ...rules, - forEach((fileEntry) => { - if (tree.exists(fileEntry.path)) { - tree.overwrite(fileEntry.path, fileEntry.content); - return null; - } - return fileEntry; - }), - - ]), - ); - - return rule(tree, context); - }; -} - diff --git a/schematics/scully/src/files/blog-module/blog-routing.module.ts b/schematics/scully/src/files/blog-module/blog-routing.module.ts index 4d6908c80..0c9fd513d 100644 --- a/schematics/scully/src/files/blog-module/blog-routing.module.ts +++ b/schematics/scully/src/files/blog-module/blog-routing.module.ts @@ -9,7 +9,6 @@ interface HeroRoute extends Route { type HeroRoutes = HeroRoute[]; const routes: HeroRoutes = [ - {path: '', component: BlogComponent}, { path: ':slug', component: BlogComponent, diff --git a/schematics/scully/src/files/markdown-module/__name@dasherize__-routing.module.ts.template b/schematics/scully/src/files/markdown-module/__name@dasherize__-routing.module.ts.template index 6e55a18e6..b24cc3191 100644 --- a/schematics/scully/src/files/markdown-module/__name@dasherize__-routing.module.ts.template +++ b/schematics/scully/src/files/markdown-module/__name@dasherize__-routing.module.ts.template @@ -10,7 +10,7 @@ type HeroRoutes = HeroRoute[]; const routes: HeroRoutes = [ { - path: ':id', + path: ':<%= slug %>', component: <%= classify(name) %>Component, } ]; @@ -20,3 +20,4 @@ const routes: HeroRoutes = [ exports: [RouterModule], }) export class <%= classify(name) %>RoutingModule {} + diff --git a/schematics/scully/src/utils/utils.ts b/schematics/scully/src/utils/utils.ts index 5bac967b3..665adcf76 100644 --- a/schematics/scully/src/utils/utils.ts +++ b/schematics/scully/src/utils/utils.ts @@ -1,3 +1,5 @@ +import {apply, forEach, mergeWith, Rule, SchematicContext, Source, Tree} from '@angular-devkit/schematics'; + interface Data { name: string; type: string; @@ -6,7 +8,10 @@ interface Data { export function addRouteToScullyConfig(scullyConfigJs: string, data: Data) { const addRoute = `\n '/${data.name}/:${data.slug}': { - type: '${data.type}' + type: '${data.type}', + ${data.slug}: { + folder: "./${data.name}" + } },`; let output; if (+scullyConfigJs.search(/routes: \{/g) > 0) { @@ -40,3 +45,24 @@ function needComa(fullText: string, matchs: string[]) { return ''; } */ + + +export function applyWithOverwrite(source: Source, rules: Rule[]): Rule { + return (tree: Tree, context: SchematicContext) => { + const rule = mergeWith( + apply(source, [ + ...rules, + forEach((fileEntry) => { + if (tree.exists(fileEntry.path)) { + tree.overwrite(fileEntry.path, fileEntry.content); + return null; + } + return fileEntry; + }), + + ]), + ); + + return rule(tree, context); + }; +}