Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
85358dd
refactor(scully): cleaned up schematic `scully`
d-koppenhagen Jan 10, 2020
c8e56d3
refactor(scully): use consistent id's and titles
d-koppenhagen Jan 10, 2020
4c900d4
refactor(scully): use context.logger instead of console.log
d-koppenhagen Jan 10, 2020
15b6f91
chore(scully): remove unused dependency chalk
d-koppenhagen Jan 10, 2020
5a75b9f
refactor(scully): replace all log with context.logger logs
d-koppenhagen Jan 10, 2020
832a433
refactor(scully): outsource yaml converter
d-koppenhagen Jan 10, 2020
b404a9a
style(scully): fix typo
d-koppenhagen Jan 10, 2020
42186ae
refactor(scully): use `post` schematic for initial post
d-koppenhagen Jan 10, 2020
9712e01
refactor(scully): use getFileContents utils function
d-koppenhagen Jan 10, 2020
7703987
refactor(scully): cleanup markdown schematic including tests
d-koppenhagen Jan 10, 2020
868e7f6
refactor(scully): use normalized paths
d-koppenhagen Jan 10, 2020
f7d49a2
fix(scully): use correct dasherized module path
d-koppenhagen Jan 10, 2020
e689242
test(scully): add more tests for markdown schematic
d-koppenhagen Jan 10, 2020
cf0ea45
refactor(scully): camelize slug and use correct defaults
d-koppenhagen Jan 11, 2020
7caeed0
fix(scully): remove unused title prop from schema
d-koppenhagen Jan 12, 2020
c4a9749
refactor(scully): remove unused options from methods
d-koppenhagen Jan 12, 2020
440f33f
fix(scully): set correct defaults calling other schematic
d-koppenhagen Jan 12, 2020
c393424
test(scully): update test for blog schematic
d-koppenhagen Jan 12, 2020
a7969b1
test(scully): test blog schematic with specific routingScope
d-koppenhagen Jan 12, 2020
5cd6481
test(scully): verify existing post won't be overridden
d-koppenhagen Jan 12, 2020
e56b6e1
test(scully): verify blog schematic is not called
d-koppenhagen Jan 12, 2020
d0fc7ab
fix(scully): set correct projectRoot src path
d-koppenhagen Jan 12, 2020
0402a3c
test(scully): test fail when no angular.json
d-koppenhagen Jan 12, 2020
4afccb2
test(scully): fix error check in test
d-koppenhagen Jan 12, 2020
3a9b5f6
test(scully): add test when appModule not exists
d-koppenhagen Jan 12, 2020
aa2455c
feat(scully): remove blog promt for ng-add
d-koppenhagen Jan 15, 2020
11dd01a
test(scully): fix test after merge
d-koppenhagen Jan 15, 2020
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@types/request": "^2.48.4",
"@types/yargs": "^13.0.3",
"asciidoctor.js": "^1.5.9",
"chalk": "^2.4.2",
"codelyzer": "^5.1.2",
"conventional-changelog": "^2.0.3",
"cz-conventional-changelog": "3.0.2",
Expand Down
Binary file added schematics/scully/scullyio-init-0.0.16.tgz
Binary file not shown.
2 changes: 2 additions & 0 deletions schematics/scully/src/add-blog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export default function(options: Schema): Rule {
const makrdownOptions: MarkownSchema = {
name: 'blog',
slug: 'slug',
sourceDir: 'blog',
route: 'blog',
};

if (options.routingScope) {
Expand Down
27 changes: 26 additions & 1 deletion schematics/scully/src/add-blog/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,41 @@ describe('add-blog schematic', () => {
appTree = await schematicRunner.runSchematicAsync('blog', defaultOptions, appTree).toPromise();
});

it('should have run the markdown schematic', () => {
it('should have run the markdown schematic with default options', () => {
expect(
schematicRunner.tasks.some(
task =>
task.name === 'run-schematic' &&
(task.options as any).name === 'create-markdown' &&
(task.options as any).options.name === 'blog' &&
(task.options as any).options.sourceDir === 'blog' &&
(task.options as any).options.route === 'blog' &&
(task.options as any).options.slug === 'slug'
)
).toBe(true);
});
});

describe('when setting an explicit `routingScope`', () => {
beforeEach(async () => {
appTree = await schematicRunner
.runSchematicAsync('blog', {...defaultOptions, routingScope: 'Root'}, appTree)
.toPromise();
});

it('should have run the markdown schematic with defaults and `routingScope`', () => {
expect(
schematicRunner.tasks.some(
task =>
task.name === 'run-schematic' &&
(task.options as any).name === 'create-markdown' &&
(task.options as any).options.name === 'blog' &&
(task.options as any).options.sourceDir === 'blog' &&
(task.options as any).options.route === 'blog' &&
(task.options as any).options.slug === 'slug' &&
(task.options as any).options.routingScope === 'Root'
)
).toBe(true);
});
});
});
4 changes: 2 additions & 2 deletions schematics/scully/src/add-blog/schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/schema",
"id": "add-blog",
"title": "Scully add component schematic",
"id": "@scullyio/init:blog",
"title": "Scully: Add a complete blog schematic",
"type": "object",
"properties": {
"routingScope": {
Expand Down
20 changes: 5 additions & 15 deletions schematics/scully/src/add-post/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {strings} from '@angular-devkit/core';
import fs = require('fs');
import yaml = require('js-yaml');

import {Schema} from './schema';
import {yamlToJson, jsonToJaml} from '../utils/utils';

export default function(options: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
Expand All @@ -19,19 +18,10 @@ export default function(options: Schema): Rule {
};

if (options.metaDataFile) {
let metaDataContents = '';
try {
metaDataContents = fs.readFileSync(options.metaDataFile, 'utf8');
} catch (e) {
throw new SchematicsException(`File ${options.metaDataFile} not found`);
}

try {
// check if yaml is valid
metaData = yaml.safeLoad(metaDataContents);
const metaDataAsJson = yamlToJson(options.metaDataFile);
if (metaDataAsJson) {
metaData = metaDataAsJson;
context.logger.info(`✅️ Meta Data File ${options.metaDataFile} successfully parsed`);
} catch (e) {
throw new SchematicsException(`${options.metaDataFile} contains no valid yaml`);
}
}

Expand All @@ -40,7 +30,7 @@ export default function(options: Schema): Rule {

if (!host.exists(filename)) {
const content = `---
${yaml.safeDump(metaData)}---
${jsonToJaml(metaData)}---

# ${name}
`;
Expand Down
17 changes: 17 additions & 0 deletions schematics/scully/src/add-post/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,21 @@ describe('add-post', () => {
expect(mdFileContent).toMatch(/language: en/g);
});
});

describe('when target file already exists', () => {
beforeEach(() => {
appTree.create(expectedFileName, 'foo');
});

it('should not touch existing file', async () => {
let error = '';
try {
await schematicRunner.runSchematicAsync('post', defaultOptions, appTree).toPromise();
} catch (e) {
error = e;
}
expect(error).toMatch(/Error: foo-bar-baz exist/g);
expect(getFileContent(appTree, expectedFileName)).toEqual('foo');
});
});
});
4 changes: 2 additions & 2 deletions schematics/scully/src/add-post/schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/schema",
"id": "Scully-ng-add-blog",
"title": "Scully ng-add-blog schematic",
"id": "@scullyio/init:post",
"title": "Scully: Add a blog post schematic",
"type": "object",
"properties": {
"name": {
Expand Down
2 changes: 1 addition & 1 deletion schematics/scully/src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"scully": {
"description": "Add scully to your angular app.",
"factory": "./scully/index#scully",
"factory": "./scully/index",
"schema": "./scully/schema.json",
"aliases": ["run"]
},
Expand Down
131 changes: 64 additions & 67 deletions schematics/scully/src/create-markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,82 @@
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 {Schema} from './schema';
import {Schema as PostSchema} from '../add-post/schema';
import {
addRouteToModule,
addRouteToScullyConfig,
applyWithOverwrite,
getPrefix,
getSrc,
getFileContents,
} from '../utils/utils';
import {RunSchematicTask} from '@angular-devkit/schematics/tasks';

const SCULLY_CONF_FILE = '/scully.config.js';
const ANGULAR_CONF_FILE = './angular.json';

export default function(options: MyServiceSchema): Rule {
return (host: Tree, context: SchematicContext) => {
try {
const sourceDir = getSrc(host);
const name = options.name ? options.name : 'blog';
const nameDasherized = strings.dasherize(options.name);
const targetDirName = options.sourceDir
? strings.dasherize(options.sourceDir) // use sourceDir when provided
: strings.dasherize(options.name); // fall back to name when not provided
const date = new Date();
// format yyyy-mm-dd
const fullDay = date.toISOString().substring(0, 10);
const path = `${targetDirName}/${fullDay}-${nameDasherized}.md`;
if (!host.exists(path)) {
host.create(
path,
`---
title: This is the ${name}
description: ${name} description
publish: false
---
export default (options: Schema): Rule => {
options.name = options.name || 'blog';
options.slug = options.slug || 'id';
options.sourceDir = options.sourceDir || options.name;
return chain([
addPost(options, options.sourceDir),
updateScullyConfig(options.sourceDir, options),
addModule(options),
]);
};

# Page ${name} example
`
);
context.logger.info(`✅ ${path} file created`);
}
const addPost = (options: Schema, target: string) => (tree: Tree, context: SchematicContext) => {
const nameDasherized = strings.dasherize(options.name);
const targetDirName = strings.dasherize(target);
const date = new Date();
// format yyyy-mm-dd
const fullDay = date.toISOString().substring(0, 10);
const baseFileName = `${fullDay}-${nameDasherized}`;
if (!tree.exists(`${targetDirName}/${baseFileName}.md`)) {
const postOptions: PostSchema = {
name: baseFileName,
target: targetDirName,
};
context.addTask(new RunSchematicTask('post', postOptions), []);
}
};

let scullyJs;
try {
scullyJs = host.read(SCULLY_CONF_FILE).toString();
} catch (e) {
// for test in schematics
scullyJs = `exports.config = {
projectRoot: "${getSrc(host)}/app",
routes: {
},
};`;
}
const newScullyJs = addRouteToScullyConfig(scullyJs, {
name,
slug: options.slug,
type: 'contentFolder',
sourceDir,
route: options.route,
});
host.overwrite(SCULLY_CONF_FILE, newScullyJs);
context.logger.info(`✅️ Update ${SCULLY_CONF_FILE}`);
const updateScullyConfig = (target: string, options: Schema) => (tree: Tree, context: SchematicContext) => {
const scullyJs = getFileContents(tree, SCULLY_CONF_FILE);
if (!scullyJs) {
context.logger.error(`No scully configuration file found ${SCULLY_CONF_FILE}`);
}
const newScullyJs = addRouteToScullyConfig(scullyJs, {
name: options.name,
slug: options.slug,
type: 'contentFolder',
sourceDir: target,
route: options.route,
});

const pathName = strings.dasherize(`${sourceDir}/app/${name}`);
let prefix = 'app';
if (host.exists(ANGULAR_CONF_FILE)) {
prefix = getPrefix(host.read(ANGULAR_CONF_FILE).toString());
addRouteToModule(host, options);
}
tree.overwrite(SCULLY_CONF_FILE, newScullyJs);
context.logger.info(`✅️ Update ${SCULLY_CONF_FILE}`);
};

const templateSource = applyWithOverwrite(url('../files/markdown-module'), [
applyTemplates({
classify: strings.classify,
dasherize: strings.dasherize,
name: options.name,
slug: options.slug,
prefix,
}),
move(normalize(pathName)),
]);
const addModule = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const sourceDir = getSrc(tree);
const pathName = strings.dasherize(`${sourceDir}/app/${options.name}`);
let prefix = 'app';
if (tree.exists(ANGULAR_CONF_FILE)) {
prefix = getPrefix(getFileContents(tree, ANGULAR_CONF_FILE));
addRouteToModule(tree, options);
}

return chain([templateSource]);
} catch (e) {}
};
}
return applyWithOverwrite(url('../files/markdown-module'), [
applyTemplates({
classify: strings.classify,
dasherize: strings.dasherize,
camelize: strings.camelize,
name: options.name,
slug: options.slug,
prefix,
}),
move(normalize(pathName)),
]);
};
Loading