-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
update-manifest.js
executable file
·140 lines (123 loc) · 4.72 KB
/
update-manifest.js
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
/**
* This script retrieves information about the latest releases of the extension on GitHub
* and updates the extension manifest to provide firefox update information.
*
* You can set the environment variable `GITHUB_TOKEN` with a GitHub access token.
*
* It will fetch release details, check for .xpi files in the assets, and update the manifest with version and download links.
*
* On old node versions, run this script with `node --experimental-fetch update-manifest.js`
*/
/**
* @typedef {Object} ReleaseAsset
* @property {string} name - The name of the release asset.
* @property {string} browser_download_url - The URL to download the release asset.
*/
/**
* @typedef {Object} GithubRelease
* @property {string} tag_name - The tag name of the release.
* @property {string} html_url - The HTML URL of the release on GitHub.
* @property {boolean} draft - Indicates if the release is a draft.
* @property {ReleaseAsset[]} assets - An array of release assets.
*/
/**
* @typedef {Object} Manifest
* @property {Object} browser_specific_settings - Browser-specific settings in the manifest.
* @property {Object} browser_specific_settings.gecko - Gecko-specific settings.
* @property {string} browser_specific_settings.gecko.id - The extension's ID for Gecko-based browsers.
*/
/**
* @typedef {Object} ManifestUpdate
* @property {Object} addons - Addons object in the manifest.
* @property {Object} addons[addonId] - Addon details for a specific ID.
* @property {ReleaseDetails[]} addons[addonId].updates - An array of release details.
*/
/**
* @typedef {Object} ReleaseDetails
* @property {string} version - The version of the release.
* @property {string} update_link - The URL to download the update.
* @property {string} update_info_url - The URL with information about the release.
*/
const fs = require('fs');
const path = require('path');
const BASE = path.dirname(__filename);
/**
* Get the extension manifest.
* @returns {Manifest} The extension manifest object.
*/
function getExtensionManifest() {
const manifestPath = path.join(BASE, 'ophirofox', 'manifest.json');
const manifest = fs.readFileSync(manifestPath, 'utf8');
return JSON.parse(manifest);
}
/**
* Get GitHub releases using the fetch API.
* @returns {Promise<GithubRelease[]>} A Promise that resolves to an array of release objects.
*/
async function getGithubReleases() {
const token = process.env.GITHUB_TOKEN;
const headers = token ? { 'authorization': `Bearer ${token}` } : {};
const url = 'https://api.github.com/repos/lovasoa/ophirofox/releases';
try {
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`Failed to fetch GitHub releases. Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw new Error(`Error fetching GitHub releases: ${error.message}`);
}
}
/**
* Get details of a release.
* @param {GithubRelease} release - The release object.
* @returns {ReleaseDetails|undefined} Details of the release, or undefined if not found.
*/
function versionDetails(release) {
const version = release.tag_name.slice(1);
const xpi_asset = release.assets.find(asset => asset.name.endsWith('.xpi'));
const changelog_asset = release.assets.find(asset => asset.name === 'changelog.txt');
return xpi_asset && {
version,
update_link: xpi_asset.browser_download_url,
update_info_url:
changelog_asset ? changelog_asset.browser_download_url : release.html_url,
};
}
/**
* Update the extension manifest.
* @returns {ManifestUpdate} The updated manifest object.
*/
async function updateManifest() {
const manifest = getExtensionManifest();
const addonId = manifest.browser_specific_settings.gecko.id;
const releases = await getGithubReleases();
return {
addons: {
[addonId]: {
updates: [
{
version: process.env.OPHIROFOX_VERSION || "0.0.0.0",
update_link: "https://github.com/lovasoa/ophirofox/releases/latest/download/ophirofox.xpi",
update_info_url: "https://github.com/lovasoa/ophirofox/releases/latest/download/changelog.txt",
},
...releases
.filter(release => !release.draft)
.map(versionDetails)
.filter(Boolean),
]
},
},
};
}
/**
* Main function to update and log the manifest.
*/
async function main() {
const manifestUpdate = await updateManifest();
console.log(JSON.stringify(manifestUpdate, null, 2));
}
if (require.main === module) {
main();
}