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
11 changes: 6 additions & 5 deletions docs/blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ ng generate @scullyio/init:post --name="This is my post"

The following table shows all available options:

| option | description | default |
| -------------- | ------------------------------------------------------ | --------- |
| `name` | Define the name for the created post | 'blog-X' |
| `target` | Define the target directory for the new post file | 'blog' |
| `metaDataFile` | Use a meta data yaml template from a file for the post | undefined |
| option | description | default |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------- |
| `name` | Define the name for the created post | 'blog-X' |
| `target` | Define the target directory for the new post file | 'blog' |
| `metaDataFile` | Use a meta data yaml template from a file for the post | undefined |
| `extension` | define the file extension for the target file.<br>Out of the box Scully supports `adoc` and `md`.<br>You can handle other files by setting up your own [_File Handle Plugin_](plugins.md#file-plugin). | 'md' |
10 changes: 9 additions & 1 deletion libs/scully-schematics/src/add-post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ import { yamlToJson, jsonToJaml, removeWrongCharacters } from '../utils/utils';
export default function(options: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
const name = options.name;
const extension = options.extension || 'md';
const nameDasherized = options.name
? strings.dasherize(options.name)
: 'blog-X';
const targetDasherized = options.target
? strings.dasherize(options.target)
: 'blog';

if (!/^[\w]+$/.test(extension)) {
throw new SchematicsException(
`${extension} is not a valid file extension`
);
}

const filename = `./${targetDasherized}/${removeWrongCharacters(
nameDasherized
)}.md`;
)}.${extension}`;

let metaData = {
title: '',
Expand Down
37 changes: 37 additions & 0 deletions libs/scully-schematics/src/add-post/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,41 @@ describe('add-post', () => {
expect(getFileContent(appTree, defaultExpectedFileName)).toEqual('foo');
});
});

describe('when specify a file extension', () => {
it('should use the specified file extension', async () => {
const extension = 'adoc';
const options = { ...defaultOptions, extension };
appTree = await customRunner
.runSchematicAsync('post', options, appTree)
.toPromise();
expect(appTree.files).toContain('/blog/foo-bar-baz.adoc');
});

it('should use `md` file extension by default', async () => {
const extension = '';
const options = { ...defaultOptions, extension };
appTree = await customRunner
.runSchematicAsync('post', options, appTree)
.toPromise();
expect(appTree.files).toContain('/blog/foo-bar-baz.md');
});

it('should throw an error when file extension is invalid', async () => {
const extension = 'invalid?ext';
const options = { ...defaultOptions, extension };
let error = '';
try {
appTree = await customRunner
.runSchematicAsync('post', options, appTree)
.toPromise();
} catch (e) {
error = e;
}
expect(appTree.files).toContain('/blog/foo-bar-baz.adoc');
expect(error).toMatch(
/Error: invalid\?ext is not a valid file extension/g
);
});
});
});
5 changes: 5 additions & 0 deletions libs/scully-schematics/src/add-post/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"x-prompt": "What's the target folder for this post?",
"default": "blog"
},
"extension": {
"type": "string",
"description": "define the file extension for the target file",
"default": "md"
},
"metaDataFile": {
"type": "string",
"description": "use a meta data template file that's data will be added to the post",
Expand Down
4 changes: 4 additions & 0 deletions libs/scully-schematics/src/add-post/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export interface Schema {
* define the target directory for the new post file
*/
target?: string;
/**
* define the file extension for the target file
*/
extension?: string;
/**
* use a meta data template file that's data will be added to the post
*/
Expand Down