bin: add "-w" to force overwrite previous json files

This commit is contained in:
Micooz 2017-10-20 13:53:58 +08:00
parent ff03246e31
commit 403e75461b
No known key found for this signature in database
GPG Key ID: 002FB5DD584D6CB1
2 changed files with 22 additions and 8 deletions

@ -29,6 +29,7 @@ const usage = `
-v, --version output blinksocks version
-c, --config file json file with configuration
-m, --minimal generate minimal json files
-w, --write overwrite previous json files
--list-presets list all built-in presets
Examples:
@ -69,7 +70,8 @@ function main() {
if (options[0] === 'init') {
const isMinimal = hasOption('-m') || hasOption('--minimal');
return init({isMinimal});
const isOverwrite = hasOption('-w') || hasOption('--write');
return init({isMinimal, isOverwrite});
}
// parse options
@ -100,10 +102,10 @@ function main() {
if (hasOption('--list-presets')) {
const {presets: {presets, legacyPresets}} = modules;
console.log(chalk.bold.underline('Installed'));
console.log(chalk.bold.underline('[Built-In]'));
console.log(presets.join(os.EOL));
console.log('');
console.log(chalk.bold.underline('Deprecated'));
console.log(chalk.bold.underline('[Deprecated]'));
console.log(legacyPresets.map((name) => `${chalk.gray(name)}`).join(os.EOL));
return;
}

@ -22,10 +22,10 @@ function random(array, len) {
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.ceil(max);
return Math.floor(crypto.randomBytes(1)[0] / 0xff * (max - min + 1)) + min;
return Math.floor(crypto.randomBytes(1)[0] / 0xff * (max - min)) + min;
}
module.exports = function init({isMinimal}) {
module.exports = function init({isMinimal, isOverwrite}) {
const key = random('abcdefghjkmnpqrstuvwxyz23456789!@#$%^&*()_+<>?:|{}-=[];,./ABCDEFGHJKLMNPQRSTUVWXYZ', 16);
const port = getRandomInt(1024, 65535);
const timeout = getRandomInt(200, 1000);
@ -96,9 +96,21 @@ module.exports = function init({isMinimal}) {
delete serverJson.log_max_days;
}
fs.writeFileSync('blinksocks.client.json', JSON.stringify(clientJson, null, ' '));
fs.writeFileSync('blinksocks.server.json', JSON.stringify(serverJson, null, ' '));
const clientJsonPath = 'blinksocks.client.json';
const serverJsonPath = 'blinksocks.server.json';
console.log('> Generate Done. For help please see:');
if (fs.existsSync(clientJsonPath) && !isOverwrite) {
console.log(`> File ${clientJsonPath} exist, skip. Use "-w" to overwrite.`);
} else {
fs.writeFileSync(clientJsonPath, JSON.stringify(clientJson, null, ' '));
}
if (fs.existsSync(serverJsonPath) && !isOverwrite) {
console.log(`> File ${serverJsonPath} exist, skip. Use "-w" to overwrite.`);
} else {
fs.writeFileSync(serverJsonPath, JSON.stringify(serverJson, null, ' '));
}
console.log('> Generate Done, for help please refer to:');
console.log('> https://github.com/blinksocks/blinksocks/tree/master/docs/config/README.md');
};