diff --git a/src/Artifact.ts b/src/Artifact.ts index 10877eea7..ebce3707f 100644 --- a/src/Artifact.ts +++ b/src/Artifact.ts @@ -18,7 +18,15 @@ export default abstract class Artifact { this.flags = flags; } - async run(options: PackemonOptions): Promise { + cleanup(): Awaitable {} + + preBuild(options: PackemonOptions): Awaitable {} + + build(options: PackemonOptions): Awaitable {} + + postBuild(options: PackemonOptions): Awaitable {} + + async runBuild(options: PackemonOptions): Promise { const start = Date.now(); try { @@ -38,12 +46,6 @@ export default abstract class Artifact { this.result.time = Date.now() - start; } - preBuild(options: PackemonOptions): Awaitable {} - - build(options: PackemonOptions): Awaitable {} - - postBuild(options: PackemonOptions): Awaitable {} - isComplete(): boolean { return this.state === 'passed' || this.state === 'failed'; } diff --git a/src/Package.ts b/src/Package.ts index 19209837f..0c49cc408 100644 --- a/src/Package.ts +++ b/src/Package.ts @@ -43,6 +43,10 @@ export default class Package { return artifact; } + async cleanup(): Promise { + await Promise.all(this.artifacts.map((artifact) => artifact.cleanup())); + } + getName(): string { return this.packageJson.name; } @@ -110,7 +114,7 @@ export default class Package { } // Process artifacts in parallel - await Promise.all(this.artifacts.map((artifact) => artifact.run(options))); + await Promise.all(this.artifacts.map((artifact) => artifact.runBuild(options))); // Sync `package.json` in case it was modified await this.syncPackageJson(); diff --git a/src/Packemon.ts b/src/Packemon.ts index fa91341b4..99470395c 100644 --- a/src/Packemon.ts +++ b/src/Packemon.ts @@ -102,6 +102,10 @@ export default class Packemon extends Contract { const { errors } = await pipeline.run(); + // Always cleanup whether a successful or failed build + await Promise.all(this.packages.map((pkg) => pkg.cleanup())); + + // Throw to trigger an error screen in the terminal if (errors.length > 0) { throw errors[0]; } diff --git a/src/TypesArtifact.ts b/src/TypesArtifact.ts index eec1b77a1..99d7e60a0 100644 --- a/src/TypesArtifact.ts +++ b/src/TypesArtifact.ts @@ -16,6 +16,13 @@ const extractorConfig = require(path.join(__dirname, '../api-extractor.json')) a }; export default class TypesArtifact extends Artifact { + private apiExtractorConfigPaths: string[] = []; + + async cleanup(): Promise { + // Absolute paths to each temporary config file + await Promise.all(this.apiExtractorConfigPaths.map((cfgPath) => fs.remove(cfgPath))); + } + async build(): Promise { const tsConfig = this.package.tsconfigJson; @@ -95,8 +102,8 @@ export default class TypesArtifact extends Artifact { ); } - // Remove the config file - await fs.unlink(configPath); + // Enqueue to remove the config file + this.apiExtractorConfigPaths.push(configPath); return result; }