Skip to content

Commit

Permalink
Absolutize ignored files so the comparison against the watcher yielde…
Browse files Browse the repository at this point in the history
…d files works
  • Loading branch information
airhorns committed Dec 4, 2024
1 parent 4ac12b7 commit 8f93ad1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 62 deletions.
17 changes: 0 additions & 17 deletions src/Options.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/Project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import type { Compiler } from "./Compiler.js";
import type { ProjectConfig } from "./Options.js";
import { PathTrie } from "./PathTrie.js";
import type { ProjectConfig } from "./ProjectConfig.js";
import type { Supervisor } from "./Supervisor.js";
import { log } from "./utils.js";

Expand Down
56 changes: 56 additions & 0 deletions src/ProjectConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { Options as SwcOptions } from "@swc/core";
import fs from "fs-extra";
import _ from "lodash";
import path from "path";
import { log } from "./utils.js";

export type SwcConfig = string | SwcOptions;

export interface RunOptions {
argv: string[];
terminalCommands: boolean;
reloadOnChanges: boolean;
}

export interface ProjectConfig {
ignore: string[];
swc?: SwcConfig;
esm?: boolean;
extensions: string[];
cacheDir: string;
}

export const projectConfig = async (root: string): Promise<ProjectConfig> => {
const location = path.join(root, "wds.js");
const base: ProjectConfig = {
ignore: [],
extensions: [".ts", ".tsx", ".jsx"],
cacheDir: path.join(root, "node_modules/.cache/wds"),
esm: true,
};

try {
await fs.access(location);
} catch (error: any) {
log.debug(`Not loading project config from ${location}`);
return base;
}

let required = await import(location);
if (required.default) {
required = required.default;
}
log.debug(`Loaded project config from ${location}`);
const result = _.defaults(required, base);

const projectRootDir = path.dirname(location);
// absolutize the cacheDir if not already
if (!result.cacheDir.startsWith("/")) {
result.cacheDir = path.resolve(projectRootDir, result.cacheDir);
}

// absolutize the ignore paths if not already
result.ignore = result.ignore.map((p: string) => (p.startsWith("/") ? p : path.resolve(projectRootDir, p)));

return result;
};
2 changes: 1 addition & 1 deletion src/Supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { ChildProcess, StdioOptions } from "child_process";
import { spawn } from "child_process";
import { EventEmitter, once } from "events";
import { setTimeout } from "timers/promises";
import type { RunOptions } from "./Options.js";
import type { Project } from "./Project.js";
import type { RunOptions } from "./ProjectConfig.js";
import { log } from "./utils.js";

/** */
Expand Down
4 changes: 2 additions & 2 deletions src/SwcCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import path from "path";
import { fileURLToPath } from "url";
import writeFileAtomic from "write-file-atomic";
import type { Compiler } from "./Compiler.js";
import type { ProjectConfig } from "./Options.js";
import { log, projectConfig } from "./utils.js";
import { projectConfig, type ProjectConfig } from "./ProjectConfig.js";
import { log } from "./utils.js";

const __filename = fileURLToPath(import.meta.url);
const require = createRequire(import.meta.url);
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { fileURLToPath } from "url";
import Watcher from "watcher";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import type { ProjectConfig, RunOptions } from "./Options.js";
import { Project } from "./Project.js";
import { projectConfig, type ProjectConfig, type RunOptions } from "./ProjectConfig.js";
import { Supervisor } from "./Supervisor.js";
import { MissingDestinationError, SwcCompiler } from "./SwcCompiler.js";
import { MiniServer } from "./mini-server.js";
import { log, projectConfig } from "./utils.js";
import { log } from "./utils.js";

const dirname = fileURLToPath(new URL(".", import.meta.url));

Expand Down Expand Up @@ -63,15 +63,18 @@ const startTerminalCommandListener = (project: Project) => {
return reader;
};

const gitDir = `${path.sep}.git${path.sep}`;
const nodeModulesDir = `${path.sep}node_modules${path.sep}`;

const startFilesystemWatcher = (project: Project) => {
const watcher = new Watcher([project.workspaceRoot], {
ignoreInitial: true,
recursive: true,
ignore: ((filePath: string) => {
if (filePath.includes("node_modules")) return true;
if (filePath.includes(nodeModulesDir)) return true;
if (filePath.endsWith(".d.ts")) return true;
if (filePath.endsWith(".map")) return true;
if (filePath.endsWith(".git")) return true;
if (filePath.includes(gitDir)) return true;
if (filePath.endsWith(".DS_Store")) return true;
if (filePath.endsWith(".tsbuildinfo")) return true;

Expand Down
37 changes: 0 additions & 37 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import fs from "fs-extra";
import _ from "lodash";
import path from "path";
import { threadId } from "worker_threads";
// @ts-expect-error see https://github.com/microsoft/TypeScript/issues/52529, can't import types from .cts to .ts files that are ESM
import type { ProjectConfig } from "./Options";

const logPrefix = `[wds pid=${process.pid} thread=${threadId}]`;
export const log = {
debug: (...args: any[]) => process.env["WDS_DEBUG"] && console.warn(logPrefix, ...args),
Expand All @@ -20,34 +14,3 @@ export async function time<T>(run: () => Promise<T>) {

return (diff[0] + diff[1] / 1e9).toFixed(5);
}

export const projectConfig = async (root: string): Promise<ProjectConfig> => {
const location = path.join(root, "wds.js");
const base: ProjectConfig = {
ignore: [],
extensions: [".ts", ".tsx", ".jsx"],
cacheDir: path.join(root, "node_modules/.cache/wds"),
esm: true,
};

try {
await fs.access(location);
} catch (error: any) {
log.debug(`Not loading project config from ${location}`);
return base;
}

let required = await import(location);
if (required.default) {
required = required.default;
}
log.debug(`Loaded project config from ${location}`);
const result = _.defaults(required, base);

// absolutize the cacheDir if not already
if (!result.cacheDir.startsWith("/")) {
result.cacheDir = path.resolve(path.dirname(location), result.cacheDir);
}

return result;
};

0 comments on commit 8f93ad1

Please sign in to comment.