Skip to content

Commit 52d134f

Browse files
Use Async Pool and Node 14's ESM Support (#81)
* Add merged type defintions for index.d.ts * Use Async Helper, doesn't work cause of Node ESM problems * Uses new depdency * Use ESM * Move to Node 14 * Upgrade to latest async helper, ensure Node >= 12 is used * Introduce formatting via github action, remove post commit and pre push hooks * Forgot to add content to format.yml * Upgrade to typescript-esm 0.0.6 * Add missing format script * Pushed Changes from Github Actions Bot * Run unit tests on Node 14 only
1 parent c951864 commit 52d134f

File tree

11 files changed

+56
-66
lines changed

11 files changed

+56
-66
lines changed

.github/workflows/format.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Format
2+
on: pull_request
3+
jobs:
4+
format:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
node-version: [12.x]
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
- name: Use Node.js ${{ matrix.node-version }}
13+
uses: actions/setup-node@v1
14+
with:
15+
node-version: ${{ matrix.node-version }}
16+
- name: Format
17+
run: yarn install; yarn format
18+
- name: Push changes
19+
uses: kristoferbaxter/github-push-action@master
20+
with:
21+
force: true
22+
github_token: ${{ secrets.GITHUB_TOKEN }}
23+
branch: ${{ github.head_ref }}

.github/workflows/unit-test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
node-version: [12.x]
10+
node-version: [14.x]
1111
steps:
1212
- uses: actions/checkout@v1
1313
- name: Use Node.js ${{ matrix.node-version }}
1414
uses: actions/setup-node@v1
1515
with:
1616
node-version: ${{ matrix.node-version }}
17-
- run: yarn install
18-
- run: yarn test
17+
- run: yarn install; yarn test

package.json

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"files": [
1010
"dist"
1111
],
12+
"engines": {
13+
"node": ">=12"
14+
},
1215
"bin": {
1316
"filesize": "./dist/filesize"
1417
},
@@ -17,16 +20,19 @@
1720
"url": "https://github.com/ampproject/filesize.git"
1821
},
1922
"scripts": {
23+
"clean": "rimraf dist output test/output",
24+
"format": "prettier --write '**/*.ts'",
2025
"build-tests": "tsc -p test/tsconfig.json",
26+
"postbuild-tests": "tsc-esm -p test/output",
2127
"pretest": "yarn npm-run-all build build-tests",
2228
"test": "ava -v --timeout=2m",
23-
"clean": "rimraf dist output test/output",
24-
"build": "rollup -c; shx chmod a+x dist/filesize",
29+
"build": "rollup -c; chmod a+x dist/filesize",
2530
"try": "./dist/filesize",
2631
"release": "np --no-2fa",
2732
"prepublishOnly": "yarn npm-run-all clean build"
2833
},
2934
"dependencies": {
35+
"@kristoferbaxter/async": "0.0.7",
3036
"bytes": "3.1.0",
3137
"fast-glob": "3.2.2",
3238
"kleur": "3.0.3",
@@ -41,19 +47,17 @@
4147
"@types/mri": "1.1.0",
4248
"@types/node": "12.12.36",
4349
"ava": "3.7.1",
44-
"husky": "4.2.5",
45-
"lint-staged": "10.1.6",
4650
"np": "https://github.com/pixelastic/np/tarball/c3ab2e3b053c7da0ce40a572ca1616273ac080f8",
4751
"npm-run-all": "4.1.5",
4852
"prettier": "2.0.4",
4953
"rimraf": "3.0.2",
5054
"rollup": "2.6.1",
51-
"shx": "0.3.2",
5255
"tslib": "1.11.1",
53-
"typescript": "3.8.3"
56+
"typescript": "3.8.3",
57+
"typescript-esm": "0.0.6"
5458
},
5559
"volta": {
56-
"node": "12.16.2",
60+
"node": "14.0.0",
5761
"yarn": "1.22.4"
5862
},
5963
"filesize": {
@@ -76,20 +80,9 @@
7680
},
7781
"ava": {
7882
"files": [
79-
"test/output/**/*.test.js"
83+
"test/output/**/*.test.mjs"
8084
]
8185
},
82-
"lint-staged": {
83-
"*.ts": [
84-
"prettier --write"
85-
]
86-
},
87-
"husky": {
88-
"hooks": {
89-
"pre-push": "yarn npm-run-all clean test",
90-
"pre-commit": "lint-staged"
91-
}
92-
},
9386
"publishConfig": {
9487
"access": "public"
9588
},

src/compress.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { cpus } from 'os';
1817
import { Context, Compression, OrderedCompressionValues, maxSize } from './validation/Condition';
1918
import { readFile } from './helpers/fs';
2019
import { Report } from './log/report';
2120
import { compressor } from './compressor';
22-
23-
const COMPRESSION_CONCURRENCY = cpus().length;
21+
import { pool } from '@kristoferbaxter/async';
2422

2523
export interface CompressionItem {
2624
path: string;
@@ -84,31 +82,8 @@ export default async function compress(
8482
return true;
8583
}
8684

87-
const returnable: Array<Promise<boolean>> = [];
88-
const executing: Array<Promise<boolean>> = [];
8985
const reportInstance: Report | null = report ? new report(context) : null;
90-
let success = true;
91-
for (const item of toCompress) {
92-
const promise: Promise<boolean> = Promise.resolve(item).then((item) => compressor(context, reportInstance, item));
93-
returnable.push(promise);
94-
95-
if (COMPRESSION_CONCURRENCY <= toCompress.length) {
96-
const execute: any = promise.then((successful) => {
97-
if (!successful) {
98-
success = successful;
99-
}
100-
executing.splice(executing.indexOf(execute), 1);
101-
});
102-
executing.push(execute);
103-
if (executing.length >= COMPRESSION_CONCURRENCY) {
104-
await Promise.race(executing);
105-
}
106-
}
107-
}
108-
if ((await Promise.all(returnable)).includes(false)) {
109-
success = false;
110-
}
111-
86+
const successful = await pool(toCompress, (item: CompressionItem) => compressor(context, reportInstance, item));
11287
reportInstance?.end();
113-
return success;
88+
return successful.every((success) => success);
11489
}

src/log/helpers/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { write } from './output';
18-
const kleur = require('kleur');
18+
import kleur from 'kleur';
1919

2020
// Disable output colors for test runs.
2121
kleur.enabled = !('AVA_PATH' in process.env);

src/log/helpers/format.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { OrderedCompressionValues, Context } from '../../validation/Condition';
1818
import { prettyBytes } from './bytes';
1919

20-
const COMPRESSED_NAMES_LENGTH = OrderedCompressionValues.map(compression => compression.length);
20+
const COMPRESSED_NAMES_LENGTH = OrderedCompressionValues.map((compression) => compression.length);
2121
const SPACE_AFTER_COMPRESSION = 2;
2222

2323
/**
@@ -47,5 +47,5 @@ export function maxFormatDisplay(context: Context): number {
4747
* @param maxFormatDisplay
4848
*/
4949
export function formats(maxFormatDisplay: number): string {
50-
return OrderedCompressionValues.map(compression => compression.padEnd(maxFormatDisplay)).join('');
50+
return OrderedCompressionValues.map((compression) => compression.padEnd(maxFormatDisplay)).join('');
5151
}

src/validation/File.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { Compression, Context, SizeMapValue, SizeMapValueIndex } from './Condition';
1818
import { resolve, isAbsolute } from 'path';
1919
import { isFile } from '../helpers/fs';
20-
const bytes = require('bytes');
20+
import bytes from 'bytes';
2121

2222
/**
2323
* Format input string to a known Compression Enum Value.

src/validation/Project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { MakeError } from '../log/helpers/error';
2424
* @param context
2525
*/
2626
const Project: ConditionFunction = (context: Context) => {
27-
return async function() {
27+
return async function () {
2828
const projectPath: string = resolve(context.projectPath);
2929
if (!(await isDirectory(projectPath))) {
3030
return MakeError(`project specified '${context.projectPath}' doesn't exist, is this a valid project?`);

test/config-validation/config-validation.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import test from 'ava';
1818
import Config from '../../src/validation/Config';
1919
import { Context } from '../../src/validation/Condition';
2020

21-
test('missing package.json should fail', async t => {
21+
test('missing package.json should fail', async (t) => {
2222
const context: Context = {
2323
packagePath: 'test/config-validation/fixtures/missing-package-json/package.json',
2424
projectPath: 'test/config-validation/fixtures/missing-package-json',
@@ -35,7 +35,7 @@ test('missing package.json should fail', async t => {
3535
t.is(message, `Could not read the configuration in '${context.packagePath}'`);
3636
});
3737

38-
test('unparseable package.json should fail', async t => {
38+
test('unparseable package.json should fail', async (t) => {
3939
const context: Context = {
4040
packagePath: 'test/config-validation/fixtures/unparseable-package-json/package.json',
4141
projectPath: 'test/config-validation/fixtures/unparseable-package-json',
@@ -52,7 +52,7 @@ test('unparseable package.json should fail', async t => {
5252
t.is(message, `Could not parse '${context.packagePath}'`);
5353
});
5454

55-
test("missing 'filesize' key from package.json should fail", async t => {
55+
test("missing 'filesize' key from package.json should fail", async (t) => {
5656
const context: Context = {
5757
packagePath: 'test/config-validation/fixtures/missing-configuration/package.json',
5858
projectPath: 'test/config-validation/fixtures/missing-configuration',
@@ -69,7 +69,7 @@ test("missing 'filesize' key from package.json should fail", async t => {
6969
t.is(message, `There is no 'filesize' configuration in '${context.packagePath}'`);
7070
});
7171

72-
test("missing path from item in 'filesize' should fail", async t => {
72+
test("missing path from item in 'filesize' should fail", async (t) => {
7373
const context: Context = {
7474
packagePath: 'test/config-validation/fixtures/item-path-missing/package.json',
7575
projectPath: 'test/config-validation/fixtures/item-path-missing',
@@ -86,7 +86,7 @@ test("missing path from item in 'filesize' should fail", async t => {
8686
t.is(message, `There is no data inside the 'filesize' configuration in '${context.packagePath}'`);
8787
});
8888

89-
test("missing maxSize from item in 'filesize' should fail", async t => {
89+
test("missing maxSize from item in 'filesize' should fail", async (t) => {
9090
const context: Context = {
9191
packagePath: 'test/config-validation/fixtures/max-size-missing/package.json',
9292
projectPath: 'test/config-validation/fixtures/max-size-missing',
@@ -103,7 +103,7 @@ test("missing maxSize from item in 'filesize' should fail", async t => {
103103
t.is(message, `Configuration for 'index.js' is invalid. (size unspecified)`);
104104
});
105105

106-
test("missing compression from item in 'filesize' should fail", async t => {
106+
test("missing compression from item in 'filesize' should fail", async (t) => {
107107
const context: Context = {
108108
packagePath: 'test/config-validation/fixtures/compression-missing/package.json',
109109
projectPath: 'test/config-validation/fixtures/compression-missing',

test/project-validation/project-validation.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Project from '../../src/validation/Project';
1919
import { resolve } from 'path';
2020
import { Context } from '../../src/validation/Condition';
2121

22-
test('valid directory should pass', async t => {
22+
test('valid directory should pass', async (t) => {
2323
const context: Context = {
2424
packagePath: '',
2525
projectPath: 'test/project-validation/fixtures/contains-package-json',
@@ -36,7 +36,7 @@ test('valid directory should pass', async t => {
3636
t.is(message, null);
3737
});
3838

39-
test('invalid directory should fail', async t => {
39+
test('invalid directory should fail', async (t) => {
4040
const context = {
4141
packagePath: '',
4242
projectPath: 'test/project-validation/fixtures-invalid',
@@ -53,7 +53,7 @@ test('invalid directory should fail', async t => {
5353
t.is(message, `error project specified '${context.projectPath}' doesn't exist, is this a valid project?`);
5454
});
5555

56-
test('directory missing package.json should fail', async t => {
56+
test('directory missing package.json should fail', async (t) => {
5757
const context = {
5858
packagePath: '',
5959
projectPath: 'test/project-validation/fixtures/missing-package-json',

0 commit comments

Comments
 (0)