Skip to content

Commit 632615a

Browse files
committed
cleanup
1 parent 8bd8285 commit 632615a

File tree

4 files changed

+78
-77
lines changed

4 files changed

+78
-77
lines changed

packages/mongodb-ts-autocomplete/scripts/extract-types.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,35 @@ const deps: Record<string, string[]> = {
189189
],
190190
};
191191

192+
function resolveModulePath(moduleName: string): string {
193+
const { resolvedModule } = resolve(moduleName);
194+
if (!resolvedModule) {
195+
console.log(resolve(moduleName));
196+
throw new Error(`Could not resolve module: ${moduleName}`);
197+
}
198+
return resolvedModule.resolvedFileName;
199+
}
200+
192201
async function run() {
193-
// TODO: switch require.resolve() to resolve()
202+
const mqlPath = path.join(
203+
path.dirname(resolveModulePath('@mongodb-js/mql-typescript')),
204+
'..',
205+
'out',
206+
'schema.d.ts',
207+
);
208+
194209
const input: Record<string, string> = {
195210
// mql imports bson but right now so does shell-api. We could bake the types
196211
// those use into the files we generate using api-extractor, but maybe
197212
// including it just once is not so bad.
198-
'/bson.ts': path.join(require.resolve('bson'), '..', '..', 'bson.d.ts'),
213+
'/bson.ts': resolveModulePath('bson'),
199214
// mql imports the mongodb driver. We could also use api-extractor there to
200215
// bake the few mongodb types we use into the schema.
201-
'/mongodb.ts': path.join(
202-
require.resolve('mongodb'),
203-
'..',
204-
'..',
205-
'mongodb.d.ts',
206-
),
216+
'/mongodb.ts': resolveModulePath('mongodb'),
207217
// We wouldn't have to include mql if we used it straight from shell-api,
208218
// but since we're using it straight here for now to bypass the complicated
209219
// generics on the shell-api side it is included here for now.
210-
'/mql.ts': path.join(
211-
require.resolve('@mongodb-js/mql-typescript'),
212-
'..',
213-
'..',
214-
'out',
215-
'schema.d.ts',
216-
),
220+
'/mql.ts': mqlPath,
217221
};
218222
for (const [moduleName, filePaths] of Object.entries(deps)) {
219223
const { resolvedModule } = resolve(moduleName);
@@ -225,10 +229,8 @@ async function run() {
225229
0,
226230
-resolvedModule.packageId.subModuleName.length,
227231
);
228-
//console.log({ basePath});
229232
for (const filePath of filePaths) {
230233
const fullPath = path.join(basePath, filePath);
231-
//console.log({ fullPath });
232234
// these are in the format import of typescript imports
233235
input[`${moduleName}/${filePath}`] = fullPath;
234236
}

packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ describe('MongoDBAutocompleter', function () {
1818
let autocompleterContext: AutocompletionContext;
1919
let autocompleter: MongoDBAutocompleter;
2020

21+
before(function () {
22+
// make sure that we fall back to the default ts.sys file methods so that
23+
// encounteredPaths will be filled
24+
process.env.CI = 'true';
25+
});
26+
2127
beforeEach(function () {
2228
autocompleterContext = {
2329
currentDatabaseAndConnection: () => ({
@@ -72,6 +78,16 @@ describe('MongoDBAutocompleter', function () {
7278
});
7379
});
7480

81+
afterEach(function () {
82+
// this is what tells us what we're missing in extract-types.ts
83+
const encounteredPaths = autocompleter.listEncounteredPaths();
84+
expect(encounteredPaths).to.deep.equal({
85+
fileExists: [],
86+
getScriptSnapshot: [],
87+
readFile: [],
88+
});
89+
});
90+
7591
it('deals with no connection', async function () {
7692
// The body of tests are all wrapped in loops so that we exercise the
7793
// caching logic in the autocompleter.
@@ -202,18 +218,6 @@ describe('MongoDBAutocompleter', function () {
202218
result: 'db.foo.find({ foo',
203219
},
204220
]);
205-
206-
// this is what tells us what we're missing in extract-types.ts
207-
const encounteredPaths = autocompleter.listEncounteredPaths();
208-
if (
209-
encounteredPaths.getScriptSnapshot.length ||
210-
encounteredPaths.fileExists.length ||
211-
encounteredPaths.readFile.length
212-
) {
213-
// eslint-disable-next-line no-console
214-
console.log(JSON.stringify(encounteredPaths, null, 2));
215-
expect.fail('There should be no encountered paths left over');
216-
}
217221
}
218222

219223
// then hit a different collection to make sure the caching works

packages/ts-autocomplete/src/index.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ describe('Autocompleter', function () {
2121
let CODE_TS: string;
2222

2323
before(async function () {
24+
// make sure that we fall back to the default ts.sys file methods so that ts
25+
// will load the lib files from disk
26+
process.env.CI = 'true';
27+
2428
CODE_TS = await fs.readFile(
2529
path.resolve(__dirname, '..', 'test', 'fixtures', 'code.ts'),
2630
'utf8',

packages/ts-autocomplete/src/index.ts

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,11 @@ const debugError = createDebug('ts-autocomplete:error');
99

1010
type TypeFilename = string;
1111

12-
type UpdateDefinitionFunction = (newDef: Record<TypeFilename, string>) => void;
13-
14-
// TODO
15-
/*
16-
function createIOError(code: string, details = ""): NodeJS.ErrnoException {
17-
const err: NodeJS.ErrnoException = new Error(`${code} ${details}`);
18-
err.code = code;
19-
if (Error.captureStackTrace) Error.captureStackTrace(err, createIOError);
20-
return err;
21-
}
22-
*/
12+
type UpdateDefinitionFunction = (
13+
newDef: Record<TypeFilename, string | true>,
14+
) => void;
15+
2316
function relativeNodePath(fileName: string): string {
24-
//console.log(fileName);
2517
const parts = fileName.split(/\/node_modules\//g);
2618
if (parts.length === 1 && fileName.endsWith('package.json')) {
2719
// special case: when it looks up this package itself it isn't going to find
@@ -35,9 +27,6 @@ type EncounteredPaths = {
3527
getScriptSnapshot: string[];
3628
fileExists: string[];
3729
readFile: string[];
38-
//readDirectory: string[],
39-
//directoryExists: string[],
40-
//getDirectories: string[]
4130
};
4231

4332
function getVirtualLanguageService(): {
@@ -60,7 +49,7 @@ function getVirtualLanguageService(): {
6049
allowImportingTsExtensions: true,
6150
};
6251

63-
const updateCode = (newDef: Record<TypeFilename, string>): void => {
52+
const updateCode = (newDef: Record<TypeFilename, string | true>): void => {
6453
for (const [key, value] of Object.entries(newDef)) {
6554
codeHolder[key] = value;
6655
versions[key] = (versions[key] ?? 0) + 1;
@@ -75,9 +64,6 @@ function getVirtualLanguageService(): {
7564
getScriptSnapshot: [],
7665
fileExists: [],
7766
readFile: [],
78-
//readDirectory: [], // unused
79-
//directoryExists: [], // unused
80-
//getDirectories: [] // unused
8167
};
8268
const listEncounteredPaths = (): EncounteredPaths => {
8369
encounteredPaths.getScriptSnapshot.sort();
@@ -100,61 +86,66 @@ function getVirtualLanguageService(): {
10086
return ts.ScriptSnapshot.fromString(codeHolder[fileName].toString());
10187
}
10288

103-
// TODO: this should not be encountered outside of CI
104-
const result = ts.ScriptSnapshot.fromString(
105-
// NOTE: some files do not exist. A good example is "typescript/lib/es2023.ts"
106-
ts.sys.readFile(fileName) || '',
107-
);
89+
if (process.env.CI) {
90+
const result = ts.ScriptSnapshot.fromString(
91+
// NOTE: some files do not exist. A good example is "typescript/lib/es2023.ts"
92+
ts.sys.readFile(fileName) || '',
93+
);
10894

109-
encounteredPaths.getScriptSnapshot.push(relativeNodePath(fileName));
110-
return result;
95+
encounteredPaths.getScriptSnapshot.push(relativeNodePath(fileName));
96+
return result;
97+
}
11198
},
11299
getCurrentDirectory: () => process.cwd(),
113100
getCompilationSettings: () => options,
114101
getDefaultLibFileName: (options) => {
115-
const defaultLibFileName = ts.getDefaultLibFilePath(options);
116-
//console.log({ defaultLibFileName })
117-
//return '/lib.es2022.full.d.ts'
118-
return defaultLibFileName;
102+
return ts.getDefaultLibFilePath(options);
119103
},
120104
fileExists: (fileName) => {
121105
fileName = relativeNodePath(fileName);
122106
if (fileName in codeHolder) {
123107
return true;
124108
}
125109

126-
// TODO: this should not be encountered outside of CI when tests will fail
127-
const result = ts.sys.fileExists(fileName);
128-
if (result) {
129-
encounteredPaths.fileExists.push(relativeNodePath(fileName));
110+
if (process.env.CI) {
111+
const result = ts.sys.fileExists(fileName);
112+
if (result) {
113+
encounteredPaths.fileExists.push(relativeNodePath(fileName));
114+
}
115+
return result;
130116
}
131-
return result;
117+
118+
return false;
132119
},
133120
readFile: (fileName) => {
134121
fileName = relativeNodePath(fileName);
135122
if (fileName in codeHolder) {
136123
return codeHolder[fileName].toString();
137124
}
138125

139-
// TODO: this should not be encountered outside of CI when tests will fail
140-
const result = ts.sys.readFile(fileName);
141-
encounteredPaths.readFile.push(relativeNodePath(fileName));
142-
return result;
126+
if (process.env.CI) {
127+
const result = ts.sys.readFile(fileName);
128+
encounteredPaths.readFile.push(relativeNodePath(fileName));
129+
return result;
130+
}
143131
},
144132
readDirectory: (...args) => {
145-
// TODO: this should not be encountered outside of CI when tests will fail
146-
const result = ts.sys.readDirectory(...args);
147-
return result;
133+
if (process.env.CI) {
134+
return ts.sys.readDirectory(...args);
135+
}
136+
return [];
148137
},
149138
directoryExists: (...args) => {
150-
// TODO: this should not be encountered outside of CI when tests will fail
151-
const result = ts.sys.directoryExists(...args);
152-
return result;
139+
if (process.env.CI) {
140+
return ts.sys.directoryExists(...args);
141+
}
142+
return false;
153143
},
154144
getDirectories: (...args) => {
155-
// TODO: this should not be encountered outside of CI when tests will fail
156-
const result = ts.sys.getDirectories(...args);
157-
return result;
145+
if (process.env.CI) {
146+
return ts.sys.getDirectories(...args);
147+
}
148+
return [];
158149
},
159150
log: (...args) => debugLog(args),
160151
trace: (...args) => debugTrace(args),

0 commit comments

Comments
 (0)