74 lines
1.5 KiB
JavaScript
74 lines
1.5 KiB
JavaScript
import fs from 'fs'
|
|
import path from 'path'
|
|
import os from 'os'
|
|
|
|
function matchObject(item, str) {
|
|
return Object.prototype.toString.call(item)
|
|
=== '[object ' + str + ']'
|
|
}
|
|
|
|
function checkStat(name, fn) {
|
|
try {
|
|
return fn(name)
|
|
} catch (err) {
|
|
if (/^(ENOENT|EPERM|EACCES)$/.test(err.code)) {
|
|
if (err.code !== 'ENOENT') {
|
|
console.warn('Warning: Cannot access %s', name)
|
|
}
|
|
return false
|
|
}
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export function nil(item) {
|
|
return item == null
|
|
}
|
|
export function array(item) {
|
|
return Array.isArray(item)
|
|
}
|
|
export function emptyObject(item) {
|
|
for (var key in item) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
export function buffer(item) {
|
|
return Buffer.isBuffer(item)
|
|
}
|
|
export function regExp(item) {
|
|
return matchObject(item, 'RegExp')
|
|
}
|
|
export function string(item) {
|
|
return matchObject(item, 'String')
|
|
}
|
|
export function func(item) {
|
|
return typeof item === 'function'
|
|
}
|
|
export function number(item) {
|
|
return matchObject(item, 'Number')
|
|
}
|
|
export function exists(name) {
|
|
return fs.existsSync(name)
|
|
}
|
|
export function file(name) {
|
|
return checkStat(name, function(n) {
|
|
return fs.statSync(n).isFile()
|
|
})
|
|
}
|
|
export function samePath(a, b) {
|
|
return path.resolve(a) === path.resolve(b)
|
|
}
|
|
export function directory(name) {
|
|
return checkStat(name, function(n) {
|
|
return fs.statSync(n).isDirectory()
|
|
})
|
|
}
|
|
export function symbolicLink(name) {
|
|
return checkStat(name, function(n) {
|
|
return fs.lstatSync(n).isSymbolicLink()
|
|
})
|
|
}
|
|
export function windows() {
|
|
return os.platform() === 'win32'
|
|
}
|