-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcleanCollateral.ts
87 lines (76 loc) · 3.59 KB
/
cleanCollateral.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as fs from 'fs';
import * as Path from 'path';
import rimraf from 'rimraf';
import { getOrThrowFromProcess } from './helpers/getOrThrowFromProcess';
import { getGameDeploymentRootPaths } from './helpers/getGameDeploymentRootPaths';
/** @deprecated Use {@link getStandardCleanPaths} instead of {@link STANDARD_CLEAN_PATHS} */
export const STANDARD_CLEAN_PATHS = [
'LOCALAPPDATA/Packages/Microsoft.MinecraftUWP_8wekyb3d8bbwe/LocalState/games/com.mojang/development_behavior_packs/PROJECT_NAME',
'LOCALAPPDATA/Packages/Microsoft.MinecraftUWP_8wekyb3d8bbwe/LocalState/games/com.mojang/development_resource_packs/PROJECT_NAME',
'LOCALAPPDATA/Packages/Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe/LocalState/games/com.mojang/development_behavior_packs/PROJECT_NAME',
'LOCALAPPDATA/Packages/Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe/LocalState/games/com.mojang/development_resource_packs/PROJECT_NAME',
];
export function getStandardCleanPaths() {
const projectName = getOrThrowFromProcess('PROJECT_NAME');
const results = Object.values(getGameDeploymentRootPaths())
.filter(rootPath => rootPath !== undefined)
.flatMap(rootPath => [
Path.resolve(rootPath, 'development_behavior_packs', projectName),
Path.resolve(rootPath, 'development_resource_packs', projectName),
]);
if (results.length === 0) {
// Fallback logic
return STANDARD_CLEAN_PATHS;
}
return results;
}
/**
* Cleans the specified outputs. Outputs could be either folders or files. Has support for the following variable replacements
*
* APPDATA, LOCALAPPDATA, PROJECT_NAME
*
* This constant is replaced at task execution with a value provided by the process environment.
*
*/
export function cleanCollateralTask(pathsToClean: string[]) {
return () => {
const projectName = getOrThrowFromProcess('PROJECT_NAME');
// The following variables are not used on all platforms. In those cases, set up an error token so that if the
// config requires these on the platform, we error out immediately.
const errorToken = '$ERROR_TOKEN$';
let appData = process.env.APPDATA;
if (!appData) {
console.warn('Proceeding without APPDATA on this platform. File copy will fail if APPDATA is required.');
appData = errorToken;
}
let localAppData = process.env.LOCALAPPDATA;
if (!localAppData) {
console.warn(
'Proceeding without LOCALAPPDATA on this platform. File copy will fail if LOCALAPPDATA is required.'
);
localAppData = errorToken;
}
// For each output path, replace tokens with env values
for (const cleanPathRaw of pathsToClean) {
const cleanPath = cleanPathRaw
.replace('LOCALAPPDATA', localAppData)
.replace('APPDATA', appData)
.replace('PROJECT_NAME', projectName);
if (cleanPath.includes(errorToken)) {
console.warn(
`Skipping clean of ${cleanPath} on current platform due to APPDATA or LOCALAPPDATA being missing.`
);
continue;
}
try {
const stats = fs.statSync(cleanPath);
console.log(`Cleaning ${stats.isDirectory() ? 'directory' : 'file'} ${Path.resolve(cleanPath)}.`);
rimraf.sync(cleanPath);
} catch (_: unknown) {
// File or directory did not exist, so we no-op
}
}
};
}