-
Notifications
You must be signed in to change notification settings - Fork 12
/
build.js
136 lines (115 loc) · 3.63 KB
/
build.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
const fs = require('fs');
const https = require('https');
const path = require('path');
const glob = require('glob');
const cssmin = require('cssmin');
const uglify = require('uglify-js');
const { styleNames } = require('./alias');
// minify all css files
let cssTask = Promise.all(
glob.sync('Highlight/scripts/styles/*.css', {absolute: true}).map( (filepath) => {
return new Promise( (resolve, reject) => {
fs.readFile(filepath, {encoding: 'utf8'}, (err, data) => {
if (err) return reject(err);
let filename = path.basename(filepath, '.css');
let newPath = filepath.replace(/\.css$/, '.min.css');
let style = styleNames[ filename ] || filename.replace(/(?:^|[._-])([a-zA-Z0-9])/g, ($0, $1) => ' ' + $1.toUpperCase() ).trim();
fs.unlink(filepath, () => {});
fs.writeFile( newPath, cssmin(data), (err) => {
if (err) return reject(err);
resolve({filename, style});
} );
} );
} );
} )
)
.then( (names) => {
console.log('✨ Successfully minified css files.');
return (
'public let hlStyles:[String: String] = [\n' +
names.map( ({filename, style}) => `\t"${style}": "${filename}",\n` ).join('') +
']'
);
} )
.catch( err => {
console.log(err);
} );
//
let jsTask = Promise.all(
glob.sync('node_modules/highlight.js/lib/languages/+([a-zA-Z0-9_-]).js', {absolute: true}).map( (filepath) => {
return new Promise( (resolve, reject) => {
fs.readFile(filepath, {encoding: 'utf8'}, (err, data) => {
if (err) return reject(err);
let filename = path.basename(filepath, '.js');
data = '(function() {' + data + '})();'
data = data.replace(/module.exports\s*=\s*(\w+)/, `highlightGlobalInstance.registerLanguage('${filename}', $1)`)
resolve( { lang: filename, data } );
} );
} );
} )
)
.then( (langs) => {
let hljs = fs.readFileSync('node_modules/highlight.js/lib/core.js', {encoding: 'utf8'});
hljs = hljs.replace(/module.exports\s*=\s*(\w+);/, `var highlightGlobalInstance = highlight;`)
// load the order of registration
let order = (fs.readFileSync('node_modules/highlight.js/lib/index.js')+"")
.match(/'[\w-]+(?=')/g)
.map( s => s.substring(1) );
langs.sort( (a, b) => order.indexOf(a.lang) - order.indexOf(b.lang) );
hljs += langs.map( ({data}) => data ).join('');
console.log('⏱ Compiling the Highlight package file...');
return new Promise( (resolve, reject) => {
const code = hljs;
//const code = uglify.minify(hljs, { sourceMap: false, mangle: false }).code;
fs.writeFile('Highlight/scripts/highlight.pack.js', code, (err) => {
if (err) return reject(err);
console.log('✨ Successfully generated highlight.pack.js');
resolve(`public let hlLangCount: UInt16 = ${langs.length}`);
} );
} );
} )
.catch( err => {
console.log(err);
} );
// Language name map
let mapTask = new Promise( (resolve, reject) => {
https.get( 'https://highlightjs.org/download/', (res) => {
var html = '';
var map = [];
res.on('data', data => { html += data; } );
res.on('end', () => {
let regex = /name="([\w-]+).js"(?: checked)?>([^<>]+)<\/label>/g, match;
while (match = regex.exec(html) ) {
map.push( `\t"${match[2].trim()}": "${match[1]}",\n` );
}
resolve(
'public let hlLanguages:[String: String] = [\n' +
map.join('') +
']'
);
});
})
.on('error', (e) => reject(e) );
} )
.catch( err => {
console.log(err);
} );
// Generate the constant file.
Promise.all( [ jsTask, cssTask, mapTask ] ).then( ([lang, style, langMap]) => {
fs.writeFile(
'Highlight/Constant.swift',
`
//
// Constant.swift
// Highlight
//
// This file is automatically generated.
// DO NOT MODIFY THIS FILE MANUALLY
//
${lang}
${langMap}
${style}
`.trim() + '\n',
() => {}
)
} );