Skip to content

Commit fdd921c

Browse files
committed
refactor postinstall.js symlink flow
1 parent 74576aa commit fdd921c

File tree

1 file changed

+50
-97
lines changed

1 file changed

+50
-97
lines changed

postinstall.js

Lines changed: 50 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
const fs = require("fs");
22
const path = require("path");
33

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+
411
function ensureSymlink(sourcePath, targetPath) {
512
const source = path.resolve(sourcePath);
613
const target = path.resolve(targetPath);
714

15+
ensureDir(path.dirname(target));
16+
17+
if (!fs.existsSync(source)) {
18+
console.warn(`Skipping symlink; source missing: ${source}`);
19+
return false;
20+
}
21+
822
try {
923
const stat = fs.lstatSync(target);
1024
if (!stat.isSymbolicLink()) {
1125
console.log(`Removing non-symlink at: ${target}`);
1226
fs.rmSync(target, { recursive: true, force: true });
1327
} 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);
28+
const exists = fs.readlinkSync(target);
29+
if (path.resolve(exists) === source) {
30+
console.log(`Symlink exists at: ${target}`);
31+
return;
32+
}
33+
console.log(`Replacing symlink at: ${target}`);
34+
fs.unlinkSync(target);
2135
}
2236
} catch (e) {
2337
if (e.code !== "ENOENT") {
@@ -26,11 +40,7 @@ function ensureSymlink(sourcePath, targetPath) {
2640
}
2741

2842
try {
29-
fs.symlinkSync(
30-
path.resolve(source),
31-
path.resolve(target),
32-
"junction"
33-
);
43+
fs.symlinkSync(source, target, LINK_TYPE);
3444
console.log(`Symlink created: ${sourcePath} -> ${targetPath}`);
3545
} catch (e) {
3646
if (e.code !== "EEXIST") {
@@ -39,88 +49,31 @@ function ensureSymlink(sourcePath, targetPath) {
3949
}
4050
}
4151

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-
);
52+
ensureDir("nbclassic/static/components");
53+
54+
[
55+
"marked",
56+
"font-awesome",
57+
"backbone",
58+
"bootstrap",
59+
"bootstrap-tour",
60+
"jed",
61+
"moment",
62+
"text-encoding",
63+
"underscore",
64+
"jquery",
65+
"jquery-ui",
66+
"jquery-typeahead",
67+
"codemirror",
68+
"react",
69+
"react-dom",
70+
"es6-promise",
71+
"requirejs",
72+
"requirejs-plugins",
73+
"requirejs-text",
74+
"google-caja-sanitizer",
75+
"mathjax",
76+
].forEach((pkg) => {
77+
const dst = pkg === "mathjax" ? "MathJax" : pkg;
78+
ensureSymlink(`node_modules/${pkg}`, `nbclassic/static/components/${dst}`);
79+
});

0 commit comments

Comments
 (0)