This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathsource-maps.ts
50 lines (40 loc) · 1.81 KB
/
source-maps.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
import { join, relative, basename } from 'path';
import { ensureDir, mkdirpSync } from 'fs-extra';
import * as Constants from './constants';
import { copyFileAsync, getBooleanPropertyValue, mkDirpAsync, readDirAsync, unlinkAsync } from './helpers';
import { BuildContext } from './interfaces';
export async function copySourcemaps(context: BuildContext, shouldPurge: boolean) {
const copyBeforePurge = getBooleanPropertyValue(Constants.ENV_VAR_MOVE_SOURCE_MAPS);
if (copyBeforePurge) {
await mkDirpAsync(context.sourcemapDir);
}
const fileNames = await readDirAsync(context.buildDir);
// only include js source maps
const sourceMaps = fileNames.filter(fileName => fileName.endsWith('.map'));
const toCopy = sourceMaps.filter(fileName => fileName.endsWith('.js.map'));
const toCopyFullPaths = toCopy.map(fileName => join(context.buildDir, fileName));
const toPurge = sourceMaps.map(sourceMap => join(context.buildDir, sourceMap));
const copyFilePromises: Promise<any>[] = [];
if (copyBeforePurge) {
for (const fullPath of toCopyFullPaths) {
const fileName = basename(fullPath);
copyFilePromises.push(copyFileAsync(fullPath, join(context.sourcemapDir, fileName)));
}
}
await Promise.all(copyFilePromises);
// okay cool, all of the files have been copied over, so go ahead and blow them all away
const purgeFilePromises: Promise<any>[] = [];
if (shouldPurge) {
for (const fullPath of toPurge) {
purgeFilePromises.push(unlinkAsync(fullPath));
}
}
return await Promise.all(purgeFilePromises);
}
export function purgeSourceMapsIfNeeded(context: BuildContext): Promise<any> {
if (getBooleanPropertyValue(Constants.ENV_VAR_GENERATE_SOURCE_MAP)) {
// keep the source maps and just return
return copySourcemaps(context, false);
}
return copySourcemaps(context, true);
}