UNPKG

2.71 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs');
4var _ = require('lodash');
5
6var templates = {
7 ruleSourcePath: _.template('rules/<%= ruleName %>.js'),
8 ruleDocumentationPath: _.template('docs/rules/<%= ruleName %>.md'),
9 ruleExamplesPath: _.template('examples/<%= ruleName %>.js'),
10 styleguide: _.template('[<%= name %> by <%= type %> - <%= description %>](<%= link %>)'),
11 styleguideShort: _.template('[<%= name %>](<%= link %>)'),
12 styleguideLinks: {
13 johnpapa: _.template('https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-<%= name %>')
14 }
15};
16
17var templatesDir = './scripts/templates/';
18var templateSettings = {
19 imports: {
20 formatStyleguideReference: function(styleRef) {
21 return templates.styleguide(styleguideReferenceTemplateContext(styleRef));
22 },
23 formatStyleguideReferenceListShort: function(rule) {
24 if (!rule.styleguideReferences || rule.styleguideReferences.length === 0) {
25 return '';
26 }
27 return ' (' + rule.styleguideReferences
28 .map(styleguideReferenceTemplateContext)
29 .map(templates.styleguideShort).join(', ') +
30 ')';
31 },
32 formatConfigAsJson: function(examples) {
33 var config = examples[0].displayOptions;
34 if (!config) {
35 return 2;
36 }
37 return JSON.stringify([2].concat(config));
38 },
39 formatConfigAsMarkdown: function(examples) {
40 var config = examples[0].displayOptions;
41 if (!config) {
42 return '';
43 }
44 return '`' + config.map(JSON.stringify).join('` and `') + '`';
45 },
46 indent: function(content, indentation) {
47 var spaces = new Array(indentation + 1).join(' ');
48 return content.replace(/\n/g, '\n' + spaces);
49 }
50 }
51};
52
53fs.readdirSync(templatesDir).forEach(function(templateFilename) {
54 var templateName = templateFilename.split('.')[0];
55 if (templates[templateName] !== undefined) {
56 throw new Error('Can not create from template "' + templateFilename + '" because template key "' +
57 templateName + '" already exists.');
58 }
59
60 templates[templateName] = _.template(fs.readFileSync(templatesDir + templateFilename), templateSettings);
61});
62
63module.exports = templates;
64
65function styleguideReferenceTemplateContext(styleRef) {
66 var linkTemplate = templates.styleguideLinks[styleRef.type];
67 if (!linkTemplate) {
68 throw new Error('No styleguide link template for styleguide type: "' + styleRef.type);
69 }
70 return _.extend({
71 link: linkTemplate(styleRef)
72 }, styleRef);
73}