UNPKG

2.3 kBJavaScriptView Raw
1var fs = require('fs-extra')
2var path = require('path')
3var isbinaryfile = require('isbinaryfile')
4var pathModule = require('path')
5
6module.exports = {
7 absolutePath: absolutePath,
8 relativePath: relativePath,
9 walkSync: walkSync,
10 resolveDirectory: resolveDirectory
11}
12
13/**
14 * returns the absolute path of the given @arg path
15 *
16 * @param {String} path - relative path (Unix style which is the one used by Remix IDE)
17 * @param {String} sharedFolder - absolute shared path. platform dependent representation.
18 * @return {String} platform dependent absolute path (/home/user1/.../... for unix, c:\user\...\... for windows)
19 */
20function absolutePath (path, sharedFolder) {
21 path = normalizePath(path)
22 if (path.indexOf(sharedFolder) !== 0) {
23 path = pathModule.resolve(sharedFolder, path)
24 }
25 return path
26}
27
28/**
29 * return the relative path of the given @arg path
30 *
31 * @param {String} path - absolute platform dependent path
32 * @param {String} sharedFolder - absolute shared path. platform dependent representation
33 * @return {String} relative path (Unix style which is the one used by Remix IDE)
34 */
35function relativePath (path, sharedFolder) {
36 var relative = pathModule.relative(sharedFolder, path)
37 return normalizePath(relative)
38}
39
40function normalizePath (path) {
41 if (process.platform === 'win32') {
42 return path.replace(/\\/g, '/')
43 }
44 return path
45}
46
47function walkSync (dir, filelist, sharedFolder) {
48 var files = fs.readdirSync(dir)
49 filelist = filelist || {}
50 files.forEach(function (file) {
51 var subElement = path.join(dir, file)
52 if (!fs.lstatSync(subElement).isSymbolicLink()) {
53 if (fs.statSync(subElement).isDirectory()) {
54 filelist = walkSync(subElement, filelist, sharedFolder)
55 } else {
56 var relative = relativePath(subElement, sharedFolder)
57 filelist[relative] = isbinaryfile.sync(subElement)
58 }
59 }
60 })
61 return filelist
62}
63
64function resolveDirectory (dir, sharedFolder) {
65 var ret = {}
66 var files = fs.readdirSync(dir)
67 files.forEach(function (file) {
68 var subElement = path.join(dir, file)
69 if (!fs.lstatSync(subElement).isSymbolicLink()) {
70 var relative = relativePath(subElement, sharedFolder)
71 ret[relative] = { isDirectory: fs.statSync(subElement).isDirectory() }
72 }
73 })
74 return ret
75}