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' }, + }) + } +})()