diff --git a/bin/cli.js b/bin/cli.js old mode 100755 new mode 100644 index 3a5cb46..73a9a64 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,11 +1,10 @@ -#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const chalk = require('chalk'); const init = require('./init'); const bootstrap = require('./bootstrap'); const modules = require('./modules'); -const {version} = require('../package.json'); +const version = global.__WEBPACK__ ? global.__VERSION__ : require('../package.json').version; const examples = [ ['Generate json file', '$ blinksocks init'], @@ -33,51 +32,53 @@ ${examples.map(([description, example]) => ` ${chalk.gray('-')} ${description}\ const argv = process.argv; -if (argv.length < 3) { - return console.log(usage); -} - -const options = argv.slice(2); - -function hasOption(opt) { - return options.indexOf(opt) !== -1; -} - -function getOptionValue(opt) { - const index = options.indexOf(opt); - if (index !== -1) { - return options[index + 1]; - } - return undefined; -} - -if (hasOption('-h') || hasOption('--help')) { - return console.log(usage); -} - -if (hasOption('-v') || hasOption('--version')) { - return console.log(version); -} - -if (hasOption('-c') || hasOption('--config')) { - let configPath = getOptionValue('-c') || getOptionValue('--config'); - - if (configPath === undefined) { - return console.log(chalk.red('config file must be provided')); +(function main() { + if (argv.length < 3) { + return console.log(usage); } - configPath = path.resolve(process.cwd(), configPath); + const options = argv.slice(2); - if (!fs.existsSync(configPath)) { - return console.log(chalk.red('config file is not found')); + function hasOption(opt) { + return options.indexOf(opt) !== -1; } - return bootstrap(configPath, modules); -} + function getOptionValue(opt) { + const index = options.indexOf(opt); + if (index !== -1) { + return options[index + 1]; + } + return undefined; + } -if (options[0] === 'init') { - return init(); -} + if (hasOption('-h') || hasOption('--help')) { + return console.log(usage); + } -// other cases -console.log(usage); + if (hasOption('-v') || hasOption('--version')) { + return console.log(version); + } + + if (hasOption('-c') || hasOption('--config')) { + let configPath = getOptionValue('-c') || getOptionValue('--config'); + + if (configPath === undefined) { + return console.log(chalk.red('config file must be provided')); + } + + configPath = path.resolve(process.cwd(), configPath); + + if (!fs.existsSync(configPath)) { + return console.log(chalk.red('config file is not found')); + } + + return bootstrap(configPath, modules); + } + + if (options[0] === 'init') { + return init(); + } + + // other cases + console.log(usage); +})(); diff --git a/bin/start.js b/bin/start.js new file mode 100755 index 0000000..3ca31c1 --- /dev/null +++ b/bin/start.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('./cli'); diff --git a/package.json b/package.json index a946054..ad4b08f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "AUTHORS" ], "bin": { - "blinksocks": "bin/cli.js" + "blinksocks": "bin/start.js" }, "scripts": { "test": "npm run lint && npm run test:coverage", diff --git a/src/index.js b/src/index.js index f969720..0b879cb 100755 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1 @@ -import fs from 'fs'; -import path from 'path'; -import * as __modules__ from './core'; - -// only for webpack bundle -if (global.__WEBPACK__) { - const argv = process.argv; - const usage = 'Usage: node blinksocks.js -c/--config '; - - if (argv.length !== 4 || (argv[2] !== '-c' && argv[2] !== '--config') || !argv[3].endsWith('.json')) { - console.log(usage); - process.exit(0); - } - - const file = path.resolve(process.cwd(), argv[3]); - - let json = null; - try { - const jsonFile = fs.readFileSync(file); - json = JSON.parse(jsonFile); - } catch (err) { - throw Error(`fail to parse your '${file}': ${err.message}`); - } - - const app = new __modules__.Hub(json); - app.on('close', () => process.exit(0)); - app.run(); - - process.on('SIGINT', () => app.terminate()); -} - -module.exports = __modules__; +module.exports = require('./core'); diff --git a/webpack.config.js b/webpack.config.js index 4aa79cf..d6cd0aa 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,7 +1,7 @@ const path = require('path'); const webpack = require('webpack'); const BabiliPlugin = require('babili-webpack-plugin'); -const packageJson = require('./package.json'); +const {version} = require('./package.json'); // TODO: prevent packing src/presets/__tests__/*.test.js into blinksocks.js // ./src/presets ^\.\/.*$ 789 bytes {0} [optional] [built] @@ -14,7 +14,7 @@ module.exports = { target: 'node', entry: { - 'blinksocks': './src/index.js' + 'blinksocks': './bin/cli.js' }, output: { @@ -57,14 +57,15 @@ module.exports = { new BabiliPlugin(), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production'), - 'global.__WEBPACK__': JSON.stringify(true) + 'global.__WEBPACK__': JSON.stringify(true), + 'global.__VERSION__': JSON.stringify(version) }), new webpack.IgnorePlugin(/__mocks__/, /__tests__/), new webpack.BannerPlugin({ banner: [ 'Copyright (c) 2016-present, [name]. All rights reserved.\n', 'name: [file]', - `version: v${packageJson.version}`, + `version: v${version}`, 'hash: [hash]' ].join('\n'), entryOnly: true