UNPKG

2.97 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10const BroccoliPlugin = require("broccoli-plugin");
11const path = require("path");
12const fs = __importStar(require("fs-extra"));
13const tmp = require("tmp");
14/* eslint-disable-next-line @typescript-eslint/no-var-requires */
15const ensureSymlink = require("ensure-symlink");
16/**
17 * Creates symlinks to the source files specified.
18 *
19 * BroccoliSymbolicLinker
20 */
21class BroccoliSymbolicLinker extends BroccoliPlugin {
22 constructor(fileMap, options = {}) {
23 let pluginOpts = { needsCache: false };
24 Object.assign(pluginOpts, options);
25 super([], pluginOpts);
26 this.files = Object.assign({}, fileMap);
27 }
28 reset(fileMap) {
29 this.files = Object.assign({}, fileMap);
30 }
31 /**
32 * Record that a symlink should be created from src to dest.
33 *
34 * This can be called many times before the build method is invoked.
35 * Calling it after will not have an effect until the next time build() is
36 * invoked.
37 *
38 * @param src The file that should be symlinked into the tree.
39 * @param dest the relative path from the tree's root to the location of the
40 * symlink. the filename does not have to be the same.
41 * @returns the absolute path to the location where the symlink will be created.
42 */
43 // eslint-disable-next-line @typescript-eslint/camelcase
44 ln_s(src, dest) {
45 this.files[dest] = src;
46 let tartgetDir = this.outputPath;
47 if (!tartgetDir) {
48 this.fakeOutputPath = this.fakeOutputPath || tmp.dirSync().name;
49 tartgetDir = this.fakeOutputPath;
50 }
51 return path.join(tartgetDir, dest);
52 }
53 /**
54 * Returns the number of symlinks that will be created.
55 */
56 numberOfFiles() {
57 return Object.keys(this.files).length;
58 }
59 /**
60 * Create the symlinks. Directories to them will be created as necessary.
61 */
62 build() {
63 // eslint-disable-next-line no-console
64 // console.log(`Building ${this.numberOfFiles()} symlinks for ${this["_annotation"]}.`);
65 for (let dest of Object.keys(this.files)) {
66 let src = this.files[dest];
67 // console.log(`linking ${src} to ${dest}`);
68 dest = path.join(this.outputPath, dest);
69 let dir = path.dirname(dest);
70 fs.mkdirpSync(dir);
71 ensureSymlink(src, dest);
72 }
73 }
74 /**
75 * Output the symlinks that will be created for debugging.
76 */
77 debug() {
78 return Object.keys(this.files).join("\n");
79 }
80}
81exports.BroccoliSymbolicLinker = BroccoliSymbolicLinker;
82//# sourceMappingURL=broccoli-ln-s.js.map
\No newline at end of file