Skip to content

Commit a45f158

Browse files
refactor: Reuse indexAction in snapshotAction
1 parent d8ef2f7 commit a45f158

File tree

2 files changed

+21
-43
lines changed

2 files changed

+21
-43
lines changed

packages/pyright-scip/src/MainCommand.ts

Lines changed: 1 addition & 0 deletions
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

Lines changed: 20 additions & 43 deletions
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,34 @@ 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+
dev: true,
76+
output: options.output,
77+
cwd: projectRoot,
78+
quiet: options.quiet,
79+
infer: { projectVersionFromCommit: false },
80+
showProgressRateLimit: undefined,
81+
});
10582

83+
const scipIndexPath = path.join(projectRoot, options.output);
84+
const scipIndex = scip.Index.deserializeBinary(fs.readFileSync(scipIndexPath));
10685
for (const doc of scipIndex.documents) {
10786
if (doc.relative_path.startsWith('..')) {
10887
continue;
@@ -120,8 +99,6 @@ function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void {
12099
writeSnapshot(outputPath, obtained);
121100
}
122101
}
123-
124-
process.chdir(originalWorkdir);
125102
}
126103
}
127104

0 commit comments

Comments
 (0)