@atlaspack/fs 2.14.5-dev.55 → 2.14.5-dev.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.NodeFS = void 0;
7
- // $FlowFixMe[prop-missing] handled by the throwing constructor
8
- class NodeFS {
9
- constructor() {
10
- throw new Error("NodeFS isn't available in the browser");
11
- }
12
- }
13
- exports.NodeFS = NodeFS;
package/lib/NodeFS.js DELETED
@@ -1,281 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.NodeFS = void 0;
7
- function _gracefulFs() {
8
- const data = _interopRequireDefault(require("graceful-fs"));
9
- _gracefulFs = function () {
10
- return data;
11
- };
12
- return data;
13
- }
14
- function _fs() {
15
- const data = _interopRequireDefault(require("fs"));
16
- _fs = function () {
17
- return data;
18
- };
19
- return data;
20
- }
21
- function _ncp() {
22
- const data = _interopRequireDefault(require("ncp"));
23
- _ncp = function () {
24
- return data;
25
- };
26
- return data;
27
- }
28
- function _path() {
29
- const data = _interopRequireDefault(require("path"));
30
- _path = function () {
31
- return data;
32
- };
33
- return data;
34
- }
35
- function _os() {
36
- const data = require("os");
37
- _os = function () {
38
- return data;
39
- };
40
- return data;
41
- }
42
- function _util() {
43
- const data = require("util");
44
- _util = function () {
45
- return data;
46
- };
47
- return data;
48
- }
49
- function _buildCache() {
50
- const data = require("@atlaspack/build-cache");
51
- _buildCache = function () {
52
- return data;
53
- };
54
- return data;
55
- }
56
- function _utils() {
57
- const data = require("@atlaspack/utils");
58
- _utils = function () {
59
- return data;
60
- };
61
- return data;
62
- }
63
- function _featureFlags() {
64
- const data = require("@atlaspack/feature-flags");
65
- _featureFlags = function () {
66
- return data;
67
- };
68
- return data;
69
- }
70
- function _watcher() {
71
- const data = _interopRequireDefault(require("@parcel/watcher"));
72
- _watcher = function () {
73
- return data;
74
- };
75
- return data;
76
- }
77
- var _package = _interopRequireDefault(require("../package.json"));
78
- function searchNative() {
79
- const data = _interopRequireWildcard(require("@atlaspack/rust"));
80
- searchNative = function () {
81
- return data;
82
- };
83
- return data;
84
- }
85
- var searchJS = _interopRequireWildcard(require("./find"));
86
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
87
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
88
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
89
- // Most of this can go away once we only support Node 10+, which includes
90
- // require('fs').promises
91
- const realpath = (0, _util().promisify)(process.platform === 'win32' ? _gracefulFs().default.realpath : _gracefulFs().default.realpath.native);
92
- const isPnP = process.versions.pnp != null;
93
- function getWatchmanWatcher() {
94
- // This is here to trick atlaspack into ignoring this require...
95
- const packageName = ['@atlaspack', 'watcher-watchman-js'].join('/');
96
-
97
- // $FlowFixMe
98
- return require(packageName);
99
- }
100
- class NodeFS {
101
- readFile = (0, _util().promisify)(_gracefulFs().default.readFile);
102
- copyFile = (0, _util().promisify)(_gracefulFs().default.copyFile);
103
- stat = (0, _util().promisify)(_gracefulFs().default.stat);
104
- readdir = (0, _util().promisify)(_gracefulFs().default.readdir);
105
- symlink = (0, _util().promisify)(_gracefulFs().default.symlink);
106
- unlink = (0, _util().promisify)(_gracefulFs().default.unlink);
107
- utimes = (0, _util().promisify)(_gracefulFs().default.utimes);
108
- ncp = (0, _util().promisify)(_ncp().default);
109
- createReadStream = _gracefulFs().default.createReadStream;
110
- cwd = () => process.cwd();
111
- chdir = directory => process.chdir(directory);
112
- statSync = path => _gracefulFs().default.statSync(path);
113
- realpathSync = process.platform === 'win32' ? _gracefulFs().default.realpathSync : _gracefulFs().default.realpathSync.native;
114
- existsSync = _gracefulFs().default.existsSync;
115
- readdirSync = _gracefulFs().default.readdirSync;
116
- findAncestorFile = isPnP ? (...args) => searchJS.findAncestorFile(this, ...args) : searchNative().findAncestorFile;
117
- findNodeModule = isPnP ? (...args) => searchJS.findNodeModule(this, ...args) : searchNative().findNodeModule;
118
- findFirstFile = isPnP ? (...args) => searchJS.findFirstFile(this, ...args) : searchNative().findFirstFile;
119
- watcher() {
120
- return (0, _featureFlags().getFeatureFlag)('useWatchmanWatcher') ? getWatchmanWatcher() : _watcher().default;
121
- }
122
- createWriteStream(filePath, options) {
123
- // Make createWriteStream atomic
124
- let tmpFilePath = getTempFilePath(filePath);
125
- let failed = false;
126
- const move = async () => {
127
- if (!failed) {
128
- try {
129
- await _gracefulFs().default.promises.rename(tmpFilePath, filePath);
130
- } catch (e) {
131
- // This is adapted from fs-write-stream-atomic. Apparently
132
- // Windows doesn't like renaming when the target already exists.
133
- if (process.platform === 'win32' && e.syscall && e.syscall === 'rename' && e.code && e.code === 'EPERM') {
134
- let [hashTmp, hashTarget] = await Promise.all([(0, _utils().hashFile)(this, tmpFilePath), (0, _utils().hashFile)(this, filePath)]);
135
- await this.unlink(tmpFilePath);
136
- if (hashTmp != hashTarget) {
137
- throw e;
138
- }
139
- }
140
- }
141
- }
142
- };
143
- let writeStream = _gracefulFs().default.createWriteStream(tmpFilePath, {
144
- ...options,
145
- fs: {
146
- ..._gracefulFs().default,
147
- close: (fd, cb) => {
148
- _gracefulFs().default.close(fd, err => {
149
- if (err) {
150
- cb(err);
151
- } else {
152
- move().then(() => cb(), err => cb(err));
153
- }
154
- });
155
- }
156
- }
157
- });
158
- writeStream.once('error', () => {
159
- failed = true;
160
- try {
161
- _gracefulFs().default.unlinkSync(tmpFilePath);
162
- } catch (_err) {
163
- // ignore
164
- }
165
- });
166
- return writeStream;
167
- }
168
- async writeFile(filePath, contents, options) {
169
- let tmpFilePath = getTempFilePath(filePath);
170
- await _gracefulFs().default.promises.writeFile(tmpFilePath, contents, options);
171
- await _gracefulFs().default.promises.rename(tmpFilePath, filePath);
172
- }
173
- readFileSync(filePath, encoding) {
174
- if (encoding != null) {
175
- return _gracefulFs().default.readFileSync(filePath, encoding);
176
- }
177
- return _gracefulFs().default.readFileSync(filePath);
178
- }
179
- async realpath(originalPath) {
180
- try {
181
- return await realpath(originalPath, 'utf8');
182
- } catch (e) {
183
- // do nothing
184
- }
185
- return originalPath;
186
- }
187
- exists(filePath) {
188
- return new Promise(resolve => {
189
- _gracefulFs().default.exists(filePath, resolve);
190
- });
191
- }
192
- watch(dir, fn, opts) {
193
- return this.watcher().subscribe(dir, fn, opts);
194
- }
195
- getEventsSince(dir, snapshot, opts) {
196
- return this.watcher().getEventsSince(dir, snapshot, opts);
197
- }
198
- async writeSnapshot(dir, snapshot, opts) {
199
- await this.mkdirp(_path().default.dirname(snapshot));
200
- await this.watcher().writeSnapshot(dir, snapshot, opts);
201
- }
202
- static deserialize() {
203
- return new NodeFS();
204
- }
205
- serialize() {
206
- return null;
207
- }
208
- async mkdirp(filePath) {
209
- await _fs().default.promises.mkdir(filePath, {
210
- recursive: true
211
- });
212
- }
213
- async rimraf(filePath) {
214
- if (_gracefulFs().default.promises.rm) {
215
- await _gracefulFs().default.promises.rm(filePath, {
216
- recursive: true,
217
- force: true
218
- });
219
- return;
220
- }
221
-
222
- // fs.promises.rm is not supported in node 12...
223
- let stat;
224
- try {
225
- stat = await this.stat(filePath);
226
- } catch (err) {
227
- return;
228
- }
229
- if (stat.isDirectory()) {
230
- // $FlowFixMe
231
- await _fs().default.promises.rmdir(filePath, {
232
- recursive: true
233
- });
234
- } else {
235
- await _fs().default.promises.unlink(filePath);
236
- }
237
- }
238
- }
239
- exports.NodeFS = NodeFS;
240
- (0, _buildCache().registerSerializableClass)(`${_package.default.version}:NodeFS`, NodeFS);
241
- let writeStreamCalls = 0;
242
- let threadId;
243
- try {
244
- ({
245
- threadId
246
- } = require('worker_threads'));
247
- } catch {
248
- //
249
- }
250
- let useOsTmpDir;
251
- function shouldUseOsTmpDir(filePath) {
252
- if (useOsTmpDir != null) {
253
- return useOsTmpDir;
254
- }
255
- try {
256
- const tmpDir = (0, _os().tmpdir)();
257
- _fs().default.accessSync(tmpDir, _fs().default.constants.R_OK | _fs().default.constants.W_OK);
258
- const tmpDirStats = _fs().default.statSync(tmpDir);
259
- const filePathStats = _fs().default.statSync(filePath);
260
- // Check the tmpdir is on the same partition as the target directory.
261
- // This is required to ensure renaming is an atomic operation.
262
- useOsTmpDir = tmpDirStats.dev === filePathStats.dev;
263
- } catch (e) {
264
- // We don't have read/write access to the OS tmp directory
265
- useOsTmpDir = false;
266
- }
267
- return useOsTmpDir;
268
- }
269
-
270
- // Generate a temporary file path used for atomic writing of files.
271
- function getTempFilePath(filePath) {
272
- writeStreamCalls = writeStreamCalls % Number.MAX_SAFE_INTEGER;
273
- let tmpFilePath = filePath;
274
-
275
- // If possible, write the tmp file to the OS tmp directory
276
- // This reduces the amount of FS events the watcher needs to process during the build
277
- if (shouldUseOsTmpDir(filePath)) {
278
- tmpFilePath = _path().default.join((0, _os().tmpdir)(), _path().default.basename(filePath));
279
- }
280
- return tmpFilePath + '.' + process.pid + (threadId != null ? '.' + threadId : '') + '.' + (writeStreamCalls++).toString(36);
281
- }
@@ -1,176 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.NodeVCSAwareFS = void 0;
7
- function _path() {
8
- const data = _interopRequireDefault(require("path"));
9
- _path = function () {
10
- return data;
11
- };
12
- return data;
13
- }
14
- var _NodeFS = require("./NodeFS");
15
- function _rust() {
16
- const data = require("@atlaspack/rust");
17
- _rust = function () {
18
- return data;
19
- };
20
- return data;
21
- }
22
- function _buildCache() {
23
- const data = require("@atlaspack/build-cache");
24
- _buildCache = function () {
25
- return data;
26
- };
27
- return data;
28
- }
29
- function _logger() {
30
- const data = _interopRequireWildcard(require("@atlaspack/logger"));
31
- _logger = function () {
32
- return data;
33
- };
34
- return data;
35
- }
36
- function _featureFlags() {
37
- const data = require("@atlaspack/feature-flags");
38
- _featureFlags = function () {
39
- return data;
40
- };
41
- return data;
42
- }
43
- var _package = _interopRequireDefault(require("../package.json"));
44
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
45
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
46
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
47
- // $FlowFixMe
48
- class NodeVCSAwareFS extends _NodeFS.NodeFS {
49
- /**
50
- * These files are excluded from 'dirty file' tracking even if they are
51
- * modified.
52
- */
53
- #excludePatterns;
54
- /**
55
- * Logging function for the diff between watcher events and vcs events.
56
- */
57
- #logEventDiff;
58
- /**
59
- * The path of the git repository containing the project root. Null if the
60
- * project is not a git repository.
61
- */
62
- #gitRepoPath;
63
- constructor(options) {
64
- super();
65
- this.#excludePatterns = options.excludePatterns;
66
- this.#logEventDiff = options.logEventDiff;
67
- this.#gitRepoPath = options.gitRepoPath;
68
- }
69
-
70
- // $FlowFixMe[incompatible-extend] the serialization API is not happy with inheritance
71
- static deserialize(data) {
72
- const fs = new NodeVCSAwareFS({
73
- excludePatterns: data.excludePatterns,
74
- logEventDiff: null,
75
- gitRepoPath: data.gitRepoPath
76
- });
77
- return fs;
78
- }
79
-
80
- // $FlowFixMe[incompatible-extend] the serialization API is not happy with inheritance
81
- serialize() {
82
- return {
83
- excludePatterns: this.#excludePatterns,
84
- logEventDiff: null,
85
- gitRepoPath: this.#gitRepoPath
86
- };
87
- }
88
- setGitRepoPath(gitRepoPath) {
89
- this.#gitRepoPath = gitRepoPath;
90
- }
91
- async getEventsSince(dir, snapshot, opts) {
92
- const gitRepoPath = this.#gitRepoPath;
93
- if (gitRepoPath == null) {
94
- return this.watcher().getEventsSince(dir, snapshot, opts);
95
- }
96
- const {
97
- nativeSnapshotPath,
98
- vcsState
99
- } = await (0, _logger().instrumentAsync)('NodeVCSAwareFS.readSnapshot', async () => {
100
- // Note: can't use toString() directly, or it won't resolve the promise
101
- const snapshotFile = await this.readFile(snapshot);
102
- const snapshotFileContent = snapshotFile.toString();
103
- return JSON.parse(snapshotFileContent);
104
- });
105
- let watcherEventsSince = [];
106
- const vcsEventsSince = vcsState != null ? (await (0, _logger().instrumentAsync)('NodeVCSAwareFS::rust.getEventsSince', () => (0, _rust().getEventsSince)(gitRepoPath, vcsState, null))).map(e => ({
107
- path: e.path,
108
- type: e.changeType
109
- })) : null;
110
- if ((0, _featureFlags().getFeatureFlagValue)('vcsMode') !== 'NEW' && vcsEventsSince != null) {
111
- var _this$logEventDiff;
112
- watcherEventsSince = await (0, _logger().instrumentAsync)('NodeVCSAwareFS::watchman.getEventsSince', () => this.watcher().getEventsSince(dir, nativeSnapshotPath, opts));
113
- (_this$logEventDiff = this.#logEventDiff) === null || _this$logEventDiff === void 0 || _this$logEventDiff.call(this, watcherEventsSince, vcsEventsSince);
114
- }
115
- if (['NEW_AND_CHECK', 'NEW'].includes((0, _featureFlags().getFeatureFlagValue)('vcsMode'))) {
116
- if (vcsEventsSince == null) {
117
- _logger().default.error({
118
- origin: '@atlaspack/fs',
119
- message: 'Missing VCS state. There was an error when writing the snapshot. Please clear your cache.',
120
- meta: {
121
- trackableEvent: 'vcs_state_snapshot_read_failed'
122
- }
123
- });
124
- return [];
125
- }
126
- return vcsEventsSince;
127
- }
128
- return watcherEventsSince;
129
- }
130
- async writeSnapshot(dir, snapshot, opts) {
131
- const gitRepoPath = this.#gitRepoPath;
132
- if (gitRepoPath == null) {
133
- await this.watcher().writeSnapshot(dir, snapshot, opts);
134
- return;
135
- }
136
- const snapshotDirectory = _path().default.dirname(snapshot);
137
- await this.mkdirp(snapshotDirectory);
138
- const filename = _path().default.basename(snapshot, '.txt');
139
- const nativeSnapshotPath = _path().default.join(snapshotDirectory, `${filename}.native-snapshot.txt`);
140
- if ((0, _featureFlags().getFeatureFlagValue)('vcsMode') !== 'NEW') {
141
- await (0, _logger().instrumentAsync)('NodeVCSAwareFS::watchman.writeSnapshot', async () => {
142
- await this.watcher().writeSnapshot(dir, nativeSnapshotPath, opts);
143
- });
144
- }
145
- let vcsState = null;
146
- try {
147
- var _vcsState, _vcsState2;
148
- vcsState = await (0, _logger().instrumentAsync)('NodeVCSAwareFS::getVcsStateSnapshot', () => (0, _rust().getVcsStateSnapshot)(gitRepoPath, this.#excludePatterns));
149
- _logger().default.info({
150
- origin: '@atlaspack/fs',
151
- message: 'Expose VCS timing metrics',
152
- meta: {
153
- trackableEvent: 'vcs_timing_metrics',
154
- dirtyFilesExecutionTime: (_vcsState = vcsState) === null || _vcsState === void 0 ? void 0 : _vcsState.dirtyFilesExecutionTime,
155
- yarnStatesExecutionTime: (_vcsState2 = vcsState) === null || _vcsState2 === void 0 ? void 0 : _vcsState2.yarnStatesExecutionTime
156
- }
157
- });
158
- } catch (err) {
159
- _logger().default.error({
160
- origin: '@atlaspack/fs',
161
- message: `Failed to get VCS state snapshot: ${err.message}`,
162
- meta: {
163
- trackableEvent: 'vcs_state_snapshot_failed',
164
- error: err
165
- }
166
- });
167
- }
168
- const snapshotContents = {
169
- vcsState,
170
- nativeSnapshotPath
171
- };
172
- await this.writeFile(snapshot, JSON.stringify(snapshotContents));
173
- }
174
- }
175
- exports.NodeVCSAwareFS = NodeVCSAwareFS;
176
- (0, _buildCache().registerSerializableClass)(`${_package.default.version}:NodeVCSAwareFS`, NodeVCSAwareFS);