From cd094e376069ba985b59030777eefc5a20817c6e Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 30 Oct 2017 10:59:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=9C=A8=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E4=B9=8B=E5=90=8E=EF=BC=8C=E8=87=AA=E5=8A=A8=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E6=98=AF=E5=90=A6=E6=98=AF=E5=B7=B2=E5=AE=89=E8=A3=85?= =?UTF-8?q?Chromium=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=B2=A1=E6=9C=89=EF=BC=8C?= =?UTF-8?q?=E5=88=99=E8=87=AA=E5=8A=A8=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 32 +++++------------------------- isChromiumExist.js | 42 ++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- scripts/chromium-checker | 27 ++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 28 deletions(-) create mode 100644 isChromiumExist.js create mode 100755 scripts/chromium-checker diff --git a/index.js b/index.js index 124bdb9..8eb51ca 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const path = require('path'); -const fs = require('fs'); const chalk = require('chalk'); +const checkIsChromiumExist = require('./isChromiumExist'); +const config = require('./config'); console.info(`process ${chalk.blue(process.pid)} ${chalk.green('start')}.`); @@ -22,35 +23,12 @@ process.on('unhandledRejection', (reason, p) => { console.error('Unhandled Rejection at:', p, 'reason:', reason); }); -const puppeteerModulePath = path.join(__dirname, 'node_modules', 'puppeteer'); +const puppeteerModulePath = path.join(config.paths.root, 'node_modules', 'puppeteer'); const localChromiumPath = path.join(puppeteerModulePath, '.local-chromium'); -try { - const stat = fs.statSync(localChromiumPath); +const isChromiumExist = checkIsChromiumExist(); - // 不是目录 - 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; - } -} catch (err) { - if (err) { - console.error(err); - } +if (isChromiumExist === false) { console.error( `Please make sure ${chalk.green('chromium')} have install at ${chalk.yellow(localChromiumPath)}` ); diff --git a/isChromiumExist.js b/isChromiumExist.js new file mode 100644 index 0000000..081a2ef --- /dev/null +++ b/isChromiumExist.js @@ -0,0 +1,42 @@ +const fs = require('fs'); +const path = require('path'); +const config = require('./config'); + +/** + * check the chromium have been install in local or not + * @returns {boolean} + */ +module.exports = function() { + const puppeteerModulePath = path.join(config.paths.root, 'node_modules', 'puppeteer'); + const localChromiumPath = path.join(puppeteerModulePath, '.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; +}; diff --git a/package.json b/package.json index ac2c14c..83cedc7 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "generate": "all-contributors generate" + "generate": "all-contributors generate", + "postinstall": "./scripts/chromium-checker" }, "keywords": [ "sms", diff --git a/scripts/chromium-checker b/scripts/chromium-checker new file mode 100755 index 0000000..8d2baee --- /dev/null +++ b/scripts/chromium-checker @@ -0,0 +1,27 @@ +#!/usr/bin/env node + +/* +* 检查chromium是否正确下载 +* 如果已下载,则跳过 +* 如果未下载,则执行下载脚本 +* */ + +const chalk = require('chalk'); + +console.info(`${chalk.blue('Checking Chromium')}...`); + +const path = require('path'); +const config = require('../config'); +const isChromiumExist = require('../isChromiumExist'); +const puppeteerModulePath = path.join(config.paths.root, 'node_modules', 'puppeteer'); + +const existed = isChromiumExist(); + +if (existed) { + console.info(`${chalk.green('Chromium exist...')}`); +} else { + console.info(`${chalk.red('Chromium not exist...')}`); + console.info(`${chalk.blue('Chromium installing...')}`); + // run install script + require(path.join(puppeteerModulePath, 'install.js')); +}