fix(cli): prefer sync fs functions to avoid ticks (#2401)

This commit is contained in:
Tommy Nguyen 2023-05-09 12:23:37 +02:00 коммит произвёл GitHub
Родитель e38580b417
Коммит 3db064452f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 23 добавлений и 43 удалений

Просмотреть файл

@ -0,0 +1,5 @@
---
"@rnx-kit/cli": patch
---
Refactor `copy-assets` command to rely less on async fs functions

Просмотреть файл

@ -187,7 +187,7 @@ export async function assembleAarBundle(
}
const findUp = require("find-up");
const gradlew = await findUp(
const gradlew = findUp.sync(
os.platform() === "win32" ? "gradlew.bat" : "gradlew"
);
if (!gradlew) {
@ -214,7 +214,7 @@ export async function assembleAarBundle(
};
const outputDir = path.join(context.options.assetsDest, "aar");
await fs.ensureDir(outputDir);
fs.ensureDirSync(outputDir);
const dest = path.join(outputDir, `${targetName}-${version}.aar`);
@ -314,13 +314,13 @@ export async function assembleAarBundle(
"",
].join("\n");
await fs.ensureDir(buildDir);
await fs.writeFile(path.join(buildDir, "build.gradle"), buildGradle);
await fs.writeFile(
fs.ensureDirSync(buildDir);
fs.writeFileSync(path.join(buildDir, "build.gradle"), buildGradle);
fs.writeFileSync(
path.join(buildDir, "gradle.properties"),
gradleProperties
);
await fs.writeFile(path.join(buildDir, "settings.gradle"), settingsGradle);
fs.writeFileSync(path.join(buildDir, "settings.gradle"), settingsGradle);
// Run only one Gradle task at a time
run(gradlew, targets, { cwd: buildDir, stdio: "inherit", env });
@ -329,35 +329,15 @@ export async function assembleAarBundle(
await Promise.all(targetsToCopy.map(([src, dest]) => fs.copy(src, dest)));
}
async function copyFiles(files: unknown, destination: string): Promise<void> {
function copyFiles(files: unknown, destination: string): Promise<void>[] {
if (!Array.isArray(files) || files.length === 0) {
return;
return [];
}
await fs.ensureDir(destination);
await Promise.all(
files.map((file) => {
const basename = path.basename(file);
return fs.copy(file, `${destination}/${basename}`);
})
);
}
async function copyXcodeAssets(
xcassets: unknown,
destination: string
): Promise<void> {
if (!Array.isArray(xcassets) || xcassets.length === 0) {
return;
}
await fs.ensureDir(destination);
await Promise.all(
xcassets.map((catalog) => {
const dest = `${destination}/${path.basename(catalog)}`;
return fs.copy(catalog, dest);
})
);
fs.ensureDirSync(destination);
return files.map((file) => {
return fs.copy(file, `${destination}/${path.basename(file)}`);
});
}
export async function copyAssets(
@ -366,12 +346,12 @@ export async function copyAssets(
{ assets, strings, xcassets }: NativeAssets
): Promise<void> {
const tasks = [
copyFiles(assets, `${assetsDest}/assets/${packageName}`),
copyFiles(strings, `${assetsDest}/strings/${packageName}`),
...copyFiles(assets, `${assetsDest}/assets/${packageName}`),
...copyFiles(strings, `${assetsDest}/strings/${packageName}`),
];
if (typeof xcassetsDest === "string") {
tasks.push(copyXcodeAssets(xcassets, xcassetsDest));
tasks.push(...copyFiles(xcassets, xcassetsDest));
}
await Promise.all(tasks);
@ -477,10 +457,7 @@ export async function gatherConfigs({
*/
export async function copyProjectAssets(options: Options): Promise<void> {
const projectRoot = findPackageDir() || process.cwd();
const content = await fs.readFile(`${projectRoot}/package.json`, {
encoding: "utf-8",
});
const manifest: PackageManifest = JSON.parse(content);
const manifest = readPackage(projectRoot);
const context = { projectRoot, manifest, options };
const assetConfigs = await gatherConfigs(context);
if (!assetConfigs) {

Просмотреть файл

@ -13,10 +13,8 @@ fs.__setMockFiles = (files) => {
fs.__toJSON = () => vol.toJSON();
fs.copy = (...args) => vol.promises.copyFile(...args);
fs.ensureDir = (dir) => vol.promises.mkdir(dir, { recursive: true });
fs.ensureDirSync = (dir) => vol.mkdirSync(dir, { recursive: true });
fs.existsSync = (...args) => vol.existsSync(...args);
fs.pathExists = (...args) => Promise.resolve(vol.existsSync(...args));
fs.readFile = (...args) => vol.promises.readFile(...args);
fs.writeFile = (...args) => vol.promises.writeFile(...args);
fs.writeFileSync = (...args) => vol.writeFileSync(...args);
module.exports = fs;