Skip to content

Commit

Permalink
new: Add new clean command.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Oct 26, 2020
1 parent 4e0c8f8 commit c6fe0bc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@types/fs-extra": "^9.0.2",
"@types/node": "^14.14.2",
"@types/react": "^16.9.53",
"@types/rimraf": "^3.0.0",
"@types/semver": "^7.3.4"
},
"dependencies": {
Expand Down Expand Up @@ -91,6 +92,7 @@
"ink-progress-bar": "^3.0.0",
"ink-spinner": "^4.0.1",
"react": "^16.14.0",
"rimraf": "^3.0.2",
"rollup": "^2.32.1",
"rollup-plugin-node-externals": "^2.2.0",
"semver": "^7.3.2",
Expand Down
58 changes: 54 additions & 4 deletions src/Packemon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs-extra';
import rimraf from 'rimraf';
import {
Blueprint,
json,
Expand Down Expand Up @@ -108,17 +109,66 @@ export default class Packemon {
const { errors } = await pipeline.run();

// Always cleanup whether a successful or failed build
debug('Cleaning build artifacts');

await Promise.all(this.packages.map((pkg) => pkg.cleanup()));
await this.cleanTemporaryFiles();

// Throw to trigger an error screen in the terminal
if (errors.length > 0) {
throw errors[0];
}
}

protected async findPackages(skipPrivate: boolean) {
async clean() {
debug('Starting clean process');

await this.findPackages();
await this.cleanTemporaryFiles();

const formatFolders = '{cjs,dts,esm,lib,mjs,umd}';
const pathsToRemove: string[] = [];

if (this.project.isWorkspacesEnabled()) {
this.project.workspaces.forEach((ws) => {
let path = ws;

if (path.endsWith('*')) {
path += `/${formatFolders}`;
} else if (path.endsWith('/')) {
path += formatFolders;
}

pathsToRemove.push(path);
});
} else {
pathsToRemove.push(`./${formatFolders}`);
}

debug('Cleaning build artifacts');

await Promise.all(
pathsToRemove.map(
(path) =>
new Promise((resolve, reject) => {
debug('\t%s', path);

rimraf(path, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
}),
),
);
}

protected async cleanTemporaryFiles() {
debug('Cleaning temporary build files');

await Promise.all(this.packages.map((pkg) => pkg.cleanup()));
}

protected async findPackages(skipPrivate: boolean = false) {
debug('Finding packages in project');

const pkgPaths: Path[] = [];
Expand Down
7 changes: 5 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Program, checkPackageOutdated } from '@boost/cli';
// eslint-disable-next-line unicorn/import-index
import { BuildCommand } from './index';
import { BuildCommand, CleanCommand } from './index';

const version = String(require('../package.json').version);

Expand All @@ -14,7 +14,10 @@ async function run() {
version,
});

program.middleware(checkPackageOutdated('packemon', version)).register(new BuildCommand());
program
.middleware(checkPackageOutdated('packemon', version))
.register(new BuildCommand())
.register(new CleanCommand());

await program.runAndExit(process.argv);
}
Expand Down
16 changes: 16 additions & 0 deletions src/commands/Clean.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Arg, Command, Config, GlobalOptions } from '@boost/cli';
import Packemon from '../Packemon';

export type CleanParams = [string];

@Config('clean', 'Clean build artifacts from all packages.')
export class CleanCommand extends Command<GlobalOptions, CleanParams> {
@Arg.Params<CleanParams>({
description: 'Project root that contains a `package.json`',
label: 'cwd',
type: 'string',
})
async run(cwd: string = process.cwd()) {
await new Packemon(cwd).clean();
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import TypesArtifact from './TypesArtifact';
import { getBabelInputConfig, getBabelOutputConfig } from './babel/config';

export * from './commands/Build';
export * from './commands/Clean';
export * from './constants';
export * from './types';

Expand Down
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,14 @@
dependencies:
"@types/node" "*"

"@types/glob@*":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
integrity sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==
dependencies:
"@types/minimatch" "*"
"@types/node" "*"

"@types/graceful-fs@^4.1.2":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.3.tgz#039af35fe26bec35003e8d86d2ee9c586354348f"
Expand Down Expand Up @@ -1882,6 +1890,11 @@
dependencies:
"@types/braces" "*"

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==

"@types/node@*", "@types/node@^14.14.2":
version "14.14.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.2.tgz#d25295f9e4ca5989a2c610754dc02a9721235eeb"
Expand Down Expand Up @@ -1922,6 +1935,14 @@
dependencies:
"@types/node" "*"

"@types/rimraf@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-3.0.0.tgz#b9d03f090ece263671898d57bb7bb007023ac19f"
integrity sha512-7WhJ0MdpFgYQPXlF4Dx+DhgvlPCfz/x5mHaeDQAKhcenvQP1KCpLQ18JklAqeGMYSAT2PxLpzd0g2/HE7fj7hQ==
dependencies:
"@types/glob" "*"
"@types/node" "*"

"@types/semver@^7.3.4":
version "7.3.4"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.4.tgz#43d7168fec6fa0988bb1a513a697b29296721afb"
Expand Down

0 comments on commit c6fe0bc

Please sign in to comment.