fix(core): update
This commit is contained in:
69
ts/fs.ts
69
ts/fs.ts
@@ -389,27 +389,52 @@ export const listFileTree = async (
|
||||
};
|
||||
|
||||
/**
|
||||
* checks wether a file is ready for processing
|
||||
* Watches for file stability before resolving the promise.
|
||||
*/
|
||||
export const waitForFileToBeReady = async (filePathArg: string): Promise<void> => {
|
||||
if (!plugins.path.isAbsolute(filePathArg)) {
|
||||
filePathArg = plugins.path.resolve(filePathArg);
|
||||
}
|
||||
const limitedArray = new plugins.lik.LimitedArray<number>(3);
|
||||
let fileReady = false;
|
||||
while (!fileReady) {
|
||||
const stats = await plugins.fsExtra.stat(filePathArg);
|
||||
limitedArray.addOne(stats.size);
|
||||
if (
|
||||
limitedArray.array.length < 3 ||
|
||||
!(
|
||||
limitedArray.array[0] === limitedArray.array[1] &&
|
||||
limitedArray.array[1] === limitedArray.array[2]
|
||||
)
|
||||
) {
|
||||
await plugins.smartdelay.delayFor(5000);
|
||||
} else {
|
||||
fileReady = true;
|
||||
}
|
||||
}
|
||||
export const waitForFileToBeReady = (filePathArg: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let lastSize = -1;
|
||||
let stableCheckTimeout: NodeJS.Timeout | null = null;
|
||||
|
||||
const clearStableCheckTimeout = () => {
|
||||
if (stableCheckTimeout) {
|
||||
clearTimeout(stableCheckTimeout);
|
||||
stableCheckTimeout = null;
|
||||
}
|
||||
};
|
||||
|
||||
const watcher = plugins.fs.watch(filePathArg, (eventType, filename) => {
|
||||
if (eventType === 'change') {
|
||||
plugins.fs.stat(filePathArg, (err, stats) => {
|
||||
if (err) {
|
||||
watcher.close();
|
||||
clearStableCheckTimeout();
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
if (stats.size === lastSize) {
|
||||
clearStableCheckTimeout();
|
||||
stableCheckTimeout = setTimeout(() => {
|
||||
watcher.close();
|
||||
resolve();
|
||||
}, 5000); // stability duration
|
||||
} else {
|
||||
lastSize = stats.size;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
watcher.on('error', (error) => {
|
||||
clearStableCheckTimeout();
|
||||
watcher.close();
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user