-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwriter.js
214 lines (180 loc) · 5.05 KB
/
writer.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
'use strict';
const fs = require('fs');
const path = require('path');
const log = require('./log.js');
// Manages the files and folders in .meteor/local/types
module.exports = class Writer {
constructor(appPath) {
this.appPath = appPath;
this.typesPath = path.resolve(appPath, '.meteor/local/types');
this.npmPackagePath = path.resolve(this.typesPath, 'node_modules/package-types');
this.existingPackages = new Map();
this.packages = new Map();
}
setup() {
log('starting setup');
this.mkdirIfMIssing(this.typesPath, { recursive: true });
this.mkdirIfMIssing(this.npmPackagePath, { recursive: true });
fs.writeFileSync(
path.resolve(this.npmPackagePath, 'package.json'),
JSON.stringify(
{
"name": "package-types"
},
null,
2
)
);
const dirents = fs.readdirSync(this.npmPackagePath, { withFileTypes: true });
dirents.forEach(dirent => {
if (dirent.isDirectory()) {
log('checking', dirent.name);
let nodeModulesPath = this.readlinkOrNull(
path.resolve(
this.npmPackagePath,
dirent.name,
'node_modules'
)
);
let packagePath = this.readlinkOrNull(
path.resolve(
this.npmPackagePath,
dirent.name,
'package'
)
);
if (nodeModulesPath === null || packagePath === null) {
log('partial - cleaning', dirent.name);
this.cleanPackage(path.resolve(this.npmPackagePath, dirent.name));
return;
}
this.existingPackages.set(dirent.name, {
path: path.resolve(this.npmPackagePath, dirent.name),
nodeModulesPath,
packagePath
});
log('add existing', dirent.name);
}
});
log('finish setup');
}
addPackage(name, packagePath, typesPath) {
log('adding package', name);
this.packages.set(this.normalizeName(name), { packagePath, typesPath });
}
normalizeName(name) {
return name.replace(':', '_');
}
writeToDisk() {
log('writing to disk');
for (const entry of this.packages.entries()) {
let name = entry[0];
let packagePath = entry[1].packagePath;
let existing = this.existingPackages.get(name);
let nodeModulesPath = this.findNodeModulesPath(packagePath);
let upToDate = existing && existing.nodeModulesPath === nodeModulesPath
&& existing.packagePath === packagePath;
if (upToDate) {
log('up to date', name);
continue;
}
if (existing) {
log('has existing - is cleaning', name);
this.cleanPackage(existing.path);
}
log('writing', name);
let packageTypesPath = path.resolve(this.npmPackagePath, name);
this.mkdirIfMIssing(packageTypesPath);
fs.symlinkSync(
nodeModulesPath,
path.resolve(packageTypesPath, 'node_modules'),
'junction'
);
fs.symlinkSync(
packagePath,
path.resolve(packageTypesPath, 'package'),
'junction'
);
this.existingPackages.set(name, {
path: packageTypesPath,
nodeModulesPath,
packagePath
});
}
for (const name of this.existingPackages.keys()) {
if (this.packages.has(name)) {
continue;
}
this.cleanPackage(this.existingPackages.get(name).path);
}
let declaration = this.generateDeclaration();
fs.writeFileSync(
path.resolve(this.typesPath, 'packages.d.ts'),
declaration
);
this.packages.clear();
}
generateDeclaration() {
let content = '';
for (const entry of this.packages.entries()) {
let name = entry[0];
let typesPath = entry[1].typesPath;
let standardName = name.replace('_', ':');
// When typescript resolves the file, it assumes the path doesn't
// have the extension.
let finalTypesPath = typesPath.replace('.ts', '');
content += `
declare module 'meteor/${standardName}' {
import exports = require('package-types/${name}/package/${finalTypesPath}');
export = exports;
}
`;
}
return content;
}
findNodeModulesPath(packagePath) {
return path.resolve(packagePath, 'npm/node_modules');
}
cleanPackage(packageTypesPath) {
log('cleaning', packageTypesPath);
[
path.resolve(packageTypesPath, 'node_modules'),
path.resolve(packageTypesPath, 'package'),
].forEach(pathToUnlink => {
try {
fs.unlinkSync(pathToUnlink);
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
});
try {
fs.rmdirSync(packageTypesPath);
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
}
mkdirIfMIssing(dirPath, options) {
try {
fs.mkdirSync(dirPath, options);
} catch (e) {
if (e.code == 'EEXIST') {
return;
}
throw e;
}
}
readlinkOrNull(linkPath) {
try {
return fs.readlinkSync(linkPath);
} catch (e) {
if (e.code === 'ENOENT') {
return null;
}
throw e;
}
}
}