logic-helper
Version:
This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
45 lines (43 loc) • 1.28 kB
JavaScript
const fs = require('fs');
const path = require('path');
const excluding = ['node_modules', 'dist'];
const excludingRef = /^\.[a-zA-Z_]+/;
const chalk = require('chalk');
exports.readDirectory = function readDirectory(dirPath) {
const files = fs.readdirSync(dirPath);
const result = {};
for (const file of files) {
const filePath = path.join(dirPath, file);
const stats = fs.statSync(filePath);
if (excluding.includes(file) || excludingRef.test(file)) {
continue;
}
if (stats.isDirectory()) {
result[file] = readDirectory(filePath);
} else {
result[file] = null;
}
}
return result;
}
exports.getRelPath = function getRelPath(filePath) {
const rootPath = path.resolve(process.cwd(), '.logic');
const outputPath = path.resolve(process.cwd(), filePath);
const dirPath = path.dirname(outputPath);
if (dirPath === '/') {
return './.logic'
}
let relativePath = path.relative(dirPath, rootPath);
if (relativePath === '.logic') {
relativePath = './.logic'
}
return relativePath;
}
//输出文件
exports.writeFile = function writeFile(filePath, content) {
try {
fs.writeFileSync(path.resolve(process.cwd(), filePath), content);
} catch (e) {
console.log('写入错误: ', chalk.red(e));
}
}