-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
251 lines (210 loc) · 7.16 KB
/
gulpfile.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* refs:
* - https://stackoverflow.com/a/36592176 (merge stream)
* - https://stackoverflow.com/a/40572663 (browser-sync not reloading)
* - https://nightlycommit.github.io/twing/templates.html
* - https://stackoverflow.com/questions/67641687/cannot-disable-cache-in-twing-template-engine-node-js-express-js
* - https://nightlycommit.github.io/twing/language-reference/tags/include.html
* - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
* - https://www.tutorialspoint.com/http/http_url_encoding.htm
*/
const
fs = require('fs'),
{ src, dest, watch, series, parallel } = require('gulp'),
merge = require('merge-stream'),
concat = require('gulp-concat'),
order = require('gulp-order'),
rename = require('gulp-rename'),
bookmarklet = require('gulp-bookmarklet'),
change = require('gulp-change'),
twing = require('gulp-twing'),
{ TwingEnvironment, TwingLoaderRelativeFilesystem } = require('twing'),
twingEnv = new TwingEnvironment(new TwingLoaderRelativeFilesystem()),
cheerio = require('cheerio'),
yargs = require('yargs/yargs'),
{ hideBin } = require('yargs/helpers'),
argv = yargs(hideBin(process.argv)).argv,
browserSync = require('browser-sync').create();
// flag to append browser-sync script tag (otherwise it won't auto-reload page)
// because htmlsingle file generated by gulp-bookmarklet doesn't include
// a body tag, so browser-sync can't automatically append its script tag
let APPEND_BROWSER_SYNC_TAG = false;
function appendBrowserSyncTag(cb) {
APPEND_BROWSER_SYNC_TAG = true;
cb();
}
// flag to automatically ignore scripts in development (marked with starting _)
// this is useful to leave unfinished scripts out of prod build (publish)
let IGNORE_UNFINISHED_SCRIPTS = false;
function ignoreUnfinishedScripts(cb) {
IGNORE_UNFINISHED_SCRIPTS = true;
cb();
}
function transformInputStringIntoArray(args) {
// if args is passed without values
if (!args || typeof args === 'boolean') return [];
let str = args.toString().trim();
if (str.substring(str.length, str.length - 1) === ',') {
str = str.slice(0, -1);
}
return str.split(',');
}
const scriptsArg = argv.logs;
let logScriptsArr = transformInputStringIntoArray(scriptsArg);
function newScript(cb) {
const args = yargs(hideBin(process.argv))
.help(false)
.version(false)
.option('name', {
alias: 'n',
describe: 'choose script name'
})
.option('type', {
alias: 't',
describe: 'choose new script type',
choices: ['basic', 'togglable', 'miniapp', 'dialog'],
default: 'basic'
})
.demandOption(['name', 'type'], 'Please provide a name and a valid type')
.fail(function (msg, err, yargs) {
if (err) throw err;
console.error(msg);
console.error(yargs.help());
cb();
process.exit(1);
})
.parse();
return src('src/building_blocks/stubs/'+ args.type +'.js.twig')
.pipe(twing(
twingEnv,
{
scriptName: args.name
},
{ outputExt: '' }
))
.pipe(rename((path) => {
path.basename = '_' + args.name;
}))
.pipe(dest('src'));
}
function build() {
// needed to bypass twing caching mechanism,
// otherwise would not reflect changes while watching files changes
const twingEnv = new TwingEnvironment(new TwingLoaderRelativeFilesystem());
// this provides us a way to use twing templating "tags"
// while still using js/css/html syntax highlight from the editor
function removeTwingPlaceholders(content) {
return content
.replaceAll("// @twing ", "") // js
.replaceAll("// @twing-include ", "") // js
.replaceAll("/* @twing-start", "") // js/css
.replaceAll("@twing-end */", "") // js/css
.replaceAll("<!-- @twing-start", "") // html
.replaceAll("@twing-end -->", ""); // html
}
function logScripts(content) {
logScriptsArr.forEach((scriptName) => {
if (content.indexOf(scriptName) > 0) {
console.log(scriptName + ' content:');
console.log(content);
}
});
}
let scriptsGlob = ['src/*.js'];
if (IGNORE_UNFINISHED_SCRIPTS) {
scriptsGlob.push('!src/_*.js');
}
let bookmarks = src(scriptsGlob)
.pipe(twing(
twingEnv,
{},
{ outputExt: '' }
))
.pipe(change(removeTwingPlaceholders))
.pipe(change(logScripts))
.pipe(bookmarklet({
format: 'htmlsingle',
file: 'bookmarks.html'
}));
let styling = src('src/appends/styles.html');
let merged = merge(bookmarks, styling);
if (APPEND_BROWSER_SYNC_TAG) {
merged.add(src('src/appends/browser-sync.html'));
}
return merged
.pipe(order(['bookmarks.html', '*.html']))
.pipe(concat('index.html'))
.pipe(dest('dist'));
}
function displayBookmarkletsLength(cb) {
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(
/[!'()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
);
}
/**
* Chrome appears to use a file-based solution to store bookmarks, so it might be limited by storage size
* Firefox appears to use SQLite database to store bookmarks therefore is limited by its maximum page size
* - https://support.mozilla.org/en-US/questions/1259005#answer-1221738
* + "I think that the maximum for a bookmarklet is a few tens of bytes below 64KB
* (around 65500 bytes; I have one over 64000) (...) I gave it another test and can get to *65536*."
* + Note: 65536 bytes equals 65536 characters (1:1)
* - https://www.sqlite.org/limits.html
* + "Every database consists of one or more "pages". Within a single database, every page is the same size,
* but different databases can have page sizes that are powers of two between 512 and *65536*, inclusive.
* The maximum size of a database file is 4294967294 pages."
*/
const scripts = [];
const indexContent = fs.readFileSync('./dist/index.html', { encoding:'utf8', flag:'r' });
const $ = cheerio.load(indexContent);
const pageLinks = $('a');
pageLinks.each(function (index, el) {
const scriptString = $(el).attr('href');
const len = fixedEncodeURIComponent(scriptString).length;
let status = '';
if (len > 65536) {
status = 'error';
} else if (len > 50000) {
status = 'warning';
} else {
status = 'safe';
}
let scriptData = {
'name': $(el).text(),
'length': scriptString.length,
'length (encoded)': len,
'status': status
};
scripts.push(scriptData);
});
console.table(scripts);
cb();
}
function reload(cb) {
browserSync.reload();
cb();
}
function serve() {
browserSync.init({
startPath: 'index.html',
server: {
baseDir: './dist/'
},
cors: true
});
watch(['./dist/**/*'], { events: ['change'] }, reload);
}
const buildAndLog = series(build, displayBookmarkletsLength);
function listen() {
watch(['./src/**/*'], { events: ['change'] }, build);
}
const devTasks = series(appendBrowserSyncTag, buildAndLog, parallel(serve, listen));
module.exports = {
new: newScript,
build: build,
publish: series(ignoreUnfinishedScripts, build),
dev: devTasks,
log: displayBookmarkletsLength,
default: devTasks,
}