Skip to content

Commit

Permalink
added Src interface to StaticFile
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed May 24, 2024
1 parent 396aee9 commit e6f3b66
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
1 change: 1 addition & 0 deletions cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export async function build(
solaris: "xdg-open",
illumos: "xdg-open",
windows: "explorer",
android: "xdg-open",
};

new Deno.Command(commands[Deno.build.os], {
Expand Down
15 changes: 8 additions & 7 deletions core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { documentToString, stringToDocument } from "./utils/dom.ts";
import type { MergeStrategy } from "./utils/merge_data.ts";
import type { ProxyComponents } from "./source.ts";
import type { Entry } from "./fs.ts";
import type { Optional } from "./utils/object.ts";

/** A page of the site */
export class Page<D extends Data = Data> {
/** The src info */
src: Src;
src: Optional<Src, "entry">;

/** Used to save the page data */
data: D = {} as D;
Expand Down Expand Up @@ -132,23 +133,23 @@ export class Page<D extends Data = Data> {
}

export interface StaticFile {
/** The Entry instance of the file */
entry: Entry;

/** The final url destination */
outputPath: string;

/** The Src info */
src: Src;
}

/** The .src property for a Page */
/** The .src property for a Page or StaticFile */
export interface Src {
/** The path to the file (without extension) */
path: string;

/** The extension of the file */
ext: string;

/** The original entry instance (empty for pages generated dynamically) */
entry?: Entry;
/** The original entry instance */
entry: Entry;
}

/** The .content property for a Page */
Expand Down
8 changes: 4 additions & 4 deletions core/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ export default class Site {
const pages = this.pages.filter((page) => page.src.entry === entry).map((
page,
) => page.outputPath);
const files = this.files.filter((file) => file.entry === entry).map((
const files = this.files.filter((file) => file.src.entry === entry).map((
file,
) => file.outputPath);
await this.writer.removeFiles([...pages, ...files]);
Expand Down Expand Up @@ -730,7 +730,7 @@ export default class Site {
path = page.data.url;
} else {
// It's a static file
const file = this.files.find((file) => file.entry.path === path);
const file = this.files.find((file) => file.src.entry.path === path);

if (file) {
path = file.outputPath;
Expand Down Expand Up @@ -771,7 +771,7 @@ export default class Site {
const index = this.files.findIndex((f) => f.outputPath === url);

if (index > -1) {
const { entry } = this.files.splice(index, 1)[0];
const { entry } = this.files.splice(index, 1)[0].src;
const data = await entry.getContent(loader) as Data;
const page = Page.create({ ...data, url });
this.pages.push(page);
Expand Down Expand Up @@ -821,7 +821,7 @@ export default class Site {
const staticFile = this.files.find((f) => f.outputPath === file);

if (staticFile) {
return (await staticFile.entry.getContent(loader)).content as
return (await staticFile.src.entry.getContent(loader)).content as
| string
| Uint8Array;
}
Expand Down
17 changes: 15 additions & 2 deletions core/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,15 @@ export default class Source {
}

const outputPath = getOutputPath(entry, destPath, destFn);
yield { entry, outputPath };
const ext = posix.extname(entry.name);
yield {
outputPath,
src: {
ext,
path: entry.path.slice(1, -ext.length),
entry,
},
};
}

if (entry.type === "directory") {
Expand All @@ -455,9 +463,14 @@ export default class Source {
): StaticFile[] {
if (entry.type === "file") {
if (!dirOnly) {
const ext = posix.extname(entry.name);
return [{
entry,
outputPath: getOutputPath(entry, path, dest),
src: {
ext,
path: entry.path.slice(1, -ext.length),
entry,
},
}];
}
return [];
Expand Down
3 changes: 3 additions & 0 deletions core/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export type DeepPartial<T> = T extends object ? {
}
: T;

/** TypeScript helper to make a single property optional */
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;

/** Check if the argument passed is a plain object */
export function isPlainObject(obj: unknown): obj is Record<string, unknown> {
return typeof obj === "object" && obj !== null &&
Expand Down
2 changes: 1 addition & 1 deletion core/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class FSWriter implements Writer {
* Returns a boolean indicating if the file has saved
*/
async copyFile(file: StaticFile): Promise<boolean> {
const { entry } = file;
const { entry } = file.src;

if (entry.flags.has("saved")) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export async function assertSiteSnapshot(
pages.sort((a, b) =>
compare(a.src.path, b.src.path) || compare(a.outputPath, b.outputPath)
);
files.sort((a, b) => compare(a.entry.path, b.entry.path));
files.sort((a, b) => compare(a.src.entry.path, b.src.entry.path));

// Normalize data of the pages
const normalizedPages = pages.map((page) => {
Expand Down Expand Up @@ -188,9 +188,9 @@ export async function assertSiteSnapshot(

const normalizedFiles = files.map((file) => {
return {
...file,
entry: file.entry.path,
flags: [...file.entry.flags],
outputPath: file.outputPath,
entry: file.src.entry.path,
flags: [...file.src.entry.flags],
};
});

Expand Down

0 comments on commit e6f3b66

Please sign in to comment.