Build artifact management and cleanup functionality for TypeScript projects using gts.
Remove build output files based on TypeScript configuration.
/**
* Remove files generated by the build process
* @param options - Configuration options including target directory
* @returns Promise resolving to true on successful cleanup, false on error
*/
function clean(options: Options): Promise<boolean>;Usage Example:
import { clean } from "gts/build/src/clean";
import { Options } from "gts/build/src/cli";
const options: Options = {
dryRun: false,
gtsRootDir: "/path/to/gts",
targetRootDir: process.cwd(),
yes: false,
no: false,
logger: console,
};
const success = await clean(options);
if (success) {
console.log("Build artifacts cleaned successfully");
} else {
console.error("Clean operation failed");
}# Clean build artifacts
gts clean
# Preview cleanup without deleting (dry run)
gts clean --dry-runThe clean function performs the following steps:
compilerOptions.outDir is defined and not "."The clean operation requires a valid tsconfig.json file with:
{
"compilerOptions": {
"outDir": "build" // Must be defined and not "."
}
}The clean function integrates with TypeScript's configuration system:
interface TSConfig {
compilerOptions: ts.CompilerOptions;
}The function uses TypeScript's CompilerOptions interface to read the outDir setting, ensuring compatibility with all valid TypeScript configurations.
The clean function handles several error conditions:
If compilerOptions.outDir is not defined in tsconfig.json:
ERROR: The clean command requires compilerOptions.outDir to be defined in tsconfig.json.If outDir is set to "." (current directory):
ERROR: compilerOptions.outDir cannot use the value ".". That would delete all of our sources.File system errors during directory removal are propagated as exceptions.
Example with error handling:
import { clean } from "gts/build/src/clean";
try {
const success = await clean(options);
if (!success) {
console.error("Clean failed - check tsconfig.json configuration");
process.exit(1);
}
} catch (error) {
console.error("System error during cleanup:", error.message);
process.exit(1);
}The clean function respects TypeScript's configuration hierarchy:
extends configuration inheritanceSeveral safety features prevent accidental data loss:
outDir is "."outDir configurationTypical project structure before and after cleaning:
Before cleaning:
project/
├── src/
│ └── index.ts
├── build/ # Generated by TypeScript compiler
│ ├── src/
│ │ └── index.js
│ └── test/
│ └── test.js
├── tsconfig.json
└── package.jsonAfter cleaning:
project/
├── src/
│ └── index.ts # Source files preserved
├── tsconfig.json
└── package.jsonThe clean operation:
rimraf for efficient recursive directory removalCommonly used in npm scripts for development workflows:
{
"scripts": {
"clean": "gts clean",
"build": "npm run clean && tsc",
"rebuild": "npm run clean && npm run compile"
}
}