Skip to content

Commit

Permalink
Fix counts to show only writes (not fetches)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Nov 13, 2024
1 parent 84a4336 commit f1aa241
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
10 changes: 5 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ let entries = await importer.getEntries({
importer.toFiles(entries);

// Log results
let { fetches, errors } = importer.getCounts();
let counts = importer.getCounts();
let sourcesDisplay = importer.getSources().map(source => source.constructor.TYPE_FRIENDLY || source.constructor.TYPE).join(", ");

let content = [];
content.push(kleur.green("Imported"));
content.push(kleur.green(Logger.plural(entries.length, "document")));
content.push(kleur.green("Wrote"));
content.push(kleur.green(Logger.plural(counts.files, "document")));
content.push(kleur.green("and"));
content.push(kleur.green(Logger.plural(fetches.assets, "asset")));
content.push(kleur.green(Logger.plural(counts.assets, "asset")));
content.push(kleur.green(`from ${sourcesDisplay}`));
content.push(kleur[errors > 0 ? "red" : "gray"](`(${Logger.plural(errors, "error")})`));
content.push(kleur[counts.errors > 0 ? "red" : "gray"](`(${Logger.plural(counts.errors, "error")})`));
content.push(`in ${Logger.time(Date.now() - start)}`);

Logger.log(content.join(" "));
29 changes: 14 additions & 15 deletions src/Fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class Fetcher {
this.isVerbose = true;
this.dryRun = false;
this.safeMode = true;
this.counts = {
assets: 0,
};
}

setVerbose(isVerbose) {
Expand All @@ -70,14 +73,8 @@ class Fetcher {
}

getCounts() {
let total = this.fetchedUrls.size;
let assets = this.writtenAssetFiles.size;

return {
fetches: {
data: total - assets,
assets,
},
assets: this.counts.assets,
errors: this.errors.size,
}
}
Expand Down Expand Up @@ -107,19 +104,19 @@ class Fetcher {
let fullOutputLocation = path.join(outputFolder, assetUrlLocation);
let urlValue = `/${assetUrlLocation}`;

if(!result?.body || this.writtenAssetFiles.has(fullOutputLocation)) {
if(this.writtenAssetFiles.has(fullOutputLocation)) {
return urlValue;
}

this.writtenAssetFiles.add(fullOutputLocation);

if(this.safeMode && fs.existsSync(fullOutputLocation)) {
if(this.isVerbose) {
Logger.skipping("asset", fullOutputLocation, url);
}
return urlValue;
}

this.writtenAssetFiles.add(fullOutputLocation);

if(this.#directoryManager) {
this.#directoryManager.createDirectoryForPath(fullOutputLocation);
}
Expand All @@ -132,6 +129,8 @@ class Fetcher {
}

if(!this.dryRun) {
this.counts.assets++;

fs.writeFileSync(fullOutputLocation, result.body);
}

Expand All @@ -151,20 +150,20 @@ class Fetcher {
fetchOptions: {},
}, options);

if(!this.fetchedUrls.has(url)) {
if(!this.fetchedUrls.has(url) && this.isVerbose && verbose) {
let logAdds = [];
if(Boolean(options?.fetchOptions?.headers?.Authorization)) {
logAdds.push(kleur.blue("Auth"));
}
if(opts.duration) {
logAdds.push(kleur.green(`(${opts.duration} cache)`));
}
if(this.isVerbose && verbose) {
Logger.log(kleur.gray("Fetching"), url, logAdds.join(" ") );
}
this.fetchedUrls.add(url);

Logger.log(kleur.gray("Fetching"), url, logAdds.join(" ") );
}

this.fetchedUrls.add(url);

if(!opts.fetchOptions.headers) {
opts.fetchOptions.headers = {};
}
Expand Down
20 changes: 12 additions & 8 deletions src/Importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Importer {
this.isVerbose = true;
this.dryRun = false;
this.safeMode = true;
this.skipCount = 0;
this.counts = {
files: 0
};

this.markdownService = new MarkdownToHtml();
this.htmlTransformer = new HtmlTransformer();
Expand All @@ -39,6 +41,8 @@ class Importer {

setSafeMode(safeMode) {
this.safeMode = Boolean(safeMode);

this.fetcher.setSafeMode(safeMode);
}

setDryRun(isDryRun) {
Expand All @@ -60,7 +64,7 @@ class Importer {

getCounts() {
return {
skipped: this.skipCount,
...this.counts,
...this.fetcher.getCounts(),
}
}
Expand Down Expand Up @@ -280,7 +284,6 @@ ${entry.content}`
Logger.skipping("post", pathname, entry.url);
}

this.skipCount++;
continue;
}

Expand All @@ -289,10 +292,6 @@ ${entry.content}`
}
filepathConflicts[pathname] = entry.url || true;

if(!this.dryRun) {
this.directoryManager.createDirectoryForPath(pathname);
}

if(this.isVerbose) {
Logger.importing("post", pathname, entry.url, {
size: content.length,
Expand All @@ -301,8 +300,13 @@ ${entry.content}`
}

if(!this.dryRun) {
fs.writeFileSync(pathname, content, { recursive: true, encoding: "utf8" });
this.counts.files++;

this.directoryManager.createDirectoryForPath(pathname);

fs.writeFileSync(pathname, content, { encoding: "utf8" });
}

filesWrittenCount++;
}
}
Expand Down

0 comments on commit f1aa241

Please sign in to comment.