Skip to content

Commit 96e8e5d

Browse files
Add API for future use with Github Actions
1 parent 42a65bc commit 96e8e5d

File tree

16 files changed

+169
-31
lines changed

16 files changed

+169
-31
lines changed

api.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Project from './validation/Project';
18+
import Config from './validation/Config';
19+
import { Context, ItemConfig, CompressionMap } from './validation/Condition';
20+
import compress from './compress';
21+
22+
export async function report(project: string): Promise<Map<ItemConfig['path'], CompressionMap>> {
23+
const conditions = [Project, Config];
24+
let context: Context = {
25+
project,
26+
package: '',
27+
config: [],
28+
silent: true,
29+
};
30+
31+
for (const condition of conditions) {
32+
const [success, message] = await condition(context)();
33+
if (!success) {
34+
throw message;
35+
}
36+
}
37+
38+
const { 1: report } = await compress(context);
39+
return report;
40+
}

compress.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async function compressor(item: ItemConfig): Promise<boolean> {
9595
* Given a context, compress all Items within splitting work eagly per cpu core to achieve some concurrency.
9696
* @param context Finalized Valid Context from Configuration
9797
*/
98-
export default async function compress(context: Context): Promise<boolean> {
98+
export default async function compress(context: Context): Promise<[boolean, Map<ItemConfig['path'], CompressionMap>]> {
9999
initReport(context.config);
100100

101101
let success: boolean = true;
@@ -106,6 +106,6 @@ export default async function compress(context: Context): Promise<boolean> {
106106
}
107107
}
108108

109-
LogReport(reported);
110-
return success;
109+
LogReport(context, reported);
110+
return [success, reported];
111111
}

index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const args = mri(process.argv.slice(2), {
3535
project,
3636
package: '',
3737
config: [],
38+
silent: false,
3839
};
3940

4041
for (const condition of conditions) {
@@ -45,7 +46,7 @@ const args = mri(process.argv.slice(2), {
4546
}
4647
}
4748

48-
const compressionSuccess = await compress(context);
49+
const [compressionSuccess] = await compress(context);
4950
if (!compressionSuccess) {
5051
process.exit(6);
5152
}

log.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616

1717
const kleur = require('kleur');
18-
import bytes from 'bytes';
19-
import { ItemConfig, CompressionMap } from './validation/Condition';
18+
const bytes = require('bytes');
19+
import { ItemConfig, CompressionMap, Context } from './validation/Condition';
2020

2121
// Disable output colors for test runs.
2222
kleur.enabled = !('AVA_PATH' in process.env);
@@ -63,11 +63,11 @@ function compressedExtension(compression: string): string {
6363
* Display report to the console.
6464
* @param report
6565
*/
66-
export function LogReport(report: Map<ItemConfig['path'], CompressionMap>) {
66+
export function LogReport({ silent }: Context, report: Map<ItemConfig['path'], CompressionMap>) {
6767
let success: number = 0;
6868
let failure: number = 0;
6969

70-
if ([...report.keys()].length > 0) {
70+
if (!silent && [...report.keys()].length > 0) {
7171
console.log(bold('\nFilesize Report'));
7272
for (const [originalPath, values] of report) {
7373
console.log(grey(`\npath: ${originalPath}`));
@@ -86,11 +86,12 @@ export function LogReport(report: Map<ItemConfig['path'], CompressionMap>) {
8686
}
8787
}
8888
if (success > 0 || failure > 0) {
89-
console.log('\n ' + green(success + ` ${success === 1 ? 'check' : 'checks'} passed`));
90-
console.log(' ' + red(failure + ` ${failure === 1 ? 'check' : 'checks'} failed`) + (failure === 0 ? ' 🎉' : ''));
89+
console.log('\n ' + green(success + ` ${success === 1 ? 'check' : 'checks'} passed`) + (failure === 0 ? ' 🎉' : ''));
90+
const failureColor = failure < 1 ? grey : red;
91+
console.log(' ' + failureColor(failure + ` ${failure === 1 ? 'check' : 'checks'} failed`));
9192
}
9293
console.log();
93-
} else {
94+
} else if (!silent) {
9495
MakeError('No report available.');
9596
}
9697
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"author": "Kristofer Baxter",
66
"license": "Apache-2.0",
77
"main": "dist/filesize",
8+
"module": "dist/api.mjs",
89
"files": [
910
"dist"
1011
],
@@ -73,7 +74,7 @@
7374
{
7475
"path": "./dist/index.js",
7576
"compression": "brotli",
76-
"maxSize": "4 kB"
77+
"maxSize": "10 kB"
7778
}
7879
],
7980
"ava": {

rollup.config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ const external = ['os', 'zlib', 'path', 'fs'];
3737
const plugins = executable => [
3838
resolve({ preferBuiltins: true }),
3939
commonjs({ include: 'node_modules/**' }),
40-
typescript({ include: '**/*.ts' }),
41-
compiler(),
40+
typescript({ include: '**/*.ts', exclude: 'dist/**/*.ts' }),
41+
executable ? compiler() : null,
4242
executable ? makeExecutable() : null,
4343
];
4444

@@ -63,4 +63,14 @@ export default [
6363
external,
6464
plugins: plugins(false),
6565
},
66+
{
67+
input: 'api.ts',
68+
output: {
69+
file: 'dist/api.mjs',
70+
format: 'esm',
71+
sourcemap: true,
72+
},
73+
external,
74+
plugins: plugins(false),
75+
}
6676
];

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ test('missing package.json should fail', async t => {
2222
package: 'test/config-validation/fixtures/missing-package-json/package.json',
2323
config: [],
2424
project: 'test/config-validation/fixtures/missing-package-json',
25+
silent: false,
2526
};
2627
const [success, message] = await Config(context)();
2728

@@ -34,6 +35,7 @@ test('unparseable package.json should fail', async t => {
3435
package: 'test/config-validation/fixtures/unparseable-package-json/package.json',
3536
config: [],
3637
project: 'test/config-validation/fixtures/unparseable-package-json',
38+
silent: false,
3739
};
3840
const [success, message] = await Config(context)();
3941

@@ -46,6 +48,7 @@ test("missing 'filesize' key from package.json should fail", async t => {
4648
package: 'test/config-validation/fixtures/missing-configuration/package.json',
4749
config: [],
4850
project: 'test/config-validation/fixtures/missing-configuration',
51+
silent: false,
4952
};
5053
const [success, message] = await Config(context)();
5154

@@ -58,6 +61,7 @@ test("missing path from item in 'filesize' should fail", async t => {
5861
package: 'test/config-validation/fixtures/item-path-missing/package.json',
5962
config: [],
6063
project: 'test/config-validation/fixtures/item-path-missing',
64+
silent: false,
6165
};
6266
const [success, message] = await Config(context)();
6367

@@ -70,6 +74,7 @@ test("missing maxSize from item in 'filesize' should fail", async t => {
7074
package: 'test/config-validation/fixtures/max-size-missing/package.json',
7175
config: [],
7276
project: 'test/config-validation/fixtures/max-size-missing',
77+
silent: false,
7378
};
7479
const [success, message] = await Config(context)();
7580

@@ -82,6 +87,7 @@ test("missing compression from item in 'filesize' should fail", async t => {
8287
package: 'test/config-validation/fixtures/compression-missing/package.json',
8388
config: [],
8489
project: 'test/config-validation/fixtures/compression-missing',
90+
silent: false,
8591
};
8692
const [success, message] = await Config(context)();
8793

test/end-to-end/fixtures/successful/index.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"filesize": [
3+
{
4+
"path": "test/end-to-end/fixtures/successful/index.js",
5+
"maxSize": "10 KB",
6+
"compression": "none"
7+
},
8+
{
9+
"path": "test/end-to-end/fixtures/successful/index.js",
10+
"maxSize": "4 KB",
11+
"compression": "gzip"
12+
},
13+
{
14+
"path": "test/end-to-end/fixtures/successful/index.js",
15+
"maxSize": "3.5 KB",
16+
"compression": "brotli"
17+
}
18+
]
19+
}

test/end-to-end/successful.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import test from 'ava';
18+
import { exec } from 'child_process';
19+
import { report } from '../../api';
20+
import { CompressionMap } from '../../validation/Condition';
21+
22+
test.cb('item under requested filesize limit passes', t => {
23+
const executeFailure = exec('./dist/filesize -p=test/end-to-end/fixtures/successful');
24+
25+
executeFailure.on('exit', code => {
26+
t.is(code, 0);
27+
t.end();
28+
});
29+
});
30+
31+
test('item under requested filesize limit passes from API', async t => {
32+
const toReport = 'test/end-to-end/fixtures/successful';
33+
34+
const sizes: CompressionMap = new Map([
35+
['brotli', [3410, 3584]],
36+
['gzip', [3737, 4096]],
37+
['none', [9327, 10240]],
38+
]);
39+
const expected: Map<string, CompressionMap> = new Map([['test/end-to-end/fixtures/successful/index.js', sizes]]);
40+
const results = await report(toReport);
41+
t.deepEqual(results, expected);
42+
});

0 commit comments

Comments
 (0)