This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
/
generateReadme.js
executable file
·91 lines (86 loc) · 3.45 KB
/
generateReadme.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
87
88
89
90
91
var fs = require('fs');
var async = require('async');
var DOCUMENTATION_TOP =
'# sublime-react\n\n' +
'Snippets for ReactJS. This package used to provide JSX syntax highlighting and has been DEPRECATED in favor of babel/babel-sublime.\n\n' +
'![alt tag](https://raw.github.com/jgebhardt/sublime-react/master/docs/img/sr-rcc-out.gif)\n\n' +
'## Installation\n\n' +
'Install the React package via Sublime\'s Package Manager\n\n' +
'You will need the Sublime [Package Manager](https://sublime.wbond.net/installation).\n\n' +
'- Open the command palette: `⌘+shift+p` on MacOS/Linux, `ctrl+shift+p` on Windows\n\n' +
'- type `install`, select `Package Control: Install Package`\n\n' +
'- type `React`, select `ReactJS`\n\n' +
'## Usage\n\n' +
'### Syntax highlighting\n\n' +
'*Syntax highlighting is no longer provided by this packages*. We recommend that you use ([babel-sublime](https://github.com/babel/babel-sublime)) instead.\n\n' +
'### Snippets\n\n' +
'It\'s easy! Simply activate snippets by typing a mnemonic followed by TAB.\n\n' +
'![alt tag](https://raw.github.com/jgebhardt/sublime-react/master/docs/img/sr-snippets-out.gif)\n\n' +
'Snippets are available for both JSX and CJSX ([React CoffeeScript](https://github.com/jsdf/coffee-react-transform)).\n\n' +
'#### Documentation of available snippets (JSX):\n\n' +
'```\n';
var DOCUMENTATION_BOTTOM =
'```\n\n' +
'## Contributing\n\n' +
'### Rebuilding the docs\n\n' +
'After making changes to snippet files, run `npm install && npm run build-docs` to automatically generate this document from source. **Do not** make changes to README.md directly.\n\n' +
'### Contributor License Agreement\n\n' +
'Contributions are very welcome, but we ask that you please fill out our ' +
'[CLA](https://code.facebook.com/cla) in order for us to accept your pull request.\n\n';
fs.readdir('./snippets/js', function(err, files) {
var snippets = files.filter(function(file) {
return file.substr(-16) === '.sublime-snippet';
}).map(function(file) {
return './snippets/js/' + file;
});
async.map(snippets, readAndInspect, function(err, results) {
if (err) {
console.error('error mapping snippets', err);
}
var snippetDocs =
DOCUMENTATION_TOP +
results.map(function(snippet) {
return inspectFile(snippet);
}).sort(function(a, b) {
return a.abbreviation > b.abbreviation
? 1
: a.abbreviation === b.abbreviation
? 0
: -1;
}).map(function(snippet) {
return snippet.docBlock;
}).join('') +
DOCUMENTATION_BOTTOM;
fs.writeFile('README.md', snippetDocs, function (err) {
if (err) {
console.error('error appending README:', err);
}
});
});
});
function readAndInspect(fileName, cb) {
fs.readFile(fileName, 'utf-8', function(err, contents) {
if (!err) {
cb(null, contents);
}
});
}
function inspectFile(contents) {
var match = contents.match(
/[\s\S]*<tabTrigger>(.*?)<\/tabTrigger>[\s\S]*?<description>(React: )?(.*?)<\/description>[\s\S]*/i
);
var docBlock = '';
var abbreviation = '';
var description = '';
if (match !== null) {
abbreviation = match[1];
description = match[3];
var shortCut = ' '.substring(0, 5 - abbreviation.length) + abbreviation;
docBlock = ' ' + shortCut + '→ ' + description + '\n\n';
}
return {
docBlock: docBlock,
abbreviation: abbreviation,
description: description
};
}