You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.8 KiB
92 lines
2.8 KiB
5 years ago
|
import { ResolveDirectory, Filelist } from '../types'
|
||
5 years ago
|
|
||
5 years ago
|
const fs = require('fs-extra')
|
||
|
const path = require('path')
|
||
|
const isbinaryfile = require('isbinaryfile')
|
||
|
const pathModule = require('path')
|
||
8 years ago
|
|
||
|
/**
|
||
|
* returns the absolute path of the given @arg path
|
||
|
*
|
||
|
* @param {String} path - relative path (Unix style which is the one used by Remix IDE)
|
||
|
* @param {String} sharedFolder - absolute shared path. platform dependent representation.
|
||
|
* @return {String} platform dependent absolute path (/home/user1/.../... for unix, c:\user\...\... for windows)
|
||
|
*/
|
||
5 years ago
|
function absolutePath (path: string, sharedFolder:string): string {
|
||
8 years ago
|
path = normalizePath(path)
|
||
|
if (path.indexOf(sharedFolder) !== 0) {
|
||
|
path = pathModule.resolve(sharedFolder, path)
|
||
|
}
|
||
|
return path
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* return the relative path of the given @arg path
|
||
|
*
|
||
|
* @param {String} path - absolute platform dependent path
|
||
|
* @param {String} sharedFolder - absolute shared path. platform dependent representation
|
||
|
* @return {String} relative path (Unix style which is the one used by Remix IDE)
|
||
|
*/
|
||
5 years ago
|
function relativePath (path: string, sharedFolder: string): string {
|
||
5 years ago
|
const relative: string = pathModule.relative(sharedFolder, path)
|
||
|
|
||
8 years ago
|
return normalizePath(relative)
|
||
|
}
|
||
|
|
||
5 years ago
|
function normalizePath (path: string): string {
|
||
8 years ago
|
if (process.platform === 'win32') {
|
||
|
return path.replace(/\\/g, '/')
|
||
|
}
|
||
|
return path
|
||
|
}
|
||
|
|
||
5 years ago
|
function walkSync (dir: string, filelist: Filelist, sharedFolder: string): Filelist {
|
||
5 years ago
|
const files: string[] = fs.readdirSync(dir)
|
||
5 years ago
|
|
||
8 years ago
|
filelist = filelist || {}
|
||
|
files.forEach(function (file) {
|
||
5 years ago
|
const subElement = path.join(dir, file)
|
||
|
|
||
8 years ago
|
if (!fs.lstatSync(subElement).isSymbolicLink()) {
|
||
|
if (fs.statSync(subElement).isDirectory()) {
|
||
|
filelist = walkSync(subElement, filelist, sharedFolder)
|
||
|
} else {
|
||
5 years ago
|
const relative = relativePath(subElement, sharedFolder)
|
||
|
|
||
8 years ago
|
filelist[relative] = isbinaryfile.sync(subElement)
|
||
|
}
|
||
8 years ago
|
}
|
||
|
})
|
||
|
return filelist
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
function resolveDirectory (dir: string, sharedFolder: string): ResolveDirectory {
|
||
|
const ret: ResolveDirectory = {}
|
||
5 years ago
|
const files: string[] = fs.readdirSync(dir)
|
||
5 years ago
|
|
||
7 years ago
|
files.forEach(function (file) {
|
||
5 years ago
|
const subElement = path.join(dir, file)
|
||
5 years ago
|
|
||
7 years ago
|
if (!fs.lstatSync(subElement).isSymbolicLink()) {
|
||
5 years ago
|
const relative: string = relativePath(subElement, sharedFolder)
|
||
5 years ago
|
|
||
|
ret[relative] = { isDirectory: fs.statSync(subElement).isDirectory() }
|
||
7 years ago
|
}
|
||
|
})
|
||
|
return ret
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
/**
|
||
|
* returns the absolute path of the given @arg url
|
||
|
*
|
||
|
* @param {String} url - Remix-IDE URL instance
|
||
|
* @return {String} extracted domain name from url
|
||
|
*/
|
||
|
function getDomain(url: string) {
|
||
|
const domainMatch = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img)
|
||
|
|
||
|
return domainMatch ? domainMatch[0] : null
|
||
|
}
|
||
|
|
||
|
export { absolutePath, relativePath, walkSync, resolveDirectory, getDomain }
|