This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebootstrap.js
66 lines (58 loc) · 2.1 KB
/
debootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict'; // eslint-disable-line strict
// Otherwise we can’t use `let` in node v4.
const fs = require('fs');
const pathModule = require('path');
const chalk = require('chalk');
const b = chalk.bold;
const newError = require('./_/newError');
const packagePaths = require('./_/packagePaths');
/* (see git.io/rtype)
({
path = process.cwd(): String,
// Path to your project directory
scope = require('path').basename(path): String,
// The npm scope of all packages in your repo
}) =>
Void
*/
module.exports = (params) => {
const path = params.path || process.cwd();
const scope = params.scope || pathModule.basename(path);
const symlinkError = (symlinkPath) => newError(
`We’ve expected ${b(symlinkPath)} to be a symlink ` +
`(that’s what ${b('monopod bootstrap')} creates) – but it’s ` +
'a regular file or directory. We don’t want to break anything, ' +
'so please remove it by hand and try again.'
);
// Check if scope symlink is safe to remove
const nodeModulesPath = `${path}/node_modules`;
const scopeSymlinkPath = `${nodeModulesPath}/@${scope}`;
try {
const symlink = fs.lstatSync(scopeSymlinkPath);
if (!symlink.isSymbolicLink()) throw symlinkError(scopeSymlinkPath);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
// Check if package dep symlinks are safe to remove
const packageDepsPaths = packagePaths(path).map(
packagePath => `${packagePath}/node_modules`
);
const packageDepsSymlinkPaths = packageDepsPaths.filter((packageDepsPath) => {
try {
const symlink = fs.lstatSync(packageDepsPath);
if (!symlink.isSymbolicLink()) throw symlinkError(packageDepsPath);
return true;
} catch (error) {
if (error.code !== 'ENOENT') throw error;
return false;
}
});
// Remove scope symlink
try {
fs.unlinkSync(scopeSymlinkPath);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
// Remove package deps symlinks
packageDepsSymlinkPaths.forEach(symlinkPath => fs.unlinkSync(symlinkPath));
};