Skip to content

Commit 217f914

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 217f914

1 file changed

Lines changed: 12 additions & 24 deletions

File tree

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

Lines changed: 12 additions & 24 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 { randomUUID } from 'node:crypto';
2+
import { join, resolve } from 'node:path';
3+
import { cp } from 'node:fs/promises';
44
import { getGlobalVariable } from './env';
5-
import { resolve } from 'node:path';
65
import { copyFile } from './fs';
76
import { installWorkspacePackages, setRegistry } from './packages';
87
import { useBuiltPackagesVersions } from './project';
98

10-
export function assetDir(assetName: string) {
9+
export function assetDir(assetName: string): string {
1110
return join(__dirname, '../e2e/assets', assetName);
1211
}
1312

14-
export function copyProjectAsset(assetName: string, to?: string) {
13+
export function copyProjectAsset(assetName: string, to?: string): Promise<void> {
1514
const tempRoot = join(getGlobalVariable('projects-root'), 'test-project');
1615
const sourcePath = assetDir(assetName);
1716
const targetPath = join(tempRoot, to || assetName);
1817

1918
return copyFile(sourcePath, targetPath);
2019
}
2120

22-
export function copyAssets(assetName: string, to?: string) {
23-
const seed = +Date.now();
24-
const tempRoot = join(getGlobalVariable('projects-root'), 'assets', assetName + '-' + seed);
21+
export async function copyAssets(assetName: string, to?: string): Promise<string> {
22+
const projectRoot = getGlobalVariable('projects-root');
23+
const tempRoot = join(projectRoot, 'assets', `${assetName}-${randomUUID()}`);
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)