-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 独立出chromium文件,用于检测,获取目录,下载,等等,并且完善下载/解压提示
- Loading branch information
Showing
3 changed files
with
107 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const util = require('util'); | ||
const vm = require('vm'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
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 | ||
__ChromiumDownloader.downloadURLs = downloadURLs; | ||
Object.assign(__ChromiumDownloader,module.exports); | ||
`; | ||
|
||
const ChromiumDownloader = {}; | ||
|
||
const context = { | ||
require, | ||
console, | ||
__dirname: path.dirname(downLoaderPath), | ||
__filename: downLoaderPath, | ||
module, | ||
exports, | ||
global, | ||
__ChromiumDownloader: ChromiumDownloader | ||
}; | ||
|
||
const script = new vm.Script(`${installScript}`); | ||
|
||
script.runInNewContext(context); | ||
|
||
const Chromium = { | ||
get revision() { | ||
return puppeteerPkg.puppeteer.chromium_revision; | ||
}, | ||
get platform() { | ||
return ChromiumDownloader.currentPlatform(); | ||
}, | ||
get downloadUrl() { | ||
const url = ChromiumDownloader.downloadURLs[this.platform]; | ||
return util.format(url, this.revision); | ||
}, | ||
/** | ||
* | ||
* @returns {string} | ||
*/ | ||
get path() { | ||
return path.join(config.paths.puppeteer, LOCAL_CHROMIUM, this.platform + '-' + this.revision); | ||
}, | ||
get isExist() { | ||
const localChromiumPath = path.join(config.paths.puppeteer, LOCAL_CHROMIUM); | ||
|
||
let isExisted = false; | ||
|
||
try { | ||
const stat = fs.statSync(localChromiumPath); | ||
|
||
// 不是目录 | ||
if (!stat.isDirectory()) { | ||
throw null; | ||
} | ||
|
||
const files = fs.readdirSync(localChromiumPath); | ||
|
||
if (files.length <= 0) { | ||
throw null; | ||
} | ||
|
||
const firstFile = files[0]; | ||
|
||
const firstFileStat = fs.statSync(path.join(localChromiumPath, firstFile)); | ||
|
||
// 不是目录 | ||
if (!firstFileStat.isDirectory()) { | ||
throw null; | ||
} | ||
|
||
isExisted = true; | ||
} catch (err) {} | ||
|
||
return isExisted; | ||
}, | ||
Downloader: ChromiumDownloader | ||
}; | ||
|
||
console.log(Chromium.downloadUrl); | ||
|
||
module.exports = Chromium; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.