Skip to content

Commit 62d082d

Browse files
committed
refactor: asset parser async fs
1 parent 3cfc129 commit 62d082d

File tree

1 file changed

+20
-16
lines changed
  • packages/scripts/src/commands/app-data/postProcess

1 file changed

+20
-16
lines changed

packages/scripts/src/commands/app-data/postProcess/assets.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
2-
import * as fs from "fs-extra";
2+
import fse from "fs-extra";
3+
import fs from "fs/promises";
34
import chalk from "chalk";
45
import { Command } from "commander";
56
import {
@@ -8,7 +9,7 @@ import {
89
IContentsEntry,
910
logOutput,
1011
logWarning,
11-
createTempDir,
12+
createTempDirAsync,
1213
IContentsEntryHashmap,
1314
kbToMB,
1415
setNestedProperty,
@@ -56,7 +57,7 @@ export default program
5657
const mappedOptions: IAssetPostProcessorOptions = {
5758
sourceAssetsFolders: options.sourceAssetsFolders.split(",").map((s) => s.trim()),
5859
};
59-
new AssetsPostProcessor(mappedOptions).run();
60+
await new AssetsPostProcessor(mappedOptions).run();
6061
});
6162

6263
/***************************************************************************************
@@ -67,22 +68,22 @@ export class AssetsPostProcessor {
6768
private stagingDir: string;
6869
constructor(private options: IAssetPostProcessorOptions) {}
6970

70-
public run() {
71+
public async run() {
7172
const { app_data } = this.activeDeployment;
7273
const { sourceAssetsFolders } = this.options;
7374
const { _parent_config } = this.activeDeployment;
7475
const appAssetsFolder = path.resolve(app_data.output_path, "assets");
75-
fs.ensureDirSync(appAssetsFolder);
76+
await fs.mkdir(appAssetsFolder, { recursive: true });
7677
// Populate merged assets staging to run quality control checks and generate full contents lists
77-
this.stagingDir = createTempDir();
78+
this.stagingDir = await createTempDirAsync();
7879
const mergedAssetsHashmap: IContentsEntryHashmap = {};
7980

8081
// Include parent config in list of source assets
8182
// TODO - may want to reconsider this functionality in the future given ability to use
8283
// multiple input sources instead
8384
if (_parent_config) {
8485
const parentAssetsFolder = path.resolve(_parent_config._workspace_path, "app_data", "assets");
85-
fs.ensureDirSync(parentAssetsFolder);
86+
await fs.mkdir(parentAssetsFolder);
8687
sourceAssetsFolders.unshift(parentAssetsFolder);
8788
}
8889

@@ -111,9 +112,9 @@ export class AssetsPostProcessor {
111112

112113
// copy deployment assets to main folder and write merged contents file
113114
replicateDir(this.stagingDir, appAssetsFolder);
114-
fs.removeSync(this.stagingDir);
115+
await fs.rm(this.stagingDir, { recursive: true, force: true });
115116

116-
this.writeAssetsContentsFiles(appAssetsFolder, tracked, untracked);
117+
await this.writeAssetsContentsFiles(appAssetsFolder, tracked, untracked);
117118
console.log(chalk.green("Asset Process Complete"));
118119
}
119120

@@ -123,34 +124,37 @@ export class AssetsPostProcessor {
123124
* `untracked-assets.json` provides a summary of all assets that appear in translation or theme folders
124125
* but do not have corresponding default global entries (only populated if entries exist)
125126
*/
126-
private writeAssetsContentsFiles(
127+
private async writeAssetsContentsFiles(
127128
appAssetsFolder: string,
128129
assetEntries: IAssetEntryHashmap,
129130
missingEntries: IAssetEntryHashmap
130131
) {
131-
if (fs.existsSync(appAssetsFolder)) {
132+
if (await fse.pathExists(appAssetsFolder)) {
132133
const contentsTarget = path.resolve(appAssetsFolder, "contents.json");
133-
fs.writeFileSync(contentsTarget, JSON.stringify(sortJsonKeys(assetEntries), null, 2));
134+
await fs.writeFile(contentsTarget, JSON.stringify(sortJsonKeys(assetEntries), null, 2));
134135
const missingTarget = path.resolve(appAssetsFolder, "untracked-assets.json");
135-
if (fs.existsSync(missingTarget)) fs.removeSync(missingTarget);
136+
if (await fse.pathExists(missingTarget)) {
137+
await fs.rm(missingTarget, { recursive: true, force: true });
138+
}
139+
136140
if (Object.keys(missingEntries).length > 0) {
137141
logWarning({
138142
msg1: "Assets override found without corresponding entry",
139143
msg2: Object.keys(missingEntries).join("\n"),
140144
});
141-
fs.writeFileSync(missingTarget, JSON.stringify(sortJsonKeys(missingEntries), null, 2));
145+
await fs.writeFile(missingTarget, JSON.stringify(sortJsonKeys(missingEntries), null, 2));
142146
}
143147
}
144148
}
145149

146-
private mergeParentAssets(sourceAssets: { [relativePath: string]: IContentsEntry }) {
150+
private async mergeParentAssets(sourceAssets: { [relativePath: string]: IContentsEntry }) {
147151
const { _parent_config } = this.activeDeployment;
148152
const mergedAssets = { ...sourceAssets };
149153

150154
// If parent config exists also include any parent files that would not be overwritten by source
151155
if (_parent_config) {
152156
const parentAssetsFolder = path.resolve(_parent_config._workspace_path, "app_data", "assets");
153-
fs.ensureDirSync(parentAssetsFolder);
157+
await fs.mkdir(parentAssetsFolder);
154158
const parentAssets = generateFolderFlatMap(parentAssetsFolder, { includeLocalPath: true });
155159
const filteredParentAssets = this.filterAppAssets(parentAssets);
156160
Object.keys(filteredParentAssets).forEach((relativePath) => {

0 commit comments

Comments
 (0)