javascript - js 自動(dòng)根據(jù)配置文件生成目錄結(jié)構(gòu)
問題描述
目前在初始化組件庫,為了靈活,需要一個(gè)快速的初始化目錄結(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樹這么直觀
有沒有大神有解決辦法的,或者對(duì)我的解決思路有建議的
問題解答
回答1:做了一個(gè)淺顯的版本,對(duì)于多層文件目錄的結(jié)構(gòu)還沒有考慮好,暫時(shí)還沒用遞歸
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);}) } }); });});
自己寫一個(gè) Node 輔助函數(shù),逐級(jí)讀取配置文件,生成需要的文件和文件夾就可以啦。就遞歸一下下。
回答3:自己用fs模塊寫一個(gè)嘛,不要偷懶
相關(guān)文章:
1. 哪位大神知道MySql怎么修改多行多列的數(shù)據(jù)?求指點(diǎn)2. 這是什么情況???3. 編輯管理員信息時(shí),為什么沒有修改過的內(nèi)容會(huì)為空?4. phpadmin的數(shù)據(jù)庫,可以設(shè)置自動(dòng)變化時(shí)間的變量嗎?就是不需要接收時(shí)間數(shù)據(jù),自動(dòng)變化5. mysql - thinkphp5 在MAC電腦本地正常,部署LINUX服務(wù)器之后,模型不存在6. 老哥們求助啊7. javascript - 百度圖片切換圖片時(shí)url會(huì)改變,但無刷新,沒用hash,IE8也支持,請(qǐng)問是用了什么技術(shù)?8. PHP類封裝的插入數(shù)據(jù),總是插入不成功,返回false;9. APP上傳到電腦服務(wù)器,出現(xiàn)數(shù)據(jù)上傳不完整的問題10. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時(shí)間會(huì)消失是什么情況?
