Created
April 18, 2020 13:42
-
-
Save 35d/456e3fd944d0690518360b3d3f592883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// json をマージするスクリプト | |
const fs = require('fs'); | |
const path = require('path'); | |
const outputFileName = 'db.json'; | |
// ルートパスを設定 | |
const root = path.resolve('./', 'mock'); | |
const fixturesPath = root + '/fixtures'; | |
const json = {}; | |
// 古いファイルを一度削除 | |
try { | |
fs.unlinkSync(root + '/' + outputFileName); | |
} catch (error) { | |
console.log(error) | |
} | |
// json ファイルの生成 | |
fs.readdirSync(fixturesPath).reduce((api, file) => { | |
if (api === undefined) api = {}; | |
if (path.extname(file) == '.json') { | |
const endpoint = path.basename(file, path.extname(file)); | |
if (api[endpoint] === undefined) api[endpoint] = {}; | |
json[endpoint] = JSON.parse(fs.readFileSync(fixturesPath + '/' + file, 'utf-8')); | |
} | |
}, {}); | |
// 書き込み | |
fs.writeFile(root + '/' + outputFileName, JSON.stringify(json), function(err) { | |
if (err) throw err; | |
console.log('=== Create ' + outputFileName); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment