-
Notifications
You must be signed in to change notification settings - Fork 10
/
bundle.ts
112 lines (100 loc) · 2.78 KB
/
bundle.ts
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
import * as fs from 'fs'
import * as path from 'path'
import * as child_process from 'child_process'
import * as archiver from 'archiver'
import { version } from './package.json'
console.log('Bundling started')
type Browser = 'chrome' | 'firefox'
function writeToDist(browser: Browser, fileName: string, content: any) {
const dir = path.join(__dirname, 'dist', browser)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
fs.writeFileSync(path.join(dir, fileName), content)
}
// MANIFESTS
const commonManifest = {
name: 'Github Issue Reactions',
version,
description:
'List a link of reactions on a github issue and pull request page',
content_scripts: [
{
matches: ['*://*.github.com/*'],
js: ['index.js'],
run_at: 'document_end',
},
],
permissions: ['storage'],
icons: {
'16': 'icon_16.png',
'48': 'icon_48.png',
'128': 'icon_128.png',
'256': 'icon_256.png',
},
host_permissions: ['https://www.github.com/', 'http://www.github.com/'],
}
// CHROME
const chromeManifest = {
manifest_version: 3,
...commonManifest,
}
writeToDist('chrome', 'manifest.json', JSON.stringify(chromeManifest, null, 2))
// FIREFOX
const { host_permissions, ...firefoxManifest } = {
manifest_version: 2,
...commonManifest,
permissions: [
...commonManifest.host_permissions,
...commonManifest.permissions,
],
browser_specific_settings: {
gecko: {
id: '{f6ec3962-fd1d-4a7b-8dab-d211fbf91389}',
strict_min_version: '57.0a1',
},
},
}
writeToDist(
'firefox',
'manifest.json',
JSON.stringify(firefoxManifest, null, 2)
)
// ICONS
const iconFiles = fs.readdirSync(path.join(__dirname, 'assets', 'icons'))
for (const iconFile of iconFiles) {
const iconData = fs.readFileSync(
path.join(__dirname, 'assets', 'icons', iconFile)
)
writeToDist('chrome', iconFile, iconData)
writeToDist('firefox', iconFile, iconData)
}
// CONTENT SCRIPT
const tscCommand = (browser: Browser) => {
const command = `yarn tsc src/index.ts --outDir dist/${browser}`
console.log(command)
return command
}
child_process.exec(tscCommand('chrome'), () => callBack('chrome'))
child_process.exec(tscCommand('firefox'), () => callBack('firefox'))
// If it should z(h)ip it when done
function callBack(browser: Browser) {
if (process.argv.includes('zip')) {
const archive = archiver('zip', {
zlib: { level: 9 },
})
const output = fs.createWriteStream(
path.join(__dirname, `${browser}_${version}.zip`)
)
archive.pipe(output)
archive.directory(path.join(__dirname, 'dist', `${browser}`), false)
archive.finalize()
}
}
setTimeout(() => {
const timeStampWithoutMs = new Date()
.toISOString()
.split('.')[0]
.replace('T', ' ')
console.log('Bundling finished', timeStampWithoutMs)
}, 5000)