Skip to content

Commit

Permalink
chore: switch to ESM (BREAKING) (#197)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: switch to ESM
  • Loading branch information
DiegoRBaquero committed Sep 17, 2021
1 parent a3733c1 commit 0bc380d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
62 changes: 31 additions & 31 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#!/usr/bin/env node
'use strict'

const chalk = require('chalk')
const cp = require('child_process')
const createTorrent = require('create-torrent')
const ecstatic = require('ecstatic')
const fs = require('fs')
const http = require('http')
const mime = require('mime')
const moment = require('moment')
const networkAddress = require('network-address')
const parseTorrent = require('parse-torrent')
const path = require('path')
const MemoryChunkStore = require('memory-chunk-store')
const prettierBytes = require('prettier-bytes')
const stripIndent = require('common-tags/lib/stripIndent')
const vlcCommand = require('vlc-command')
const WebTorrent = require('webtorrent')
const yargs = require('yargs')()
const { hideBin } = require('yargs/helpers')
const open = require('open')

const { version: webTorrentCliVersion } = require('../package.json')
const { version: webTorrentVersion } = require('webtorrent/package.json')
import chalk from 'chalk'
import cp from 'child_process'
import createTorrent from 'create-torrent'
import ecstatic from 'ecstatic'
import fs from 'fs'
import http from 'http'
import mime from 'mime'
import moment from 'moment'
import networkAddress from 'network-address'
import parseTorrent from 'parse-torrent'
import path from 'path'
import MemoryChunkStore from 'memory-chunk-store'
import prettierBytes from 'prettier-bytes'
import stripIndent from 'common-tags/lib/stripIndent/index.js'
import vlcCommand from 'vlc-command'
import WebTorrent from 'webtorrent'
import Yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import open from 'open'

const { version: webTorrentCliVersion } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)))
const { version: webTorrentVersion } = JSON.parse(fs.readFileSync(new URL('../node_modules/webtorrent/package.json', import.meta.url)))

const yargs = Yargs()

// Group options into sections (used in yargs configuration)
const options = {
Expand Down Expand Up @@ -116,7 +116,7 @@ yargs
.locale('en')
.fail((msg, err) => { console.log(chalk`\n{red Error:} ${msg || err}`); process.exit(1) })
.usage(
fs.readFileSync(path.join(__dirname, 'ascii-logo.txt'), 'utf8')
fs.readFileSync(new URL('ascii-logo.txt', import.meta.url), 'utf-8')
.split('\n')
.map(line => chalk`{bold ${line.substring(0, 20)}}{red ${line.substring(20)}}`)
.join('\n')
Expand Down Expand Up @@ -276,7 +276,7 @@ function runCreate (input) {
})
}

function runDownload (torrentId) {
async function runDownload (torrentId) {
if (!argv.out && !argv.stdout && !playerName) {
argv.out = process.cwd()
}
Expand Down Expand Up @@ -397,7 +397,7 @@ function runDownload (torrentId) {
onSelection(index)
}

function onSelection (index) {
async function onSelection (index) {
href = (argv.airplay || argv.chromecast || argv.xbmc || argv.dlna)
? `http://${networkAddress()}:${server.address().port}`
: `http://localhost:${server.address().port}`
Expand Down Expand Up @@ -464,15 +464,15 @@ function runDownload (torrentId) {
}

if (argv.airplay) {
const airplay = require('airplay-js')
const airplay = await import('airplay-js')

airplay.createBrowser()
.on('deviceOn', device => device.play(href, 0, () => { }))
.start()
}

if (argv.chromecast) {
const chromecasts = require('chromecasts')()
const chromecasts = (await import('chromecasts'))()

const opts = {
title: `WebTorrent - ${torrent.files[index].name}`
Expand Down Expand Up @@ -502,14 +502,14 @@ function runDownload (torrentId) {
}

if (argv.xbmc) {
const xbmc = require('nodebmc')
const xbmc = await import('nodebmc')

new xbmc.Browser()
.on('deviceOn', device => device.play(href, () => { }))
}

if (argv.dlna) {
const dlnacasts = require('dlnacasts')()
const dlnacasts = (await import('dlnacasts'))()

dlnacasts.on('update', player => {
const opts = {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "webtorrent-cli",
"type": "module",
"description": "WebTorrent, the streaming torrent client. For the command line.",
"version": "3.5.4",
"author": {
Expand Down Expand Up @@ -41,7 +42,10 @@
"xtend": "4.0.2"
},
"engines": {
"node": ">=12"
"node": ">=12.20.0"
},
"exports": {
"./bin/cmd.js": "./bin/cmd.js"
},
"homepage": "https://webtorrent.io",
"keywords": [
Expand All @@ -56,7 +60,6 @@
"webtorrent"
],
"license": "MIT",
"main": "index.js",
"optionalDependencies": {
"airplay-js": "^0.3.0",
"chromecasts": "^1.10.0",
Expand Down
23 changes: 11 additions & 12 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const cp = require('child_process')
const extend = require('xtend')
const fixtures = require('webtorrent-fixtures')
const parseTorrent = require('parse-torrent')
const path = require('path')
const spawn = require('cross-spawn')
const test = require('tape')
// const fs = require('fs')

const CMD_PATH = path.resolve(__dirname, '..', 'bin', 'cmd.js')
import { readFileSync } from 'fs'
import cp from 'child_process'
import extend from 'xtend'
import fixtures from 'webtorrent-fixtures'
import parseTorrent from 'parse-torrent'
import spawn from 'cross-spawn'
import test from 'tape'

const CMD_PATH = new URL('../bin/cmd.js', import.meta.url).pathname
const CMD = `node ${CMD_PATH}`

test('Command line: webtorrent help', t => {
Expand All @@ -31,7 +30,7 @@ test('Command line: webtorrent help', t => {

test('Command line: webtorrent version', t => {
t.plan(6)
const expectedVersion = `${require('../package.json').version}\n`
const expectedVersion = `${JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf-8')).version}\n`

cp.exec(`${CMD} version`, (err, data) => {
t.error(err)
Expand Down Expand Up @@ -101,7 +100,7 @@ test('Command line: webtorrent create /path/to/file', t => {
test('Command line: webtorrent download <torrent file> (with local content)', t => {
t.plan(2)

const fixturesPath = path.join(path.dirname(require.resolve('webtorrent-fixtures')), 'fixtures')
const fixturesPath = new URL('../node_modules/webtorrent-fixtures/fixtures', import.meta.url).pathname

cp.exec(`${CMD} download ${fixtures.leaves.torrentPath} --out ${fixturesPath}`, (err, data) => {
t.error(err)
Expand Down

0 comments on commit 0bc380d

Please sign in to comment.