-
Notifications
You must be signed in to change notification settings - Fork 12k
/
assets.ts
65 lines (55 loc) · 1.93 KB
/
assets.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
import { join } from 'path';
import { chmod } from 'fs/promises';
import glob from 'glob';
import { getGlobalVariable } from './env';
import { relative, resolve } from 'path';
import { copyFile, writeFile } from './fs';
import { installWorkspacePackages } from './packages';
import { useBuiltPackages } from './project';
export function assetDir(assetName: string) {
return join(__dirname, '../assets', assetName);
}
export function copyProjectAsset(assetName: string, to?: string) {
const tempRoot = join(getGlobalVariable('projects-root'), 'test-project');
const sourcePath = assetDir(assetName);
const targetPath = join(tempRoot, to || assetName);
return copyFile(sourcePath, targetPath);
}
export function copyAssets(assetName: string, to?: string) {
const seed = +Date.now();
const tempRoot = join(getGlobalVariable('projects-root'), 'assets', assetName + '-' + seed);
const root = assetDir(assetName);
return Promise.resolve()
.then(() => {
const allFiles = glob.sync('**/*', { dot: true, nodir: true, cwd: root });
return allFiles.reduce((promise, filePath) => {
const toPath =
to !== undefined
? resolve(getGlobalVariable('projects-root'), 'test-project', to, filePath)
: join(tempRoot, filePath);
return promise
.then(() => copyFile(join(root, filePath), toPath))
.then(() => chmod(toPath, 0o777));
}, Promise.resolve());
})
.then(() => tempRoot);
}
export async function createProjectFromAsset(
assetName: string,
useNpmPackages = false,
skipInstall = false,
) {
const dir = await copyAssets(assetName);
process.chdir(dir);
if (!useNpmPackages) {
await useBuiltPackages();
if (!getGlobalVariable('ci')) {
const testRegistry = getGlobalVariable('package-registry');
await writeFile('.npmrc', `registry=${testRegistry}`);
}
}
if (!skipInstall) {
await installWorkspacePackages();
}
return dir;
}