diff --git a/lib/index.js b/lib/index.js index b440f66..f36aa62 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,21 +1,56 @@ -import fs from 'node:fs'; +import { spawn } from 'child_process'; import process from 'node:process'; -import {fileURLToPath} from 'node:url'; -import BinWrapper from 'bin-wrapper'; -const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url))); -const url = `https://raw.githubusercontent.com/imagemin/optipng-bin/v${pkg.version}/vendor/`; +function getArch() { + switch (process.arch) { + case 'x32': + return 'x86'; + case 'x64': + return 'x64' + case 'arm': + return 'arm'; + case 'arm64': + return 'arm64'; + default: + return null; + } +} -const binWrapper = new BinWrapper() - .src(`${url}macos/optipng`, 'darwin') - .src(`${url}linux/x86/optipng`, 'linux', 'x86') - .src(`${url}linux/x64/optipng`, 'linux', 'x64') - .src(`${url}freebsd/x86/optipng`, 'freebsd', 'x86') - .src(`${url}freebsd/x64/optipng`, 'freebsd', 'x64') - .src(`${url}sunos/x86/optipng`, 'sunos', 'x86') - .src(`${url}sunos/x64/optipng`, 'sunos', 'x64') - .src(`${url}win/optipng.exe`, 'win32') - .dest(fileURLToPath(new URL('../vendor', import.meta.url))) - .use(process.platform === 'win32' ? 'optipng.exe' : 'optipng'); +function getPlatform() { + switch (process.platform) { + case 'darwin': + return 'macos'; + case 'freebsd': + return 'freebsd' + case 'linux': + return 'linux'; + case 'sunos': + return 'sunos'; + case 'win32': + return 'win'; + default: + return null; + } +} -export default binWrapper; +const lib = { + path: function () { + const arch = getArch(); + const platform = getPlatform(); + if (!arch || !platform) { + return null; + } + const url = './vendor' + return platform == 'win' + ? `${url}/${platform}/optipng.exe` + : `${url}/${platform}/${arch}/optipng` + }, + dest: function () { + return import.meta.url; + }, + run: function (args) { + return spawn(this.path(), args); + } +} + +export default lib; diff --git a/vendor/linux/arm64/optipng b/vendor/linux/arm64/optipng new file mode 100755 index 0000000..bd82685 Binary files /dev/null and b/vendor/linux/arm64/optipng differ