@awsless/ts-file-cache 0.0.18 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3,65 +3,21 @@ let fs_promises = require("fs/promises");
3
3
  let path = require("path");
4
4
  let crypto = require("crypto");
5
5
  let node_module = require("node:module");
6
- let _swc_core = require("@swc/core");
7
- let swc_walk = require("swc-walk");
8
- let swc_walk_baseVisitor = require("swc-walk/baseVisitor");
6
+ let estree_walker = require("estree-walker");
7
+ let oxc_parser = require("oxc-parser");
9
8
  let yaml = require("yaml");
10
9
  //#region src/import.ts
11
- var PatchedBaseVisitor = class extends swc_walk_baseVisitor.BaseVisitor {
12
- FunctionBody(node, state, callback) {
13
- for (const statement of node.stmts) callback(statement, state);
14
- }
15
- };
16
- const baseVisitor = new PatchedBaseVisitor();
17
- const parseOptions = (file) => {
18
- if (file.endsWith(".tsx")) return {
19
- syntax: "typescript",
20
- tsx: true,
21
- decorators: true
22
- };
23
- if (file.endsWith(".ts") || file.endsWith(".mts") || file.endsWith(".cts")) return {
24
- syntax: "typescript",
25
- decorators: true
26
- };
27
- return {
28
- syntax: "ecmascript",
29
- jsx: true,
30
- decorators: true
31
- };
32
- };
33
10
  const findImports = async (file, code) => {
34
- let ast;
35
- try {
36
- ast = await (0, _swc_core.parse)(code, parseOptions(file));
37
- } catch (error) {
38
- throw new Error(`Failed to parse: ${file}`, { cause: error });
39
- }
11
+ const ast = (0, oxc_parser.parseSync)(file, code);
12
+ if (ast.errors.length > 0) throw new Error(`Failed to parse: ${file}`, { cause: ast.errors[0] });
40
13
  const importing = /* @__PURE__ */ new Set();
41
- try {
42
- (0, swc_walk.simple)(ast, {
43
- ImportDeclaration(node) {
44
- importing.add(node.source.value);
45
- },
46
- ExportAllDeclaration(node) {
47
- importing.add(node.source.value);
48
- },
49
- ExportNamedDeclaration(node) {
50
- if (node.source) importing.add(node.source.value);
51
- },
52
- CallExpression(node) {
53
- if (node.callee.type === "Import") {
54
- const first = node.arguments.at(0);
55
- if (first && first.expression.type === "StringLiteral") importing.add(first.expression.value);
56
- }
57
- },
58
- TsImportEqualsDeclaration(node) {
59
- if (node.moduleRef.type === "TsExternalModuleReference") importing.add(node.moduleRef.expression.value);
60
- }
61
- }, baseVisitor);
62
- } catch (error) {
63
- throw new Error(`Failed to walk the AST of: ${file}`, { cause: error });
64
- }
14
+ (0, estree_walker.walk)(ast.program, { enter(node) {
15
+ if (node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration") importing.add(node.source.value);
16
+ if (node.type === "ExportNamedDeclaration" && node.source) importing.add(node.source.value);
17
+ if (node.type === "ImportExpression" && node.source.type === "Literal") importing.add(node.source.value);
18
+ const importEquals = node;
19
+ if (importEquals.type === "TSImportEqualsDeclaration" && importEquals.moduleReference.type === "TSExternalModuleReference") importing.add(importEquals.moduleReference.expression.value);
20
+ } });
65
21
  return [...importing].map((importee) => {
66
22
  if (importee.startsWith(".")) return (0, path.resolve)((0, path.dirname)(file), importee);
67
23
  const parts = importee.split("/");
@@ -74,7 +30,7 @@ const findImports = async (file, code) => {
74
30
  const findFile = async (files) => {
75
31
  for (const file of files) try {
76
32
  if ((await (0, fs_promises.stat)(file)).isFile()) return file;
77
- } catch (_) {
33
+ } catch {
78
34
  continue;
79
35
  }
80
36
  throw new Error(`No such file: ${files.join(", ")}`);
@@ -114,7 +70,7 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
114
70
  try {
115
71
  file = await resolveModuleImportFile(file, allowedExtensions);
116
72
  } catch (error) {
117
- throw new Error(`Can't find imported file: "${file}" inside the source: "${sourceFile}"`);
73
+ throw new Error(`Can't find imported file: "${file}" inside the source: "${sourceFile}"`, { cause: error });
118
74
  }
119
75
  const relFile = (0, path.relative)(workspace.cwd, file).split(path.sep).join("/");
120
76
  if (hashes.has(relFile)) return;
@@ -140,11 +96,11 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
140
96
  }
141
97
  return;
142
98
  }
143
- if (node_module.builtinModules.includes(module.replace(/^node\:/, ""))) return;
99
+ if (node_module.builtinModules.includes(module.replace(/^node:/, ""))) return;
144
100
  throw new Error(`Can't find the dependency version for: ${file} inside the source: ${sourceFile}`);
145
101
  };
146
102
  const mergeHashes = (hashes) => {
147
- const names = Array.from(hashes.keys()).sort();
103
+ const names = Array.from(hashes.keys()).toSorted();
148
104
  const merged = (0, crypto.createHash)("sha1");
149
105
  for (const name of names) {
150
106
  merged.update(name);
@@ -160,7 +116,7 @@ const getPackageName = (importee) => {
160
116
  throw new Error(`Malformed importee: ${importee}`);
161
117
  };
162
118
  const findDependency = (workspace, module, source) => {
163
- const pkg = Object.values(workspace.packages).filter((p) => source === p.path || source.startsWith(p.path + path.sep)).sort((a, b) => b.path.split(path.sep).length - a.path.split(path.sep).length).find((p) => p.dependencies[module]);
119
+ const pkg = Object.values(workspace.packages).filter((p) => source === p.path || source.startsWith(p.path + path.sep)).toSorted((a, b) => b.path.split(path.sep).length - a.path.split(path.sep).length).find((p) => p.dependencies[module]);
164
120
  if (!pkg) return;
165
121
  return pkg.dependencies[module];
166
122
  };
@@ -219,7 +175,7 @@ const bun = async (cwd, lockFile) => {
219
175
  const parseJsonc = (text) => {
220
176
  try {
221
177
  return JSON.parse(text);
222
- } catch (_) {
178
+ } catch {
223
179
  return JSON.parse(stripJsoncSyntax(text));
224
180
  }
225
181
  };
@@ -317,7 +273,7 @@ const loadPackageManager = async (search, level = 5) => {
317
273
  const fileExist = async (file) => {
318
274
  try {
319
275
  if ((await (0, fs_promises.lstat)(file)).isFile()) return true;
320
- } catch (error) {}
276
+ } catch {}
321
277
  return false;
322
278
  };
323
279
  const buildPackages = async (cwd, importers) => {
package/dist/index.js CHANGED
@@ -2,65 +2,21 @@ import { builtinModules } from "node:module";
2
2
  import { lstat, readFile, readdir, stat } from "fs/promises";
3
3
  import { basename, dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "path";
4
4
  import { createHash } from "crypto";
5
- import { parse } from "@swc/core";
6
- import { simple } from "swc-walk";
7
- import { BaseVisitor } from "swc-walk/baseVisitor";
8
- import { parse as parse$1 } from "yaml";
5
+ import { walk } from "estree-walker";
6
+ import { parseSync } from "oxc-parser";
7
+ import { parse } from "yaml";
9
8
  //#region src/import.ts
10
- var PatchedBaseVisitor = class extends BaseVisitor {
11
- FunctionBody(node, state, callback) {
12
- for (const statement of node.stmts) callback(statement, state);
13
- }
14
- };
15
- const baseVisitor = new PatchedBaseVisitor();
16
- const parseOptions = (file) => {
17
- if (file.endsWith(".tsx")) return {
18
- syntax: "typescript",
19
- tsx: true,
20
- decorators: true
21
- };
22
- if (file.endsWith(".ts") || file.endsWith(".mts") || file.endsWith(".cts")) return {
23
- syntax: "typescript",
24
- decorators: true
25
- };
26
- return {
27
- syntax: "ecmascript",
28
- jsx: true,
29
- decorators: true
30
- };
31
- };
32
9
  const findImports = async (file, code) => {
33
- let ast;
34
- try {
35
- ast = await parse(code, parseOptions(file));
36
- } catch (error) {
37
- throw new Error(`Failed to parse: ${file}`, { cause: error });
38
- }
10
+ const ast = parseSync(file, code);
11
+ if (ast.errors.length > 0) throw new Error(`Failed to parse: ${file}`, { cause: ast.errors[0] });
39
12
  const importing = /* @__PURE__ */ new Set();
40
- try {
41
- simple(ast, {
42
- ImportDeclaration(node) {
43
- importing.add(node.source.value);
44
- },
45
- ExportAllDeclaration(node) {
46
- importing.add(node.source.value);
47
- },
48
- ExportNamedDeclaration(node) {
49
- if (node.source) importing.add(node.source.value);
50
- },
51
- CallExpression(node) {
52
- if (node.callee.type === "Import") {
53
- const first = node.arguments.at(0);
54
- if (first && first.expression.type === "StringLiteral") importing.add(first.expression.value);
55
- }
56
- },
57
- TsImportEqualsDeclaration(node) {
58
- if (node.moduleRef.type === "TsExternalModuleReference") importing.add(node.moduleRef.expression.value);
59
- }
60
- }, baseVisitor);
61
- } catch (error) {
62
- throw new Error(`Failed to walk the AST of: ${file}`, { cause: error });
63
- }
13
+ walk(ast.program, { enter(node) {
14
+ if (node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration") importing.add(node.source.value);
15
+ if (node.type === "ExportNamedDeclaration" && node.source) importing.add(node.source.value);
16
+ if (node.type === "ImportExpression" && node.source.type === "Literal") importing.add(node.source.value);
17
+ const importEquals = node;
18
+ if (importEquals.type === "TSImportEqualsDeclaration" && importEquals.moduleReference.type === "TSExternalModuleReference") importing.add(importEquals.moduleReference.expression.value);
19
+ } });
64
20
  return [...importing].map((importee) => {
65
21
  if (importee.startsWith(".")) return resolve(dirname(file), importee);
66
22
  const parts = importee.split("/");
@@ -73,7 +29,7 @@ const findImports = async (file, code) => {
73
29
  const findFile = async (files) => {
74
30
  for (const file of files) try {
75
31
  if ((await stat(file)).isFile()) return file;
76
- } catch (_) {
32
+ } catch {
77
33
  continue;
78
34
  }
79
35
  throw new Error(`No such file: ${files.join(", ")}`);
@@ -113,7 +69,7 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
113
69
  try {
114
70
  file = await resolveModuleImportFile(file, allowedExtensions);
115
71
  } catch (error) {
116
- throw new Error(`Can't find imported file: "${file}" inside the source: "${sourceFile}"`);
72
+ throw new Error(`Can't find imported file: "${file}" inside the source: "${sourceFile}"`, { cause: error });
117
73
  }
118
74
  const relFile = relative(workspace.cwd, file).split(sep).join("/");
119
75
  if (hashes.has(relFile)) return;
@@ -139,11 +95,11 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
139
95
  }
140
96
  return;
141
97
  }
142
- if (builtinModules.includes(module.replace(/^node\:/, ""))) return;
98
+ if (builtinModules.includes(module.replace(/^node:/, ""))) return;
143
99
  throw new Error(`Can't find the dependency version for: ${file} inside the source: ${sourceFile}`);
144
100
  };
145
101
  const mergeHashes = (hashes) => {
146
- const names = Array.from(hashes.keys()).sort();
102
+ const names = Array.from(hashes.keys()).toSorted();
147
103
  const merged = createHash("sha1");
148
104
  for (const name of names) {
149
105
  merged.update(name);
@@ -159,7 +115,7 @@ const getPackageName = (importee) => {
159
115
  throw new Error(`Malformed importee: ${importee}`);
160
116
  };
161
117
  const findDependency = (workspace, module, source) => {
162
- const pkg = Object.values(workspace.packages).filter((p) => source === p.path || source.startsWith(p.path + sep)).sort((a, b) => b.path.split(sep).length - a.path.split(sep).length).find((p) => p.dependencies[module]);
118
+ const pkg = Object.values(workspace.packages).filter((p) => source === p.path || source.startsWith(p.path + sep)).toSorted((a, b) => b.path.split(sep).length - a.path.split(sep).length).find((p) => p.dependencies[module]);
163
119
  if (!pkg) return;
164
120
  return pkg.dependencies[module];
165
121
  };
@@ -218,7 +174,7 @@ const bun = async (cwd, lockFile) => {
218
174
  const parseJsonc = (text) => {
219
175
  try {
220
176
  return JSON.parse(text);
221
- } catch (_) {
177
+ } catch {
222
178
  return JSON.parse(stripJsoncSyntax(text));
223
179
  }
224
180
  };
@@ -269,7 +225,7 @@ const stripJsoncSyntax = (text) => {
269
225
  //#endregion
270
226
  //#region src/package-manager/pnpm.ts
271
227
  const pnpm = async (cwd, lockFile) => {
272
- const data = parse$1(lockFile);
228
+ const data = parse(lockFile);
273
229
  const importers = {};
274
230
  for (const [path, importee] of Object.entries(data.importers)) {
275
231
  const deps = {
@@ -316,7 +272,7 @@ const loadPackageManager = async (search, level = 5) => {
316
272
  const fileExist = async (file) => {
317
273
  try {
318
274
  if ((await lstat(file)).isFile()) return true;
319
- } catch (error) {}
275
+ } catch {}
320
276
  return false;
321
277
  };
322
278
  const buildPackages = async (cwd, importers) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsless/ts-file-cache",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "bugs": {
5
5
  "url": "https://github.com/awsless/awsless/issues"
6
6
  },
@@ -24,8 +24,8 @@
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
- "@swc/core": "1.16.1",
28
- "swc-walk": "3.0.0-rc.0",
27
+ "estree-walker": "3.0.3",
28
+ "oxc-parser": "0.146.0",
29
29
  "yaml": "2.9.0"
30
30
  },
31
31
  "scripts": {