1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const path = require('path');
|
14 | const fse = require('fs-extra');
|
15 | const chalk = require('chalk');
|
16 | const { getOrCreateLogger } = require('./log-common');
|
17 |
|
18 | async function clean(dir, logger) {
|
19 | if (!await fse.pathExists(dir)) {
|
20 | return;
|
21 | }
|
22 | try {
|
23 | await fse.remove(dir);
|
24 | logger.info(`${chalk.green('ok:')} removed ${path.relative(process.cwd(), dir)}`);
|
25 | } catch (e) {
|
26 | logger.error(`${chalk.red('error:')} unable to remove ${path.relative(process.cwd(), dir)}: ${e.message}`);
|
27 | }
|
28 | }
|
29 |
|
30 | class CleanCommand {
|
31 | constructor(logger = getOrCreateLogger()) {
|
32 | this._logger = logger;
|
33 | this._cwd = process.cwd();
|
34 | this._target = null;
|
35 | this._cacheDir = null;
|
36 | }
|
37 |
|
38 | withDirectory(dir) {
|
39 | this._cwd = dir;
|
40 | return this;
|
41 | }
|
42 |
|
43 | withTargetDir(target) {
|
44 | this._target = target;
|
45 | return this;
|
46 | }
|
47 |
|
48 | async run() {
|
49 | if (!this._target) {
|
50 | this._target = path.resolve(this._cwd, '.hlx', 'build');
|
51 | }
|
52 | if (!this._cacheDir) {
|
53 | this._cacheDir = path.resolve(this._cwd, '.hlx', 'cache');
|
54 | }
|
55 |
|
56 | await Promise.all([
|
57 | clean(this._target, this._logger),
|
58 | clean(this._cacheDir, this._logger),
|
59 | ]);
|
60 | }
|
61 | }
|
62 |
|
63 | module.exports = CleanCommand;
|