Skip to content
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

Add controls on config file #59

Merged
merged 5 commits into from
Sep 4, 2023
Merged
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
3 changes: 2 additions & 1 deletion src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ERROR_CODES from '../services/errors/errorCodes';
import logErrorOnSentry from '../services/errors/Sentry';

import { DEFAULT_SAMPLES } from '../constants';
export const DEFAULT_CONFIG_FILE = './.greenframe.yml';

import createNewAnalysis from '../tasks/createNewAnalysis';
import detectDockerVersion from '../tasks/detectDockerVersion';
Expand All @@ -34,7 +35,7 @@ class AnalyzeCommand extends Command {
];

static defaultFlags = {
configFile: './.greenframe.yml',
configFile: DEFAULT_CONFIG_FILE,
samples: DEFAULT_SAMPLES,
distant: false,
useAdblock: false,
Expand Down
4 changes: 2 additions & 2 deletions src/examples/greenframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const greenframe = async (page) => {
page.waitForNavigation({
/* url: 'https://greenframe.io/', */ waitUntil: 'networkidle',
}),
page.scrollToElement('text=Try for free'),
page.click('text=Try for free'),
page.scrollToElement('text=Try it for free'),
page.click('text=Try it for free'),
]);
await page.addMilestone('Go Back home');

Expand Down
27 changes: 24 additions & 3 deletions src/services/parseConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ const fs = require('node:fs');
const yaml = require('js-yaml');
const util = require('node:util');
const ConfigurationError = require('./errors/ConfigurationError');
const analyze = require('../commands/analyze');
const FILE_NOT_FOUND = 'ENOENT';

const readFile = util.promisify(fs.readFile);

const isMissingDefaultConfigFile = (path, error) => {
return path === analyze.DEFAULT_CONFIG_FILE && error.code === FILE_NOT_FOUND;
};

const parseConfigFile = async (path) => {
try {
const file = await readFile(path, 'utf8');
let fileContent;

if (file) {
fileContent = yaml.load(file);
}

if (typeof fileContent !== 'object') {
throw new yaml.YAMLException(`${path} is not a valid yaml`);
}

if (fileContent) {
const {
scenario,
scenarios,
Expand All @@ -31,7 +47,8 @@ const parseConfigFile = async (path) => {
ignoreHTTPSErrors,
locale,
timezoneId,
} = yaml.load(file);
} = fileContent;

return {
args: {
scenarios,
Expand Down Expand Up @@ -60,8 +77,12 @@ const parseConfigFile = async (path) => {
},
};
}
} catch {
// Do Nothing
} catch (error) {
if (error.name === 'YAMLException') {
throw new yaml.YAMLException(`${path} is not a valid yaml`);
} else if (!isMissingDefaultConfigFile(path, error)) {
throw error;
}
}
};

Expand Down
Loading