UNPKG

3.02 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 // console.log(`will link ${src} to ${dest}`);
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 * Returns the number of symlinks that will be created.
65 */
66 numberOfFiles(): number {
67 return Object.keys(this.files).length;
68 }
69 /**
70 * Create the symlinks. Directories to them will be created as necessary.
71 */
72 build(): void {
73 // eslint-disable-next-line no-console
74 // console.log(`Building ${this.numberOfFiles()} symlinks for ${this["_annotation"]}.`);
75 for (let dest of Object.keys(this.files)) {
76 let src = this.files[dest];
77 // console.log(`linking ${src} to ${dest}`);
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 * Output the symlinks that will be created for debugging.
86 */
87 debug(): string {
88 return Object.keys(this.files).join("\n");
89 }
90}