Skip to content

Commit

Permalink
feat: added file loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
eser committed Jan 14, 2024
1 parent 1eaf066 commit 773eecc
Show file tree
Hide file tree
Showing 42 changed files with 497 additions and 278 deletions.
10 changes: 5 additions & 5 deletions _etc/tasks/check-license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { walk } from "./deps.ts";
const EXTENSIONS = ["*.js", ".ts", "*.jsx", ".tsx"];

const ROOT = new URL("../../", import.meta.url);
const CHECK = runtime.args.includes("--check");
const CHECK = runtime.current.getArgs().includes("--check");
const BASE_YEAR = "2023";
// const CURRENT_YEAR = new Date().getFullYear();
const RX_COPYRIGHT = new RegExp(
Expand All @@ -30,7 +30,7 @@ for await (
includeDirs: false,
})
) {
const content = await runtime.readTextFile(entry.path);
const content = await runtime.current.readTextFile(entry.path);
const match = content.match(RX_COPYRIGHT);

if (!match) {
Expand All @@ -39,7 +39,7 @@ for await (
failed = true;
} else {
const contentWithCopyright = COPYRIGHT + "\n" + content;
await runtime.writeTextFile(entry.path, contentWithCopyright);
await runtime.current.writeTextFile(entry.path, contentWithCopyright);
console.log("Copyright header automatically added to " + entry.path);
}
} else if (match[1] !== BASE_YEAR) {
Expand All @@ -51,13 +51,13 @@ for await (
const contentWithoutCopyright = content.replace(match[0], "");
const contentWithCopyright = contentWithoutCopyright.substring(0, index) +
COPYRIGHT + "\n" + contentWithoutCopyright.substring(index);
await runtime.writeTextFile(entry.path, contentWithCopyright);
await runtime.current.writeTextFile(entry.path, contentWithCopyright);
console.log("Copyright header automatically updated in " + entry.path);
}
}
}

if (failed) {
console.info(`Copyright header should be "${COPYRIGHT}"`);
runtime.exit(1);
runtime.current.exit(1);
}
2 changes: 1 addition & 1 deletion _etc/tasks/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as walk from "https://deno.land/std@0.211.0/fs/walk.ts";
export * as walk from "https://deno.land/std@0.212.0/fs/walk.ts";
9 changes: 8 additions & 1 deletion appserver/appserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export class AppServer {
di: typeof di;
channels: Map<string, Channel>;
modules: Map<string, Module>;
// deno-lint-ignore no-explicit-any
awaits: Array<Promise<any>> = [];

constructor() {
this.runMode = runModes.RunMode.Development;
this.runMode = runModes.RunMode.NotSet;
this.events = events;
this.di = di;
this.channels = new Map<string, Channel>();
Expand All @@ -40,6 +42,11 @@ export class AppServer {
this.di.register(AppServer.default, this);
}

async awaitAll() {
await Promise.all(this.awaits);
this.awaits.splice(0);
}

// execute(_options: unknown) {
// }
}
2 changes: 1 addition & 1 deletion appserver/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as semver from "https://deno.land/std@0.211.0/semver/mod.ts";
export * as semver from "https://deno.land/std@0.212.0/semver/mod.ts";
8 changes: 4 additions & 4 deletions appserver/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
export interface Module {
name?: string;

manifest: unknown; // TODO(@eser): type this
manifest: unknown; // TODO(@eser) type this

uses?: ReadonlyArray<string>; // TODO(@eser): type this
provides: ReadonlyArray<unknown>; // TODO(@eser): type this
uses?: ReadonlyArray<string>; // TODO(@eser) type this
provides: ReadonlyArray<unknown>; // TODO(@eser) type this

entrypoint: () => void; // TODO(@eser): type this
entrypoint: () => void; // TODO(@eser) type this
}
4 changes: 2 additions & 2 deletions collector/deps-dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as assert from "https://deno.land/std@0.211.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.211.0/testing/bdd.ts";
export * as assert from "https://deno.land/std@0.212.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.212.0/testing/bdd.ts";
6 changes: 3 additions & 3 deletions collector/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as path from "https://deno.land/std@0.211.0/path/mod.ts";
export * as posix from "https://deno.land/std@0.211.0/path/posix/mod.ts";
export * as walk from "https://deno.land/std@0.211.0/fs/walk.ts";
export * as path from "https://deno.land/std@0.212.0/path/mod.ts";
export * as posix from "https://deno.land/std@0.212.0/path/posix/mod.ts";
export * as walk from "https://deno.land/std@0.212.0/fs/walk.ts";
2 changes: 1 addition & 1 deletion collector/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as runtime from "../standards/runtime.ts";

export const format = async (input: string) => {
const proc = new runtime.Command(runtime.execPath(), {
const proc = new runtime.current.Command(runtime.current.execPath(), {
args: ["fmt", "-"],
stdin: "piped",
stdout: "piped",
Expand Down
4 changes: 2 additions & 2 deletions collector/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const writeManifestToString = async (
const used = new Set<string>();
const imports = [];
const manifest = {
baseUrl: placeholder("import.meta.url"),
// baseUrl: placeholder("import.meta.url"),
exports: [] as Array<string>,
};

Expand Down Expand Up @@ -145,7 +145,7 @@ export const buildManifest = async (
const manifestStr = await writeManifestToString(collection);
const manifestPath = path.join(options.baseDir, "./manifest.gen.ts");

await runtime.writeTextFile(manifestPath, manifestStr);
await runtime.current.writeTextFile(manifestPath, manifestStr);

const exportModules = Object.values(collection);
const exportCount = exportModules.reduce((acc, [, moduleFns]) => {
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as posix from "https://deno.land/std@0.211.0/path/posix/mod.ts";
export * as posix from "https://deno.land/std@0.212.0/path/posix/mod.ts";
6 changes: 3 additions & 3 deletions di/deps-dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as assert from "https://deno.land/std@0.211.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.211.0/testing/bdd.ts";
export * as mock from "https://deno.land/std@0.211.0/testing/mock.ts";
export * as assert from "https://deno.land/std@0.212.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.212.0/testing/bdd.ts";
export * as mock from "https://deno.land/std@0.212.0/testing/mock.ts";
2 changes: 1 addition & 1 deletion dotenv/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as dotenv from "https://deno.land/std@0.211.0/dotenv/mod.ts";
export * as dotenv from "https://deno.land/std@0.212.0/dotenv/mod.ts";
28 changes: 15 additions & 13 deletions dotenv/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { defaultEnvValue, defaultEnvVar, env, type EnvMap } from "./base.ts";

// interface definitions
export interface LoaderOptions {
baseDir?: string;
defaultEnvVar?: string;
defaultEnvValue?: string;
baseDir: string;
defaultEnvVar: string;
defaultEnvValue: string;
}

// public functions
Expand All @@ -22,15 +22,15 @@ export const parseEnvFromFile = async (
filepath: string,
): Promise<ReturnType<typeof parseEnvString>> => {
try {
const data = await runtime.readFile(filepath);
const data = await runtime.current.readFile(filepath);
const decoded = new TextDecoder("utf-8").decode(data);
const escaped = decodeURIComponent(decoded);

const result = parseEnvString(escaped);

return result;
} catch (e) {
if (e instanceof runtime.errors.NotFound) {
if (e instanceof runtime.current.errors.NotFound) {
return {};
}

Expand All @@ -39,16 +39,18 @@ export const parseEnvFromFile = async (
};

export const load = async (
options?: LoaderOptions,
options?: Partial<LoaderOptions>,
): Promise<EnvMap> => {
const options_ = {
baseDir: ".",
defaultEnvVar: defaultEnvVar,
defaultEnvValue: defaultEnvValue,
...(options ?? {}),
};
const options_: LoaderOptions = Object.assign(
{
baseDir: ".",
defaultEnvVar: defaultEnvVar,
defaultEnvValue: defaultEnvValue,
},
options,
);

const sysVars = runtime.env.toObject();
const sysVars = runtime.current.getEnv();
const envName = sysVars[options_.defaultEnvVar] ?? options_.defaultEnvValue;

const vars = new Map<typeof env | string, string>();
Expand Down
6 changes: 3 additions & 3 deletions events/deps-dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as assert from "https://deno.land/std@0.211.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.211.0/testing/bdd.ts";
export * as mock from "https://deno.land/std@0.211.0/testing/mock.ts";
export * as assert from "https://deno.land/std@0.212.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.212.0/testing/bdd.ts";
export * as mock from "https://deno.land/std@0.212.0/testing/mock.ts";
7 changes: 7 additions & 0 deletions file-loader/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as JSONC from "https://deno.land/std@0.212.0/jsonc/mod.ts";
export * as TOML from "https://deno.land/std@0.212.0/toml/mod.ts";
export * as YAML from "https://deno.land/std@0.212.0/yaml/mod.ts";
export * as path from "https://deno.land/std@0.212.0/path/mod.ts";
export * as fs from "https://deno.land/std@0.212.0/fs/mod.ts";
92 changes: 92 additions & 0 deletions file-loader/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

import * as runtime from "../standards/runtime.ts";
import { fs, JSONC, path, TOML, YAML } from "./deps.ts";

// TODO(@eser) introduce strategy pattern for "search parents" and "recursive search" options

export const locate = async (
baseDir: string,
filenames: Array<string>,
searchParents = false,
): Promise<string | undefined> => {
let dir = baseDir;

while (true) {
for (const name of filenames) {
const filepath = path.join(dir, name);
const isExists = await fs.exists(filepath, { isFile: true });

if (isExists) {
return filepath;
}
}

if (!searchParents) {
break;
}

const parent = path.dirname(dir);
if (parent === dir) {
break;
}

dir = parent;
}

return undefined;
};

export const parse = async <T>(
filepath: string,
extension?: string,
): Promise<T> => {
const ext = extension ?? path.extname(filepath);

const file = await runtime.current.readTextFile(filepath);

if (ext === ".json") {
return JSON.parse(file) as T;
}

if (ext === ".jsonc") {
return JSONC.parse(file) as T;
}

if (ext === ".yaml" || ext === ".yml") {
return YAML.parse(file) as T;
}

if (ext === ".toml") {
return TOML.parse(file) as T;
}

throw new Error(`Unsupported file extension: ${ext}`);
};

export type LoadResult<T> = {
content: T | undefined;
path: string | undefined;
};

export const load = async <T>(
baseDir: string,
filenames: Array<string>,
searchParents = false,
): Promise<LoadResult<T>> => {
const filepath = await locate(baseDir, filenames, searchParents);

if (filepath === undefined) {
return {
content: undefined,
path: undefined,
};
}

const result = await parse<T>(filepath);

return {
content: result,
path: filepath,
};
};
4 changes: 4 additions & 0 deletions file-loader/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * from "./loader.ts";
export * from "./resolver.ts";
19 changes: 19 additions & 0 deletions file-loader/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export const resolvePath = (
filepath: string,
baseUrl: string | null = null,
) => {
if (baseUrl === null || filepath[0] === "/") {
return filepath;
}

const base = baseUrl[0] === "/" ? `file://${baseUrl}/` : baseUrl;
const url = new URL(filepath, base);

if (url.protocol === "file:") {
return url.href.substring(7);
}

return url.href;
};
14 changes: 9 additions & 5 deletions fp/append-to-array.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import { appendToArray } from "./append-to-array.ts";

const group = "append-to-array";

runtime.bench("cool/fp/append-to-array", { group, baseline: true }, () => {
const arr1 = ["a", "b"];
runtime.current.bench(
"cool/fp/append-to-array",
{ group, baseline: true },
() => {
const arr1 = ["a", "b"];

appendToArray(arr1, "c");
});
appendToArray(arr1, "c");
},
);

runtime.bench("spread operator", { group }, () => {
runtime.current.bench("spread operator", { group }, () => {
const arr1 = ["a", "b"];

[...arr1, "c"];
Expand Down
Loading

0 comments on commit 773eecc

Please sign in to comment.