๐Ÿ“ฆ colinhacks / zshy

๐Ÿ“„ utils.ts ยท 165 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165import * as fs from "node:fs";
import * as path from "node:path";
import * as ts from "typescript";

export function formatForLog(data: unknown) {
  return JSON.stringify(data, null, 2).split("\n").join("\n   ");
}

// Global logging state
let isSilent = false;

export function setSilent(silent: boolean) {
  isSilent = silent;
}

export const log = {
  prefix: undefined as string | undefined,

  info: function (message: string) {
    if (!isSilent) {
      console.log((this.prefix || "") + message);
    }
  },

  error: function (message: string) {
    if (!isSilent) {
      console.error((this.prefix || "") + message);
    }
  },

  warn: function (message: string) {
    if (!isSilent) {
      console.warn((this.prefix || "") + message);
    }
  },
};

export function isSourceFile(filePath: string): boolean {
  // Declaration files are not source files
  if (filePath.endsWith(".d.ts") || filePath.endsWith(".d.mts") || filePath.endsWith(".d.cts")) {
    return false;
  }

  // TypeScript source files
  return (
    filePath.endsWith(".ts") || filePath.endsWith(".mts") || filePath.endsWith(".cts") || filePath.endsWith(".tsx")
  );
}

export function removeExtension(filePath: string): string {
  return filePath.split(".").slice(0, -1).join(".") || filePath;
}

export function readTsconfig(tsconfigPath: string) {
  // Read and parse tsconfig.json
  const configPath = path.resolve(tsconfigPath);
  const configDir = path.dirname(configPath);

  const configFile = ts.readConfigFile(configPath, ts.sys.readFile);

  if (configFile.error) {
    console.error(
      "Error reading tsconfig.json:",
      ts.formatDiagnostic(configFile.error, {
        getCurrentDirectory: () => configDir,
        getCanonicalFileName: (fileName) => fileName,
        getNewLine: () => ts.sys.newLine,
      })
    );
    process.exit(1);
  }

  // Parse the config with explicit base path
  const parsedConfig = ts.parseJsonConfigFileContent(
    configFile.config,
    {
      ...ts.sys,
      // Override getCurrentDirectory to use the tsconfig directory
      getCurrentDirectory: () => configDir,
    },
    configDir
  );

  if (parsedConfig.errors.length > 0) {
    log.error("Error parsing tsconfig.json:");
    for (const error of parsedConfig.errors) {
      console.error(
        ts.formatDiagnostic(error, {
          getCurrentDirectory: () => configDir,
          getCanonicalFileName: (fileName) => fileName,
          getNewLine: () => ts.sys.newLine,
        })
      );
    }
    process.exit(1);
  }

  if (!parsedConfig.options) {
    log.error("Error reading tsconfig.json#/compilerOptions");
    process.exit(1);
  }
  return parsedConfig.options!;
}

export const jsExtensions: Set<string> = new Set([".js", ".mjs", ".cjs", ".ts", ".mts", ".cts", ".tsx"]);

export function isAssetFile(filePath: string): boolean {
  const ext = path.extname(filePath).toLowerCase();
  if (ext === "") return false;
  return !jsExtensions.has(ext);
}

export const toPosix = (p: string): string => p.replaceAll(path.sep, path.posix.sep);

export const relativePosix = (from: string, to: string): string => {
  const relativePath = path.relative(from, to);
  return toPosix(relativePath);
};

export function isTestFile(filePath: string): boolean {
  const posixPath = toPosix(filePath);

  // Exclude files in __tests__ directories
  if (posixPath.includes("/__tests__/") || posixPath.includes("\\__tests__\\")) {
    return true;
  }

  // Exclude files matching .test.{ext} or .spec.{ext} pattern
  const testPattern = /\.(test|spec)\.(ts|tsx|mts|cts)$/;
  if (testPattern.test(filePath)) {
    return true;
  }

  return false;
}

export function findConfigPath(fileName: string): string | null {
  let resultPath = `./${fileName}`;
  let currentDir = process.cwd();

  while (currentDir !== path.dirname(currentDir)) {
    const candidatePath = path.join(currentDir, fileName);
    if (fs.existsSync(candidatePath)) {
      resultPath = candidatePath;
      break;
    }
    currentDir = path.dirname(currentDir);
  }

  return fs.existsSync(resultPath) ? resultPath : null;
}

export function detectConfigIndentation(fileContents: string): string | number {
  let indent: string | number = 2; // Default to 2 spaces
  const indentMatch = fileContents.match(/^([ \t]+)/m);

  if (indentMatch?.[1]) {
    indent = indentMatch[1];
  } else if (!fileContents.includes("\n")) {
    indent = 0; // minified
  }

  return indent;
}