Skip to content

Commit

Permalink
Use minimist instead of yargs to get rid of dependencies (#23)
Browse files Browse the repository at this point in the history
* Use minimist instead of yargs
Using yargs causes 'critical dependency' warning in Electron,
adapt it to minimist to get rid of the warning
  • Loading branch information
CW-B-W authored Apr 16, 2023
1 parent 2c03367 commit 476529f
Show file tree
Hide file tree
Showing 8 changed files with 785 additions and 1,361 deletions.
1,010 changes: 192 additions & 818 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
},
"homepage": "https://github.com/9bany/curl-to-json#readme",
"dependencies": {
"mocha": "^10.0.0",
"yargs": "^17.5.1"
"minimist": "^1.2.8"
},
"devDependencies": {
"mocha": "^10.0.0"
}
}
16 changes: 16 additions & 0 deletions src/matcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
matchArgv: function(s) {
// ref: https://stackoverflow.com/questions/366202/regex-for-splitting-a-string-using-space-when-not-surrounded-by-single-or-double
return s
.replace(/\\\n/g, ' ')
.match(/"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|[^\s]+/g)
.map((s) => {
if ((s.startsWith(`'`) && s.endsWith(`'`)) || (s.startsWith(`"`) && s.endsWith(`"`))) {
// remove quotes on both sides
return s.substring(1, s.length - 1)
} else {
return s
}
})
}
}
18 changes: 17 additions & 1 deletion src/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = [

// -o, --output <file> Write output to . Can use --create-dirs in conjunction with this to create any directories specified in the -o path.
{
name: '',
name: 'output',
alias: ['o', 'output'],
description: '<file> Write output to . Can use --create-dirs in conjunction with this to create any directories specified in the -o path.',
convertor: null,
Expand Down Expand Up @@ -126,4 +126,20 @@ module.exports = [
description: 'The request method to use.',
convertor: null,
},

// -A, --user-agent <name>
{
name: 'user-agent',
alias: ['A', 'user-agent'],
description: 'Specify the User-Agent send to the HTTP server.',
convertor: null,
},

// -e, --referer <URL>
{
name: 'referer',
alias: ['e', 'referer'],
description: 'Sends the "Referrer Page" information to the HTTP server.',
convertor: null,
},
]
10 changes: 8 additions & 2 deletions src/pare-json.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const yargs = require('yargs/yargs');
const minimistParser = require('minimist');
const convertor = require('./convertor');

const options = require('./option');
const matcher = require('./matcher')

module.exports = exports.default = function (data) {
const argv = yargs(data).argv
if (typeof data === 'string' || data instanceof String) {
// minimistParser cannot parse from string
// parse string to argvs array for minimistParser
data = matcher.matchArgv(data)
}
const argv = minimistParser(data)

let result = {}

Expand Down
3 changes: 3 additions & 0 deletions test/argv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const proc = require('node:process')

console.log(JSON.stringify(proc.argv.slice(2)));
19 changes: 19 additions & 0 deletions test/matcher.matchArgv.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var assert = require('assert');
const matcher = require('../src/matcher');
const testDataList = require('./data.test');
const fs = require('fs');

const childProcess = require('child_process');

describe('matcher.matchArgv test', function () {
testDataList.forEach(element => {

it(`${element.name || 'Test case '}`, function () {
childProcess.exec(`node ./test/argv.js ${element.curl.trim()}`, function(error, stdout, buffer){
const parsedArgv = JSON.stringify(matcher.matchArgv(element.curl));
const answer = stdout;
assert.equal(parsedArgv.trim(), answer.trim());
});
});
})
});
Loading

0 comments on commit 476529f

Please sign in to comment.