UNPKG

2.09 kBJavaScriptView Raw
1/* global $ */
2
3// eslint-disable-next-line no-unused-vars
4function getMapping (plugModules) {
5 plugModules.run()
6
7 const knownKeys = Object.keys(plugModules._nameMapping)
8 const unknownKeys = plugModules.getUnknownModules()
9
10 const knownModules = knownKeys.map((key) => ({
11 isUnknown: false,
12 name: key,
13 original: plugModules.resolveName(key),
14 module: plugModules.require(key)
15 }))
16 const unknownModules = unknownKeys.map((key) => ({
17 isUnknown: true,
18 name: '?',
19 original: key,
20 module: plugModules.require(key)
21 }))
22
23 // build mappings
24 const partsMapping = {}
25 knownModules.forEach((mod) => {
26 const name = mod.name.split('/')
27 const obsc = mod.original.split('/')
28
29 obsc.forEach((part, i) => {
30 partsMapping[part] = name[i]
31 })
32 })
33
34 // guess module names based on known paths
35 unknownModules.forEach((mod) => {
36 mod.name = mod.original
37 .split('/')
38 .map((part) => partsMapping[part] || part)
39 .join('/')
40 })
41
42 // modules that were not remapped by plug-modules, but were partially guessed
43 const unknownPlugModules = unknownModules.filter(
44 (mod) => mod.name.indexOf('plug/') === 0
45 )
46
47 void unknownPlugModules // It's not used atm
48
49 // build full mapping of original names to (possibly partially guessed) proper names
50 const fullMapping = {}
51 knownModules.concat(unknownModules).forEach((mod) => {
52 fullMapping[mod.original] = mod.name
53 })
54
55 const scriptSources = $('script[src*="cdn.plug"]').toArray().map((el) => el.src)
56
57 // get plug.dj version (it appears in one of the inline <script> tags)
58 const js = $('script:not([src])').text()
59 const version = /_v="(.*?)"/.exec(js)[1]
60
61 const appUrl = scriptSources.find(contains('js/app'))
62 const langUrl = scriptSources.find(contains('js/lang/'))
63 const avatarsUrl = scriptSources.find(contains('js/avatars'))
64
65 return JSON.stringify({
66 version: version,
67 appUrl: appUrl,
68 langUrl: langUrl,
69 avatarsUrl: avatarsUrl,
70 mapping: fullMapping
71 })
72
73 function contains (str) {
74 return (src) => src.indexOf(str) > 0
75 }
76}