Skip to content

Commit 7d2b295

Browse files
committed
Add automated publishing scripts for Chrome and Firefox extension stores
- create a script allowing firefox add-ons store auto-deployments. necessary env variables put in .env-sample - create deploy-chrome.ts script - rename deploy to publish - remove comments - do we always use the same sass package?
1 parent 9acdde2 commit 7d2b295

6 files changed

Lines changed: 340 additions & 7 deletions

File tree

‎apps/browser/.env-sample‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Firefox Extension Store (AMO) API Credentials
2+
# You can generate these at: https://addons.mozilla.org/en-US/developers/addon/api/key/
3+
FIREFOX_JWT_ISSUER=
4+
FIREFOX_JWT_SECRET=
5+
6+
# The {UUID} of your extension (found in Technical Details section of the edit page)
7+
FIREFOX_EXTENSION_ID=
8+
9+
# Chrome Web Store API Credentials
10+
# https://developer.chrome.com/docs/webstore/using_webstore_api/
11+
CHROME_CLIENT_ID=
12+
CHROME_CLIENT_SECRET=
13+
CHROME_REFRESH_TOKEN=
14+
15+
# The ID of your Chrome extension
16+
CHROME_EXTENSION_ID=ljookipcanaglfaocjbgdicfbdhhjffp

‎apps/browser/.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/dist
22
/dist-firefox
33
/extension-*.zip
4-
/extension-*.zip
4+
/.env

‎apps/browser/package.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
"build": "tsc --noEmit && node scripts/build.mjs && tsx scripts/create-firefox-build.ts && tsx scripts/create-zip-archives.ts",
55
"watch": "node scripts/build.mjs --watch",
66
"typecheck": "tsc --noEmit",
7-
"bump": "node scripts/bump.cjs"
7+
"bump": "node scripts/bump.cjs",
8+
"publish:firefox": "tsx scripts/publish-firefox.ts",
9+
"publish:chrome": "tsx scripts/publish-chrome.ts"
810
},
911
"devDependencies": {
1012
"@types/node": "22.x",
1113
"@types/react": "18.2.79",
1214
"@types/react-dom": "18.2.25",
1315
"@types/webextension-polyfill": "0.12.6",
1416
"baseline-browser-mapping": "2.11.35",
15-
"sass": "1.93.3",
17+
"sass-embedded": "1.89.2",
1618
"tsx": "4.23.15",
1719
"vite": "8.3.6",
1820
"webextension-polyfill": "0.12.0"
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
import { fileURLToPath } from 'url'
4+
5+
const __filename = fileURLToPath(import.meta.url)
6+
const __dirname = path.dirname(__filename)
7+
const base_dir = path.join(__dirname, '..')
8+
9+
const env_path = path.join(base_dir, '.env')
10+
if (fs.existsSync(env_path)) {
11+
process.loadEnvFile(env_path)
12+
}
13+
14+
const get_version = (): string => {
15+
const manifest_path = path.join(base_dir, 'src', 'manifest.json')
16+
const manifest = JSON.parse(fs.readFileSync(manifest_path, 'utf8'))
17+
return manifest.version
18+
}
19+
20+
const log = (msg: string) => console.log(`[publish-chrome] ${msg}`)
21+
22+
const main = async () => {
23+
const client_id = process.env.CHROME_CLIENT_ID
24+
const client_secret = process.env.CHROME_CLIENT_SECRET
25+
const refresh_token = process.env.CHROME_REFRESH_TOKEN
26+
const extension_id = process.env.CHROME_EXTENSION_ID
27+
28+
if (!client_id || !client_secret || !refresh_token || !extension_id) {
29+
console.error(
30+
'[publish-chrome] Missing required environment variables (CHROME_CLIENT_ID, CHROME_CLIENT_SECRET, CHROME_REFRESH_TOKEN, CHROME_EXTENSION_ID).'
31+
)
32+
process.exit(1)
33+
}
34+
35+
const version = get_version()
36+
const zip_name = `extension-${version}-chrome.zip`
37+
const zip_path = path.join(base_dir, zip_name)
38+
39+
if (!fs.existsSync(zip_path)) {
40+
console.error(`[publish-chrome] Could not find ZIP file: ${zip_name}`)
41+
console.error(`[publish-chrome] Ensure you run the "build" step first.`)
42+
process.exit(1)
43+
}
44+
45+
log(`Publishing version ${version} to Chrome Web Store...`)
46+
47+
log('Fetching access token...')
48+
const token_res = await fetch('https://oauth2.googleapis.com/token', {
49+
method: 'POST',
50+
headers: { 'Content-Type': 'application/json' },
51+
body: JSON.stringify({
52+
client_id,
53+
client_secret,
54+
refresh_token,
55+
grant_type: 'refresh_token'
56+
})
57+
})
58+
59+
if (!token_res.ok) {
60+
const error_text = await token_res.text()
61+
console.error(
62+
`[publish-chrome] Failed to get access token (status ${token_res.status}):`,
63+
error_text
64+
)
65+
process.exit(1)
66+
}
67+
68+
const token_data = await token_res.json()
69+
const access_token = token_data.access_token
70+
71+
if (!access_token) {
72+
console.error('[publish-chrome] No access token returned.')
73+
process.exit(1)
74+
}
75+
76+
log(`Uploading ${zip_name}...`)
77+
const zip_buffer = fs.readFileSync(zip_path)
78+
79+
const upload_res = await fetch(
80+
`https://www.googleapis.com/upload/chromewebstore/v1.1/items/${extension_id}`,
81+
{
82+
method: 'PUT',
83+
headers: {
84+
Authorization: `Bearer ${access_token}`,
85+
'x-goog-api-version': '2'
86+
},
87+
body: zip_buffer
88+
}
89+
)
90+
91+
if (!upload_res.ok) {
92+
const error_text = await upload_res.text()
93+
console.error(
94+
`[publish-chrome] Upload failed with status ${upload_res.status}:`,
95+
error_text
96+
)
97+
process.exit(1)
98+
}
99+
100+
const upload_data = await upload_res.json()
101+
102+
if (upload_data.uploadState !== 'SUCCESS') {
103+
console.error('[publish-chrome] Upload failed validation:')
104+
console.error(JSON.stringify(upload_data, null, 2))
105+
process.exit(1)
106+
}
107+
108+
log('Upload successful. Publishing item...')
109+
110+
const publish_res = await fetch(
111+
`https://www.googleapis.com/chromewebstore/v1.1/items/${extension_id}/publish`,
112+
{
113+
method: 'POST',
114+
headers: {
115+
Authorization: `Bearer ${access_token}`,
116+
'x-goog-api-version': '2'
117+
}
118+
}
119+
)
120+
121+
if (!publish_res.ok) {
122+
const error_text = await publish_res.text()
123+
console.error(
124+
`[publish-chrome] Publish failed with status ${publish_res.status}:`,
125+
error_text
126+
)
127+
process.exit(1)
128+
}
129+
130+
const publish_data = await publish_res.json()
131+
132+
if (!publish_data.status || publish_data.status[0] !== 'OK') {
133+
console.warn('[publish-chrome] Publish status is not OK. Response:')
134+
console.warn(JSON.stringify(publish_data, null, 2))
135+
}
136+
137+
log(`Successfully published version ${version} to the Chrome Web Store!`)
138+
}
139+
140+
main().catch((err) => {
141+
console.error('[publish-chrome] Unexpected error:', err)
142+
process.exit(1)
143+
})
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import * as crypto from 'crypto'
2+
import * as fs from 'fs'
3+
import * as path from 'path'
4+
import { fileURLToPath } from 'url'
5+
6+
const __filename = fileURLToPath(import.meta.url)
7+
const __dirname = path.dirname(__filename)
8+
const base_dir = path.join(__dirname, '..')
9+
10+
const env_path = path.join(base_dir, '.env')
11+
if (fs.existsSync(env_path)) {
12+
process.loadEnvFile(env_path)
13+
}
14+
15+
const get_version = (): string => {
16+
const manifest_path = path.join(base_dir, 'src', 'manifest.json')
17+
const manifest = JSON.parse(fs.readFileSync(manifest_path, 'utf8'))
18+
return manifest.version
19+
}
20+
21+
const create_jwt = (issuer: string, secret: string) => {
22+
const header = Buffer.from(
23+
JSON.stringify({ alg: 'HS256', typ: 'JWT' })
24+
).toString('base64url')
25+
const iat = Math.floor(Date.now() / 1000)
26+
const exp = iat + 300
27+
const jti = crypto.randomBytes(16).toString('hex')
28+
const payload = Buffer.from(
29+
JSON.stringify({ iss: issuer, jti, iat, exp })
30+
).toString('base64url')
31+
32+
const signature = crypto
33+
.createHmac('sha256', secret)
34+
.update(`${header}.${payload}`)
35+
.digest('base64url')
36+
37+
return `${header}.${payload}.${signature}`
38+
}
39+
40+
const log = (msg: string) => console.log(`[publish-firefox] ${msg}`)
41+
42+
const main = async () => {
43+
const api_key = process.env.FIREFOX_JWT_ISSUER
44+
const api_secret = process.env.FIREFOX_JWT_SECRET
45+
const extension_id = process.env.FIREFOX_EXTENSION_ID
46+
47+
if (!api_key || !api_secret || !extension_id) {
48+
console.error(
49+
'[publish-firefox] Missing required environment variables (FIREFOX_JWT_ISSUER, FIREFOX_JWT_SECRET, FIREFOX_EXTENSION_ID).'
50+
)
51+
process.exit(1)
52+
}
53+
54+
const version = get_version()
55+
const zip_name = `extension-${version}-firefox.zip`
56+
const zip_path = path.join(base_dir, zip_name)
57+
58+
if (!fs.existsSync(zip_path)) {
59+
console.error(`[publish-firefox] Could not find ZIP file: ${zip_name}`)
60+
console.error(`[publish-firefox] Ensure you run the "build" step first.`)
61+
process.exit(1)
62+
}
63+
64+
log(`Publishing version ${version} to Mozilla Add-ons (AMO) store...`)
65+
66+
const token = create_jwt(api_key, api_secret)
67+
const headers = { Authorization: `JWT ${token}` }
68+
69+
log(`Uploading ${zip_name}...`)
70+
const zip_buffer = fs.readFileSync(zip_path)
71+
const blob = new Blob([zip_buffer], { type: 'application/zip' })
72+
const form = new FormData()
73+
form.append('upload', blob, zip_name)
74+
form.append('channel', 'listed')
75+
76+
const upload_res = await fetch(
77+
'https://addons.mozilla.org/api/v5/addons/upload/',
78+
{
79+
method: 'POST',
80+
headers,
81+
body: form
82+
}
83+
)
84+
85+
if (!upload_res.ok) {
86+
const error_text = await upload_res.text()
87+
console.error(
88+
`[publish-firefox] Upload failed with status ${upload_res.status}:`,
89+
error_text
90+
)
91+
process.exit(1)
92+
}
93+
94+
const upload_data = await upload_res.json()
95+
const upload_uuid = upload_data.uuid
96+
log(`Upload successful (UUID: ${upload_uuid}). Waiting for validation...`)
97+
98+
let processed = false
99+
let valid = false
100+
let poll_count = 0
101+
102+
while (!processed && poll_count < 60) {
103+
await new Promise((r) => setTimeout(r, 5000))
104+
const check_res = await fetch(
105+
`https://addons.mozilla.org/api/v5/addons/upload/${upload_uuid}/`,
106+
{ headers }
107+
)
108+
109+
if (!check_res.ok) {
110+
log(`Validation check returned status ${check_res.status}, retrying...`)
111+
continue
112+
}
113+
114+
const check_data = await check_res.json()
115+
processed = check_data.processed
116+
valid = check_data.valid
117+
118+
if (processed && !valid) {
119+
console.error('[publish-firefox] AMO Validation failed:')
120+
console.error(JSON.stringify(check_data.validation, null, 2))
121+
process.exit(1)
122+
}
123+
124+
if (!processed) {
125+
log('Still validating...')
126+
}
127+
poll_count++
128+
}
129+
130+
if (!processed) {
131+
console.error('[publish-firefox] Validation timed out after 5 minutes.')
132+
process.exit(1)
133+
}
134+
135+
log('Validation passed. Creating new version on the store...')
136+
137+
const version_res = await fetch(
138+
`https://addons.mozilla.org/api/v5/addons/addon/${extension_id}/versions/`,
139+
{
140+
method: 'POST',
141+
headers: {
142+
...headers,
143+
'Content-Type': 'application/json'
144+
},
145+
body: JSON.stringify({ upload: upload_uuid })
146+
}
147+
)
148+
149+
if (version_res.status === 409) {
150+
log(`Version ${version} already exists on AMO.`)
151+
process.exit(0)
152+
}
153+
154+
if (!version_res.ok) {
155+
const error_text = await version_res.text()
156+
console.error(
157+
`[publish-firefox] Version creation failed with status ${version_res.status}:`,
158+
error_text
159+
)
160+
process.exit(1)
161+
}
162+
163+
log(`Successfully published version ${version} to the Firefox Add-ons store!`)
164+
}
165+
166+
main().catch((err) => {
167+
console.error('[publish-firefox] Unexpected error:', err)
168+
process.exit(1)
169+
})

‎pnpm-lock.yaml‎

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)