Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-sull authored and astrobot-houston committed Aug 22, 2022
1 parent c4af872 commit 2c40fba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
34 changes: 26 additions & 8 deletions packages/integrations/image/src/build/ssg.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { bgGreen, black, cyan, dim, green, bold } from 'kleur/colors';
import { bgGreen, black, cyan, dim, green } from 'kleur/colors';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { OUTPUT_DIR } from '../constants.js';
import type { SSRImageService, TransformOptions } from '../loaders/index.js';
import { isRemoteImage, loadLocalImage, loadRemoteImage } from '../utils/images.js';
import { debug, info, LoggerLevel, warn } from '../utils/logger.js';
import { ensureDir } from '../utils/paths.js';
import { debug, info, warn, LoggerLevel } from '../utils/logger.js';

function getTimeStat(timeStart: number, timeEnd: number) {
const buildTime = timeEnd - timeStart;
Expand All @@ -24,8 +24,14 @@ export interface SSGBuildParams {
export async function ssgBuild({ loader, staticImages, srcDir, outDir, logLevel }: SSGBuildParams) {
const timer = performance.now();

info({ level: logLevel, prefix: false, message: `${bgGreen(black(` optimizing ${staticImages.size} image${staticImages.size > 1 ? 's' : ''} `))}` });

info({
level: logLevel,
prefix: false,
message: `${bgGreen(
black(` optimizing ${staticImages.size} image${staticImages.size > 1 ? 's' : ''} `)
)}`,
});

const inputFiles = new Set<string>();

// process transforms one original image file at a time
Expand All @@ -47,7 +53,7 @@ export async function ssgBuild({ loader, staticImages, srcDir, outDir, logLevel

if (!inputBuffer) {
// eslint-disable-next-line no-console
warn({ level: logLevel, message : `"${src}" image could not be fetched` });
warn({ level: logLevel, message: `"${src}" image could not be fetched` });
continue;
}

Expand All @@ -65,7 +71,11 @@ export async function ssgBuild({ loader, staticImages, srcDir, outDir, logLevel
const timeChange = getTimeStat(timeStart, timeEnd);
const timeIncrease = `(+${timeChange})`;
const pathRelative = inputFile.replace(fileURLToPath(srcDir), '');
debug({ level: logLevel, prefix: false, message: ` ${cyan('└─')} ${dim(`(original) ${pathRelative}`)} ${dim(timeIncrease)}` });
debug({
level: logLevel,
prefix: false,
message: ` ${cyan('└─')} ${dim(`(original) ${pathRelative}`)} ${dim(timeIncrease)}`,
});
}

// process each transformed versiono of the
Expand All @@ -91,9 +101,17 @@ export async function ssgBuild({ loader, staticImages, srcDir, outDir, logLevel
const timeChange = getTimeStat(timeStart, timeEnd);
const timeIncrease = `(+${timeChange})`;
const pathRelative = outputFile.replace(fileURLToPath(outDir), '');
debug({ level: logLevel, prefix: false, message: ` ${cyan('└─')} ${dim(pathRelative)} ${dim(timeIncrease)}` });
debug({
level: logLevel,
prefix: false,
message: ` ${cyan('└─')} ${dim(pathRelative)} ${dim(timeIncrease)}`,
});
}
}

info({ level: logLevel, prefix: false, message: (dim(`Completed in ${getTimeStat(timer, performance.now())}.\n`)) });
info({
level: logLevel,
prefix: false,
message: dim(`Completed in ${getTimeStat(timer, performance.now())}.\n`),
});
}
8 changes: 7 additions & 1 deletion packages/integrations/image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
const loader = globalThis?.astroImage?.loader;

if (loader && 'transform' in loader && staticImages.size > 0) {
await ssgBuild({ loader, staticImages, srcDir: _config.srcDir, outDir: dir, logLevel: resolvedOptions.logLevel });
await ssgBuild({
loader,
staticImages,
srcDir: _config.srcDir,
outDir: dir,
logLevel: resolvedOptions.logLevel,
});
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions packages/integrations/image/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ function getPrefix(level: LoggerLevel, timestamp: boolean) {
return prefix;
}

const log = (_level: LoggerLevel, dest: (message: string) => void) =>
const log =
(_level: LoggerLevel, dest: (message: string) => void) =>
({ message, level, prefix = true, timestamp = true }: LogMessage) => {
if (levels[_level] >= levels[level]) {
dest(`${prefix ? getPrefix(level, timestamp) : ''}${message}`);
}
}
};

export const info = log('info', console.info);
export const debug = log('debug', console.debug);
export const warn = log('warn', console.warn);
export const error = log('error', console.error);

0 comments on commit 2c40fba

Please sign in to comment.