-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpre.js
More file actions
58 lines (53 loc) · 1.76 KB
/
pre.js
File metadata and controls
58 lines (53 loc) · 1.76 KB
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
'use strict'
/* global Pear */
const Localdrive = require('localdrive')
const cenc = require('compact-encoding')
const path = require('bare-path')
const pipe = require('pear-pipe')()
const unixpathresolve = require('unix-path-resolve')
function srcs(html) {
return [
...html
.replace(/<!--[\s\S]*?-->/g, '')
.matchAll(/<script\b[^>]*?\bsrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gis)
].map((m) => m[1] || m[2] || m[3])
}
async function configure(options) {
const { stage = {} } = options
const url = new URL(global.Pear.config.applink + '/')
const pathname = normalize(url.pathname)
const drive = new Localdrive(pathname)
const main = unixpathresolve('/', options.gui?.main || 'index.html')
const html = (await drive.get(main)).toString()
const entrypoints = srcs(html).map((e) => unixpathresolve(path.dirname(main), e))
stage.entrypoints = Array.isArray(stage.entrypoints)
? [...stage.entrypoints, ...entrypoints]
: entrypoints
options.stage = stage
const pkg =
options.assets?.ui && !options.assets.ui.only
? null
: JSON.parse(await drive.get('node_modules/pear-electron/package.json'))
options.assets = options.assets ?? pkg?.pear?.assets
options.assets.ui.only = options.assets?.ui?.only ?? pkg?.pear?.assets?.ui?.only
return options
}
pipe.on('end', () => {
Pear.pipe.end()
})
pipe.once('data', (data) => {
const options = cenc.decode(cenc.any, data)
configure(options).then(
(config) => {
const buffer = cenc.encode(cenc.any, { tag: 'configure', data: config })
pipe.end(buffer)
},
(err) => {
pipe.destroy(err)
}
)
})
function normalize(pathname) {
if (pathname[0] === '/' && pathname[2] === ':') return path.normalize(pathname.slice(1))
return path.normalize(pathname)
}