UNPKG

1.8 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13const path = require('path');
14const fse = require('fs-extra');
15const chalk = require('chalk');
16const { getOrCreateLogger } = require('./log-common');
17
18async 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
30class 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
63module.exports = CleanCommand;