diff --git a/schematics/scully/herodevs-init-0.0.1.tgz b/schematics/scully/herodevs-init-0.0.1.tgz deleted file mode 100644 index f33c1a189..000000000 Binary files a/schematics/scully/herodevs-init-0.0.1.tgz and /dev/null differ diff --git a/schematics/scully/package-lock.json b/schematics/scully/package-lock.json index ed506794d..3469bf662 100644 --- a/schematics/scully/package-lock.json +++ b/schematics/scully/package-lock.json @@ -1,6 +1,6 @@ { - "name": "@scully/init", - "version": "0.0.2", + "name": "@scullyio/init", + "version": "0.0.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/schematics/scully/package.json b/schematics/scully/package.json index f7d64c008..87ea2948d 100644 --- a/schematics/scully/package.json +++ b/schematics/scully/package.json @@ -1,6 +1,6 @@ { "name": "@scullyio/init", - "version": "0.0.2", + "version": "0.0.3", "description": "Add scully to your angular app", "repository": { "type": "GIT", diff --git a/schematics/scully/src/add-markdown/schema.json b/schematics/scully/src/add-markdown/schema.json index d743e1051..df10f23cc 100644 --- a/schematics/scully/src/add-markdown/schema.json +++ b/schematics/scully/src/add-markdown/schema.json @@ -13,6 +13,10 @@ "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 index 995dbae30..2a8aa79f4 100644 --- a/schematics/scully/src/add-markdown/schema.ts +++ b/schematics/scully/src/add-markdown/schema.ts @@ -1,3 +1,4 @@ export interface Schema { name: string; + slug?: string; } diff --git a/schematics/scully/src/create-blog/index.ts b/schematics/scully/src/create-blog/index.ts index 7b4c169e7..5ad11d64b 100644 --- a/schematics/scully/src/create-blog/index.ts +++ b/schematics/scully/src/create-blog/index.ts @@ -1,4 +1,6 @@ import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; +import {addRouteToScullyConfig} from '../utils/utils'; +import {strings} from '@angular-devkit/core'; // @ts-ignore export default function(options: any): Rule { return (host: Tree, context: SchematicContext) => { @@ -18,14 +20,13 @@ publish: false `); context.logger.info(`✅️Blog ${fullDay}-${name} file created`); } - - context.logger.info(`start json scully`); +/* // 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 */ + /* tslint:disable:no-string-literal jsonContent.routes['/blog/:slug'] = { type: 'contentFolder', slug: { @@ -37,6 +38,26 @@ publish: false } catch (e) { context.logger.error('Cant update scully.json'); } +*/ + let scullyJson; + try { + scullyJson = (host.read('/scully.config.js')).toString(); + } 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'}); + host.overwrite(`/scully.config.js`, newScullyJson); + context.logger.info('✅️ Update scully.config.js'); + + options.path = options.path ? options.path : strings.dasherize(`./src/app/${name}`); // test schematics let path = './src/files/blog-module/'; diff --git a/schematics/scully/src/create-markdown/index.ts b/schematics/scully/src/create-markdown/index.ts index f8fd10b87..cfb543d80 100644 --- a/schematics/scully/src/create-markdown/index.ts +++ b/schematics/scully/src/create-markdown/index.ts @@ -6,6 +6,7 @@ import { import { strings, normalize } from '@angular-devkit/core'; import {Schema as MyServiceSchema} from './schema'; +import {addRouteToScullyConfig} from '../utils/utils'; export default function(options: MyServiceSchema): Rule { return (host: Tree, context: SchematicContext) => { @@ -26,24 +27,26 @@ publish: false `); context.logger.info(`✅ ${fullDay}-${name} file created`); } - context.logger.info(`start json scully`); - // add into scully config + + let scullyJson; 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[`${name}/:id`] = { - type: 'contentFolder', - id: { - folder: './${name}' - } - }; - host.overwrite(`/scully.json`, JSON.stringify(jsonContent, undefined, 2)); - context.logger.info('✅️ Update scully.json'); + scullyJson = (host.read('/scully.config.js')).toString(); } catch (e) { - context.logger.error('Cant update scully.json'); + // for test in schematics + scullyJson = `exports.config = { + projectRoot: "./src/app", + routes: { + '/demo/:id': { + type: 'fake', + numberOfPages: 100 + }, + }, +};`; } + const slug = options.slug ? options.slug : 'id'; + const newScullyJson = addRouteToScullyConfig(scullyJson, {name, 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}`); diff --git a/schematics/scully/src/create-markdown/schema.json b/schematics/scully/src/create-markdown/schema.json index 39b40485e..c5d477e6c 100644 --- a/schematics/scully/src/create-markdown/schema.json +++ b/schematics/scully/src/create-markdown/schema.json @@ -13,6 +13,10 @@ "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/create-markdown/schema.ts b/schematics/scully/src/create-markdown/schema.ts index 6a383b0d3..c62ba62a8 100644 --- a/schematics/scully/src/create-markdown/schema.ts +++ b/schematics/scully/src/create-markdown/schema.ts @@ -1,4 +1,5 @@ export interface Schema { name: string; path?: string; + slug?: string; } diff --git a/schematics/scully/src/scully/index.ts b/schematics/scully/src/scully/index.ts index 0ecc22f64..a5e74d62f 100644 --- a/schematics/scully/src/scully/index.ts +++ b/schematics/scully/src/scully/index.ts @@ -22,14 +22,13 @@ export function scully(options: any): Rule { context.logger.info('✅️ Update package.json'); // add config file - if (!tree.exists('./scully.json')) { - tree.create('./scully.json', - `{ - "projectRoot": "./src/app", - "routes": { + if (!tree.exists('./scully.config.js')) { + tree.create('./scully.config.js', + `exports.config = { + projectRoot: "./src/app", + routes: { } -} -`); +};`); } // end return diff --git a/schematics/scully/src/utils/utils.ts b/schematics/scully/src/utils/utils.ts new file mode 100644 index 000000000..5bac967b3 --- /dev/null +++ b/schematics/scully/src/utils/utils.ts @@ -0,0 +1,42 @@ +interface Data { + name: string; + type: string; + slug: string; +} + +export function addRouteToScullyConfig(scullyConfigJs: string, data: Data) { + const addRoute = `\n '/${data.name}/:${data.slug}': { + type: '${data.type}' + },`; + let output; + if (+scullyConfigJs.search(/routes: \{/g) > 0) { + const position = +scullyConfigJs.search(/routes: \{/g) + 'routes: {'.length; + output = [scullyConfigJs.slice(0, position), addRoute, scullyConfigJs.slice(position)].join(''); + } else if (+scullyConfigJs.search(/routes:\{/g) > 0) { + const position = +scullyConfigJs.search(/routes:\{/g) + 'routes:{'.length; + output = [scullyConfigJs.slice(0, position), addRoute, scullyConfigJs.slice(position)].join(''); + } else { + console.log('Scully can\'t found the scully.config.js'); + return scullyConfigJs; + } + return output; +} + +/* +function needComa(fullText: string, matchs: string[]) { + let matchers = ''; + matchs.forEach((m, i) => { + let pipe = '|'; + if (i === 0 || i === match.length) { + pipe = ''; + } + matchers += `m${pipe}`; + }); + const match = `\(([^()]*(${matchers})[^()]*)\)`; + // @ts-ignore + if (fullText.search(match).toString !== '-1') { + return ','; + } + return ''; +} +*/