Skip to content

Commit

Permalink
feat: 独立出chromium文件,用于检测,获取目录,下载,等等,并且完善下载/解压提示
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 6, 2017
1 parent f1359cc commit 1a7dbd3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 50 deletions.
95 changes: 95 additions & 0 deletions chromium.js
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;
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const chalk = require('chalk');
const checkIsChromiumExist = require('./isChromiumExist');
const Chromium = require('./chromium');
const config = require('./config');

console.info(`process ${chalk.blue(process.pid)} ${chalk.green('start')}.`);
Expand All @@ -24,17 +24,22 @@ process.on('unhandledRejection', (reason, p) => {
});

// 运行时检查是已安装Chromium
if (checkIsChromiumExist() === false) {
if (Chromium.isExist === false) {
console.error(
`Please make sure ${chalk.green('chromium')} have install at ${chalk.yellow(
path.join(config.paths.puppeteer, '.local-chromium')
)}`
);
console.error(
`Try to reinstall: ${chalk.green(
'node ' + path.join(config.paths.puppeteer, 'install.js')
)}`
`Try to reinstall: ${chalk.green('node ' + path.join(config.paths.puppeteer, 'install.js'))}\n`
);

console.info(
`If you got network trouble, You can install ${chalk.green(
Chromium.downloadUrl
)} by your self and extract to ${chalk.yellow(Chromium.path)}`
);

process.exit(1);
}

Expand All @@ -48,16 +53,14 @@ const defaultOptions = {
name: '隔壁老王1024',
password: 'abc123abc123',
phone: '13000000000', // do not set default phone number
once: isProduction === false,
once: isProduction === false
};

module.exports = function(phoneNumber, options) {
if (typeof phoneNumber !== 'string' && !isNaN(+phoneNumber)) {
throw new Error(`Invalid phone number ${phoneNumber}`);
}
const app = new App(
Object.assign({}, defaultOptions, options, { phone: phoneNumber })
);
const app = new App(Object.assign({}, defaultOptions, options, { phone: phoneNumber }));
process.on('exit', () => {
app.close();
});
Expand Down
41 changes: 0 additions & 41 deletions isChromiumExist.js

This file was deleted.

0 comments on commit 1a7dbd3

Please sign in to comment.