Skip to content

Commit 80dbbcf

Browse files
committed
fix(vscode): remove json caching in various places
1 parent a92b965 commit 80dbbcf

File tree

17 files changed

+117
-193
lines changed

17 files changed

+117
-193
lines changed

libs/language-server/watcher/src/lib/native-watcher.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ export class NativeWatcher {
2626
private watcher: Watcher | undefined;
2727
private stopped = false;
2828

29-
constructor(private workspacePath: string, private callback: () => unknown) {
29+
constructor(
30+
private workspacePath: string,
31+
private callback: () => unknown,
32+
) {
3033
this.initWatcher();
3134
}
3235

@@ -39,7 +42,7 @@ export class NativeWatcher {
3942
const native = await importNxPackagePath<typeof import('nx/src/native')>(
4043
this.workspacePath,
4144
'src/native/index.js',
42-
lspLogger
45+
lspLogger,
4346
);
4447
this.watcher = new native.Watcher(this.workspacePath);
4548

@@ -57,13 +60,13 @@ export class NativeWatcher {
5760
path.endsWith('workspace.json') ||
5861
path.endsWith('tsconfig.base.json') ||
5962
NX_PLUGIN_PATTERNS_TO_WATCH.some((pattern) =>
60-
minimatch([path], pattern, { dot: true })
63+
minimatch([path], pattern, { dot: true }),
6164
) ||
6265
NativeWatcher.openDocuments.has(path)) &&
6366
!path.startsWith('node_modules') &&
6467
!path.startsWith(normalize('.nx/cache')) &&
6568
!path.startsWith(normalize('.yarn/cache')) &&
66-
!path.startsWith(normalize('.nx/workspace-data'))
69+
!path.startsWith(normalize('.nx/workspace-data')),
6770
)
6871
) {
6972
if (this.stopped) {
@@ -73,7 +76,7 @@ export class NativeWatcher {
7376
lspLogger.log(
7477
`native watcher detected project changes in files ${events
7578
.map((e) => normalize(e.path))
76-
.join(', ')}`
79+
.join(', ')}`,
7780
);
7881
this.callback();
7982
}

libs/language-server/workspace/src/lib/get-generator-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readAndCacheJsonFile } from '@nx-console/shared-file-system';
1+
import { readJsonFile } from '@nx-console/shared-file-system';
22
import { Option } from '@nx-console/shared-schema';
33
import { normalizeSchema } from '@nx-console/shared-schema';
44
import { nxWorkspace } from '@nx-console/shared-nx-workspace-info';
@@ -10,7 +10,7 @@ export async function getGeneratorOptions(
1010
generatorName: string,
1111
generatorPath: string,
1212
): Promise<Option[]> {
13-
const generatorSchema = await readAndCacheJsonFile(
13+
const generatorSchema = await readJsonFile(
1414
generatorPath,
1515
undefined,
1616
lspLogger,

libs/shared/file-system/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
export { fileExists } from './lib/file-exists';
22
export { directoryExists } from './lib/directory-exists';
3-
export { cacheJson } from './lib/cache-json';
4-
export { readAndCacheJsonFile } from './lib/cache-json';
5-
export { clearJsonCache } from './lib/cache-json';
6-
export { readAndParseJson } from './lib/cache-json';
3+
export { readJsonFile } from './lib/read-json-file';
4+
export { readAndParseJson } from './lib/read-json-file';
75
export { listFiles } from './lib/list-files';
86
export { readDirectory } from './lib/read-directory';
97
export { readFile } from './lib/read-file';

libs/shared/file-system/src/lib/cache-json.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

libs/shared/file-system/src/lib/read-file.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { crossFs } from './cache-json';
1+
import { PosixFS } from '@yarnpkg/fslib';
2+
import { ZipOpenFS, getLibzipSync as libzip } from '@yarnpkg/libzip';
3+
4+
const zipOpenFs = new ZipOpenFS({ libzip });
5+
export const crossFs = new PosixFS(zipOpenFs);
26

37
export async function readFile(filePath: string): Promise<string> {
48
try {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as path from 'path';
2+
import { Logger } from '@nx-console/shared-utils';
3+
import { readFileSync, existsSync } from 'fs';
4+
import { parse as parseJson, ParseError } from 'jsonc-parser';
5+
6+
export async function readAndParseJson(filePath: string) {
7+
const content = readFileSync(filePath, 'utf8');
8+
try {
9+
return JSON.parse(content);
10+
} catch {
11+
const errors: ParseError[] = [];
12+
const result = parseJson(content, errors);
13+
return result;
14+
}
15+
}
16+
17+
export async function readJsonFile(
18+
filePath: string | undefined,
19+
basedir = '',
20+
logger?: Logger,
21+
): Promise<{ path: string; json: any }> {
22+
if (!filePath) {
23+
return {
24+
path: '',
25+
json: {},
26+
};
27+
}
28+
29+
let fullFilePath = basedir ? path.join(basedir, filePath) : filePath;
30+
if (fullFilePath.startsWith('file:\\')) {
31+
fullFilePath = fullFilePath.replace('file:\\', '');
32+
}
33+
if (fullFilePath.startsWith('file://')) {
34+
fullFilePath = fullFilePath.replace('file://', '');
35+
}
36+
37+
try {
38+
if (existsSync(fullFilePath)) {
39+
const json = await readAndParseJson(fullFilePath);
40+
return {
41+
path: fullFilePath,
42+
json,
43+
};
44+
}
45+
} catch (e) {
46+
logger?.log(`${fullFilePath} does not exist`);
47+
}
48+
49+
return {
50+
path: fullFilePath,
51+
json: {},
52+
};
53+
}

libs/shared/npm/src/lib/check-is-nx-workspace.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { join } from 'path';
2-
import {
3-
fileExists,
4-
readAndCacheJsonFile,
5-
} from '@nx-console/shared-file-system';
2+
import { fileExists, readJsonFile } from '@nx-console/shared-file-system';
63
import { coerce } from 'semver';
74
import { workspaceDependencyPath } from './workspace-dependencies';
85

@@ -18,15 +15,15 @@ export async function checkIsNxWorkspace(
1815
return false;
1916
}
2017

21-
const lernaPackageJson = await readAndCacheJsonFile(
18+
const lernaPackageJson = await readJsonFile(
2219
join(lernaPath, 'package.json'),
2320
);
2421
const lernaVersion = coerce(lernaPackageJson.json.version);
2522

2623
if (lernaVersion?.major ?? 0 >= 6) {
2724
isNxWorkspace = true;
2825
} else {
29-
const lerna = await readAndCacheJsonFile('lerna.json', workspacePath);
26+
const lerna = await readJsonFile('lerna.json', workspacePath);
3027
isNxWorkspace = lerna.json.useNx ?? false;
3128
}
3229
}
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import { readAndCacheJsonFile } from '@nx-console/shared-file-system';
1+
import { readJsonFile } from '@nx-console/shared-file-system';
22
import { packageDetails } from './package-details';
33
import { normalize } from 'node:path';
44

55
jest.mock('@nx-console/shared-file-system');
66

77
describe('packageDetails', () => {
88
it('should return package path, package name and JSON contents', async () => {
9-
const readAndCacheJsonFileMock = jest
10-
.mocked(readAndCacheJsonFile)
11-
.mockResolvedValueOnce({
12-
path: 'path',
13-
json: {
14-
name: 'utils',
15-
version: '1.0.0',
16-
main: 'dist/index.js',
17-
types: 'dist/index.d.ts',
18-
},
19-
});
9+
const readJsonFileMock = jest.mocked(readJsonFile).mockResolvedValueOnce({
10+
path: 'path',
11+
json: {
12+
name: 'utils',
13+
version: '1.0.0',
14+
main: 'dist/index.js',
15+
types: 'dist/index.d.ts',
16+
},
17+
});
2018

2119
expect(await packageDetails('libs/utils')).toEqual({
2220
packagePath: 'libs/utils',
@@ -28,24 +26,22 @@ describe('packageDetails', () => {
2826
types: 'dist/index.d.ts',
2927
},
3028
});
31-
expect(readAndCacheJsonFileMock).toBeCalledWith(
29+
expect(readJsonFileMock).toBeCalledWith(
3230
normalize('libs/utils/package.json'),
3331
);
3432
});
3533

3634
it('should return undefined package name if JSON is empty', async () => {
37-
const readAndCacheJsonFileMock = jest
38-
.mocked(readAndCacheJsonFile)
39-
.mockResolvedValueOnce({
40-
path: '',
41-
json: {},
42-
});
35+
const readJsonFileMock = jest.mocked(readJsonFile).mockResolvedValueOnce({
36+
path: '',
37+
json: {},
38+
});
4339

4440
expect(await packageDetails('')).toEqual({
4541
packagePath: '',
4642
packageName: undefined,
4743
packageJson: {},
4844
});
49-
expect(readAndCacheJsonFileMock).toBeCalledWith('package.json');
45+
expect(readJsonFileMock).toBeCalledWith('package.json');
5046
});
5147
});

libs/shared/npm/src/lib/package-details.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { readAndCacheJsonFile } from '@nx-console/shared-file-system';
1+
import { readJsonFile } from '@nx-console/shared-file-system';
22
import { join } from 'path';
33

44
export async function packageDetails(packagePath: string) {
5-
const { json } = await readAndCacheJsonFile(
6-
join(packagePath, 'package.json')
7-
);
5+
const { json } = await readJsonFile(join(packagePath, 'package.json'));
86
return {
97
packagePath,
108
packageName: json.name,

libs/shared/nx-workspace-info/src/lib/get-executors.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@ import { Logger } from '@nx-console/shared-utils';
44

55
export type GetExecutorsOptions = {
66
includeHidden: boolean;
7-
clearPackageJsonCache: boolean;
87
};
98

109
export async function getExecutors(
1110
workspacePath: string,
1211
options: GetExecutorsOptions = {
1312
includeHidden: false,
14-
clearPackageJsonCache: false,
1513
},
1614
logger?: Logger,
1715
): Promise<ExecutorCollectionInfo[]> {
1816
return (
1917
await readCollections(
2018
workspacePath,
2119
{
22-
clearPackageJsonCache: options.clearPackageJsonCache,
2320
includeHidden: options.includeHidden,
2421
},
2522
logger,

0 commit comments

Comments
 (0)