bin: add start.js for package bin, use cli.js as webpack entry
This commit is contained in:
parent
327c904e29
commit
78fc22264f
87
bin/cli.js
Executable file → Normal file
87
bin/cli.js
Executable file → Normal file
@ -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);
|
||||
})();
|
||||
|
2
bin/start.js
Executable file
2
bin/start.js
Executable file
@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env node
|
||||
require('./cli');
|
@ -9,7 +9,7 @@
|
||||
"AUTHORS"
|
||||
],
|
||||
"bin": {
|
||||
"blinksocks": "bin/cli.js"
|
||||
"blinksocks": "bin/start.js"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "npm run lint && npm run test:coverage",
|
||||
|
33
src/index.js
33
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 <json_file>';
|
||||
|
||||
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');
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user