Skip to content

Instantly share code, notes, and snippets.

@35d
Created April 18, 2020 13:42
Show Gist options
  • Save 35d/456e3fd944d0690518360b3d3f592883 to your computer and use it in GitHub Desktop.
Save 35d/456e3fd944d0690518360b3d3f592883 to your computer and use it in GitHub Desktop.
// 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