-
Notifications
You must be signed in to change notification settings - Fork 14
/
generateDocs.js
86 lines (67 loc) · 2.41 KB
/
generateDocs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* eslint import/no-extraneous-dependencies: "off" */
/*
* Generate cspace-ui documentation.
*/
const fs = require('fs');
const glob = require('glob');
const jsonFile = require('jsonfile');
/**
* Remove newlines from multiline strings. These are assumed to be backtick quoted HTML messages,
* where the newlines exist for source formatting, but don't matter for rendering. Since the
* message documentation will be output as JSON, with double quoted strings, the resulting "\n"
* escapes in the messages would make them less understandable.
*/
const cleanMessage = (message) => message.replace(/\n\s*/g, ' ');
/**
* Generate documentation for react-intl messages. This must be done after the npm build script has
* executed, so that messages will have been extracted to build/messages.
*/
const generateMessagesDoc = () => {
const messages = [];
glob.sync('build/messages/**/*.json').forEach((filename) => {
jsonFile.readFileSync(filename).forEach((message) => {
messages.push(message);
});
});
messages.sort((messageA, messageB) => {
const a = messageA.id.toLowerCase();
const b = messageB.id.toLowerCase();
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
});
const docFile = fs.createWriteStream('docs/configuration/messages.js');
docFile.write('/*\n');
docFile.write(' * This file contains all messages used in cspace-ui, to be used as a reference for customization\n');
docFile.write(' * or translation. The default export is an object containing the default messages in the\n');
docFile.write(' * application, keyed by message ID. Messages may be customized by supplying overrides via the\n');
docFile.write(' * messages configuration property.\n');
docFile.write(' *\n');
docFile.write(' * Messages should conform to the ICU Message syntax: https://formatjs.io/guides/message-syntax/\n');
docFile.write(' */\n\n');
docFile.write('export default {');
messages.forEach((message) => {
const {
id,
defaultMessage,
description: desc,
} = message;
const value = cleanMessage(defaultMessage);
docFile.write('\n');
if (desc) {
docFile.write(` // ${desc}\n`);
}
docFile.write(' ');
docFile.write(JSON.stringify(id));
docFile.write(': ');
docFile.write(JSON.stringify(value));
docFile.write(',\n');
});
docFile.write('}');
docFile.end();
};
generateMessagesDoc();