@adamhl8/configs 0.17.0 → 0.17.2

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.
@@ -38,7 +38,7 @@ async function checkExports(filePaths) {
38
38
  visit(sourceFile);
39
39
  if (exportDeclarations.length === 0) return;
40
40
  const exportDeclarationText = exportDeclarations.map((declaration) => declaration.getText()).join("\n");
41
- EXPORT_ERRORS.push(`${pc.redBright("✗")} ${filePath}\n${pc.dim(exportDeclarationText)}\n`);
41
+ EXPORT_ERRORS.push(`${pc.redBright("✗")} ${pc.blue(filePath)}\n${pc.dim(exportDeclarationText)}\n`);
42
42
  });
43
43
  return Promise.all(filePromises);
44
44
  });
@@ -53,34 +53,64 @@ function getPathsMap() {
53
53
  for (let [alias, [aliasDir]] of Object.entries(tsconfig.config.compilerOptions?.paths ?? {})) {
54
54
  if (!alias.endsWith("/*")) continue;
55
55
  if (!aliasDir?.endsWith("/*")) continue;
56
- aliasDir = aliasDir.slice(0, -2);
57
56
  alias = alias.slice(0, -2);
57
+ aliasDir = aliasDir.slice(0, -2);
58
58
  pathsMap[alias] = aliasDir;
59
59
  }
60
60
  return pathsMap;
61
61
  }
62
- function changeExtension(parsedPath, newExtension) {
62
+ function resolveImportPath(importPath, filePath, pathsMap) {
63
+ const fileDir = path.dirname(filePath);
64
+ const sortedAliases = Object.keys(pathsMap).sort((a, b) => b.length - a.length);
65
+ for (const alias of sortedAliases) {
66
+ if (!importPath.startsWith(alias)) continue;
67
+ const aliasDir = pathsMap[alias];
68
+ if (!aliasDir) continue;
69
+ const relativePath = importPath.slice(alias.length + 1);
70
+ return path.resolve(aliasDir, relativePath);
71
+ }
72
+ return path.resolve(fileDir, importPath);
73
+ }
74
+ function changeExtension(importParts, newExtension) {
63
75
  return {
64
- ...parsedPath,
76
+ ...importParts,
65
77
  ext: newExtension,
66
78
  base: ""
67
79
  };
68
80
  }
69
- function transformExtension(filePath) {
70
- const pathParts = path.parse(filePath);
71
- if (!(pathParts.ext === ".js" || pathParts.ext === "")) return filePath;
72
- return path.format(changeExtension(pathParts, ".ts"));
81
+ const ALLOWED_EXTENSIONS = [
82
+ ".ts",
83
+ ".tsx",
84
+ ".d.ts"
85
+ ];
86
+ function getNewExtensionPathParts(importParts) {
87
+ const tsExtensionLookups = [
88
+ "",
89
+ ".js",
90
+ ".jsx",
91
+ ".ts",
92
+ ".tsx"
93
+ ];
94
+ if (!tsExtensionLookups.includes(importParts.ext)) return {
95
+ ext: importParts.ext,
96
+ base: importParts.base
97
+ };
98
+ if (tsExtensionLookups.includes(importParts.ext)) for (const allowedExtension of ALLOWED_EXTENSIONS) {
99
+ const targetPathParts = changeExtension(importParts, allowedExtension);
100
+ const targetPath = path.format(targetPathParts);
101
+ if (fss.existsSync(targetPath)) return {
102
+ ext: targetPathParts.ext,
103
+ base: targetPathParts.base
104
+ };
105
+ }
73
106
  }
74
- function transformRelativeImport(filePath, importPath, pathsMap) {
75
- const currentDir = path.dirname(filePath);
76
- const absoluteImportPath = path.resolve(currentDir, importPath);
77
- if (!fss.existsSync(absoluteImportPath)) return err(`skipped transform of '${importPath}': target file does not exist`, void 0);
107
+ function getAliasPathParts(importParts, pathsMap) {
108
+ const importPath = path.format(importParts);
78
109
  for (const [alias, aliasDir] of Object.entries(pathsMap)) {
79
- const relativeToAliasDir = path.relative(aliasDir, absoluteImportPath);
110
+ const relativeToAliasDir = path.relative(aliasDir, importPath);
80
111
  if (relativeToAliasDir.startsWith("..")) continue;
81
- return `${alias}/${relativeToAliasDir}`;
112
+ return { dir: path.parse(`${alias}/${relativeToAliasDir}`).dir };
82
113
  }
83
- return err(`could not find alias for '${importPath}'`, void 0);
84
114
  }
85
115
  async function fixImports(filePaths, { write, importIgnoreStrings, skipAlias }) {
86
116
  const IMPORT_ERRORS = [];
@@ -97,23 +127,37 @@ async function fixImports(filePaths, { write, importIgnoreStrings, skipAlias })
97
127
  const isAliasImport = Object.keys(pathsMap).some((alias) => importPath.startsWith(alias));
98
128
  if (!(isRelativeImport || isAliasImport)) return match;
99
129
  if (importIgnoreStrings.some((ignoreString) => importPath.includes(ignoreString))) return match;
100
- let newImportPath = transformExtension(importPath);
130
+ let newImportParts = path.parse(importPath);
131
+ const resolvedImportPath = resolveImportPath(importPath, filePath, pathsMap);
132
+ const resolvedImportParts = path.parse(resolvedImportPath);
133
+ const transformExtensionResult = getNewExtensionPathParts(resolvedImportParts);
134
+ if (transformExtensionResult) newImportParts = {
135
+ ...newImportParts,
136
+ ...transformExtensionResult
137
+ };
138
+ else {
139
+ const targetFilePath = `${path.format(changeExtension(resolvedImportParts, ""))}.{${ALLOWED_EXTENSIONS.map((ext) => ext.replace(".", "")).join()}}`;
140
+ importErrorsForFile.push(`skipped extension transform of '${importPath}': target file not found (looking for '${targetFilePath}')`);
141
+ }
101
142
  if (isRelativeImport && !skipAlias) {
102
- const transformedRelativeImport = transformRelativeImport(filePath, newImportPath, pathsMap);
103
- if (isErr(transformedRelativeImport)) importErrorsForFile.push(transformedRelativeImport.messageChain);
104
- else newImportPath = transformedRelativeImport;
143
+ const transformToAliasImportResult = getAliasPathParts(resolvedImportParts, pathsMap);
144
+ if (transformToAliasImportResult) newImportParts = {
145
+ ...newImportParts,
146
+ ...transformToAliasImportResult
147
+ };
148
+ else importErrorsForFile.push(`skipped transforming relative import path '${importPath}': could not find appropriate alias`);
105
149
  }
150
+ const newImportPath = path.format(newImportParts);
106
151
  if (newImportPath === importPath) return match;
107
152
  const { ext: originalExt } = path.parse(importPath);
108
- const newPathParts = path.parse(newImportPath);
109
- const { ext: newExt } = newPathParts;
153
+ const { ext: newExt } = newImportParts;
110
154
  let newImportPathString = newImportPath;
111
- if (newExt !== originalExt) newImportPathString = `${path.format(changeExtension(newPathParts, ""))}${pc.greenBright(newExt)}`;
155
+ if (newExt !== originalExt) newImportPathString = `${path.format(changeExtension(newImportParts, ""))}${pc.greenBright(newExt)}`;
112
156
  transformedImportsForFile.push(`'${importPath}' -> '${newImportPathString}'`);
113
157
  return match.replace(importPath, newImportPath);
114
158
  });
115
- if (importErrorsForFile.length > 0) IMPORT_ERRORS.push(`${pc.redBright("✗")} ${filePath}\n${importErrorsForFile.join("\n")}\n`);
116
- if (transformedImportsForFile.length > 0) TRANSFORMED_IMPORTS.push(`${pc.greenBright("✓")} ${filePath}\n${transformedImportsForFile.join("\n")}\n`);
159
+ if (importErrorsForFile.length > 0) IMPORT_ERRORS.push(`${pc.redBright("✗")} ${pc.blue(filePath)}\n${importErrorsForFile.join("\n")}\n`);
160
+ if (transformedImportsForFile.length > 0) TRANSFORMED_IMPORTS.push(`${pc.greenBright("✓")} ${pc.blue(filePath)}\n${transformedImportsForFile.join("\n")}\n`);
117
161
  if (transformedContent === content) return;
118
162
  if (write) await fs.writeFile(filePath, transformedContent);
119
163
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamhl8/configs",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",