Skip to content

Commit

Permalink
new: Add a cleanup method to artifacts.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Oct 18, 2020
1 parent 97f8c9a commit cf62015
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/Artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ export default abstract class Artifact<T = unknown> {
this.flags = flags;
}

async run(options: PackemonOptions): Promise<void> {
cleanup(): Awaitable {}

preBuild(options: PackemonOptions): Awaitable {}

build(options: PackemonOptions): Awaitable {}

postBuild(options: PackemonOptions): Awaitable {}

async runBuild(options: PackemonOptions): Promise<void> {
const start = Date.now();

try {
Expand All @@ -38,12 +46,6 @@ export default abstract class Artifact<T = unknown> {
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';
}
Expand Down
6 changes: 5 additions & 1 deletion src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default class Package {
return artifact;
}

async cleanup(): Promise<void> {
await Promise.all(this.artifacts.map((artifact) => artifact.cleanup()));
}

getName(): string {
return this.packageJson.name;
}
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/Packemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export default class Packemon extends Contract<PackemonOptions> {

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];
}
Expand Down
11 changes: 9 additions & 2 deletions src/TypesArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
// Absolute paths to each temporary config file
await Promise.all(this.apiExtractorConfigPaths.map((cfgPath) => fs.remove(cfgPath)));
}

async build(): Promise<void> {
const tsConfig = this.package.tsconfigJson;

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit cf62015

Please sign in to comment.