Skip to content

Commit 05bc904

Browse files
refactor: Reuse indexAction in snapshotAction (#116)
This avoids having two different code paths creating an Indexer object, and dealing with more path normalization.
1 parent d8ef2f7 commit 05bc904

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

packages/pyright-scip/src/MainCommand.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface IndexOptions {
1010
output: string;
1111
cwd: string;
1212
targetOnly?: string;
13+
infer?: { projectVersionFromCommit: boolean };
1314

1415
// Progress reporting configuration
1516
quiet: boolean;

packages/pyright-scip/src/main-impl.ts

+23-43
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function indexAction(options: IndexOptions): void {
3030
...options,
3131
projectRoot,
3232
environment,
33-
infer: { projectVersionFromCommit: true },
33+
infer: options.infer ?? { projectVersionFromCommit: true },
3434
writeIndex: (partialIndex: scip.Index): void => {
3535
fs.writeSync(output, partialIndex.serializeBinary());
3636
},
@@ -54,55 +54,37 @@ function indexAction(options: IndexOptions): void {
5454
}
5555

5656
function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void {
57-
setQuiet(options.quiet);
58-
if (options.showProgressRateLimit !== undefined) {
59-
setShowProgressRateLimit(options.showProgressRateLimit);
60-
}
61-
62-
console.log('... Snapshotting ... ');
63-
const environment = options.environment ? path.resolve(options.environment) : undefined;
64-
65-
const snapshotOnly = options.only;
66-
57+
const subdir: string = options.only;
6758
const inputDirectory = path.resolve(join(snapshotRoot, 'input'));
6859
const outputDirectory = path.resolve(join(snapshotRoot, 'output'));
6960

70-
// Either read all the directories or just the one passed in by name
7161
let snapshotDirectories = fs.readdirSync(inputDirectory);
72-
if (snapshotOnly) {
73-
snapshotDirectories = [snapshotOnly];
62+
if (subdir) {
63+
console.assert(snapshotDirectories.find((val) => val === subdir) !== undefined);
64+
snapshotDirectories = [subdir];
7465
}
7566

7667
for (const snapshotDir of snapshotDirectories) {
7768
let projectRoot = join(inputDirectory, snapshotDir);
78-
if (!fs.lstatSync(projectRoot).isDirectory()) {
79-
continue;
80-
}
81-
82-
projectRoot = path.resolve(projectRoot);
83-
const originalWorkdir = process.cwd();
84-
process.chdir(projectRoot);
85-
86-
const scipBinaryFile = path.join(projectRoot, options.output);
87-
const output = fs.openSync(scipBinaryFile, 'w');
88-
89-
if (options.index) {
90-
let indexer = new Indexer({
91-
...options,
92-
projectRoot,
93-
environment,
94-
infer: { projectVersionFromCommit: false },
95-
writeIndex: (partialIndex: any): void => {
96-
fs.writeSync(output, partialIndex.serializeBinary());
97-
},
98-
});
99-
indexer.index();
100-
fs.close(output);
101-
}
102-
103-
const contents = fs.readFileSync(scipBinaryFile);
104-
const scipIndex = scip.Index.deserializeBinary(contents);
69+
console.assert(fs.lstatSync(projectRoot).isDirectory());
70+
console.log(`Output path = ${options.output}`);
71+
72+
indexAction({
73+
projectName: options.projectName,
74+
projectVersion: options.projectVersion,
75+
projectNamespace: options.projectNamespace,
76+
environment: options.environment ? path.resolve(options.environment) : undefined,
77+
dev: options.dev,
78+
output: options.output,
79+
cwd: projectRoot,
80+
targetOnly: options.targetOnly,
81+
infer: { projectVersionFromCommit: false },
82+
quiet: options.quiet,
83+
showProgressRateLimit: undefined,
84+
});
10585

86+
const scipIndexPath = path.join(projectRoot, options.output);
87+
const scipIndex = scip.Index.deserializeBinary(fs.readFileSync(scipIndexPath));
10688
for (const doc of scipIndex.documents) {
10789
if (doc.relative_path.startsWith('..')) {
10890
continue;
@@ -120,8 +102,6 @@ function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void {
120102
writeSnapshot(outputPath, obtained);
121103
}
122104
}
123-
124-
process.chdir(originalWorkdir);
125105
}
126106
}
127107

packages/pyright-scip/test/test-main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function testMain(mode: 'check' | 'update'): void {
3232
projectVersion,
3333
'--only',
3434
subdirName,
35-
];
35+
]; // FIXME: This should pass with a --dev flag
3636
if (mode === 'check') {
3737
argv.push('--check');
3838
}

0 commit comments

Comments
 (0)