您的位置:首页技术文章
文章详情页

javascript - js 自动根据配置文件生成目录结构

【字号: 日期:2023-08-28 15:03:49浏览:52作者:猪猪

问题描述

目前在初始化组件库,为了灵活,需要一个快速的初始化目录结构。目前用的angular2目录结构的配置文件可能如下

+ grid - col - grid - row

这样希望能够生成grid.config.tsgrid.module.tsindex.tsSTATION.mdcol.component.ts,col.component.html,col.component.scss,grid.component.ts,...

自己也在github找了filemap跟baya,filemap测试了,已经不能使用了,baya文件夹可以生成,文件不能生成

自己可能打算是把模板文件做成json,用gulp去读,不过没有tree树这么直观

有没有大神有解决办法的,或者对我的解决思路有建议的

问题解答

回答1:

做了一个浅显的版本,对于多层文件目录的结构还没有考虑好,暂时还没用递归

const gulp = require(’gulp’);const fs = require(’fs’);const path = require(’path’);const mkdirp = require(’mkdirp’);function writeFile(i) { if (!fs.existsSync(i)) { fs.writeFile(i, ’’, ’utf-8’); }}function pack(i) { return [’index.ts’, ’STATION.md’].concat(i + ’.config.ts’, i + ’.module.ts’);}function createList(path) { return [].concat(path + ’.component.ts’, path + ’.component.html’, path + ’.component.scss’)}function splitFlag(value, flag) { return value.split(flag)[1].replace(/s+/g, '');}gulp.task(’try’, function () { const paths = path.join(__dirname, './tempalte'); fs.readFile(paths, ’utf-8’, function (err, data) { if (err) throw err; const array = data.split(’n’); array.forEach(f![图片描述][1]unction (i) { if (i.indexOf(’+’) > -1) {const folder = splitFlag(i, ’+’);mkdirp(folder);pack(folder).forEach(function (item) { writeFile(folder + ’/’ + item);}) } }); var parent; array.forEach(function (i) { if (i.indexOf(’+’) > -1) {parent = splitFlag(i, ’+’); } else {const pa = parent + ’/’ + splitFlag(i, ’-’);createList(pa).forEach(function (item) { writeFile(item);}) } }); });});

javascript - js 自动根据配置文件生成目录结构

回答2:

自己写一个 Node 辅助函数,逐级读取配置文件,生成需要的文件和文件夹就可以啦。就递归一下下。

回答3:

自己用fs模块写一个嘛,不要偷懒

标签: JavaScript
相关文章: