/
fs.ts
235 lines (186 loc) Β· 6.49 KB
/
fs.ts
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
226
227
228
229
230
231
232
233
234
235
import {Gzip} from 'zlib';
import {xfs, NodeFS, PortablePath, ppath, Filename} from '@yarnpkg/fslib';
const klaw = require('klaw');
const tarFs = require('tar-fs');
const tmp = require('tmp');
const zlib = require('zlib');
const miscUtils = require('./misc');
const IS_WIN32 = process.platform === `win32`;
exports.walk = function walk(
source: PortablePath,
{filter, relative = false}: {filter?: Array<string>, relative?: boolean} = {},
): Promise<Array<PortablePath>> {
return new Promise((resolve, reject) => {
const paths = [];
const walker = klaw(NodeFS.fromPortablePath(source), {
filter: itemPath => {
if (!filter) {
return true;
}
itemPath = NodeFS.toPortablePath(itemPath);
const stat = xfs.statSync(itemPath);
if (stat.isDirectory()) {
return true;
}
const relativePath = ppath.relative(source, itemPath);
if (miscUtils.filePatternMatch(relativePath, filter)) {
return true;
}
return false;
},
});
walker.on('data', ({path: itemPath}) => {
itemPath = NodeFS.toPortablePath(itemPath);
const relativePath = ppath.relative(source, itemPath);
if (!filter || miscUtils.filePatternMatch(relativePath, filter)) {
paths.push(relative ? relativePath : itemPath);
}
// This item has been accepted only because it's a directory; it doesn't match the filter
return;
});
walker.on('end', () => {
resolve(paths);
});
});
};
exports.packToStream = function packToStream(
source: PortablePath,
{virtualPath = null}: {virtualPath?: PortablePath | null} = {},
): Gzip {
if (virtualPath) {
if (!ppath.isAbsolute(virtualPath)) {
throw new Error('The virtual path has to be an absolute path');
} else {
virtualPath = ppath.resolve(virtualPath);
}
}
const zipperStream = zlib.createGzip();
const packStream = tarFs.pack(NodeFS.fromPortablePath(source), {
map: header => {
header.name = NodeFS.toPortablePath(header.name);
if (true) {
header.name = ppath.resolve(PortablePath.root, header.name);
header.name = ppath.relative(PortablePath.root, header.name);
}
if (virtualPath) {
header.name = ppath.resolve(PortablePath.root, virtualPath, header.name);
header.name = ppath.relative(PortablePath.root, header.name);
}
return header;
},
});
packStream.pipe(zipperStream);
packStream.on('error', error => {
zipperStream.emit('error', error);
});
return zipperStream;
};
exports.packToFile = function packToFile(target: PortablePath, source: PortablePath, options: any): Promise<void> {
const tarballStream = xfs.createWriteStream(target);
const packStream = exports.packToStream(source, options);
packStream.pipe(tarballStream);
return new Promise((resolve, reject) => {
tarballStream.on('error', error => {
reject(error);
});
packStream.on('error', error => {
reject(error);
});
tarballStream.on('close', () => {
resolve();
});
});
};
exports.unpackToDirectory = function unpackToDirectory(target: PortablePath, source: PortablePath): Promise<void> {
const tarballStream = xfs.createReadStream(source);
const gunzipStream = zlib.createUnzip();
const extractStream = tarFs.extract(NodeFS.fromPortablePath(target));
tarballStream.pipe(gunzipStream).pipe(extractStream);
return new Promise((resolve, reject) => {
tarballStream.on('error', error => {
reject(error);
});
gunzipStream.on('error', error => {
reject(error);
});
extractStream.on('error', error => {
reject(error);
});
extractStream.on('finish', () => {
resolve();
});
});
};
exports.createTemporaryFolder = function createTemporaryFolder(name?: Filename): Promise<PortablePath> {
return new Promise<string>((resolve, reject) => {
tmp.dir({unsafeCleanup: true}, (error, dirPath) => {
if (error) {
reject(error);
} else {
resolve(dirPath);
}
});
}).then(async dirPath => {
dirPath = await xfs.realpathPromise(NodeFS.toPortablePath(dirPath));
if (name) {
dirPath = ppath.join(dirPath, name as PortablePath);
await exports.mkdirp(dirPath);
}
return dirPath;
});
};
exports.createTemporaryFile = async function createTemporaryFile(filePath: PortablePath): Promise<PortablePath> {
if (filePath) {
if (ppath.normalize(filePath).match(/^(\.\.)?\//)) {
throw new Error('A temporary file path must be a forward path');
}
const folderPath = await exports.createTemporaryFolder();
return ppath.resolve(folderPath, filePath as PortablePath);
} else {
return new Promise((resolve, reject) => {
tmp.file({discardDescriptor: true}, (error, filePath) => {
if (error) {
reject(error);
} else {
resolve(NodeFS.toPortablePath(filePath));
}
});
});
}
};
exports.mkdirp = async function mkdirp(target: PortablePath): Promise<void> {
await xfs.mkdirpPromise(target);
};
exports.writeFile = async function writeFile(target: PortablePath, body: string | Buffer): Promise<void> {
await xfs.mkdirpPromise(ppath.dirname(target));
await xfs.writeFilePromise(target, body);
};
exports.readFile = function readFile(source: PortablePath, encoding?: string): Promise<any> {
return xfs.readFilePromise(source, encoding);
};
exports.writeJson = function writeJson(target: PortablePath, object: any): Promise<void> {
return exports.writeFile(target, JSON.stringify(object));
};
exports.readJson = async function readJson(source: PortablePath): Promise<any> {
const fileContent = await exports.readFile(source, 'utf8');
try {
return JSON.parse(fileContent);
} catch (error) {
throw new Error(`Invalid json file (${source})`);
}
};
exports.chmod = async function chmod(target: PortablePath, mod: number): Promise<void> {
await xfs.chmodPromise(target, mod);
};
exports.realpath = function realpath(source: PortablePath): Promise<PortablePath> {
return xfs.realpathPromise(source);
};
exports.makeFakeBinary = async function(
target: PortablePath,
{output = `Fake binary`, exitCode = 1}: {output?: string, exitCode?: number} = {},
): Promise<void> {
const realTarget = IS_WIN32 ? `${target}.cmd` as PortablePath : target;
const header = IS_WIN32 ? `@echo off\n` : `#!/bin/sh\n`;
await exports.writeFile(realTarget, `${header}printf "%s" "${output}"\nexit ${exitCode}\n`);
await exports.chmod(realTarget, 0o755);
};