-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathclear.ts
52 lines (48 loc) · 1.53 KB
/
clear.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import path from 'path';
import logger from '@docusaurus/logger';
import {
DEFAULT_BUILD_DIR_NAME,
GENERATED_FILES_DIR_NAME,
} from '@docusaurus/utils';
import clearPath from './utils/clearPath';
async function removePath(entry: {path: string; description: string}) {
if (!(await fs.pathExists(entry.path))) {
return;
}
try {
await clearPath(entry.path);
logger.success`Removed the ${entry.description} at path=${path.relative(
process.cwd(),
entry.path,
)}.`;
} catch (err) {
logger.error`Could not remove the ${entry.description} at path=${entry.path}.`;
logger.error(err);
}
}
export async function clear(siteDirParam: string = '.'): Promise<void> {
const siteDir = await fs.realpath(siteDirParam);
const generatedFolder = {
path: path.join(siteDir, GENERATED_FILES_DIR_NAME),
description: 'generated folder',
};
const buildFolder = {
path: path.join(siteDir, DEFAULT_BUILD_DIR_NAME),
description: 'build output folder',
};
// In Yarn PnP, cache is stored in `.yarn/.cache` because n_m doesn't exist
const cacheFolders = ['node_modules', '.yarn'].map((p) => ({
path: path.join(siteDir, p, '.cache'),
description: 'bundler persistent cache folder',
}));
await Promise.all(
[generatedFolder, buildFolder, ...cacheFolders].map(removePath),
);
}