forked from axetroy/sms-boom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromium.js
149 lines (137 loc) · 3.93 KB
/
chromium.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
141
142
143
144
145
146
147
148
149
const util = require('util');
const vm = require('vm');
const fs = require('fs-extra');
const path = require('path');
const Context = require('@axetroy/context');
const ProgressBar = require('progress');
const config = require('./config');
const LOCAL_CHROMIUM = '.local-chromium';
const puppeteerPkg = require(path.join(config.paths.puppeteer, 'package.json'));
const downLoaderPath = path.join(config.paths.puppeteer, 'utils', 'ChromiumDownloader.js');
const installScript =
fs.readFileSync(downLoaderPath, {
encoding: 'utf8'
}) +
`
// expose the module to outside
module.exports.downloadURLs = downloadURLs;
module.exports.DEFAULT_DOWNLOAD_HOST = DEFAULT_DOWNLOAD_HOST;
`;
const context = new Context(downLoaderPath);
const script = new vm.Script(`${installScript}`);
script.runInNewContext(context);
function toMegabytes(bytes) {
const mb = bytes / 1024 / 1024;
return `${Math.round(mb * 10) / 10} Mb`;
}
const ChromiumDownloader = context.module.exports;
const Chromium = {
local: LOCAL_CHROMIUM,
/**
* Get the path which is Chromium download path
* @returns {string}
*/
get localChromiumPath() {
return path.join(config.paths.puppeteer, LOCAL_CHROMIUM);
},
/**
* Get the file path which cache local Chromium
* @returns {string}
*/
get cacheChromiumPath() {
return path.join(config.paths.root, LOCAL_CHROMIUM);
},
/**
* Check the is Chromium have been cache in local project
* @returns {boolean}
*/
get isExistLocalCache() {
return (async () => {
const localCachePath = this.cacheChromiumPath;
const exist = await fs.pathExists(localCachePath);
if (!exist) return false;
return await fs.pathExists(path.join(localCachePath, this.platform + '-' + this.revision));
})()
.then(result => Promise.resolve(result))
.catch(() => Promise.reject(false));
},
/**
* Get chromium version should download
* @returns {*}
*/
get revision() {
return puppeteerPkg.puppeteer.chromium_revision;
},
/**
* Get current platform
* @returns {*|string}
*/
get platform() {
return ChromiumDownloader.currentPlatform();
},
/**
* Chromium download url
* @returns {string}
*/
get downloadUrl() {
const url = ChromiumDownloader.downloadURLs[this.platform];
return util.format(url, ChromiumDownloader.DEFAULT_DOWNLOAD_HOST, this.revision);
},
/**
* download Chromium
* @returns {Promise.<void>}
*/
async download() {
let progressBar = null;
await this.Downloader.downloadRevision(
this.platform,
this.revision,
ChromiumDownloader.DEFAULT_DOWNLOAD_HOST,
(bytesTotal, delta) => {
if (!progressBar) {
progressBar = new ProgressBar(
`Downloading Chromium ${this.platform}/${this.revision} - ${toMegabytes(
bytesTotal
)} [:bar] :percent :etas `,
{
complete: '=',
incomplete: ' ',
width: 20,
total: bytesTotal
}
);
}
progressBar.tick(delta);
}
);
},
/**
* Get local chromium path
* @returns {string}
*/
get path() {
return path.join(this.localChromiumPath, this.platform + '-' + this.revision);
},
/**
* Check the Chromium have been download in Puppeteer module
* @returns {boolean}
*/
get isExist() {
return (async () => {
const localChromiumPath = path.join(config.paths.puppeteer, LOCAL_CHROMIUM);
try {
const stat = await fs.stat(localChromiumPath);
// 不是目录
if (!stat.isDirectory()) return false;
} catch (err) {
return false;
}
// 验证是否下载指定的版本
return await fs.pathExists(path.join(localChromiumPath, this.platform + '-' + this.revision));
})()
.then(isExist => Promise.resolve(isExist))
.catch(() => Promise.reject(false));
},
Downloader: ChromiumDownloader
};
module.exports = Chromium;