Skip to content

Commit 284d80a

Browse files
committed
CommonJS -> ESM 💥; @derhuerst/[email protected]
1 parent 5cb152e commit 284d80a

File tree

8 files changed

+65
-58
lines changed

8 files changed

+65
-58
lines changed

.eslintrc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"extends": "eslint:recommended",
33
"env": {
4-
"commonjs": true,
54
"es6": true,
65
"node": true
76
},
87
"parserOptions": {
9-
"ecmaVersion": 2018
8+
"ecmaVersion": 2020,
9+
"sourceType": "module"
1010
},
1111
"globals": {
1212
"Atomics": "readonly",

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Because `ffmpeg-static` will download a binary specific to the OS/platform, you
2828
Returns the path of a statically linked ffmpeg binary on the local filesystem.
2929

3030
``` js
31-
var pathToFfmpeg = require('ffmpeg-static');
31+
import pathToFfmpeg from 'ffmpeg-static';
3232
console.log(pathToFfmpeg);
3333
```
3434

example.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const {resolve} = require('path')
5-
const shell = require('any-shell-escape')
6-
const {exec} = require('child_process')
7-
const pathToFfmpeg = require('.')
4+
import {resolve} from 'node:path'
5+
import shell from 'any-shell-escape'
6+
import {exec} from 'node:child_process'
7+
import pathToFfmpeg from './index.js';
88

99
const argv = process.argv.slice(2)
1010
if (argv.includes('-h') || argv.includes('--help')) {

index.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
'use strict'
22

3+
import {arch as osArch, platform as osPlatform} from 'node:os'
4+
import {fileURLToPath} from 'url'
5+
import {join as pathJoin, dirname} from 'node:path'
6+
7+
let ffmpegPath = null
8+
39
if (process.env.FFMPEG_BIN) {
4-
module.exports = process.env.FFMPEG_BIN
10+
ffmpegPath = process.env.FFMPEG_BIN
511
} else {
6-
var os = require('os')
7-
var path = require('path')
8-
9-
var binaries = Object.assign(Object.create(null), {
12+
const binaries = Object.assign(Object.create(null), {
1013
darwin: ['x64', 'arm64'],
1114
freebsd: ['x64'],
1215
linux: ['x64', 'ia32', 'arm64', 'arm'],
1316
win32: ['x64', 'ia32']
1417
})
1518

16-
var platform = process.env.npm_config_platform || os.platform()
17-
var arch = process.env.npm_config_arch || os.arch()
19+
const platform = process.env.npm_config_platform || osPlatform()
20+
const arch = process.env.npm_config_arch || osArch()
1821

19-
var ffmpegPath = path.join(
20-
__dirname,
21-
platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg'
22-
)
23-
24-
if (!binaries[platform] || binaries[platform].indexOf(arch) === -1) {
25-
ffmpegPath = null
22+
if (binaries[platform] && binaries[platform].includes(arch)) {
23+
const __dirname = dirname(fileURLToPath(import.meta.url))
24+
ffmpegPath = pathJoin(
25+
__dirname,
26+
platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg'
27+
)
2628
}
27-
28-
module.exports = ffmpegPath
2929
}
30+
31+
export default ffmpegPath

install.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
'use strict'
22

3-
var fs = require("fs");
4-
var os = require("os");
5-
const {encode: encodeQuery} = require('querystring')
6-
const {strictEqual} = require('assert')
7-
const envPaths = require('env-paths')
8-
const FileCache = require('@derhuerst/http-basic/lib/FileCache').default
9-
const {extname} = require('path')
10-
var ProgressBar = require("progress");
11-
var request = require('@derhuerst/http-basic')
12-
const {createGunzip} = require('zlib')
13-
const {pipeline} = require('stream')
14-
var ffmpegPath = require(".");
15-
var pkg = require("./package");
3+
import {statSync, createWriteStream, chmodSync} from 'node:fs'
4+
import {arch as osArch, platform as osPlatform} from 'node:os'
5+
import HttpsProxyAgent from 'https-proxy-agent'
6+
import {encode as encodeQuery} from 'node:querystring'
7+
import {strictEqual} from 'node:assert'
8+
import envPaths from 'env-paths'
9+
import httpBasic from '@derhuerst/http-basic'
10+
const {FileCache} = httpBasic
11+
import {extname} from 'node:path'
12+
import ProgressBar from 'progress'
13+
import request from '@derhuerst/http-basic'
14+
import {createGunzip} from 'node:zlib'
15+
import {pipeline} from 'node:stream'
16+
import ffmpegPath from './index.js'
17+
18+
import { createRequire } from 'node:module'
19+
const require = createRequire(import.meta.url)
20+
const pkg = require('./package.json')
1621

1722
const exitOnError = (err) => {
1823
console.error(err)
@@ -28,7 +33,7 @@ if (!ffmpegPath) {
2833
}
2934

3035
try {
31-
if (fs.statSync(ffmpegPath).isFile()) {
36+
if (statSync(ffmpegPath).isFile()) {
3237
console.info('ffmpeg is installed already.')
3338
process.exit(0)
3439
}
@@ -45,7 +50,6 @@ const proxyUrl = (
4550
process.env.http_proxy
4651
)
4752
if (proxyUrl) {
48-
const HttpsProxyAgent = require('https-proxy-agent')
4953
const {hostname, port, protocol} = new URL(proxyUrl)
5054
agent = new HttpsProxyAgent({hostname, port, protocol})
5155
}
@@ -109,7 +113,7 @@ function downloadFile(url, destinationPath, progressCallback = noop) {
109113
return;
110114
}
111115

112-
const file = fs.createWriteStream(destinationPath);
116+
const file = createWriteStream(destinationPath);
113117
const streams = isGzUrl(url)
114118
? [response.body, createGunzip(), file]
115119
: [response.body, file]
@@ -159,8 +163,8 @@ const releaseName = (
159163
pkg['ffmpeg-static']['binary-release-name'] ||
160164
release
161165
)
162-
const arch = process.env.npm_config_arch || os.arch()
163-
const platform = process.env.npm_config_platform || os.platform()
166+
const arch = process.env.npm_config_arch || osArch()
167+
const platform = process.env.npm_config_platform || osPlatform()
164168

165169
const baseUrl = `https://github.com/eugeneware/ffmpeg-static/releases/download/${release}`
166170
const downloadUrl = `${baseUrl}/${platform}-${arch}.gz`
@@ -169,7 +173,7 @@ const licenseUrl = `${baseUrl}/${platform}-${arch}.LICENSE`
169173

170174
downloadFile(downloadUrl, ffmpegPath, onProgress)
171175
.then(() => {
172-
fs.chmodSync(ffmpegPath, 0o755) // make executable
176+
chmodSync(ffmpegPath, 0o755) // make executable
173177
})
174178
.catch(exitOnError)
175179

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "ffmpeg-static",
33
"version": "4.4.1",
44
"description": "ffmpeg static binaries for Mac OSX and Linux and Windows",
5+
"type": "module",
56
"main": "index.js",
67
"files": [
78
"index.js",
@@ -47,7 +48,7 @@
4748
"node": ">=16"
4849
},
4950
"dependencies": {
50-
"@derhuerst/http-basic": "^8.2.0",
51+
"@derhuerst/http-basic": "^8.2.2",
5152
"env-paths": "^3.0.0",
5253
"https-proxy-agent": "^5.0.0",
5354
"progress": "^2.0.3"

test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
'use strict'
22

3-
const {ok, strictEqual} = require('assert')
4-
const {isAbsolute} = require('path')
5-
const fs = require('fs')
6-
const {spawnSync} = require('child_process')
7-
const shell = require('any-shell-escape')
8-
const ffmpegPath = require('.')
3+
import {ok, strictEqual} from 'node:assert'
4+
import {isAbsolute} from 'node:path'
5+
import {statSync, accessSync, constants as fsConstants} from 'node:fs'
6+
import {spawnSync} from 'node:child_process'
7+
import shell from 'any-shell-escape'
8+
import ffmpegPath from './index.js'
99

1010
console.info('TAP version 12')
1111
console.info('1..4')
1212

1313
ok(isAbsolute(ffmpegPath))
1414
console.info('ok 1 - ffmpeg path is absolute')
1515

16-
ok(fs.statSync(ffmpegPath).isFile(ffmpegPath))
16+
ok(statSync(ffmpegPath).isFile(ffmpegPath))
1717
console.info(`ok 2 - ${ffmpegPath} is a file`)
1818

19-
fs.accessSync(ffmpegPath, fs.constants.X_OK)
19+
accessSync(ffmpegPath, fsConstants.X_OK)
2020
console.info(`ok 3 - ${ffmpegPath} is executable`)
2121

2222
const {status} = spawnSync(ffmpegPath, ['--help'], {

0 commit comments

Comments
 (0)