1 | "use strict";
|
2 |
|
3 | const pathUtil = require("path");
|
4 | const fs = require("./utils/fs");
|
5 | const validate = require("./utils/validate");
|
6 | const dir = require("./dir");
|
7 |
|
8 | const validateInput = (methodName, symlinkValue, path) => {
|
9 | const methodSignature = `${methodName}(symlinkValue, path)`;
|
10 | validate.argument(methodSignature, "symlinkValue", symlinkValue, ["string"]);
|
11 | validate.argument(methodSignature, "path", path, ["string"]);
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const symlinkSync = (symlinkValue, path) => {
|
19 | try {
|
20 | fs.symlinkSync(symlinkValue, path);
|
21 | } catch (err) {
|
22 | if (err.code === "ENOENT") {
|
23 |
|
24 | dir.createSync(pathUtil.dirname(path));
|
25 | fs.symlinkSync(symlinkValue, path);
|
26 | } else {
|
27 | throw err;
|
28 | }
|
29 | }
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | const symlinkAsync = (symlinkValue, path) => {
|
37 | return new Promise((resolve, reject) => {
|
38 | fs.symlink(symlinkValue, path)
|
39 | .then(resolve)
|
40 | .catch(err => {
|
41 | if (err.code === "ENOENT") {
|
42 |
|
43 | dir
|
44 | .createAsync(pathUtil.dirname(path))
|
45 | .then(() => {
|
46 | return fs.symlink(symlinkValue, path);
|
47 | })
|
48 | .then(resolve, reject);
|
49 | } else {
|
50 | reject(err);
|
51 | }
|
52 | });
|
53 | });
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | exports.validateInput = validateInput;
|
61 | exports.sync = symlinkSync;
|
62 | exports.async = symlinkAsync;
|