forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.ts
More file actions
153 lines (124 loc) · 4.98 KB
/
tasks.ts
File metadata and controls
153 lines (124 loc) · 4.98 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
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
/* eslint no-sync: warn */
import fs from 'fs'
import { omit } from 'lodash'
import path from 'path'
import shelljs from 'shelljs'
import signale from 'signale'
import utcVersion from 'utc-version'
import { Stats } from 'webpack'
import extensionInfo from '../src/extension/manifest.spec.json'
import schema from '../src/extension/schema.json'
/**
* If true, add <all_urls> to the permissions in the manifest.
* This is needed for e2e tests because it is not possible to accept the permission prompt with puppeteer.
*/
const EXTENSION_PERMISSIONS_ALL_URLS = Boolean(
process.env.EXTENSION_PERMISSIONS_ALL_URLS && JSON.parse(process.env.EXTENSION_PERMISSIONS_ALL_URLS)
)
export type BuildEnv = 'dev' | 'prod'
type Browser = 'firefox' | 'chrome'
const BUILDS_DIR = 'build'
export const WEBPACK_STATS_OPTIONS: Stats.ToStringOptions = {
all: false,
timings: true,
errors: true,
warnings: true,
colors: true,
}
function ensurePaths(): void {
shelljs.mkdir('-p', 'build/dist')
shelljs.mkdir('-p', 'build/bundles')
shelljs.mkdir('-p', 'build/chrome')
shelljs.mkdir('-p', 'build/firefox')
}
export function copyAssets(): void {
signale.await('Copy assets')
const dir = 'build/dist'
shelljs.rm('-rf', dir)
shelljs.mkdir('-p', dir)
shelljs.cp('-R', 'src/extension/assets/*', dir)
shelljs.cp('-R', 'src/extension/views/*', dir)
signale.success('Assets copied')
}
function copyExtensionAssets(toDir: string): void {
shelljs.mkdir('-p', `${toDir}/js`, `${toDir}/css`, `${toDir}/img`)
shelljs.cp('build/dist/js/background.bundle.js', `${toDir}/js`)
shelljs.cp('build/dist/js/inject.bundle.js', `${toDir}/js`)
shelljs.cp('build/dist/js/options.bundle.js', `${toDir}/js`)
shelljs.cp('build/dist/css/style.bundle.css', `${toDir}/css`)
shelljs.cp('build/dist/css/options-style.bundle.css', `${toDir}/css`)
shelljs.cp('build/dist/css/options-style.bundle.css', `${toDir}/css`)
shelljs.cp('-R', 'build/dist/img/*', `${toDir}/img`)
shelljs.cp('build/dist/background.html', toDir)
shelljs.cp('build/dist/options.html', toDir)
}
export function copyIntegrationAssets(): void {
shelljs.mkdir('-p', 'build/integration/scripts')
shelljs.mkdir('-p', 'build/integration/css')
shelljs.cp('build/dist/js/phabricator.bundle.js', 'build/integration/scripts')
shelljs.cp('build/dist/js/integration.bundle.js', 'build/integration/scripts')
shelljs.cp('build/dist/js/extensionHostWorker.bundle.js', 'build/integration/scripts')
shelljs.cp('build/dist/css/style.bundle.css', 'build/integration/css')
shelljs.cp('src/phabricator/extensionHostFrame.html', 'build/integration')
}
const BROWSER_TITLES = {
firefox: 'Firefox',
chrome: 'Chrome',
}
const BROWSER_BUNDLE_ZIPS = {
firefox: 'firefox-bundle.xpi',
chrome: 'chrome-bundle.zip',
}
const BROWSER_BLACKLIST = {
chrome: ['applications'] as const,
firefox: ['key'] as const,
}
function writeSchema(env: BuildEnv, browser: Browser, writeDir: string): void {
fs.writeFileSync(`${writeDir}/schema.json`, JSON.stringify(schema, null, 4))
}
const version = utcVersion()
function writeManifest(env: BuildEnv, browser: Browser, writeDir: string): void {
const manifest = {
...omit(extensionInfo, ['dev', 'prod', ...BROWSER_BLACKLIST[browser]]),
...omit(extensionInfo[env], BROWSER_BLACKLIST[browser]),
}
if (EXTENSION_PERMISSIONS_ALL_URLS) {
manifest.permissions!.push('<all_urls>')
signale.info('Adding <all_urls> to permissions because of env var setting')
}
if (browser === 'firefox') {
manifest.permissions!.push('<all_urls>')
delete manifest.storage
}
delete manifest.$schema
if (env === 'prod') {
manifest.version = version
}
fs.writeFileSync(`${writeDir}/manifest.json`, JSON.stringify(manifest, null, 4))
}
function buildForBrowser(browser: Browser): (env: BuildEnv) => () => void {
ensurePaths()
return env => {
const title = BROWSER_TITLES[browser]
const buildDir = path.resolve(process.cwd(), `${BUILDS_DIR}/${browser}`)
writeManifest(env, browser, buildDir)
writeSchema(env, browser, buildDir)
return () => {
// Allow only building for specific browser targets.
// Useful in local dev for faster builds.
if (process.env.TARGETS && !process.env.TARGETS.includes(browser)) {
return
}
signale.await(`Building the ${title} ${env} bundle`)
copyExtensionAssets(buildDir)
const zipDest = path.resolve(process.cwd(), `${BUILDS_DIR}/bundles/${BROWSER_BUNDLE_ZIPS[browser]}`)
if (zipDest) {
shelljs.mkdir('-p', `./${BUILDS_DIR}/bundles`)
shelljs.exec(`cd ${buildDir} && zip -q -r ${zipDest} *`)
}
signale.success(`Done building the ${title} ${env} bundle`)
}
}
}
export const buildFirefox = buildForBrowser('firefox')
export const buildChrome = buildForBrowser('chrome')