forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.ts
57 lines (47 loc) · 1.62 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
import { join } from 'path';
import * as glob from 'glob';
import { getGlobalVariable } from './env';
import { relative } from 'path';
import { copyFile, writeFile } from './fs';
import { useBuiltPackages } from './project';
import { silentNpm } from './process';
export function assetDir(assetName: string) {
return join(__dirname, '../assets', assetName);
}
export function copyProjectAsset(assetName: string, to?: string) {
const tempRoot = join(getGlobalVariable('tmp-root'), 'test-project');
const sourcePath = assetDir(assetName);
const targetPath = join(tempRoot, to || assetName);
return copyFile(sourcePath, targetPath);
}
export function copyAssets(assetName: string) {
const seed = +Date.now();
const tempRoot = join(getGlobalVariable('tmp-root'), 'assets', assetName + '-' + seed);
const root = assetDir(assetName);
return Promise.resolve()
.then(() => {
const allFiles = glob.sync(join(root, '**/*'), { dot: true, nodir: true });
return allFiles.reduce((promise, filePath) => {
const relPath = relative(root, filePath);
const toPath = join(tempRoot, relPath);
return promise.then(() => copyFile(filePath, toPath));
}, 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();
await writeFile('.npmrc', 'registry = http://localhost:4873', 'utf8');
}
if (!skipInstall) {
await silentNpm('install');
}
return dir;
}