Skip to content

Commit e8074f7

Browse files
committed
Rename cleanAll to clean and take optional project as input
1 parent 5b361c8 commit e8074f7

9 files changed

Lines changed: 124 additions & 119 deletions

File tree

src/compiler/tsbuild.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ namespace ts {
256256

257257
export interface SolutionBuilder {
258258
build(project?: string, cancellationToken?: CancellationToken): ExitStatus;
259-
cleanAllProjects(): ExitStatus;
259+
clean(project?: string): ExitStatus;
260260

261261
// Currently used for testing but can be made public if needed:
262262
/*@internal*/ getBuildOrder(): ReadonlyArray<ResolvedConfigFileName>;
@@ -404,7 +404,7 @@ namespace ts {
404404
} :
405405
{
406406
build,
407-
cleanAllProjects,
407+
clean,
408408
getBuildOrder,
409409
getUpToDateStatusOfProject,
410410
invalidateProject,
@@ -1342,10 +1342,12 @@ namespace ts {
13421342
return priorNewestUpdateTime;
13431343
}
13441344

1345-
function getFilesToClean(): string[] {
1346-
// Get the same graph for cleaning we'd use for building
1347-
const filesToDelete: string[] = [];
1348-
for (const proj of getBuildOrder()) {
1345+
function clean(project?: string) {
1346+
const buildOrder = getBuildOrderFor(project);
1347+
if (!buildOrder) return ExitStatus.InvalidProject_OutputsSkipped;
1348+
1349+
const filesToDelete = options.dry ? [] as string[] : undefined;
1350+
for (const proj of buildOrder) {
13491351
const resolvedPath = toResolvedConfigFilePath(proj);
13501352
const parsed = parseConfigFile(proj, resolvedPath);
13511353
if (parsed === undefined) {
@@ -1356,22 +1358,19 @@ namespace ts {
13561358
const outputs = getAllProjectOutputs(parsed, !host.useCaseSensitiveFileNames());
13571359
for (const output of outputs) {
13581360
if (host.fileExists(output)) {
1359-
filesToDelete.push(output);
1361+
if (filesToDelete) {
1362+
filesToDelete.push(output);
1363+
}
1364+
else {
1365+
host.deleteFile(output);
1366+
invalidateResolvedProject(resolvedPath, ConfigFileProgramReloadLevel.None);
1367+
}
13601368
}
13611369
}
13621370
}
1363-
return filesToDelete;
1364-
}
13651371

1366-
function cleanAllProjects() {
1367-
const filesToDelete = getFilesToClean();
1368-
if (options.dry) {
1372+
if (filesToDelete) {
13691373
reportStatus(Diagnostics.A_non_dry_build_would_delete_the_following_files_Colon_0, filesToDelete.map(f => `\r\n * ${f}`).join(""));
1370-
return ExitStatus.Success;
1371-
}
1372-
1373-
for (const output of filesToDelete) {
1374-
host.deleteFile(output);
13751374
}
13761375

13771376
return ExitStatus.Success;
@@ -1429,7 +1428,23 @@ namespace ts {
14291428
cacheState = undefined;
14301429
}
14311430

1431+
function getBuildOrderFor(project: string | undefined) {
1432+
const resolvedProject = project && resolveProjectName(project);
1433+
if (resolvedProject) {
1434+
const projectPath = toResolvedConfigFilePath(resolvedProject);
1435+
const projectIndex = findIndex(
1436+
getBuildOrder(),
1437+
configFileName => toResolvedConfigFilePath(configFileName) === projectPath
1438+
);
1439+
if (projectIndex === -1) return undefined;
1440+
}
1441+
return resolvedProject ? createBuildOrder([resolvedProject]) : getBuildOrder();
1442+
}
1443+
14321444
function build(project?: string, cancellationToken?: CancellationToken): ExitStatus {
1445+
const buildOrder = getBuildOrderFor(project);
1446+
if (!buildOrder) return ExitStatus.InvalidProject_OutputsSkipped;
1447+
14331448
// Set initial build if not already built
14341449
if (allProjectBuildPending) {
14351450
allProjectBuildPending = false;
@@ -1447,16 +1462,6 @@ namespace ts {
14471462

14481463
let successfulProjects = 0;
14491464
let errorProjects = 0;
1450-
const resolvedProject = project && resolveProjectName(project);
1451-
if (resolvedProject) {
1452-
const projectPath = toResolvedConfigFilePath(resolvedProject);
1453-
const projectIndex = findIndex(
1454-
getBuildOrder(),
1455-
configFileName => toResolvedConfigFilePath(configFileName) === projectPath
1456-
);
1457-
if (projectIndex === -1) return ExitStatus.InvalidProject_OutputsSkipped;
1458-
}
1459-
const buildOrder = resolvedProject ? createBuildOrder([resolvedProject]) : getBuildOrder();
14601465
while (true) {
14611466
const invalidatedProject = getNextInvalidatedProject(buildOrder);
14621467
if (!invalidatedProject) {

src/testRunner/unittests/tsbuild/emptyFiles.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ namespace ts {
1818
host.assertDiagnosticMessages([Diagnostics.The_files_list_in_config_file_0_is_empty, "/src/no-references/tsconfig.json"]);
1919

2020
// Check for outputs to not be written.
21-
for (const output of allExpectedOutputs) {
22-
assert(!fs.existsSync(output), `Expect file ${output} to not exist`);
23-
}
21+
verifyOutputsAbsent(fs, allExpectedOutputs);
2422
});
2523

2624
it("does not have empty files diagnostic when files is empty and references are provided", () => {
@@ -33,9 +31,7 @@ namespace ts {
3331
host.assertDiagnosticMessages(/*empty*/);
3432

3533
// Check for outputs to be written.
36-
for (const output of allExpectedOutputs) {
37-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
38-
}
34+
verifyOutputsPresent(fs, allExpectedOutputs);
3935
});
4036
});
4137
}

src/testRunner/unittests/tsbuild/helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ declare const console: { log(msg: any): void; };`;
8282
return fs;
8383
}
8484

85+
export function verifyOutputsPresent(fs: vfs.FileSystem, outputs: readonly string[]) {
86+
for (const output of outputs) {
87+
assert(fs.existsSync(output), `Expect file ${output} to exist`);
88+
}
89+
}
90+
91+
export function verifyOutputsAbsent(fs: vfs.FileSystem, outputs: readonly string[]) {
92+
for (const output of outputs) {
93+
assert.isFalse(fs.existsSync(output), `Expect file ${output} to not exist`);
94+
}
95+
}
96+
8597
function generateSourceMapBaselineFiles(fs: vfs.FileSystem, mapFileNames: ReadonlyArray<string>) {
8698
for (const mapFile of mapFileNames) {
8799
if (!fs.existsSync(mapFile)) continue;

src/testRunner/unittests/tsbuild/outFile.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,14 @@ namespace ts {
366366
builder.build();
367367
host.assertDiagnosticMessages(...initialExpectedDiagnostics);
368368
// Verify they exist
369-
for (const output of expectedOutputs) {
370-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
371-
}
369+
verifyOutputsPresent(fs, expectedOutputs);
372370
host.clearDiagnostics();
373-
builder.cleanAllProjects();
371+
builder.clean();
374372
host.assertDiagnosticMessages(/*none*/);
375373
// Verify they are gone
376-
for (const output of expectedOutputs) {
377-
assert(!fs.existsSync(output), `Expect file ${output} to not exist`);
378-
}
374+
verifyOutputsAbsent(fs, expectedOutputs);
379375
// Subsequent clean shouldn't throw / etc
380-
builder.cleanAllProjects();
376+
builder.clean();
381377
});
382378

383379
it("verify buildInfo absence results in new build", () => {
@@ -392,9 +388,7 @@ namespace ts {
392388
builder.build();
393389
host.assertDiagnosticMessages(...initialExpectedDiagnostics);
394390
// Verify they exist
395-
for (const output of expectedOutputs) {
396-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
397-
}
391+
verifyOutputsPresent(fs, expectedOutputs);
398392
// Delete bundle info
399393
host.clearDiagnostics();
400394
host.deleteFile(outputFiles[project.first][ext.buildinfo]);
@@ -419,10 +413,8 @@ namespace ts {
419413
builder.build();
420414
host.assertDiagnosticMessages(...initialExpectedDiagnostics);
421415
// Verify they exist - without tsbuildinfo for third project
422-
for (const output of expectedOutputFiles.slice(0, expectedOutputFiles.length - 2)) {
423-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
424-
}
425-
assert.isFalse(fs.existsSync(outputFiles[project.third][ext.buildinfo]), `Expect file ${outputFiles[project.third][ext.buildinfo]} to not exist`);
416+
verifyOutputsPresent(fs, expectedOutputFiles.slice(0, expectedOutputFiles.length - 2));
417+
verifyOutputsAbsent(fs, [outputFiles[project.third][ext.buildinfo]]);
426418
});
427419

428420
it("rebuilds completely when version in tsbuildinfo doesnt match ts version", () => {
@@ -496,13 +488,23 @@ namespace ts {
496488
const result = builder.build(sources[project.second][source.config]);
497489
host.assertDiagnosticMessages(/*empty*/);
498490
// First and Third is not built
499-
for (const output of [...outputFiles[project.first], ...outputFiles[project.third]]) {
500-
assert.isFalse(fs.existsSync(output), `Expect file ${output} to not exist`);
501-
}
491+
verifyOutputsAbsent(fs, [...outputFiles[project.first], ...outputFiles[project.third]]);
502492
// second is built
503-
for (const output of outputFiles[project.second]) {
504-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
505-
}
493+
verifyOutputsPresent(fs, outputFiles[project.second]);
494+
assert.equal(result, ExitStatus.Success);
495+
});
496+
497+
it("cleans till project specified", () => {
498+
const fs = outFileFs.shadow();
499+
const host = new fakes.SolutionBuilderHost(fs);
500+
const builder = createSolutionBuilder(host, { verbose: false });
501+
builder.build();
502+
const result = builder.clean(sources[project.second][source.config]);
503+
host.assertDiagnosticMessages(/*empty*/);
504+
// First and Third output for present
505+
verifyOutputsPresent(fs, [...outputFiles[project.first], ...outputFiles[project.third]]);
506+
// second is cleaned
507+
verifyOutputsAbsent(fs, outputFiles[project.second]);
506508
assert.equal(result, ExitStatus.Success);
507509
});
508510

@@ -940,9 +942,7 @@ ${internal} enum internalEnum { a, b, c }`);
940942
removeFileExtension(f) + Extension.Dts + ".map",
941943
])
942944
]);
943-
for (const output of expectedOutputFiles) {
944-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
945-
}
945+
verifyOutputsPresent(fs, expectedOutputFiles);
946946
});
947947
});
948948
}

src/testRunner/unittests/tsbuild/referencesWithRootDirInParent.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ namespace ts {
2121
const builder = createSolutionBuilder(host, ["/src/src/main", "/src/src/other"], {});
2222
builder.build();
2323
host.assertDiagnosticMessages(/*empty*/);
24-
for (const output of allExpectedOutputs) {
25-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
26-
}
24+
verifyOutputsPresent(fs, allExpectedOutputs);
2725
});
2826

2927
it("verify that it reports error for same .tsbuildinfo file because no rootDir in the base", () => {
@@ -48,12 +46,8 @@ namespace ts {
4846
[Diagnostics.Building_project_0, "/src/src/main/tsconfig.json"],
4947
[Diagnostics.Cannot_write_file_0_because_it_will_overwrite_tsbuildinfo_file_generated_by_referenced_project_1, "/src/dist/tsconfig.tsbuildinfo", "/src/src/other"]
5048
);
51-
for (const output of allExpectedOutputs) {
52-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
53-
}
54-
for (const output of missingOutputs) {
55-
assert.isFalse(fs.existsSync(output), `Expect file ${output} to not exist`);
56-
}
49+
verifyOutputsPresent(fs, allExpectedOutputs);
50+
verifyOutputsAbsent(fs, missingOutputs);
5751
});
5852

5953
it("verify that it reports error for same .tsbuildinfo file", () => {
@@ -84,12 +78,8 @@ namespace ts {
8478
[Diagnostics.Building_project_0, "/src/src/main/tsconfig.json"],
8579
[Diagnostics.Cannot_write_file_0_because_it_will_overwrite_tsbuildinfo_file_generated_by_referenced_project_1, "/src/dist/tsconfig.tsbuildinfo", "/src/src/other"]
8680
);
87-
for (const output of allExpectedOutputs) {
88-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
89-
}
90-
for (const output of missingOutputs) {
91-
assert.isFalse(fs.existsSync(output), `Expect file ${output} to not exist`);
92-
}
81+
verifyOutputsPresent(fs, allExpectedOutputs);
82+
verifyOutputsAbsent(fs, missingOutputs);
9383
});
9484

9585
it("verify that it reports no error when .tsbuildinfo differ", () => {
@@ -120,9 +110,7 @@ namespace ts {
120110
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/src/main/tsconfig.main.json", "src/dist/a.js"],
121111
[Diagnostics.Building_project_0, "/src/src/main/tsconfig.main.json"]
122112
);
123-
for (const output of allExpectedOutputs) {
124-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
125-
}
113+
verifyOutputsPresent(fs, allExpectedOutputs);
126114
});
127115
});
128116
}

src/testRunner/unittests/tsbuild/resolveJsonModule.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ namespace ts {
2323
host.assertDiagnosticMessages(...expectedDiagnosticMessages);
2424
if (!expectedDiagnosticMessages.length) {
2525
// Check for outputs. Not an exhaustive list
26-
for (const output of allExpectedOutputs) {
27-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
28-
}
26+
verifyOutputsPresent(fs, allExpectedOutputs);
2927
}
3028
}
3129

@@ -71,9 +69,7 @@ export default hello.hello`);
7169
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, configFile, "src/dist/src/index.js"],
7270
[Diagnostics.Building_project_0, `/${configFile}`]
7371
);
74-
for (const output of [...allExpectedOutputs, "/src/dist/src/index.js.map"]) {
75-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
76-
}
72+
verifyOutputsPresent(fs, [...allExpectedOutputs, "/src/dist/src/index.js.map"]);
7773
host.clearDiagnostics();
7874
builder = createSolutionBuilder(host, [configFile], { verbose: true });
7975
tick();
@@ -96,9 +92,7 @@ export default hello.hello`);
9692
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, configFile, "src/src/index.js"],
9793
[Diagnostics.Building_project_0, `/${configFile}`]
9894
);
99-
for (const output of ["/src/src/index.js", "/src/src/index.d.ts"]) {
100-
assert(fs.existsSync(output), `Expect file ${output} to exist`);
101-
}
95+
verifyOutputsPresent(fs, ["/src/src/index.js", "/src/src/index.d.ts"]);
10296
host.clearDiagnostics();
10397
builder = createSolutionBuilder(host, [configFile], { verbose: true });
10498
tick();
@@ -137,7 +131,7 @@ export default hello.hello`);
137131
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, mainConfigFile, "src/main/index.js"],
138132
[Diagnostics.Building_project_0, `/${mainConfigFile}`],
139133
);
140-
assert(fs.existsSync(expectedOutput), `Expect file ${expectedOutput} to exist`);
134+
verifyOutputsPresent(fs, [expectedOutput]);
141135
host.clearDiagnostics();
142136
builder = createSolutionBuilder(host, [configFile], { verbose: true });
143137
tick();

0 commit comments

Comments
 (0)