javascript - js 自動根據(jù)配置文件生成目錄結(jié)構(gòu)
問題描述
目前在初始化組件庫,為了靈活,需要一個快速的初始化目錄結(jié)構(gòu)。目前用的angular2目錄結(jié)構(gòu)的配置文件可能如下
+ 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測試了,已經(jīng)不能使用了,baya文件夾可以生成,文件不能生成
自己可能打算是把模板文件做成json,用gulp去讀,不過沒有tree樹這么直觀
有沒有大神有解決辦法的,或者對我的解決思路有建議的
問題解答
回答1:做了一個淺顯的版本,對于多層文件目錄的結(jié)構(gòu)還沒有考慮好,暫時還沒用遞歸
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);}) } }); });});
自己寫一個 Node 輔助函數(shù),逐級讀取配置文件,生成需要的文件和文件夾就可以啦。就遞歸一下下。
回答3:自己用fs模塊寫一個嘛,不要偷懶
相關(guān)文章:
1. 我的Apache卡在這里不動了怎么辦?2. 在MySQL中新增字段時,報錯??3. 老哥們求助啊4. python - 模擬滑動驗(yàn)證碼,有源碼,求解5. java - 安卓電視盒子取得了root權(quán)限但是不能安裝第三方應(yīng)用,請問該怎么辦?6. javascript - js 寫一個正則 提取文本中的數(shù)據(jù)7. npm鏡像站全新上線8. javascript - vue-router怎么不能實(shí)現(xiàn)跳轉(zhuǎn)呢9. css3 - 請問一下在移動端CSS布局布局中通常需要用到哪些元素,屬性?10. html5 - angularjs中外部模版加載無法使用
