-
Notifications
You must be signed in to change notification settings - Fork 0
/
lndir.js
executable file
·138 lines (105 loc) · 4.15 KB
/
lndir.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
const os = require('os');
const { existsSync, mkdirSync, renameSync, symlinkSync, writeFileSync } = require('fs')
const { dirname, resolve, relative } = require('path');
const readJson = filePath => JSON.parse(require(filePath));
const writeJson = (filePath, json) => writeFileSync(filePath, JSON.stringify(json, null, 2));
(async(argv, currentPath) => {
const [ nodePath, scriptPath, ...args ] = argv;
const homeDir = os.homedir();
const configDir = `${homeDir}/.config/npm/lndir`;
const linksFilepath = `${configDir}/links.json`;
let linkThis;
// create config if it doesn't exist
if (!existsSync(linksFilepath)) {
mkdirSync(configDir, { recursive: true });
writeJson(linksFilepath, {});
console.log(`lndir map file created at: [${linksFilepath}]`);
};
console.log(`Links mapping file is located at [${linksFilepath}]`);
const mappings = require(linksFilepath);
if (args.length > 1) {
console.error('Either pass in the name of an existing link or the directory of one to create');
console.log(mappings);
}
if (args.length === 1) {
linkThis = args[0];
}
// No argument given, print mappings
if (!linkThis) {
console.log('You must pass a directory to this command to link or specify an existing mapping');
console.log(mappings);
return;
}
// check if we are given the name of a package to link and then try to link it
if (mappings[linkThis]) {
const mapping = mappings[linkThis];
const expectedLocalPackageDir = `${currentPath}/node_modules/${linkThis}`;
if (!existsSync(expectedLocalPackageDir)) {
console.error('You must install the package normally before replacing it with a linked version');
return;
}
const linkPathAt = `${expectedLocalPackageDir}/${mapping.linkedPath}`;
const backupPath = `${linkPathAt}.bak`;
console.log(`Linking:\n\t"${mapping.absolutePath}"\n\tto\n\t"${linkPathAt}"\n`);
if (existsSync(backupPath)) {
console.error(`Backup exists at:\n\t"${backupPath}"\n`);
console.error('Either restore this directory or remove it to continue');
return;
}
if (!existsSync(linkPathAt)) {
console.error(`Target path to replace with symlink does not exist [${linkPathAt}]`);
return;
}
if (existsSync(linkPathAt)) {
console.log(`Backing up [${linkPathAt}] to [${backupPath}]`);
renameSync(linkPathAt, backupPath);
}
symlinkSync(mapping.absolutePath, linkPathAt, 'dir');
return;
}
// assume we are trying to symlink a directory
if (linkThis) {
if (!existsSync(linkThis)) {
console.error(`Directory does not exist: ${linkThis}`);
return;
}
const checked = [];
const closestPackageJson = (dir) => {
if (!existsSync(dir)) {
throw Error(`Directory does not exist: "${dir}"`);
}
const testPath = resolve(dir, 'package.json');
checked.push(testPath);
if (existsSync(testPath)) {
return testPath;
}
if (dir === '/') {
throw Error(`No package.json found: ${os.EOL}\t${checked.join(`${os.EOL}\t`)}`);
}
return closestPackageJson(resolve(dir, '..'));
}
const packageJsonPath = closestPackageJson(linkThis);
const packageJson = require(packageJsonPath);
console.log(`Found package.json file: ${packageJsonPath}`);
console.log(`Current directory: ${__dirname}`);
if (typeof packageJson !== 'object' || !packageJson.name) {
console.error('package.json file must contain a name property');
return;
}
const linkName = packageJson.name;
const pathToLink = resolve(linkThis);
const linkedPath = relative(dirname(packageJsonPath), resolve(linkThis));
if (mappings[linkName]) {
console.log(`Mapping already exists for ${linkName} at ${mappings[linkName].absolutePath}`);
console.log('Previous mapping will be removed');
}
console.log(`Creating link for ${linkName} at ${pathToLink}`)
mappings[linkName] = {};
mappings[linkName].absolutePath = pathToLink;
mappings[linkName].linkedPath = linkedPath;
writeJson(linksFilepath, mappings);
console.log(mappings);
return;
}
})(process.argv, process.cwd());