|
1 |
| -import { execSync } from 'child_process'; |
2 | 1 | import { cosmiconfig, cosmiconfigSync, Loader, defaultLoaders } from 'cosmiconfig';
|
3 |
| -import { createHash } from 'crypto'; |
4 |
| -import { promises } from 'fs'; |
5 |
| -import { tmpdir } from 'os'; |
6 |
| -import { basename, join } from 'path'; |
7 | 2 | import { env } from 'string-env-interpolation';
|
8 | 3 |
|
9 | 4 | export interface ConfigSearchResult {
|
@@ -44,70 +39,12 @@ export function createCosmiConfigSync(moduleName: string, legacy: boolean) {
|
44 | 39 | return cosmiconfigSync(moduleName, options);
|
45 | 40 | }
|
46 | 41 |
|
47 |
| -const loadTypeScript: Loader = async (filepath, content) => { |
48 |
| - try { |
49 |
| - // eslint-disable-next-line @typescript-eslint/no-var-requires |
50 |
| - const { TypeScriptLoader } = require('cosmiconfig-typescript-loader'); |
51 |
| - return TypeScriptLoader({ transpileOnly: true })(filepath, content); |
52 |
| - } catch (err) { |
53 |
| - if (isRequireESMError(err)) { |
54 |
| - const hash = createHash('sha256').update(content).digest('base64url'); |
55 |
| - |
56 |
| - const tempDir = join(tmpdir(), `graphql-config`); |
57 |
| - |
58 |
| - let inTempDir: string[] = []; |
59 |
| - try { |
60 |
| - inTempDir = await promises.readdir(tempDir); |
61 |
| - } catch (err) { |
62 |
| - if (err.code === 'ENOENT') { |
63 |
| - // tsc will create the directory if it doesn't exist. |
64 |
| - } else { |
65 |
| - throw err; |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - let outDir = join(tempDir, new Date().getTime() + '-' + hash); |
70 |
| - const previousOutDir = inTempDir.find((s) => s.endsWith(hash)); |
71 |
| - |
72 |
| - if (previousOutDir) { |
73 |
| - outDir = join(tempDir, previousOutDir); |
74 |
| - } else { |
75 |
| - // We're compiling the file, because ts-node doesn't work perfectly with ESM. |
76 |
| - execSync(`tsc ${filepath} --module commonjs --outDir ${outDir} --skipLibCheck`); |
77 |
| - } |
78 |
| - |
79 |
| - const newPath = join(outDir, basename(filepath).replace(/\.(m|c)?ts$/, '.js')); |
80 |
| - const config = import(newPath).then((m) => { |
81 |
| - const config = m.default; |
82 |
| - return 'default' in config ? config.default : config; |
83 |
| - }); |
84 |
| - |
85 |
| - // If the cache has more than 10 files, we delete the oldest one. |
86 |
| - await removeOldestDirInCache(inTempDir, tempDir, 10); |
87 |
| - |
88 |
| - return config; |
89 |
| - } |
90 |
| - throw err; |
91 |
| - } |
| 42 | +const loadTypeScript: Loader = (...args) => { |
| 43 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 44 | + const { TypeScriptLoader } = require('cosmiconfig-typescript-loader'); |
| 45 | + return TypeScriptLoader({ transpileOnly: true })(...args); |
92 | 46 | };
|
93 | 47 |
|
94 |
| -async function removeOldestDirInCache(inTempDir: string[], tempDir: string, cacheLimit: number) { |
95 |
| - if (inTempDir.length > cacheLimit) { |
96 |
| - const oldest = inTempDir.sort((a, b) => { |
97 |
| - const aTime = Number(a.split('-')[0]); |
98 |
| - const bTime = Number(b.split('-')[0]); |
99 |
| - |
100 |
| - return aTime - bTime; |
101 |
| - })[0]; |
102 |
| - |
103 |
| - await promises.rm(join(tempDir, oldest), { recursive: true, force: true }); |
104 |
| - } |
105 |
| -} |
106 |
| - |
107 |
| -function isRequireESMError(err: any) { |
108 |
| - return typeof err.stack === 'string' && err.stack.startsWith('Error [ERR_REQUIRE_ESM]:'); |
109 |
| -} |
110 |
| - |
111 | 48 | const loadToml: Loader = (...args) => {
|
112 | 49 | // eslint-disable-next-line @typescript-eslint/no-var-requires
|
113 | 50 | const { loadToml } = require('cosmiconfig-toml-loader');
|
@@ -146,8 +83,6 @@ function prepareCosmiconfig(moduleName: string, legacy: boolean) {
|
146 | 83 | searchPlaces: searchPlaces.map((place) => place.replace('#', moduleName)),
|
147 | 84 | loaders: {
|
148 | 85 | '.ts': loadTypeScript,
|
149 |
| - '.mts': loadTypeScript, |
150 |
| - '.cts': loadTypeScript, |
151 | 86 | '.js': defaultLoaders['.js'],
|
152 | 87 | '.json': createCustomLoader(defaultLoaders['.json']),
|
153 | 88 | '.yaml': loadYaml,
|
|
0 commit comments