-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(scully): pick commits for prompts picked commits from #100 * feat(scully): add options for @scullyio/init:markdown * fix(scully): fix x-prompt description * fix(scully): use yyyy-mm-dd format * feat(scully): provide options for @scullyio/init:post * fix(scully): fix filename for post * fix(scully): remove defaults from `route` and `sourceDir` * docs(scully): update options * refactor(scully): split into smaller chainable Rules * test(scully): add tests for ng-add schematic * test(scully): add more schematic tests * feat(scully): add `metaDataFile` option and tests * test(scully): add test for using `target` option * fix(scully): add correct Schema infos for add-blog * test(scully): fix test
- Loading branch information
1 parent
1708e5c
commit 9a9d4ea
Showing
24 changed files
with
700 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
title: override-me | ||
thumbnail: assets/images/default.jpg | ||
author: John Doe | ||
mail: John.Doe@example.com | ||
keywords: | ||
- angular | ||
- scully | ||
language: en |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; | ||
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; | ||
import {RunSchematicTask} from '@angular-devkit/schematics/tasks'; | ||
import {Schema} from './schema'; | ||
import {Schema as MarkownSchema} from '../create-markdown/schema'; | ||
|
||
export default function(options: any): Rule { | ||
export default function(options: Schema): Rule { | ||
return (tree: Tree, context: SchematicContext) => { | ||
options.name = 'blog'; | ||
options.slug = 'slug'; | ||
context.addTask(new RunSchematicTask('create-markdown', options), []); | ||
const makrdownOptions: MarkownSchema = { | ||
name: 'blog', | ||
slug: 'slug', | ||
}; | ||
|
||
if (options.routingScope) { | ||
makrdownOptions.routingScope = options.routingScope; | ||
} | ||
context.addTask(new RunSchematicTask('create-markdown', makrdownOptions), []); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Schema { | ||
/** | ||
* The scope for the new routing module. | ||
*/ | ||
routingScope?: 'Child' | 'Root'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,54 @@ | ||
import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics'; | ||
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 { strings } from '@angular-devkit/core'; | ||
|
||
export default function(options: Schema): Rule { | ||
return (host: Tree, context: SchematicContext) => { | ||
const name = options.name; | ||
const nameDasherized = options.name ? strings.dasherize(options.name) : 'blog-X'; | ||
const targetDasherized = options.target ? strings.dasherize(options.target) : 'blog'; | ||
const filename = `./${targetDasherized}/${nameDasherized}.md`; | ||
|
||
let metaData = { | ||
title: '', | ||
description: 'blog description', | ||
publish: false, | ||
}; | ||
|
||
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 | ||
publish: false | ||
--- | ||
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); | ||
context.logger.info(`✅️ Meta Data File ${options.metaDataFile} successfully parsed`); | ||
} catch (e) { | ||
throw new SchematicsException(`${options.metaDataFile} contains no valid yaml`); | ||
} | ||
} | ||
|
||
// set title from option and override if alreay in metaDataFile template | ||
metaData.title = name; | ||
|
||
if (!host.exists(filename)) { | ||
const content = `--- | ||
${yaml.safeDump(metaData)}--- | ||
# ${name} | ||
`); | ||
context.logger.info(`✅️Blog ${name} file created`); | ||
`; | ||
host.create(filename, content); | ||
context.logger.info(`✅️ Blog ${filename} file created`); | ||
} else { | ||
// return name exist | ||
throw new SchematicsException(`${name} exist in your blog folder`); | ||
throw new SchematicsException(`${nameDasherized} exist in your ${targetDasherized} folder`); | ||
} | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {HostTree} from '@angular-devkit/schematics'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {getFileContent} from '@schematics/angular/utility/test'; | ||
import * as path from 'path'; | ||
|
||
import {setupProject} from '../utils/test-utils'; | ||
import {Schema} from './schema'; | ||
|
||
const collectionPath = path.join(__dirname, '../collection.json'); | ||
const META_DATA_TEMPLATE_PATH = 'assets/meta-data-template.yml'; | ||
|
||
describe('add-post', () => { | ||
const schematicRunner = new SchematicTestRunner('scully-schematics', collectionPath); | ||
const project = 'foo'; | ||
const defaultOptions: Schema = { | ||
name: 'Foo barBaz', | ||
}; | ||
let appTree: UnitTestTree; | ||
const expectedFileName = '/blog/foo-bar-baz.md'; | ||
|
||
beforeEach(async () => { | ||
appTree = new UnitTestTree(new HostTree()); | ||
appTree = await setupProject(appTree, schematicRunner, project); | ||
}); | ||
|
||
describe('when using the default options', () => { | ||
beforeEach(async () => { | ||
appTree = await schematicRunner.runSchematicAsync('post', defaultOptions, appTree).toPromise(); | ||
}); | ||
|
||
it('should create a new dasherized post', () => { | ||
expect(appTree.files).toContain(expectedFileName); | ||
const mdFileContent = getFileContent(appTree, expectedFileName); | ||
expect(mdFileContent).toMatch(/title: Foo barBaz/g); | ||
expect(mdFileContent).toMatch(/description: blog description/g); | ||
expect(mdFileContent).toMatch(/publish: false/g); | ||
}); | ||
}); | ||
|
||
describe('when using a different `target`', () => { | ||
beforeEach(async () => { | ||
appTree = await schematicRunner | ||
.runSchematicAsync('post', {...defaultOptions, target: 'foo/bar'}, appTree) | ||
.toPromise(); | ||
}); | ||
|
||
it('should create a new dasherized post inside the target dir', () => { | ||
const expected = '/foo/bar/foo-bar-baz.md'; | ||
expect(appTree.files).toContain(expected); | ||
const mdFileContent = getFileContent(appTree, expected); | ||
expect(mdFileContent).toMatch(/title: Foo barBaz/g); | ||
expect(mdFileContent).toMatch(/description: blog description/g); | ||
expect(mdFileContent).toMatch(/publish: false/g); | ||
}); | ||
}); | ||
|
||
describe('when using `metaDataFile` option', () => { | ||
beforeEach(async () => { | ||
appTree = await schematicRunner | ||
.runSchematicAsync('post', {...defaultOptions, metaDataFile: META_DATA_TEMPLATE_PATH}, appTree) | ||
.toPromise(); | ||
}); | ||
|
||
it('should add the meta data but keep title from options', () => { | ||
expect(appTree.files).toContain(expectedFileName); | ||
const mdFileContent = getFileContent(appTree, expectedFileName); | ||
expect(mdFileContent).toMatch(/title: Foo barBaz/g); | ||
expect(mdFileContent).toMatch(/thumbnail: assets\/images\/default\.jpg/g); | ||
expect(mdFileContent).toMatch(/author: John Doe/g); | ||
expect(mdFileContent).toMatch(/mail: John.Doe@example.com/g); | ||
expect(mdFileContent).toMatch(/keywords:\s+-\ angular\s+-\ scully/s); | ||
expect(mdFileContent).toMatch(/language: en/g); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
/** | ||
* Scully ng-add-blog schematic | ||
*/ | ||
export interface Schema { | ||
name: string; | ||
/** | ||
* add the title for the post | ||
*/ | ||
name: string; | ||
/** | ||
* define the target directory for the new post file | ||
*/ | ||
target?: string; | ||
/** | ||
* use a meta data template file that's data will be added to the post | ||
*/ | ||
metaDataFile?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.