Skip to content

Commit

Permalink
feat: 增加cli,update命令,用于同步Github的Providers
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 7, 2017
1 parent fe3ad15 commit 34ffc58
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 8 deletions.
21 changes: 16 additions & 5 deletions bin/boomer
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const program = require('caporal');
const chalk = require('chalk');
const boomer = require('../index');
const utils = require('../app/utils');
const updateProviders = require('../scripts/update-provider');

const pkg = require('../package.json');
program.version(pkg.version).description(pkg.description);
Expand All @@ -11,12 +12,12 @@ program
.argument('<phone>', '手机号码')
.option('-f, --forever', '是否持久运行,默认只运行一个周期', program.BOOLEAN, false)
.option('--dev', '是否是开发模式,如果true,则显示浏览器运行过程', program.BOOLEAN, false)
.option('--launch <provider>', '如果是开发模式,则指定站点名称<provider>', program.STRING, "")
.option('--launch <provider>', '如果是开发模式,则指定站点名称<provider>', program.STRING, '')
.action((argv, opts) => {
const app = boomer(argv.phone || process.env.PHONE || '13800000000', {
once: !opts.forever,
isProduction: !opts.dev,
launchProvider: opts.launch,
launchProvider: opts.launch
});

app
Expand All @@ -25,9 +26,7 @@ program
})
.on('next', currentTarget => {
utils.info(
`进入到 ${chalk.green(currentTarget.name)} ${chalk.green.underline(
currentTarget.url
)}`
`进入到 ${chalk.green(currentTarget.name)} ${chalk.green.underline(currentTarget.url)}`
);
})
.on('error', err => {
Expand All @@ -37,4 +36,16 @@ program
.emit('bootstrap');
});

program.command('update').action((argv, opts) => {
console.info(`Update Providers...`);
updateProviders()
.catch(err => {
console.error(err);
return Promise.resolve();
})
.then(() => {
console.info(`Get Remote Providers done!`);
});
});

program.parse(process.argv);
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const chalk = require('chalk');
const Chromium = require('./chromium');
const config = require('./config');
const App = require('./app/app');

console.info(`process ${chalk.blue(process.pid)} ${chalk.green('start')}.`);

Expand Down Expand Up @@ -65,8 +66,6 @@ if (Chromium.isExist === false) {
}
}

const App = require('./app/app');

const isProduction = process.env.NODE_ENV === 'production';

const defaultOptions = {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
"homepage": "https://github.com/axetroy/sms-boom#readme",
"dependencies": {
"@axetroy/context": "^0.1.0",
"axios": "^0.17.0",
"caporal": "^0.8.0",
"chalk": "^2.3.0",
"fs-extra": "^4.0.2",
"lodash": "^4.17.4",
"p-retry": "^1.0.0",
"p-timeout": "^1.2.0",
"puppeteer": "^0.12.0"
},
Expand Down
74 changes: 74 additions & 0 deletions scripts/update-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const fs = require('fs-extra');
const path = require('path');
const config = require('../config');
const axios = require('axios');
const pTimeout = require('p-timeout');
const pRetry = require('p-retry');

const http = axios.create({
timeout: 1000 * 10,
params: {
client_id: 'b8257841dd7ca5eef2aa',
client_secret: '4da33dd6fcb0a01d395945ad18613ecf9c12079e'
}
});

/**
* get all latest providers from Github
* @param outputDir
* @returns {Promise.<void>}
*/
async function updateProviders(outputDir = 'app/providers') {
const res = await http.get(
'https://api.github.com/repos/axetroy/sms-boom/contents/app/providers'
);

const remoteProvidersPath = path.join(config.paths.root, outputDir);

await fs.ensureDir(remoteProvidersPath);

const files = res.data;

const promiseList = files.map(file => {
// if the file is not .js, then cross this
if (/\.js$/.test(file.name) === false) return Promise.resolve();
return pTimeout(
pRetry(
async () => {
try {
const response = await http.get(file.download_url);

if (response.status >= 400) throw response;
const distFile = path.join(remoteProvidersPath, file.name);
const remoteSourceCode = response.data;

let shouldWriteFile = false;

if (await fs.pathExists(distFile)) {
// if file exist, diff and then update
const localSourceCode = await fs.readFile(distFile, { encoding: 'utf8' });
if (remoteSourceCode && remoteSourceCode.trim() !== localSourceCode.trim()) {
shouldWriteFile = true;
}
} else {
// if file not exist then create it
shouldWriteFile = true;
}
// update providers
shouldWriteFile &&
(await fs.writeFile(distFile, remoteSourceCode, { encoding: 'utf8' }));
} catch (err) {
console.error(`Get Remote file ${file.name} fail. ${err.message}`);
throw err;
}
},
{ retries: 5 }
),
1000 * 30
);
});

return await Promise.all(promiseList);
}

module.exports = updateProviders;
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ aws4@^1.6.0:
version "1.6.0"
resolved "https://registry.npmjs.org/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e"

axios@^0.17.0:
version "0.17.0"
resolved "http://registry.npm.taobao.org/axios/download/axios-0.17.0.tgz#7da747916db803f761651d6091d708789b953c6a"
dependencies:
follow-redirects "^1.2.3"
is-buffer "^1.1.5"

balanced-match@^1.0.0:
version "1.0.0"
resolved "http://registry.npm.taobao.org/balanced-match/download/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
Expand Down Expand Up @@ -305,7 +312,7 @@ debug@2.2.0:
dependencies:
ms "0.7.1"

debug@^2.2.0, debug@^2.4.1, debug@^2.6.8:
debug@^2.2.0, debug@^2.4.1, debug@^2.6.8, debug@^2.6.9:
version "2.6.9"
resolved "http://registry.npm.taobao.org/debug/download/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
dependencies:
Expand Down Expand Up @@ -424,6 +431,12 @@ find-up@^1.0.0:
path-exists "^2.0.0"
pinkie-promise "^2.0.0"

follow-redirects@^1.2.3:
version "1.2.5"
resolved "http://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.2.5.tgz#ffd3e14cbdd5eaa72f61b6368c1f68516c2a26cc"
dependencies:
debug "^2.6.9"

forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
Expand Down Expand Up @@ -601,6 +614,10 @@ is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"

is-buffer@^1.1.5:
version "1.1.6"
resolved "http://registry.npm.taobao.org/is-buffer/download/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"

is-builtin-module@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz#540572d34f7ac3119f8f76c30cbc1b1e037affbe"
Expand Down Expand Up @@ -863,6 +880,12 @@ p-finally@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"

p-retry@^1.0.0:
version "1.0.0"
resolved "http://registry.npm.taobao.org/p-retry/download/p-retry-1.0.0.tgz#3927332a4b7d70269b535515117fc547da1a6968"
dependencies:
retry "^0.10.0"

p-timeout@^1.2.0:
version "1.2.0"
resolved "https://registry.npmjs.org/p-timeout/-/p-timeout-1.2.0.tgz#9820f99434c5817868b4f34809ee5291660d5b6c"
Expand Down Expand Up @@ -1031,6 +1054,10 @@ restore-cursor@^2.0.0:
onetime "^2.0.0"
signal-exit "^3.0.2"

retry@^0.10.0:
version "0.10.1"
resolved "http://registry.npm.taobao.org/retry/download/retry-0.10.1.tgz#e76388d217992c252750241d3d3956fed98d8ff4"

rimraf@^2.6.1:
version "2.6.2"
resolved "http://registry.npm.taobao.org/rimraf/download/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
Expand Down

0 comments on commit 34ffc58

Please sign in to comment.