1 | 'use strict';
|
2 |
|
3 | const
|
4 | program = require('commander'),
|
5 | exit = require('exit'),
|
6 | fs = require('fs'),
|
7 | path = require('path'),
|
8 | isutf8 = require('isutf8'),
|
9 | lint = require('./lint'),
|
10 | printError = require('./printError'),
|
11 | Typograf = require('typograf'),
|
12 | defaultConfig = require('../typograf.json'),
|
13 | DEFAULT_USER_CONFIG = '.typograf.json';
|
14 |
|
15 | function processText(text, prefs) {
|
16 | const isJSON = path.extname(prefs.filename.toLowerCase()) === '.json';
|
17 | const typograf = new Typograf(prefs);
|
18 |
|
19 | if (prefs.lint) {
|
20 | !isJSON && lint.process(text, prefs);
|
21 | } else {
|
22 | if (isJSON) {
|
23 | processJSON(text, prefs);
|
24 | } else {
|
25 | process.stdout.write(typograf.execute(text));
|
26 | }
|
27 | }
|
28 | }
|
29 |
|
30 | function processJSON(text, prefs) {
|
31 | let json;
|
32 | try {
|
33 | json = JSON.parse(text);
|
34 | } catch(e) {
|
35 | printError(`${prefs.filename}: error parsing.`);
|
36 | exit(1);
|
37 | }
|
38 |
|
39 | const typograf = new Typograf(prefs);
|
40 | const result = JSON.stringify(json, (key, value) => {
|
41 | let needTypography = true;
|
42 |
|
43 | if (typeof value === 'string') {
|
44 | if (program.onlyJsonKeys && program.onlyJsonKeys.indexOf(key) === -1) {
|
45 | needTypography = false;
|
46 | }
|
47 |
|
48 | if (program.ignoreJsonKeys && program.ignoreJsonKeys.indexOf(key) > -1) {
|
49 | needTypography = false;
|
50 | }
|
51 |
|
52 | if (needTypography) {
|
53 | value = typograf.execute(value);
|
54 | }
|
55 | }
|
56 |
|
57 | return value;
|
58 | }, 2);
|
59 |
|
60 | process.stdout.write(result);
|
61 | }
|
62 |
|
63 | module.exports = {
|
64 | getDefaultConfigAsText() {
|
65 | return JSON.stringify(defaultConfig, ' ', 4);
|
66 | },
|
67 |
|
68 | getConfig(file) {
|
69 | let showError = true;
|
70 |
|
71 | if (!file) {
|
72 | file = DEFAULT_USER_CONFIG;
|
73 | showError = false;
|
74 | }
|
75 |
|
76 | if (fs.existsSync(file) && fs.statSync(file).isFile()) {
|
77 | const text = fs.readFileSync(file, 'utf8');
|
78 | let config;
|
79 | try {
|
80 | config = JSON.parse(text);
|
81 | } catch(e) {
|
82 | printError(`${file}: error parsing.`);
|
83 | return null;
|
84 | }
|
85 |
|
86 | return config;
|
87 | } else if (showError) {
|
88 | printError(`${file}: no such file.`);
|
89 | }
|
90 |
|
91 | return null;
|
92 | },
|
93 |
|
94 | getPrefs(program, config) {
|
95 | const prefs = {
|
96 | lint: program.lint,
|
97 | locale: [],
|
98 | htmlEntity: {}
|
99 | };
|
100 |
|
101 | for (const key of ['enableRule', 'disableRule', 'locale']) {
|
102 | if (typeof program[key] !== 'undefined') {
|
103 | prefs[key] = program[key];
|
104 | }
|
105 |
|
106 | if (config && typeof config[key] !== 'undefined') {
|
107 | prefs[key] = config[key];
|
108 | }
|
109 | }
|
110 |
|
111 | if (typeof program.htmlEntityType !== 'undefined') {
|
112 | prefs.htmlEntity.type = program.htmlEntityType;
|
113 | }
|
114 |
|
115 | if (typeof program.htmlEntityOnlyVisible !== 'undefined') {
|
116 | prefs.htmlEntity.onlyVisible = program.htmlEntityOnlyVisible;
|
117 | }
|
118 |
|
119 | if (config && config.htmlEntity) {
|
120 | prefs.htmlEntity = Object.assign(prefs.htmlEntity, config.htmlEntity);
|
121 | }
|
122 |
|
123 | return prefs;
|
124 | },
|
125 |
|
126 | processStdin(prefs, callback) {
|
127 | let text = '';
|
128 |
|
129 | process.stdin
|
130 | .setEncoding('utf8')
|
131 | .on('readable', () => {
|
132 | const chunk = process.stdin.read();
|
133 | if (chunk !== null) {
|
134 | text += chunk;
|
135 | }
|
136 | })
|
137 | .on('end', () => {
|
138 | processText(text, prefs);
|
139 | callback();
|
140 | });
|
141 | },
|
142 |
|
143 | processFile(prefs, callback) {
|
144 | const file = prefs.filename;
|
145 |
|
146 | if (fs.existsSync(file) && fs.statSync(file).isFile()) {
|
147 | const text = fs.readFileSync(file);
|
148 | if (isutf8(text)) {
|
149 | processText(text.toString(), prefs);
|
150 | } else {
|
151 | callback(true, `${file}: is not UTF-8.`);
|
152 | }
|
153 | } else {
|
154 | callback(true, `${file}: no such file.`);
|
155 | }
|
156 |
|
157 | callback(false);
|
158 | }
|
159 | };
|