Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schematics/scully config js #11

Merged
merged 3 commits into from
Dec 13, 2019
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
Binary file removed schematics/scully/herodevs-init-0.0.1.tgz
Binary file not shown.
4 changes: 2 additions & 2 deletions 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.2",
"version": "0.0.3",
"description": "Add scully to your angular app",
"repository": {
"type": "GIT",
Expand Down
4 changes: 4 additions & 0 deletions schematics/scully/src/add-markdown/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/add-markdown/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Schema {
name: string;
slug?: string;
}
27 changes: 24 additions & 3 deletions schematics/scully/src/create-blog/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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: {
Expand All @@ -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/';
Expand Down
33 changes: 18 additions & 15 deletions schematics/scully/src/create-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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}`);

Expand Down
4 changes: 4 additions & 0 deletions schematics/scully/src/create-markdown/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/create-markdown/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Schema {
name: string;
path?: string;
slug?: string;
}
13 changes: 6 additions & 7 deletions schematics/scully/src/scully/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions schematics/scully/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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 '';
}
*/