|
1 | 1 | import {Rule, Tree, url, applyTemplates, move, chain, SchematicContext} from '@angular-devkit/schematics'; |
2 | 2 | import {strings, normalize} from '@angular-devkit/core'; |
3 | | -import {Schema as MyServiceSchema} from './schema'; |
| 3 | +import {Schema} from './schema'; |
| 4 | +import {Schema as PostSchema} from '../add-post/schema'; |
4 | 5 | import { |
5 | 6 | addRouteToModule, |
6 | 7 | addRouteToScullyConfig, |
7 | 8 | applyWithOverwrite, |
8 | 9 | getPrefix, |
9 | 10 | getSrc, |
| 11 | + getFileContents, |
10 | 12 | } from '../utils/utils'; |
| 13 | +import {RunSchematicTask} from '@angular-devkit/schematics/tasks'; |
11 | 14 |
|
12 | 15 | const SCULLY_CONF_FILE = '/scully.config.js'; |
13 | 16 | const ANGULAR_CONF_FILE = './angular.json'; |
14 | 17 |
|
15 | | -export default function(options: MyServiceSchema): Rule { |
16 | | - return (host: Tree, context: SchematicContext) => { |
17 | | - try { |
18 | | - const sourceDir = getSrc(host); |
19 | | - const name = options.name ? options.name : 'blog'; |
20 | | - const nameDasherized = strings.dasherize(options.name); |
21 | | - const targetDirName = options.sourceDir |
22 | | - ? strings.dasherize(options.sourceDir) // use sourceDir when provided |
23 | | - : strings.dasherize(options.name); // fall back to name when not provided |
24 | | - const date = new Date(); |
25 | | - // format yyyy-mm-dd |
26 | | - const fullDay = date.toISOString().substring(0, 10); |
27 | | - const path = `${targetDirName}/${fullDay}-${nameDasherized}.md`; |
28 | | - if (!host.exists(path)) { |
29 | | - host.create( |
30 | | - path, |
31 | | - `--- |
32 | | -title: This is the ${name} |
33 | | -description: ${name} description |
34 | | -publish: false |
35 | | ---- |
| 18 | +export default (options: Schema): Rule => { |
| 19 | + options.name = options.name || 'blog'; |
| 20 | + options.slug = options.slug || 'id'; |
| 21 | + options.sourceDir = options.sourceDir || options.name; |
| 22 | + return chain([ |
| 23 | + addPost(options, options.sourceDir), |
| 24 | + updateScullyConfig(options.sourceDir, options), |
| 25 | + addModule(options), |
| 26 | + ]); |
| 27 | +}; |
36 | 28 |
|
37 | | -# Page ${name} example |
38 | | -` |
39 | | - ); |
40 | | - context.logger.info(`✅ ${path} file created`); |
41 | | - } |
| 29 | +const addPost = (options: Schema, target: string) => (tree: Tree, context: SchematicContext) => { |
| 30 | + const nameDasherized = strings.dasherize(options.name); |
| 31 | + const targetDirName = strings.dasherize(target); |
| 32 | + const date = new Date(); |
| 33 | + // format yyyy-mm-dd |
| 34 | + const fullDay = date.toISOString().substring(0, 10); |
| 35 | + const baseFileName = `${fullDay}-${nameDasherized}`; |
| 36 | + if (!tree.exists(`${targetDirName}/${baseFileName}.md`)) { |
| 37 | + const postOptions: PostSchema = { |
| 38 | + name: baseFileName, |
| 39 | + target: targetDirName, |
| 40 | + }; |
| 41 | + context.addTask(new RunSchematicTask('post', postOptions), []); |
| 42 | + } |
| 43 | +}; |
42 | 44 |
|
43 | | - let scullyJs; |
44 | | - try { |
45 | | - scullyJs = host.read(SCULLY_CONF_FILE).toString(); |
46 | | - } catch (e) { |
47 | | - // for test in schematics |
48 | | - scullyJs = `exports.config = { |
49 | | - projectRoot: "${getSrc(host)}/app", |
50 | | - routes: { |
51 | | - }, |
52 | | -};`; |
53 | | - } |
54 | | - const newScullyJs = addRouteToScullyConfig(scullyJs, { |
55 | | - name, |
56 | | - slug: options.slug, |
57 | | - type: 'contentFolder', |
58 | | - sourceDir, |
59 | | - route: options.route, |
60 | | - }); |
61 | | - host.overwrite(SCULLY_CONF_FILE, newScullyJs); |
62 | | - context.logger.info(`✅️ Update ${SCULLY_CONF_FILE}`); |
| 45 | +const updateScullyConfig = (target: string, options: Schema) => (tree: Tree, context: SchematicContext) => { |
| 46 | + const scullyJs = getFileContents(tree, SCULLY_CONF_FILE); |
| 47 | + if (!scullyJs) { |
| 48 | + context.logger.error(`No scully configuration file found ${SCULLY_CONF_FILE}`); |
| 49 | + } |
| 50 | + const newScullyJs = addRouteToScullyConfig(scullyJs, { |
| 51 | + name: options.name, |
| 52 | + slug: options.slug, |
| 53 | + type: 'contentFolder', |
| 54 | + sourceDir: target, |
| 55 | + route: options.route, |
| 56 | + }); |
63 | 57 |
|
64 | | - const pathName = strings.dasherize(`${sourceDir}/app/${name}`); |
65 | | - let prefix = 'app'; |
66 | | - if (host.exists(ANGULAR_CONF_FILE)) { |
67 | | - prefix = getPrefix(host.read(ANGULAR_CONF_FILE).toString()); |
68 | | - addRouteToModule(host, options); |
69 | | - } |
| 58 | + tree.overwrite(SCULLY_CONF_FILE, newScullyJs); |
| 59 | + context.logger.info(`✅️ Update ${SCULLY_CONF_FILE}`); |
| 60 | +}; |
70 | 61 |
|
71 | | - const templateSource = applyWithOverwrite(url('../files/markdown-module'), [ |
72 | | - applyTemplates({ |
73 | | - classify: strings.classify, |
74 | | - dasherize: strings.dasherize, |
75 | | - name: options.name, |
76 | | - slug: options.slug, |
77 | | - prefix, |
78 | | - }), |
79 | | - move(normalize(pathName)), |
80 | | - ]); |
| 62 | +const addModule = (options: Schema) => (tree: Tree, context: SchematicContext) => { |
| 63 | + const sourceDir = getSrc(tree); |
| 64 | + const pathName = strings.dasherize(`${sourceDir}/app/${options.name}`); |
| 65 | + let prefix = 'app'; |
| 66 | + if (tree.exists(ANGULAR_CONF_FILE)) { |
| 67 | + prefix = getPrefix(getFileContents(tree, ANGULAR_CONF_FILE)); |
| 68 | + addRouteToModule(tree, options); |
| 69 | + } |
81 | 70 |
|
82 | | - return chain([templateSource]); |
83 | | - } catch (e) {} |
84 | | - }; |
85 | | -} |
| 71 | + return applyWithOverwrite(url('../files/markdown-module'), [ |
| 72 | + applyTemplates({ |
| 73 | + classify: strings.classify, |
| 74 | + dasherize: strings.dasherize, |
| 75 | + camelize: strings.camelize, |
| 76 | + name: options.name, |
| 77 | + slug: options.slug, |
| 78 | + prefix, |
| 79 | + }), |
| 80 | + move(normalize(pathName)), |
| 81 | + ]); |
| 82 | +}; |
0 commit comments