@modyo/widget
Version:
is a widget boilerplater
83 lines (75 loc) • 1.83 kB
JavaScript
import fs from 'fs-extra'
import rimraf from 'rimraf-promise'
const readFile = (path) => {
let ret = ''
try {
ret = fs.readFileSync(path, { encoding: 'utf8' })
} catch (e) {
ret = ''
}
return ret
}
const removeDir = async (path) => rimraf(path)
const exists = (path) => {
let ret = true
try {
fs.accessSync(path, fs.constants.F_OK)
} catch (e) {
ret = false
}
return ret
}
const writeFile = (path, content) => {
let ret = true
try {
fs.writeFileSync(path, content, { encoding: 'utf8' })
} catch (e) {
ret = false
}
return ret
}
const mkdir = (path) => {
let ret = true
try {
fs.mkdirSync(path)
} catch (e) {
ret = false
}
return ret
}
const ls = (path) => {
let ret = []
try {
ret = fs.readdirSync(path)
} catch (e) {
ret = false
}
return ret
}
const move = async (srcpath, dstpath) => {
return fs.rename(srcpath, dstpath)
}
const slugify = (string) => {
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;'
const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnooooooooprrsssssttuuuuuuuuuwxyyzzz------'
const p = new RegExp(a.split('').join('|'), 'g')
return string.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w-]+/g, '') // Remove all non-word characters
.replace(/--+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}
export default exists
export {
writeFile,
removeDir,
exists,
mkdir,
readFile,
ls,
move,
slugify
}