javascript - js 自動根據配置文件生成目錄結構
問題描述
目前在初始化組件庫,為了靈活,需要一個快速的初始化目錄結構。目前用的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);}) } }); });});
自己寫一個 Node 輔助函數,逐級讀取配置文件,生成需要的文件和文件夾就可以啦。就遞歸一下下。
回答3:自己用fs模塊寫一個嘛,不要偷懶
相關文章:
1. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. 視頻文件不能播放,怎么辦?3. mysql - jdbc的問題4. python - 我在使用pip install -r requirements.txt下載時,為什么部分能下載,部分不能下載5. html5 - H5做的手機分享頁微信更新后,分享出去不再默認顯示第一個圖 作為縮略圖6. python - 編碼問題求助7. linux - python 抓取公眾號文章遇到驗證問題8. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處9. node.js - nodejs開發中常用的連接mysql的庫10. 網頁爬蟲 - python 爬取網站 并解析非json內容
