UNPKG

ice.fo.utils

Version:

85 lines (61 loc) 2.08 kB
import fs from 'fs' import path from 'path' import glob from 'glob' export const COMPONENT_FILE_PATTERN = '**/[A-Z][a-zA-z0-9]+.vue' export function normalizePath (value) { // Korean Windows PC has special sign as path slash return value.replace(new RegExp('\\' + path.sep, 'g'), '/') } export function scanVueFiles (dir) { dir = normalizePath(dir) const files = glob.sync(path.join(dir, '**/*.vue')) return files } export function scanLayoutsDirectory (layoutsDir) { layoutsDir = normalizePath(layoutsDir) return scanVueFiles(layoutsDir).map(i => i.replace(layoutsDir + '/', '').replace('.vue', '')) } export function scanVueComponentsDirectory (dir) { dir = normalizePath(dir) const matchPattern = new RegExp('^' + COMPONENT_FILE_PATTERN.substring(COMPONENT_FILE_PATTERN.lastIndexOf('/') + 1), '') const files = scanVueFiles(dir) const result = files.map(i => path.parse(i).base).filter(i => matchPattern.test(i)).map(i => i.substring(0, i.lastIndexOf('.'))) return result } export function scanCssFiles (dir) { dir = normalizePath(dir) const files = glob.sync(path.join(dir, '**/*.{css,scss}')) return files } export function scanSubdirectories (dir, recursive = false) { dir = normalizePath(dir) const dirs = glob.sync(path.join(dir, '*/')).map(normalizePath) return dirs } export function writeFileSyncRecursive (filename, content, charset) { let filepath = normalizePath(filename) let root = '' if (filepath[0] === '/') { root = '/' filepath = filepath.slice(1) } else if (filepath[1] === ':') { root = filepath.slice(0, 3) // c:\ filepath = filepath.slice(3) } const folders = filepath.split('/').slice(0, -1) folders.reduce( (acc, folder) => { const folderPath = acc + folder + '/' if (!fs.existsSync(folderPath)) { fs.mkdirSync(folderPath) } return folderPath }, root, // first 'acc', important ) // write file fs.writeFileSync(root + filepath, content, charset) } export function deleteFile (path) { fs.unlink(path, function () {}) }