/
fs.js
196 lines (156 loc) · 5.08 KB
/
fs.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
/* @flow */
import type {Gzip} from 'zlib';
const fs = require('fs-extra');
const klaw = require('klaw');
const path = require('path');
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: string,
{filter, relative = false}: {|filter?: Array<string>, relative?: boolean|} = {},
): Promise<Array<string>> {
return new Promise((resolve, reject) => {
const paths = [];
const walker = klaw(source, {
filter: itemPath => {
if (!filter) {
return true;
}
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
return true;
}
const relativePath = path.relative(source, itemPath);
if (miscUtils.filePatternMatch(relativePath, filter)) {
return true;
}
return false;
},
});
walker.on('data', ({path: itemPath}) => {
const relativePath = path.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: string,
// $FlowFixMe Flow doesn't support null default parameters in exact types
{virtualPath = null}: {|virtualPath?: ?string|} = {},
): Gzip {
if (virtualPath) {
if (!path.isAbsolute(virtualPath)) {
throw new Error('The virtual path has to be an absolute path');
} else {
virtualPath = path.resolve(virtualPath);
}
}
const zipperStream = zlib.createGzip();
const packStream = tarFs.pack(source, {
map: header => {
if (true) {
header.name = path.posix.resolve('/', header.name);
header.name = path.posix.relative('/', header.name);
}
if (virtualPath) {
header.name = path.posix.resolve('/', virtualPath, header.name);
header.name = path.posix.relative('/', header.name);
}
return header;
},
});
packStream.pipe(zipperStream);
packStream.on('error', error => {
zipperStream.emit('error', error);
});
return zipperStream;
};
exports.packToFile = function packToFile(target: string, source: string, options: *): Promise<void> {
const tarballStream = fs.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.createTemporaryFolder = function createTemporaryFolder(): Promise<string> {
return new Promise((resolve, reject) => {
tmp.dir({unsafeCleanup: true}, (error, dirPath) => {
if (error) {
reject(error);
} else {
resolve(dirPath);
}
});
});
};
exports.createTemporaryFile = async function createTemporaryFile(filePath: string): Promise<string> {
if (filePath) {
if (path.normalize(filePath).match(/^(\.\.)?\//)) {
throw new Error('A temporary file path must be a forward path');
}
const folderPath = await exports.createTemporaryFolder();
return path.resolve(folderPath, filePath);
} else {
return new Promise((resolve, reject) => {
tmp.file({discardDescriptor: true}, (error, filePath) => {
if (error) {
reject(error);
} else {
resolve(filePath);
}
});
});
}
};
exports.writeFile = async function writeFile(target: string, body: string | Buffer): Promise<void> {
await fs.mkdirp(path.dirname(target));
await fs.writeFile(target, body);
};
exports.readFile = function readFile(source: string, encoding: ?string = null): Promise<any> {
return fs.readFile(source, encoding);
};
exports.writeJson = function writeJson(target: string, object: any): Promise<void> {
return exports.writeFile(target, JSON.stringify(object));
};
exports.readJson = async function readJson(source: string): 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: string, mod: number): Promise<void> {
await fs.chmod(target, mod);
};
exports.realpath = function realpath(source: string): Promise<string> {
return fs.realpath(source);
};
exports.makeFakeBinary = async function(
target: string,
{output = `Fake binary`, exitCode = 1}: {|output: string, exitCode: number|} = {},
): Promise<void> {
const realTarget = IS_WIN32 ? `${target}.cmd` : target;
const header = IS_WIN32 ? `@echo off\n` : `#!/bin/sh\n`;
await exports.writeFile(realTarget, `${header}echo ${output}\nexit ${exitCode}\n`);
await exports.chmod(realTarget, 0o755);
};