forked from AdguardTeam/AdguardBrowserExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-chromium.js
More file actions
132 lines (112 loc) · 4.05 KB
/
browser-chromium.js
File metadata and controls
132 lines (112 loc) · 4.05 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
/**
* Chromium build
* 1. Copying common scripts and htmls (pages, lib, locales)
* 2. Copying Chromium filters
* 3. Copying webkit and Chromium scripts
* 4. Updating version of an extension in manifest
* 5. Change the extension name in localization files based on a type of a build (dev, beta or release)
* 6. Preprocessing files
* 7. Creating zip archive of an extension
* 8. Creating crx pack
*/
import fs from 'fs';
import path from 'path';
import gulp from 'gulp';
import zip from 'gulp-zip';
import Crx from 'crx';
import rename from 'gulp-rename';
import {
BUILD_DIR,
BRANCH_BETA,
BRANCH_RELEASE,
BRANCH_DEV,
PRIVATE_FILES,
CHROME_UPDATE_URL,
CHROME_CODEBASE_URL,
} from './consts';
import { version } from './parse-package';
import { updateLocalesMSGName, preprocessAll } from './helpers';
import copyCommonFiles from './copy-common';
import copyExternal from './copy-external';
// set current type of build
const BRANCH = process.env.NODE_ENV || '';
const paths = {
chrome: path.join('Extension/browser/chrome/**/*'),
filters: path.join('Extension/filters/chromium/**/*'),
webkitFiles: path.join('Extension/browser/webkit/**/*'),
cert: path.join(PRIVATE_FILES, 'certificate.pem'),
dest: path.join(BUILD_DIR, BRANCH, `chrome-${version}`),
};
const dest = {
filters: path.join(paths.dest, 'filters'),
inner: path.join(paths.dest, '**/*'),
buildDir: path.join(BUILD_DIR, BRANCH),
manifest: path.join(paths.dest, 'manifest.json'),
};
// copy common files
const copyCommon = () => copyCommonFiles(paths.dest);
// copy chromium filters
const copyFilters = () => gulp.src(paths.filters).pipe(gulp.dest(dest.filters));
// copy chromium and webkit files
const chromiumMainFiles = () => gulp.src([paths.webkitFiles, paths.chrome]).pipe(gulp.dest(paths.dest));
// preprocess with params
const preprocess = done => preprocessAll(paths.dest, { browser: 'CHROMIUM', remoteScripts: true }, done);
// change the extension name based on a type of a build (dev, beta or release)
const localesProcess = done => updateLocalesMSGName(BRANCH, paths.dest, done);
// update current version of extension
const updateManifest = (done) => {
const manifest = JSON.parse(fs.readFileSync(dest.manifest));
manifest.version = version;
fs.writeFileSync(dest.manifest, JSON.stringify(manifest, null, 4));
return done();
};
const createArchive = (done) => {
if (BRANCH !== BRANCH_BETA && BRANCH !== BRANCH_RELEASE) {
return done();
}
return gulp.src(dest.inner)
.pipe(zip(`chrome-${BRANCH}-${version}.zip`))
.pipe(gulp.dest(dest.buildDir))
// chrome.zip artifact
.pipe(rename('chrome.zip'))
.pipe(gulp.dest(BUILD_DIR))
// edge.zip artifact
.pipe(rename('edge.zip'))
.pipe(gulp.dest(BUILD_DIR));
};
const crxPack = async (done) => {
if (BRANCH === BRANCH_DEV || BRANCH === BRANCH_RELEASE) {
return done();
}
const manifest = JSON.parse(await fs.promises.readFile(dest.manifest));
manifest.update_url = CHROME_UPDATE_URL;
await fs.promises.writeFile(dest.manifest, JSON.stringify(manifest, null, 4));
const privateKey = await fs.promises.readFile(paths.cert, 'utf-8');
const crx = new Crx({
codebase: CHROME_CODEBASE_URL,
privateKey,
});
await crx.load(paths.dest);
const crxBuffer = await crx.pack();
const updateXml = await crx.generateUpdateXML();
const crxBuildFilename = `chrome-standalone-${BRANCH}-${version}.crx`;
const crxBuildPath = path.join(dest.buildDir, crxBuildFilename);
const updateXmlPath = path.join(dest.buildDir, 'update.xml');
await fs.promises.writeFile(crxBuildPath, crxBuffer);
await fs.promises.writeFile(updateXmlPath, updateXml);
return gulp.src(crxBuildPath)
.pipe(rename('chrome.crx'))
.pipe(gulp.src(updateXmlPath))
.pipe(gulp.dest(BUILD_DIR));
};
export default gulp.series(
copyExternal,
copyCommon,
copyFilters,
chromiumMainFiles,
updateManifest,
localesProcess,
preprocess,
createArchive,
crxPack
);