Skip to content

Commit

Permalink
feat!: use tinyspawn
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 14, 2023
1 parent 225e06f commit c06d8d4
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Any flag supported by `yt-dlp`.

#### options

Any option provided here will passed to [execa#options](https://github.com/sindresorhus/execa#options).
Any option provided here will passed to [spawn#options](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options).

### youtubedl.exec(url, [flags], [options])

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@
"dependencies": {
"bin-version-check": "~5.1.0",
"dargs": "~7.0.0",
"execa": "~5.1.1",
"is-unix": "~2.0.1",
"simple-get": "~4.0.1"
"simple-get": "~4.0.1",
"tinyspawn": "~1.2.0"
},
"devDependencies": {
"@commitlint/cli": "latest",
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const dargs = require('dargs')
const execa = require('execa')
const $ = require('tinyspawn')

const constants = require('./constants')

const args = (url, flags = {}) =>
[].concat(url, dargs(flags, { useEquals: false })).filter(Boolean)
const args = (flags = {}) =>
dargs(flags, { useEquals: false }).filter(Boolean).join(' ')

const isJSON = (str = '') => str.startsWith('{')

Expand All @@ -18,7 +18,7 @@ const parse = ({ stdout, stderr, ...details }) => {
const create = binaryPath => {
const fn = (url, flags, opts) =>
fn.exec(url, flags, opts).then(parse).catch(parse)
fn.exec = (url, flags, opts) => execa(binaryPath, args(url, flags), opts)
fn.exec = (url, flags, opts) => $(binaryPath, [url].concat(args(flags)), opts)
return fn
}

Expand Down
25 changes: 8 additions & 17 deletions test/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,22 @@ const test = require('ava')
const { args } = require('..')

test('no flags', t => {
const flags = args('https://example.com')
t.deepEqual(flags, ['https://example.com'])
const flags = args()
t.is(flags, '')
})

test('parse arguments into flags', t => {
const flags = args('https://example.com', {
const flags = args({
noWarnings: true,
noCheckCertificate: true,
preferFreeFormats: true,
youtubeSkipDashManifest: true,
carDir: '/tmp',
addHeader: ['referer:example.com', 'user-agent:googlebot']
addHeader: ['referer:example.com', "user-agent:'googlebot v1'"]
})

t.deepEqual(flags, [
'https://example.com',
'--no-warnings',
'--no-check-certificate',
'--prefer-free-formats',
'--youtube-skip-dash-manifest',
'--car-dir',
'/tmp',
'--add-header',
'referer:example.com',
'--add-header',
'user-agent:googlebot'
])
t.is(
flags,
"--no-warnings --no-check-certificate --prefer-free-formats --youtube-skip-dash-manifest --car-dir /tmp --add-header referer:example.com --add-header user-agent:'googlebot v1'"
)
})
5 changes: 3 additions & 2 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const test = require('ava')

const youtubedl = require('..')

test('show help', async t => {
await t.throwsAsync(youtubedl(), { instanceOf: Error })
test('no url', async t => {
const error = await t.throwsAsync(youtubedl(''), { instanceOf: Error })
t.is(error.exitCode, 2)
})

test('unsupported URLs', async t => {
Expand Down
3 changes: 0 additions & 3 deletions test/snapshots/exec.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,4 @@ Generated by [AVA](https://avajs.dev).
'stdout',
'stderr',
'stdio',
'kill',
'cancel',
'all',
]
Binary file modified test/snapshots/exec.js.snap
Binary file not shown.

0 comments on commit c06d8d4

Please sign in to comment.