UNPKG

2.96 kBPlain TextView Raw
1import BroccoliPlugin = require("broccoli-plugin");
2import path = require("path");
3import * as fs from "fs-extra";
4import tmp = require("tmp");
5
6type EnsureSymlinkSync = (srcFile: string, destLink: string) => void;
7/* eslint-disable-next-line @typescript-eslint/no-var-requires */
8const ensureSymlink: EnsureSymlinkSync = require("ensure-symlink");
9
10/**
11 * An object where the keys are the files that will be created in the tree and
12 * the values are the source files.
13 *
14 * @interface FileMap
15 */
16interface FileMap {
17 [relativePath: string]: string;
18}
19
20type BroccoliSymbolicLinkerOptions =
21 Pick<BroccoliPlugin.BroccoliPluginOptions, "annotation" | "persistentOutput">;
22
23/**
24 * Creates symlinks to the source files specified.
25 *
26 * BroccoliSymbolicLinker
27 */
28export 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 * Record that a symlink should be created from src to dest.
42 *
43 * This can be called many times before the build method is invoked.
44 * Calling it after will not have an effect until the next time build() is
45 * invoked.
46 *
47 * @param src The file that should be symlinked into the tree.
48 * @param dest the relative path from the tree's root to the location of the
49 * symlink. the filename does not have to be the same.
50 * @returns the absolute path to the location where the symlink will be created.
51 */
52 // eslint-disable-next-line @typescript-eslint/camelcase
53 ln_s(src: string, dest: string): string {
54 this.files[dest] = src;
55 let tartgetDir = this.outputPath;
56 if (!tartgetDir) {
57 this.fakeOutputPath = this.fakeOutputPath || tmp.dirSync().name;
58 tartgetDir = this.fakeOutputPath;
59 }
60 return path.join(tartgetDir, dest);
61 }
62 /**
63 * Returns the number of symlinks that will be created.
64 */
65 numberOfFiles(): number {
66 return Object.keys(this.files).length;
67 }
68 /**
69 * Create the symlinks. Directories to them will be created as necessary.
70 */
71 build(): void {
72 // eslint-disable-next-line no-console
73 // console.log(`Building ${this.numberOfFiles()} symlinks for ${this["_annotation"]}.`);
74 for (let dest of Object.keys(this.files)) {
75 let src = this.files[dest];
76 // console.log(`linking ${src} to ${dest}`);
77 dest = path.join(this.outputPath, dest)
78 let dir = path.dirname(dest);
79 fs.mkdirpSync(dir);
80 ensureSymlink(src, dest)
81 }
82 }
83 /**
84 * Output the symlinks that will be created for debugging.
85 */
86 debug(): string {
87 return Object.keys(this.files).join("\n");
88 }
89}