Skip to content

Commit 87864d6

Browse files
committed
refactor: clean up copying of assets
This code can now be cleaned up by using Node.js APIs
1 parent 0e26e9c commit 87864d6

1 file changed

Lines changed: 11 additions & 23 deletions

File tree

tests/legacy-cli/e2e/utils/assets.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
1-
import { join } from 'node:path';
2-
import { chmod } from 'node:fs/promises';
3-
import glob from 'fast-glob';
1+
import { join, resolve } from 'node:path';
2+
import { cp } from 'node:fs/promises';
43
import { getGlobalVariable } from './env';
5-
import { resolve } from 'node:path';
64
import { copyFile } from './fs';
75
import { installWorkspacePackages, setRegistry } from './packages';
86
import { useBuiltPackagesVersions } from './project';
97

10-
export function assetDir(assetName: string) {
8+
export function assetDir(assetName: string): string {
119
return join(__dirname, '../e2e/assets', assetName);
1210
}
1311

14-
export function copyProjectAsset(assetName: string, to?: string) {
12+
export function copyProjectAsset(assetName: string, to?: string): Promise<void> {
1513
const tempRoot = join(getGlobalVariable('projects-root'), 'test-project');
1614
const sourcePath = assetDir(assetName);
1715
const targetPath = join(tempRoot, to || assetName);
1816

1917
return copyFile(sourcePath, targetPath);
2018
}
2119

22-
export function copyAssets(assetName: string, to?: string) {
20+
export async function copyAssets(assetName: string, to?: string): Promise<string> {
2321
const seed = +Date.now();
24-
const tempRoot = join(getGlobalVariable('projects-root'), 'assets', assetName + '-' + seed);
22+
const projectRoot = getGlobalVariable('projects-root');
23+
const tempRoot = join(projectRoot, 'assets', `${assetName}-${seed}`);
2524
const root = assetDir(assetName);
25+
const destinationPath = to !== undefined ? resolve(projectRoot, 'test-project', to) : tempRoot;
2626

27-
return Promise.resolve()
28-
.then(() => {
29-
const allFiles = glob.sync('**/*', { dot: true, cwd: root });
27+
await cp(root, destinationPath, { recursive: true, mode: 0o777 });
3028

31-
return allFiles.reduce((promise, filePath) => {
32-
const toPath =
33-
to !== undefined
34-
? resolve(getGlobalVariable('projects-root'), 'test-project', to, filePath)
35-
: join(tempRoot, filePath);
36-
37-
return promise
38-
.then(() => copyFile(join(root, filePath), toPath))
39-
.then(() => chmod(toPath, 0o777));
40-
}, Promise.resolve());
41-
})
42-
.then(() => tempRoot);
29+
return destinationPath;
4330
}
4431

4532
/**
@@ -59,6 +46,7 @@ export async function createProjectFromAsset(
5946
if (!useNpmPackages) {
6047
await useBuiltPackagesVersions();
6148
}
49+
6250
if (!skipInstall) {
6351
await installWorkspacePackages();
6452
}

0 commit comments

Comments
 (0)