Skip to content

Commit

Permalink
feat: 新增在安装之后,自动验证是否是已安装Chromium,如果没有,则自动安装
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Oct 30, 2017
1 parent 247e1b9 commit cd094e3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 28 deletions.
32 changes: 5 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -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')}.`);

Expand All @@ -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)}`
);
Expand Down
42 changes: 42 additions & 0 deletions isChromiumExist.js
Original file line number Diff line number Diff line change
@@ -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;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions scripts/chromium-checker
Original file line number Diff line number Diff line change
@@ -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'));
}

0 comments on commit cd094e3

Please sign in to comment.