|
1 | 1 | const fs = require("fs");
|
2 | 2 | const path = require("path");
|
3 | 3 |
|
| 4 | +const isWin = process.platform === "win32"; |
| 5 | +const LINK_TYPE = isWin ? "junction" : "dir"; |
| 6 | + |
| 7 | +function ensureDir(p) { |
| 8 | + fs.mkdirSync(p, { recursive: true }); |
| 9 | +} |
| 10 | + |
4 | 11 | function ensureSymlink(sourcePath, targetPath) {
|
5 | 12 | const source = path.resolve(sourcePath);
|
6 | 13 | const target = path.resolve(targetPath);
|
7 | 14 |
|
| 15 | + ensureDir(path.dirname(target)); |
| 16 | + |
| 17 | + if (!fs.existsSync(source)) { |
| 18 | + console.warn(`Skipping symlink; source missing: ${source}`); |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
8 | 22 | try {
|
9 | 23 | const stat = fs.lstatSync(target);
|
10 |
| - if (!stat.isSymbolicLink()) { |
| 24 | + if (stat.isSymbolicLink()) { |
| 25 | + const current = fs.readlinkSync(target); |
| 26 | + if (path.resolve(current) === source) { |
| 27 | + console.log(`Symlink exists at: ${target}`); |
| 28 | + return true; |
| 29 | + } |
| 30 | + fs.unlinkSync(target); |
| 31 | + } else { |
11 | 32 | console.log(`Removing non-symlink at: ${target}`);
|
12 | 33 | fs.rmSync(target, { recursive: true, force: true });
|
13 |
| - } else { |
14 |
| - const exists = fs.readlinkSync(target); |
15 |
| - if (path.resolve(exists) === source) { |
16 |
| - console.log(`Symlink exists at: ${target}`); |
17 |
| - return; |
18 |
| - } |
19 |
| - console.log(`Replacing symlink at: ${target}`); |
20 |
| - fs.unlinkSync(target); |
21 | 34 | }
|
22 | 35 | } catch (e) {
|
23 |
| - if (e.code !== "ENOENT") { |
24 |
| - console.error(`Error checking symlink: ${e.message}`); |
25 |
| - } |
| 36 | + if (e.code !== "ENOENT") console.error(`Error checking symlink: ${e.message}`); |
26 | 37 | }
|
27 | 38 |
|
28 | 39 | try {
|
29 |
| - fs.symlinkSync( |
30 |
| - path.resolve(source), |
31 |
| - path.resolve(target), |
32 |
| - "junction" |
33 |
| - ); |
34 |
| - console.log(`Symlink created: ${sourcePath} -> ${targetPath}`); |
| 40 | + fs.symlinkSync(source, target, LINK_TYPE); |
| 41 | + console.log(`Symlink created: ${target} -> ${source}`); |
| 42 | + return true; |
35 | 43 | } catch (e) {
|
36 |
| - if (e.code !== "EEXIST") { |
37 |
| - console.error(`Error creating symlink: ${e.message}`); |
38 |
| - } |
| 44 | + console.error(`Error creating symlink: ${e.message}`); |
| 45 | + return false; |
39 | 46 | }
|
40 | 47 | }
|
41 | 48 |
|
42 |
| -// Symlink other static assets no longer in bower_components |
43 |
| -ensureSymlink( |
44 |
| - "node_modules/marked", |
45 |
| - "nbclassic/static/components/marked" |
46 |
| -); |
47 |
| -ensureSymlink( |
48 |
| - "node_modules/font-awesome", |
49 |
| - "nbclassic/static/components/font-awesome" |
50 |
| -); |
51 |
| -ensureSymlink( |
52 |
| - "node_modules/backbone", |
53 |
| - "nbclassic/static/components/backbone" |
54 |
| -); |
55 |
| -ensureSymlink( |
56 |
| - "node_modules/bootstrap", |
57 |
| - "nbclassic/static/components/bootstrap" |
58 |
| -); |
59 |
| -ensureSymlink( |
60 |
| - "node_modules/bootstrap-tour", |
61 |
| - "nbclassic/static/components/bootstrap-tour" |
62 |
| -); |
63 |
| -ensureSymlink( |
64 |
| - "node_modules/jed", |
65 |
| - "nbclassic/static/components/jed" |
66 |
| -); |
67 |
| -ensureSymlink( |
68 |
| - "node_modules/moment", |
69 |
| - "nbclassic/static/components/moment" |
70 |
| -); |
71 |
| -ensureSymlink( |
72 |
| - "node_modules/text-encoding", |
73 |
| - "nbclassic/static/components/text-encoding" |
74 |
| -); |
75 |
| -ensureSymlink( |
76 |
| - "node_modules/underscore", |
77 |
| - "nbclassic/static/components/underscore" |
78 |
| -); |
79 |
| -ensureSymlink( |
80 |
| - "node_modules/jquery", |
81 |
| - "nbclassic/static/components/jquery" |
82 |
| -); |
83 |
| -ensureSymlink( |
84 |
| - "node_modules/jquery-ui", |
85 |
| - "nbclassic/static/components/jquery-ui" |
86 |
| -); |
87 |
| -ensureSymlink( |
88 |
| - "node_modules/jquery-typeahead", |
89 |
| - "nbclassic/static/components/jquery-typeahead" |
90 |
| -); |
91 |
| -ensureSymlink( |
92 |
| - "node_modules/mathjax", |
93 |
| - "nbclassic/static/components/MathJax" |
94 |
| -); |
95 |
| -ensureSymlink( |
96 |
| - "node_modules/codemirror", |
97 |
| - "nbclassic/static/components/codemirror" |
98 |
| -); |
99 |
| -ensureSymlink( |
100 |
| - "node_modules/react", |
101 |
| - "nbclassic/static/components/react" |
102 |
| -); |
103 |
| -ensureSymlink( |
104 |
| - "node_modules/react-dom", |
105 |
| - "nbclassic/static/components/react-dom" |
106 |
| -); |
107 |
| -ensureSymlink( |
108 |
| - "node_modules/es6-promise", |
109 |
| - "nbclassic/static/components/es6-promise" |
110 |
| -); |
111 |
| -ensureSymlink( |
112 |
| - "node_modules/requirejs", |
113 |
| - "nbclassic/static/components/requirejs" |
114 |
| -); |
115 |
| -ensureSymlink( |
116 |
| - "node_modules/requirejs-plugins", |
117 |
| - "nbclassic/static/components/requirejs-plugins" |
118 |
| -); |
119 |
| -ensureSymlink( |
120 |
| - "node_modules/requirejs-text", |
121 |
| - "nbclassic/static/components/requirejs-text" |
122 |
| -); |
123 |
| -ensureSymlink( |
124 |
| - "node_modules/google-caja-sanitizer", |
125 |
| - "nbclassic/static/components/google-caja-sanitizer" |
126 |
| -); |
| 49 | +ensureDir("nbclassic/static/components"); |
| 50 | + |
| 51 | +[ |
| 52 | + "marked", |
| 53 | + "font-awesome", |
| 54 | + "backbone", |
| 55 | + "bootstrap", |
| 56 | + "bootstrap-tour", |
| 57 | + "jed", |
| 58 | + "moment", |
| 59 | + "text-encoding", |
| 60 | + "underscore", |
| 61 | + "jquery", |
| 62 | + "jquery-ui", |
| 63 | + "jquery-typeahead", |
| 64 | + "codemirror", |
| 65 | + "react", |
| 66 | + "react-dom", |
| 67 | + "es6-promise", |
| 68 | + "requirejs", |
| 69 | + "requirejs-plugins", |
| 70 | + "requirejs-text", |
| 71 | + "google-caja-sanitizer", |
| 72 | + "mathjax", |
| 73 | +].forEach((pkg) => { |
| 74 | + const dst = pkg === "mathjax" ? "MathJax" : pkg; |
| 75 | + ensureSymlink(`node_modules/${pkg}`, `nbclassic/static/components/${dst}`); |
| 76 | +}); |
0 commit comments