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 option to retain testing URL's directory structure #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 30 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function execute(options) {
});
} catch (e) {}

mkdir("-p", out);
fs.mkdirSync(out, { recursive: true });

let budgetErrors = [];
const count = options.sites.length;
Expand All @@ -50,6 +50,8 @@ function execute(options) {
const htmlOut = options.html ? " --output html" : "";
const csvOut = options.csv ? " --output csv" : "";
const filePath = path.join(out, site.file);
const parentFolder = path.dirname(filePath);
fs.mkdirSync(parentFolder, { recursive: true });
const customParams = options.params || "";
const chromeFlags =
customParams.indexOf("--chrome-flags=") === -1
Expand Down Expand Up @@ -139,7 +141,7 @@ function sitesInfo(options) {
if (!url.startsWith("//")) url = `//${url}`;
url = `https:${url}`;
}
const origName = siteName(url);
const origName = siteName(url, options);
let name = origName;

// if the same page is being tested multiple times then
Expand Down Expand Up @@ -189,23 +191,33 @@ function lighthouseScript(options, log) {
return `node ${cliPath}`;
}

function siteName(site) {
const maxLength = 100;
let name = site
.replace(/^https?:\/\//, "")
.replace(/[\/\?#:\*\$@\!\.]/g, "_");

if (name.length > maxLength) {
const hash = crypto
.createHash("sha1")
.update(name)
.digest("hex")
.slice(0, 7);

name = name.slice(0, maxLength).replace(/_+$/g, ""); // trim any `_` suffix
name = `${name}_${hash}`;
function siteName(site, options) {
if (options.retainUrlDirectory) {
const fullName = (site.endsWith("/") ? site.slice(0, -1) : site)
.replace(/^https?:\/\//, "")
.replace(/[\?#:\*\$@\!\.]/g, "_");
let splittedName = fullName.split("/");
splittedName[splittedName.length - 1] = hashNameIfTooLong(
splittedName[splittedName.length - 1]
);
return splittedName.join("/");
} else {
let name = site
.replace(/^https?:\/\//, "")
.replace(/[\/\?#:\*\$@\!\.]/g, "_");

return hashNameIfTooLong(name);
}
return name;
}

function hashNameIfTooLong(name) {
const maxLength = 100;

if (name.length <= maxLength) return name;

const hash = crypto.createHash("sha1").update(name).digest("hex").slice(0, 7);
name = name.slice(0, maxLength).replace(/_+$/g, ""); // trim any `_` suffix
return `${name}_${hash}`;
}

function updateSummary(filePath, summary, outcome, options) {
Expand Down
4 changes: 4 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ program
"-o, --out [out]",
`the output folder to place reports, defaults to '${execute.OUT}'`
)
.option(
"--retain-url-directory",
"retain the path of url as sub-directories, instead of replace slash with underscore"
)
.option(
"--score <threshold>",
`average score for each site to meet (1-100)`,
Expand Down