-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.js
225 lines (191 loc) · 5.37 KB
/
lib.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var fs = require('fs');
var fsExtra = require('fs-extra');
var path = require('path');
var partial = require('ap').partial;
module.exports = {
initializeAddonDirectory: initializeAddonDirectory,
exitWithError: exitWithError,
check: check,
list: list,
link: link,
create: create
};
function check (state) {
if (!state.addonDir) {
initializeAddonDirectory(state);
}
if (!state.definition) {
initializeAddonDefinitions(state);
}
}
// list all addons available
function list (state) {
check(state);
return fs.readdirSync(state.addonDir)
.filter(function (name) {
return fs.statSync(path.join(state.addonDir, name)).isDirectory();
});
}
// create a new directory structure and symlink it
function create (state, name) {
check(state);
var dirName = path.join(state.addonDir, name);
try {
fs.statSync(dirName);
exitWithError(state, ['Directory', dirName, 'already exists'].join(' '));
} catch (e) {
if (e.code !== 'ENOENT') {
exitWithError(state, e);
}
}
try {
console.log('Creating directories...');
logAndCreate();
Object.keys(state.definition).forEach(logAndCreate);
console.log();
} catch (e) {
exitWithError(state, e);
}
return link(state, name);
function logAndCreate (dir) {
console.log('mkdir', [name, dir || ''].join('/'));
fs.mkdirSync(path.join(dirName, dir || '.'));
}
}
function getDefinitionForAddon (state, addon) {
try {
return require(path.join(state.addonDir, addon, 'd2am.json'));
} catch (e) {
return state.definition;
}
}
// iterate over all addons and setup symlinks
// if name is passed in, just do it to that one
function link (state, name) {
check(state);
if (name) {
return linkAddon(name);
} else {
return list(state).map(linkAddon);
}
function linkAddon (addon) {
console.log('checking symbolic links for', addon);
return Object.keys(getDefinitionForAddon(state, addon)).map(partial(linkAddonDirectory, addon));
}
function linkAddonDirectory (addon, linkName) {
var baseDir = path.join(state.addonDir, addon, linkName);
var linkDir = path.join(state.dir, state.definition[linkName].replace('{ADDON_NAME}', addon));
var linkDirParts = path.parse(linkDir);
var backupDir = path.join(linkDirParts.dir, linkDirParts.base + '.old');
var isEmpty = false;
var linkResolvedDir;
var baseResolvedDir;
var linkExists = false;
if (fsExtra.pathExistsSync(backupDir)) {
fsExtra.removeSync(backupDir);
}
try {
linkResolvedDir = fs.realpathSync(linkDir);
linkExists = true;
} catch (e) {
if (e.code === 'ENOENT') {
linkResolvedDir = null;
} else {
exitWithError(state, e);
}
}
if (linkResolvedDir === baseDir) {
console.log(' *', addon, linkName, 'has reverse junctions, fixing this because volvo is terrible');
fsExtra.removeSync(linkDir);
fsExtra.moveSync(baseDir, linkDir);
fsExtra.ensureSymlinkSync(linkDir, baseDir, 'junction');
return;
}
var baseExists = false;
try {
baseResolvedDir = fs.realpathSync(baseDir);
baseExists = true;
} catch (e) {
if (e.code === 'ENOENT') {
baseResolvedDir = null;
} else {
exitWithError(state, e);
}
}
if (baseResolvedDir === linkDir || baseResolvedDir + path.sep === linkDir) {
return;
}
// newline for dramatic effect
console.log();
console.log(' *', addon, linkName, 'must be linked');
if (baseExists) {
if (baseResolvedDir !== baseDir || fs.readdirSync(baseDir).length === 0) {
fsExtra.removeSync(baseDir);
} else {
if (linkExists) {
console.log(' *', addon, linkName, 'backing up old directory');
fsExtra.moveSync(linkDir, backupDir);
}
console.log(' *', addon, linkName, 'copying into volvo directory for reverse link');
fsExtra.moveSync(baseDir, linkDir);
}
}
return fsExtra.ensureSymlinkSync(linkDir, baseDir, 'junction');
}
}
function initializeAddonDefinitions (state) {
if (!state.addonDir) {
// this probably shouldn't happen
initializeAddonDirectory(state);
}
var fileName = path.join(state.addonDir, 'definition.json');
var needsInit = false;
try {
fs.statSync(fileName);
} catch (e) {
if (e.code === 'ENOENT') {
needsInit = true;
} else {
exitWithError(state, e);
}
}
if (needsInit) {
console.log('Copying default addon directory definition file');
fsExtra.copySync(path.join(__dirname, 'definition.json'), fileName);
console.log('Done!');
}
state.definition = require(fileName);
}
function initializeAddonDirectory (state) {
var dirStat;
var needsInit = false;
state.addonDir = path.join(state.dir, 'addons');
try {
dirStat = fs.statSync(state.addonDir);
} catch (e) {
if (e.code === 'ENOENT') {
needsInit = true;
} else {
exitWithError(state, e);
}
}
if (!needsInit) {
if (!dirStat.isDirectory()) {
exitWithError(state, 'addons exists but is not a directory');
} else {
// looking good!
return;
}
}
console.log('Creating addons directory at:', state.addonDir);
fs.mkdirSync(state.addonDir);
console.log('Done!');
}
function exitWithError (state, err) {
if (state.exitOnError) {
console.error(err);
process.exit(1);
} else {
throw new Error(err);
}
}