From 86404a0116f05671fd3ecc4fed145e227980ec41 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 14 Mar 2021 19:17:41 +0900 Subject: [PATCH] =?UTF-8?q?tool/generateDataConverters.ts:=20=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cf. a0a9e950acb7928386897375e5906154af752881 https://github.com/tokyo-metropolitan-gov/covid19/pull/3008#pullrequestreview-391636626 https://github.com/tokyo-metropolitan-gov/covid19/pull/3008#pullrequestreview-391639829 --- tool/generateDataConverters.ts | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tool/generateDataConverters.ts diff --git a/tool/generateDataConverters.ts b/tool/generateDataConverters.ts new file mode 100644 index 0000000000000..35841c00fe3eb --- /dev/null +++ b/tool/generateDataConverters.ts @@ -0,0 +1,42 @@ +import { promises as fs } from 'fs' +import { camelCase, upperFirst } from 'lodash' +// eslint-disable-next-line import/order +import * as path from 'path' +import * as quicktype from 'quicktype' +// TODO: rimraf は Node v12.0.0 でビルトインに追加されたので +// Node.js のバージョンアップに合わせて除去する +import { sync as rimrafSync } from 'rimraf' + +const basePath = path.dirname(__dirname) + +const inputPath = path.resolve(basePath, 'data') +const outputPath = path.resolve(basePath, 'libraries', 'data_converter') + +// ディレクトリ utils/data_converter を削除する +rimrafSync(outputPath) +;(async () => { + // ディレクトリ utils/data_converter を作成する + await fs.mkdir(outputPath, { recursive: true }) + // ディレクトリ data 内のファイル名の配列 + const dataFileNames = await fs.readdir(inputPath) + + for (let index = 0; index < dataFileNames.length; index++) { + const dataFileName = dataFileNames[index] + const dataFileBaseName = path.parse(dataFileName).name + const pascalFileName = upperFirst(camelCase(dataFileBaseName)) + + const converterFilePath = path.resolve( + outputPath, + `convert${pascalFileName}.ts` + ) + const dataFilePath = path.resolve(inputPath, dataFileName) + + await quicktype.main({ + lang: 'ts', + topLevel: pascalFileName, + out: converterFilePath, + src: [dataFilePath], + rendererOptions: { 'nice-property-names': 'true' }, + }) + } +})()