-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.js
94 lines (84 loc) · 2.04 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
/*
* Parallel
*/
// Node core dependencies
const os = require("os");
const fs = require("fs");
const {promisify} = require("util");
//Convert to promise interfaces
const access = promisify(fs.access);
const mkdtemp = promisify(fs.mkdtemp);
const mkdir = promisify(fs.mkdir);
const rmdir = promisify(fs.rmdir);
const readdir = promisify(fs.readdir);
const readFile = promisify(fs.readFile);
const stat = promisify(fs.stat);
const unlink = promisify(fs.unlink);
const writeFile = promisify(fs.writeFile);
//Junk Bucket dependencies
const {parallel} = require("./future");
const path = require("path");
//Utilities
async function rmRecursively( target ){
const stats = await stat( target );
if( stats.isDirectory() ) {
const files = await readdir( target );
await parallel(files.map( async subfile => {
const fullPath = path.join( target, subfile );
await rmRecursively( fullPath );
}));
await rmdir( target );
} else {
await unlink( target );
}
}
async function makeTempDir( template, base = os.tmpdir() ) {
const tempPrefix = path.join( base, template);
const root = await mkdtemp( tempPrefix );
return root;
}
async function exists( name ){
try {
await access(name, fs.constants.F_OK);
console.error("Access");
return true;
}catch(e){
if( e.error == Error.ENOENT ){
return false;
} else {
throw e;
}
}
}
/**
* Creates a new temporary directory scoped to the given context. Once the context is cleaned up the temporary
* directory will be deleted.
*
* @param context The context to be attached to
* @param template Temporary directory template to utilize
* @returns {Promise<string>} The path to the temporary directory
*/
async function contextTemporaryDirectory( context, template ){
const dir = await makeTempDir(template);
context.onCleanup(async function () {
await rmRecursively(dir);
});
return dir;
}
module.exports = {
//Promise adapters
access,
mkdtemp,
mkdir,
rmdir,
readdir,
readFile,
stat,
unlink,
writeFile,
//Extensions
exists,
rmRecursively,
makeTempDir,
contextTemporaryDirectory
};