forked from gnab/remark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
221 lines (182 loc) · 5.63 KB
/
make.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require('shelljs/make');
require('shelljs/global');
// Targets
target.all = function () {
target.test();
target.minify();
target.boilerplate();
};
target.highlighter = function () {
var config = require('./package.json').config;
if (config && config.highlighter === false) {
console.log('Bundling empty highlighter skeleton...');
"module.exports = {styles:[],engine:{highlightBlock:function(){}}};"
.to('src/remark/highlighter.js');
return;
}
console.log('Bundling highlighter...');
rm('-rf', 'vendor/highlight.js');
mkdir('-p', 'vendor');
pushd('vendor');
exec('git clone https://github.com/isagalaev/highlight.js.git');
pushd('highlight.js');
exec('git checkout tags/9.12.0');
popd();
popd();
bundleHighlighter('src/remark/highlighter.js');
};
target.test = function () {
target['lint']();
target['bundle']();
target['test-bundle']();
console.log('Running tests...');
run('mocha-phantomjs test/runner.html', true);
};
target.lint = function () {
console.log('Linting...');
run('jshint src');
};
target.bundle = function () {
console.log('Bundling...');
bundleResources('src/remark/resources.js');
mkdir('-p', 'out');
run('browserify ' + components() + ' src/remark.js').stdout.to('out/remark.js');
};
function components () {
var componentsPath = './src/remark/components';
return ls(componentsPath)
.map(function (component) {
return '-r ' + componentsPath + '/' + component + '/' + component +
'.js:' + 'components/' + component;
})
.join(' ');
}
target['test-bundle'] = function () {
console.log('Bundling tests...');
[
"require('should');",
"require('sinon');",
"require('should-sinon');"
]
.concat(find('./test')
.filter(function(file) { return file.match(/\.js$/); })
.map(function (file) { return "require('./" + file + "');" })
)
.join('\n')
.to('_tests.js');
run('browserify ' + components() + ' _tests.js').stdout.to('out/tests.js');
rm('_tests.js');
};
target.boilerplate = function () {
console.log('Generating boilerplate...');
generateBoilerplateSingle("boilerplate-single.html");
};
target.minify = function () {
console.log('Minifying...');
run('uglifyjs -m -c -o out/remark.min.js out/remark.js');
};
target.deploy = function () {
var currentBranch = git('branch')
.split('\n')
.filter(function (line) {
return line[0] === '*';
})[0]
.substr(2);
var version = require('./package.json').version;
var tagForVersion = git('tag -l v' + version);
if (tagForVersion) {
console.log('Update version in package.json before deploying.');
return;
}
git('checkout HEAD');
git('add -f out');
git('commit -m "Deploy version ' + version + '."');
git('tag -a v' + version + ' -m "Version ' + version + '."');
git('checkout ' + currentBranch);
git('push origin --tags');
};
// Helper functions
var path = require('path')
, package = require('./package.json')
, version = package.version
, config = package.config
, ignoredStyles = ['brown_paper', 'school_book', 'pojoaque']
;
function bundleResources (target) {
var resources = {
VERSION: version
, DOCUMENT_STYLES: JSON.stringify(
less('src/remark.less'))
, CONTAINER_LAYOUT: JSON.stringify(
cat('src/remark.html'))
};
cat('src/templates/resources.js.template')
.replace(/%(\w+)%/g, function (match, key) {
return resources[key];
})
.to(target);
}
function bundleHighlighter (target) {
var highlightjs = 'vendor/highlight.js/src/'
, resources = {
HIGHLIGHTER_STYLES: JSON.stringify(
ls(highlightjs + 'styles/*.css').reduce(mapStyle, {}))
, HIGHLIGHTER_ENGINE:
cat(highlightjs + 'highlight.js')
, HIGHLIGHTER_LANGUAGES:
Array.prototype.sort.call(ls(highlightjs + 'languages/*.js'),
function (a, b) {
// Other languages depend on cpp, so put it first
return a.indexOf('cpp.js') !== -1 ? -1 : 0;
})
.map(function (file) {
var language = path.basename(file, path.extname(file))
return '{name:"' + language + '",create:' + cat(file) + '}';
}).join(',')
};
cat('src/templates/highlighter.js.template')
.replace(/%(\w+)%/g, function (match, key) {
return resources[key];
})
.to(target);
}
function generateBoilerplateSingle(target) {
var resources = {
REMARK_MINJS: escape(cat('out/remark.min.js')
// highlighter has a ending script tag as a string literal, and
// that causes early termination of escaped script. Split that literal.
.replace('"</script>"', '"</" + "script>"'))
};
cat('src/templates/boilerplate-single.html.template')
.replace(/%(\w+)%/g, function (match, key) {
return resources[key];
})
.to(target);
}
function mapStyle (map, file) {
var key = path.basename(file, path.extname(file))
, tmpfile = path.join(tempdir(), 'remark.tmp')
;
if (ignoredStyles.indexOf(key) === -1) {
('.hljs-' + key + ' {\n' + cat(file) + '\n}').to(tmpfile);
map[key] = less(tmpfile);
rm(tmpfile);
}
return map;
}
function less (file) {
return run('lessc -x -s ' + file).stdout.replace(/\n/g, '');
}
function git (cmd) {
return exec('git ' + cmd, {silent: true}).stdout;
}
function run (command, loud) {
var result = exec('"' + pwd() + '/node_modules/.bin/' + '"' + command, {silent: !loud, fatal: false});
if (result.code !== 0) {
if (!options || options.silent) {
console.error(result.stdout);
}
exit(1);
}
return result;
}