Skip to content

Commit

Permalink
Use native ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Oct 22, 2021
1 parent ee1aa83 commit 5c09383
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 78 deletions.
59 changes: 19 additions & 40 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
name: test

on:
push:
branches:
- master
pull_request:
branches:
- master

- push
- pull_request
jobs:
linux:
runs-on: ubuntu-latest

test:
name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

node-version:
- 16
- 14
- 12
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
env:
CI: true

macos:
runs-on: macos-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
env:
CI: true
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
6 changes: 3 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';
const {spawn} = require('child_process');
const pngcrush = require('.');
import {spawn} from 'node:child_process';
import process from 'node:process';
import pngcrush from './index.js';

const input = process.argv.slice(2);

Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
'use strict';
module.exports = require('./lib').path();
import lib from './lib/index.js';

export default lib.path();
15 changes: 9 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';
const path = require('path');
const BinWrapper = require('bin-wrapper');
const pkg = require('../package.json');
import fs from 'node:fs';
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.github.com/imagemin/pngcrush-bin/v${pkg.version}/vendor/`;

module.exports = new BinWrapper()
const binWrapper = new BinWrapper()
.src(`${url}osx/pngcrush`, 'darwin')
.src(`${url}linux/pngcrush`, 'linux')
.src(`${url}win/x64/pngcrush.exe`, 'win32', 'x64')
.src(`${url}win/x86/pngcrush.exe`, 'win32', 'x86')
.dest(path.resolve(__dirname, '../vendor'))
.dest(fileURLToPath(new URL('../vendor', import.meta.url)))
.use(process.platform === 'win32' ? 'pngcrush.exe' : 'pngcrush');

export default binWrapper;
12 changes: 7 additions & 5 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const path = require('path');
const binBuild = require('bin-build');
const bin = require('.');
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import binBuild from 'bin-build';
import bin from './index.js';

bin.run(['-version']).then(() => {
console.log('pngcrush pre-build test passed successfully');
Expand All @@ -11,7 +11,9 @@ bin.run(['-version']).then(() => {
console.info('compiling from source');

try {
await binBuild.file(path.resolve(__dirname, '../vendor/source/pngcrush-1.8.13.tar.gz'), [
const source = fileURLToPath(new URL('../vendor/source/pngcrush-1.8.13.tar.gz', import.meta.url));

await binBuild.file(source, [
`mkdir -p ${bin.dest()}`,
`make && mv ${bin.use()} ${bin.path()}`
]);
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "pngcrush wrapper that makes it seamlessly available as a local dependency",
"license": "MIT",
"repository": "imagemin/pngcrush-bin",
"type": "module",
"author": {
"name": "1000ch",
"name": "Shogo Sensui",
"email": "shogo.sensui@gmail.com",
"url": "github.com/1000ch"
},
Expand All @@ -29,7 +30,7 @@
"pngcrush": "cli.js"
},
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"postinstall": "node lib/install.js",
Expand Down Expand Up @@ -58,11 +59,11 @@
"bin-wrapper": "^4.0.0"
},
"devDependencies": {
"ava": "^3.8.0",
"ava": "^3.15.0",
"bin-check": "^4.0.1",
"compare-size": "^3.0.0",
"execa": "^4.0.0",
"tempy": "^0.5.0",
"xo": "^0.30.0"
"execa": "^5.1.1",
"tempy": "^2.0.0",
"xo": "^0.45.0"
}
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pngcrush-bin ![GitHub Actions Status](https://github.com/imagemin/pngcrush-bin/workflows/test/badge.svg?branch=master)
# pngcrush-bin ![GitHub Actions Status](https://github.com/imagemin/pngcrush-bin/workflows/test/badge.svg?branch=main)

> [pngcrush](https://pmt.sourceforge.io/pngcrush/) is an optimizer which main purpose is to reduce the size of the PNG IDAT datastream by trying various compression levels an PNG filter methods
Expand All @@ -15,10 +15,10 @@ $ npm install --save pngcrush-bin
## Usage

```js
const {execFile} = require('child_process');
const pngcrush = require('pngcrush-bin');
import {execFile} from 'node:child_process';
import pngcrush from 'pngcrush-bin';

execFile(pngcrush, ['-reduce', '-brute', 'input.png', 'output.png'], err => {
execFile(pngcrush, ['-reduce', '-brute', 'input.png', 'output.png'], error => {
console.log('Image minified');
});
```
Expand Down
25 changes: 13 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use strict';
const fs = require('fs');
const path = require('path');
const test = require('ava');
const execa = require('execa');
const tempy = require('tempy');
const binCheck = require('bin-check');
const binBuild = require('bin-build');
const compareSize = require('compare-size');
const pngcrush = require('..');
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import execa from 'execa';
import tempy from 'tempy';
import binCheck from 'bin-check';
import binBuild from 'bin-build';
import compareSize from 'compare-size';
import pngcrush from '..';

test('rebuild the pngcrush binaries', async t => {
const temporary = tempy.directory();
const source = fileURLToPath(new URL('../vendor/source/pngcrush-1.8.13.tar.gz', import.meta.url));

await binBuild.file(path.resolve(__dirname, '../vendor/source/pngcrush-1.8.13.tar.gz'), [
await binBuild.file(source, [
`mkdir -p ${temporary}`,
`make && mv pngcrush ${path.join(temporary, 'pngcrush')}`
]);
Expand All @@ -26,7 +27,7 @@ test('return path to binary and verify that it is working', async t => {

test('minify a PNG', async t => {
const temporary = tempy.directory();
const src = path.join(__dirname, 'fixtures/test.png');
const src = fileURLToPath(new URL('fixtures/test.png', import.meta.url));
const dest = path.join(temporary, 'test.png');
const args = [
'--recompress',
Expand Down

0 comments on commit 5c09383

Please sign in to comment.