Skip to content

Support Comparisons Officially #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import { NoTTYReport } from './log/no-tty-report';

const args = mri(process.argv.slice(2), {
alias: { p: 'project', c: 'config' },
default: { project: process.cwd(), config: '', silent: false },
default: { project: process.cwd(), config: '', silent: false, compare: '' },
});

/**
* Read the configuration from the specified project, validate it, perform requested compression, and report the results.
*/
(async function () {
const { project: projectPath, silent, config: requestedConfig } = args;
const { project: projectPath, silent, config: requestedConfig, compare } = args;
const conditions = [Project, Config];
const context: Context = {
projectPath,
Expand All @@ -47,6 +47,7 @@ const args = mri(process.argv.slice(2), {
compressed: new Map(),
// Stores the basis of comparison.
comparison: new Map(),
comparisonPath: compare,
fileModifier: null,
fileContents: new Map(),
};
Expand Down
73 changes: 73 additions & 0 deletions src/validation/Compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ConditionFunction, Context, SizeMap } from './Condition';
import { isDirectory, isFile, readFile } from '../helpers/fs';
import { resolve, dirname } from 'path';
import { MakeError } from '../log/helpers/error';

/**
* Ensure context contains a valid project directory and `package.json` inside.
* @param context
*/
const Compare: ConditionFunction = (context: Context) => {
return async function () {
if (context.comparisonPath !== '') {
// The comparison path was specified up front
if (!(await isFile(context.comparisonPath))) {
return MakeError(
`comparison path specified '${context.comparisonPath}' doesn't exist, is this a valid comparison?`,
);
}

const comparisonFileContent = await readFile(context.comparisonPath);
if (comparisonFileContent === null) {
return MakeError(
`comparison path specified '${context.comparisonPath}' appears invalid, does it have contents?`,
);
}

context.comparison = new Map(JSON.parse(comparisonFileContent)) as SizeMap;
// TODO: Likely need to do some validation on the contents to ensure its a SizeMap structure.
}

return null;
// if (context.packagePath !== '') {
// // The package path was specified up front, its likely not a package.json
// if (!(await isFile(context.packagePath))) {
// return MakeError(`config specified '${context.packagePath}' doesn't exist, is this a valid project?`);
// }
// context.projectPath = dirname(context.packagePath);
// return null;
// }

// const projectPath: string = resolve(context.projectPath);
// if (!(await isDirectory(projectPath))) {
// return MakeError(`project specified '${context.projectPath}' doesn't exist, is this a valid project?`);
// }

// const packagePath = resolve(context.projectPath, 'package.json');
// if (!(await isFile(packagePath))) {
// return MakeError(`Missing '${packagePath}', is this a valid project?`);
// }

// context.projectPath = projectPath;
// context.packagePath = packagePath;
// return null;
};
};

export default Compare;
2 changes: 2 additions & 0 deletions src/validation/Condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface Context {
compressed: SizeMap;
// Stores the basis of comparison.
comparison: SizeMap;
// The filepath of a comparison map
comparisonPath: string;
// Allows the API to specify a method that alters content before analysis.
fileModifier: FileModifier;
// Stores the contents of files, to avoid reading from disk per compression type.
Expand Down