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
14 changes: 8 additions & 6 deletions schematics/scully/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.3",
"version": "0.0.4",
"description": "Add scully to your angular app",
"repository": {
"type": "GIT",
Expand Down
Binary file added schematics/scully/scullyio-init-0.0.4.tgz
Binary file not shown.
13 changes: 6 additions & 7 deletions schematics/scully/src/add-post/index.ts
Original file line number Diff line number Diff line change
@@ -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`)) {
Comment thread
jorgeucano marked this conversation as resolved.
host.create(`./blog/${namD}.md`,
`---
title: ${name}
description: blog description
Expand Down
7 changes: 6 additions & 1 deletion schematics/scully/src/add-post/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
2 changes: 1 addition & 1 deletion schematics/scully/src/add-post/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface Schema {
title: boolean;
name: string;
}
68 changes: 35 additions & 33 deletions schematics/scully/src/create-blog/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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'});
Comment thread
aaronfrost marked this conversation as resolved.
host.overwrite(`/scully.config.js`, newScullyJson);
context.logger.info('✅️ Update scully.config.js');
} catch (e) {
// for test in schematics
scullyJson = `exports.config = {
Expand All @@ -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) { }
};
}

36 changes: 6 additions & 30 deletions schematics/scully/src/create-markdown/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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');

Expand All @@ -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))
]);
Expand All @@ -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);
};
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface HeroRoute extends Route {
type HeroRoutes = HeroRoute[];

const routes: HeroRoutes = [
{path: '', component: BlogComponent},
{
path: ':slug',
component: BlogComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type HeroRoutes = HeroRoute[];

const routes: HeroRoutes = [
{
path: ':id',
path: ':<%= slug %>',
component: <%= classify(name) %>Component,
}
];
Expand All @@ -20,3 +20,4 @@ const routes: HeroRoutes = [
exports: [RouterModule],
})
export class <%= classify(name) %>RoutingModule {}

28 changes: 27 additions & 1 deletion schematics/scully/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {apply, forEach, mergeWith, Rule, SchematicContext, Source, Tree} from '@angular-devkit/schematics';

interface Data {
name: string;
type: string;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
};
}