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

[feat]: Log only outDir path #228

Merged
merged 2 commits into from
Nov 29, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
8 changes: 8 additions & 0 deletions Source/Function/Integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* @module Integration
*
*/
export let systemDir: string;

export default ((...[_Option = {}]: Parameters<Type>) => {
Object.entries(_Option).forEach(([Key, Value]) =>
Object.defineProperty(_Option, Key, {
Expand Down Expand Up @@ -46,6 +48,12 @@ export default ((...[_Option = {}]: Parameters<Type>) => {
return {
name: "astro-compress",
hooks: {
"astro:config:done": async (options) => {
const { dir } = (await import("node:path")).parse(options.config.outDir.pathname);
// normalize slash and remove leading slash that always appears (conditional just in case)
systemDir = dir.replace(/\\/g, '/');
if (systemDir.startsWith("/")) { systemDir = systemDir.substring(1) }
},
"astro:build:done": async ({ dir }) => {
console.log(
`\n${(await import("kleur")).bgGreen(
Expand Down
27 changes: 20 additions & 7 deletions Source/Variable/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
*/
import { blue, green } from "kleur/colors";

import { systemDir } from "../Function/Integration";
import path from "node:path";
function sanitizePath(inputPath: string): { base: string, dir: string } {
let { base, dir } = path.parse(inputPath);
dir = path.normalize(dir)
dir = dir.replace(/\\/g, '/');
dir = dir.replace(systemDir, '');
if (!dir.endsWith("/")) {
dir = dir + '/'
}
return { base, dir };
}

export default (
await import("typescript-esbuild/Target/Function/Merge.js")
).default((await import("files-pipe/Target/Variable/Option.js")).default, {
Expand All @@ -28,17 +41,17 @@ export default (
Parser: (await import("./Parser.js")).default,
Action: {
Failed: async ({ Input }) => {
const idx = Input.lastIndexOf("/");

const { base, dir } = sanitizePath(Input);
return `${red("Error:")} Cannot compress file ${gray(
Input.slice(0, idx + 1)
)}${red(Input.slice(idx + 1))}`;
dir
)}${red(base)}`;
},
Passed: async ({ Before, Buffer: _Buffer }) =>
Before > Buffer.byteLength(_Buffer.toString()),
Accomplished: async ({ Input, Before, After }) => {
const { base, dir } = sanitizePath(Input);

const compressed = Before - After;
const idx = Input.lastIndexOf("/");

console.log("├─ ");

Expand All @@ -48,8 +61,8 @@ export default (
).default(compressed)})`
)} ${green(
`${((compressed / Before) * 100).toFixed(2)}%`
)} reduction in ${gray(Input.slice(0, idx + 1))}${blue(
Input.slice(idx + 1)
)} reduction in ${gray(dir)}${blue(
base
)}`;

return msg;
Expand Down