forked from workshopper/workshopper-adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
57 lines (44 loc) · 1.18 KB
/
util.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
const path = require('path')
, fs = require('fs')
, mkdirp = require('mkdirp')
, vw = require('visualwidth')
function repeat (ch, sz) {
return new Array(sz + 1).join(ch)
}
function idFromName (id) {
return id.toLowerCase()
.replace(/\s/g, '_')
.replace(/[^\w]/gi, '')
}
function dirFromName (exerciseDir, name) {
if (typeof exerciseDir !== 'string') {
return null;
}
return path.join(exerciseDir, idFromName(name))
}
function userDir () {
var folders = [process.env.HOME || process.env.USERPROFILE].concat(Array.prototype.slice.apply(arguments))
var dir = path.join.apply(path, folders)
mkdirp.sync(dir)
return dir
}
function getFsObject(type, file, base) {
var stat
if (typeof base !== 'string' || typeof file !== 'string')
return null
file = path.resolve(base, file)
try {
stat = fs.statSync(file)
} catch(e) {}
if (!stat || !(type === 'file' ? stat.isFile() : stat.isDirectory()))
return null
return file
}
module.exports = {
idFromName: idFromName
, dirFromName: dirFromName
, repeat: repeat
, getDir: getFsObject.bind(null, 'dir')
, getFile: getFsObject.bind(null, 'file')
, userDir: userDir
}