Skip to content

Commit 16de5d6

Browse files
Add -c config option for users preferring to avoid package.json (#87)
* Add -c config option for users preferring to avoid package.json * Pushed Changes from Github Actions Bot * Support config in API, add success and failure tests * Pushed Changes from Github Actions Bot * Update readme
1 parent caf734b commit 16de5d6

File tree

13 files changed

+222
-17
lines changed

13 files changed

+222
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Each file (key in the filesize object) must include an object with key/value pai
3232

3333
**After completing configuration**, invoke `filesize` via: `yarn filesize`.
3434

35-
Optionally one can target a different project directory via the `p` parameter `yarn filesize -p={PATH}`.
35+
Optionally one can target a different project directory via the `p` parameter `yarn filesize -p={PATH}`, or a different configuration file via the `c` parameter `yarn filesize -c=${PATH/filesize.json}`.
3636

3737
### Track Resource Size
3838

src/api.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as path from 'path';
1718
import Project from './validation/Project';
1819
import Config from './validation/Config';
1920
import { Context, FileModifier, SizeMap } from './validation/Condition';
@@ -22,14 +23,23 @@ import { Report } from './log/report';
2223
export { Report } from './log/report';
2324

2425
export async function report(
25-
projectPath: string,
26+
configOrProjectPath: string,
2627
fileModifier: FileModifier,
2728
report?: typeof Report,
2829
): Promise<SizeMap> {
30+
let projectPath = '';
31+
let packagePath = '';
32+
if (path.extname(configOrProjectPath) === '.json') {
33+
// The requested config or project path is a config.
34+
packagePath = configOrProjectPath;
35+
} else {
36+
projectPath = configOrProjectPath;
37+
}
38+
2939
const conditions = [Project, Config];
3040
let context: Context = {
3141
projectPath,
32-
packagePath: '',
42+
packagePath,
3343
packageContent: '',
3444
silent: true,
3545
originalPaths: new Map(),

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ import { TTYReport } from './log/tty-report';
2727
import { NoTTYReport } from './log/no-tty-report';
2828

2929
const args = mri(process.argv.slice(2), {
30-
alias: { p: 'project' },
31-
default: { project: process.cwd(), silent: false },
30+
alias: { p: 'project', c: 'config' },
31+
default: { project: process.cwd(), config: '', silent: false },
3232
});
3333

3434
/**
3535
* Read the configuration from the specified project, validate it, perform requested compression, and report the results.
3636
*/
3737
(async function () {
38-
const { project: projectPath, silent } = args;
38+
const { project: projectPath, silent, config: requestedConfig } = args;
3939
const conditions = [Project, Config];
4040
const context: Context = {
4141
projectPath,
42-
packagePath: '',
42+
packagePath: requestedConfig,
4343
packageContent: '',
4444
silent,
4545
originalPaths: new Map(),

src/validation/Project.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { ConditionFunction, Context } from './Condition';
1818
import { isDirectory, isFile } from '../helpers/fs';
19-
import { resolve } from 'path';
19+
import { resolve, dirname } from 'path';
2020
import { MakeError } from '../log/helpers/error';
2121

2222
/**
@@ -25,12 +25,21 @@ import { MakeError } from '../log/helpers/error';
2525
*/
2626
const Project: ConditionFunction = (context: Context) => {
2727
return async function () {
28+
if (context.packagePath !== '') {
29+
// The package path was specified up front, its likely not a package.json
30+
if (!(await isFile(context.packagePath))) {
31+
return MakeError(`config specified '${context.packagePath}' doesn't exist, is this a valid project?`);
32+
}
33+
context.projectPath = dirname(context.packagePath);
34+
return null;
35+
}
36+
2837
const projectPath: string = resolve(context.projectPath);
2938
if (!(await isDirectory(projectPath))) {
3039
return MakeError(`project specified '${context.projectPath}' doesn't exist, is this a valid project?`);
3140
}
3241

33-
const packagePath: string = resolve(context.projectPath, 'package.json');
42+
const packagePath = resolve(context.projectPath, 'package.json');
3443
if (!(await isFile(packagePath))) {
3544
return MakeError(`Missing '${packagePath}', is this a valid project?`);
3645
}

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test("missing maxSize from item in 'filesize' should fail", async (t) => {
100100
};
101101
const message = await Config(context)();
102102

103-
t.is(message, `Configuration for 'index.js' is invalid. (size unspecified)`);
103+
t.is(message, "Configuration for 'index.js' is invalid. (size unspecified)");
104104
});
105105

106106
test("missing compression from item in 'filesize' should fail", async (t) => {
@@ -117,5 +117,39 @@ test("missing compression from item in 'filesize' should fail", async (t) => {
117117
};
118118
const message = await Config(context)();
119119

120-
t.is(message, `Configuration for 'index.js' is invalid. (compression values unspecified)`);
120+
t.is(message, "Configuration for 'index.js' is invalid. (compression values unspecified)");
121+
});
122+
123+
test('standalone configuration file when valid should pass', async (t) => {
124+
const context: Context = {
125+
packagePath: 'test/config-validation/fixtures/standalone-config/filesize.json',
126+
projectPath: '',
127+
packageContent: '',
128+
originalPaths: new Map(),
129+
compressed: new Map(),
130+
comparison: new Map(),
131+
silent: false,
132+
fileModifier: null,
133+
fileContents: new Map(),
134+
};
135+
const message = await Config(context)();
136+
137+
t.is(message, null);
138+
});
139+
140+
test('standalone configuration file when path is invalid should fail', async (t) => {
141+
const context: Context = {
142+
packagePath: 'test/config-validation/fixtures/standalone-config/invalid.json',
143+
projectPath: '',
144+
packageContent: '',
145+
originalPaths: new Map(),
146+
compressed: new Map(),
147+
comparison: new Map(),
148+
silent: false,
149+
fileModifier: null,
150+
fileContents: new Map(),
151+
};
152+
const message = await Config(context)();
153+
154+
t.is(message, `Could not read the configuration in '${context.packagePath}'`);
121155
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"filesize": {
3+
"track": ["test/config-validation/fixtures/standalone-config/**/*.js"]
4+
}
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"filesize": {
3+
"track": ["test/end-to-end/fixtures/api-report/**/*.js"],
4+
"trackFormat": ["brotli"]
5+
}
6+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"filesize": {
3+
"dist/index.js": {
4+
"brotli": "15 kB",
5+
"gzip": "20 kB",
6+
"none": "500 kB"
7+
},
8+
"dist/foo.js": {
9+
"brotli": "15 kB",
10+
"gzip": "20 kB",
11+
"none": "500 kB"
12+
},
13+
"dist/bar.js": {
14+
"brotli": "15 kB",
15+
"gzip": "20 kB",
16+
"none": "500 kB"
17+
},
18+
"dist/baz.js": {
19+
"brotli": "15 kB",
20+
"gzip": "20 kB",
21+
"none": "500 kB"
22+
}
23+
}
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"filesize": {
3+
"index.js": {
4+
"none": "500 kB"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)