-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
generator.ts
65 lines (59 loc) · 2.01 KB
/
generator.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 { baseUrl } from "./common/url.js";
import { toPackageTarget } from "./install/package.js";
import TraceMap from './tracemap/tracemap.js';
export interface GeneratorOptions {
mapUrl?: URL | string;
defaultProvider?: string,
env?: string[]
}
export interface Install {
target: string;
subpath?: '.' | `./${string}`;
alias?: string;
}
export class Generator {
private traceMap: TraceMap;
private mapUrl: URL;
constructor ({
mapUrl = baseUrl,
env = ['browser', 'development'],
defaultProvider = 'jspm'
}: GeneratorOptions = {}) {
this.mapUrl = typeof mapUrl === 'string' ? new URL(mapUrl) : mapUrl;
if (!this.mapUrl.pathname.endsWith('/'))
this.mapUrl = new URL('./', this.mapUrl);
this.traceMap = new TraceMap(this.mapUrl, {
stdlib: '@jspm/core@2',
env,
defaultProvider
});
}
async install (target: string): Promise<void>;
async install (install: string | Install): Promise<void> {
if (arguments.length !== 1)
throw new Error('Install takes a single target string or object.');
if (typeof install === 'string')
install = { target: install };
if (typeof install.target !== 'string')
throw new Error('Install requires a "target".');
if (install.subpath !== undefined && (typeof install.subpath !== 'string' || (install.subpath !== '.' && !install.subpath.startsWith('./'))))
throw new Error('Install subpath must be equal to "." or start with "./".');
const finishInstall = await this.traceMap.startInstall();
try {
const { alias, target, subpath } = await toPackageTarget(install.target, this.mapUrl.href);
await this.traceMap.add(install.alias || alias, target);
const module = (install.alias || alias) + (install.subpath || subpath).slice(1);
await this.traceMap.trace(module, this.mapUrl);
}
finally {
await finishInstall(true);
}
}
getMap () {
const map = this.traceMap.map.clone();
map.flatten();
map.rebase();
map.sort();
return map.toJSON();
}
}