1 | import BroccoliPlugin = require("broccoli-plugin");
|
2 | import path = require("path");
|
3 | import * as fs from "fs-extra";
|
4 | import tmp = require("tmp");
|
5 |
|
6 | type EnsureSymlinkSync = (srcFile: string, destLink: string) => void;
|
7 |
|
8 | const ensureSymlink: EnsureSymlinkSync = require("ensure-symlink");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | interface FileMap {
|
17 | [relativePath: string]: string;
|
18 | }
|
19 |
|
20 | type BroccoliSymbolicLinkerOptions =
|
21 | Pick<BroccoliPlugin.BroccoliPluginOptions, "annotation" | "persistentOutput">;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export class BroccoliSymbolicLinker extends BroccoliPlugin {
|
29 | files: FileMap;
|
30 | fakeOutputPath: string | undefined;
|
31 | constructor(fileMap?: FileMap | undefined, options: BroccoliSymbolicLinkerOptions = {}) {
|
32 | let pluginOpts: BroccoliPlugin.BroccoliPluginOptions = {needsCache: false};
|
33 | Object.assign(pluginOpts, options);
|
34 | super([], pluginOpts);
|
35 | this.files = Object.assign({}, fileMap);
|
36 | }
|
37 | reset(fileMap?: FileMap | undefined): void {
|
38 | this.files = Object.assign({}, fileMap);
|
39 | }
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | ln_s(src: string, dest: string): string {
|
54 |
|
55 | this.files[dest] = src;
|
56 | let tartgetDir = this.outputPath;
|
57 | if (!tartgetDir) {
|
58 | this.fakeOutputPath = this.fakeOutputPath || tmp.dirSync().name;
|
59 | tartgetDir = this.fakeOutputPath;
|
60 | }
|
61 | return path.join(tartgetDir, dest);
|
62 | }
|
63 | |
64 |
|
65 |
|
66 | numberOfFiles(): number {
|
67 | return Object.keys(this.files).length;
|
68 | }
|
69 | |
70 |
|
71 |
|
72 | build(): void {
|
73 |
|
74 |
|
75 | for (let dest of Object.keys(this.files)) {
|
76 | let src = this.files[dest];
|
77 |
|
78 | dest = path.join(this.outputPath, dest)
|
79 | let dir = path.dirname(dest);
|
80 | fs.mkdirpSync(dir);
|
81 | ensureSymlink(src, dest)
|
82 | }
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | debug(): string {
|
88 | return Object.keys(this.files).join("\n");
|
89 | }
|
90 | }
|