From 24bd1509677023ddd8a749bb7fbfe876c7627b48 Mon Sep 17 00:00:00 2001 From: syuilo <4439005+syuilo@users.noreply.github.com> Date: Thu, 4 Dec 2025 16:49:25 +0900 Subject: refactor(backend): 変換後.config.jsonに統一するように+修正など (#16929) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip * Update config.ts * wip * convertは元ファイルを変更するようなニュアンスを若干感じるのでcompileに改名 * wip * Update package.json * Revert "Update package.json" This reverts commit e5c28023168f4631dc6b36a14b35cfddbad1fac0. * wip * wip * 謎 * clean up * wip * wip * Revert "wip" This reverts commit 3aa25ac7cf337d57412308e63d8f54e2536b0f7f. * wip * wip * Update dummy.yml * wip * Update compile_config.js * Update compile_config.js * wip * Revert "wip" This reverts commit fd78e097c65f747962e7a411938a0e67538ed347. * Update dummy.yml * Update compile_config.js --- packages/backend/scripts/compile_config.js | 54 +++++++++++++++++++++++++++ packages/backend/scripts/convert_config.js | 59 ------------------------------ 2 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 packages/backend/scripts/compile_config.js delete mode 100644 packages/backend/scripts/convert_config.js (limited to 'packages/backend/scripts') diff --git a/packages/backend/scripts/compile_config.js b/packages/backend/scripts/compile_config.js new file mode 100644 index 0000000000..e78fa3dc9f --- /dev/null +++ b/packages/backend/scripts/compile_config.js @@ -0,0 +1,54 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +/** + * YAMLファイルをJSONファイルに変換するスクリプト + * ビルド前に実行し、ランタイムにjs-yamlを含まないようにする + */ + +import fs from 'node:fs'; +import { resolve, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import yaml from 'js-yaml'; + +const _filename = fileURLToPath(import.meta.url); +const _dirname = dirname(_filename); + +const configDir = resolve(_dirname, '../../../.config'); +const OUTPUT_PATH = resolve(_dirname, '../../../built/.config.json'); + +// TODO: yamlのパースに失敗したときのエラーハンドリング + +/** + * YAMLファイルをJSONファイルに変換 + * @param {string} ymlPath - YAMLファイルのパス + */ +function yamlToJson(ymlPath) { + if (!fs.existsSync(ymlPath)) { + console.warn(`YAML file not found: ${ymlPath}`); + return; + } + + console.log(`${ymlPath} → ${OUTPUT_PATH}`); + + const yamlContent = fs.readFileSync(ymlPath, 'utf-8'); + const jsonContent = yaml.load(yamlContent); + if (!fs.existsSync(dirname(OUTPUT_PATH))) { + fs.mkdirSync(dirname(OUTPUT_PATH), { recursive: true }); + } + fs.writeFileSync(OUTPUT_PATH, JSON.stringify({ + '_NOTE_': 'This file is auto-generated from YAML file. DO NOT EDIT.', + ...jsonContent, + }), 'utf-8'); +} + +if (process.env.MISSKEY_CONFIG_YML) { + const customYmlPath = resolve(configDir, process.env.MISSKEY_CONFIG_YML); + yamlToJson(customYmlPath); +} else { + yamlToJson(resolve(configDir, process.env.NODE_ENV === 'test' ? 'test.yml' : 'default.yml')); +} + +console.log('Configuration compiled ✓'); diff --git a/packages/backend/scripts/convert_config.js b/packages/backend/scripts/convert_config.js deleted file mode 100644 index e93baa409a..0000000000 --- a/packages/backend/scripts/convert_config.js +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SPDX-FileCopyrightText: syuilo and misskey-project - * SPDX-License-Identifier: AGPL-3.0-only - */ - -/** - * YAMLファイルをJSONファイルに変換するスクリプト - * ビルド前に実行し、ランタイムにjs-yamlを含まないようにする - */ - -import fs from 'node:fs'; -import { resolve, dirname } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import yaml from 'js-yaml'; - -const _filename = fileURLToPath(import.meta.url); -const _dirname = dirname(_filename); - -const configDir = resolve(_dirname, '../../../.config'); - -/** - * YAMLファイルをJSONファイルに変換 - * @param {string} ymlPath - YAMLファイルのパス - * @param {string} jsonPath - JSONファイルの出力パス - */ -function convertYamlToJson(ymlPath, jsonPath) { - if (!fs.existsSync(ymlPath)) { - console.log(`skipped: ${ymlPath} is not found`); - return; - } - - const yamlContent = fs.readFileSync(ymlPath, 'utf-8'); - const jsonContent = yaml.load(yamlContent); - fs.writeFileSync(jsonPath, JSON.stringify({ - '_NOTE_': 'This file is auto-generated from YAML file. DO NOT EDIT.', - ...jsonContent, - }), 'utf-8'); - console.log(`✓ ${ymlPath} → ${jsonPath}`); -} - -// default.yml と test.yml を変換 -convertYamlToJson( - resolve(configDir, 'default.yml'), - resolve(configDir, 'default.json'), -); - -convertYamlToJson( - resolve(configDir, 'test.yml'), - resolve(configDir, 'test.json'), -); - -// MISSKEY_CONFIG_YML 環境変数が指定されている場合も変換 -if (process.env.MISSKEY_CONFIG_YML) { - const customYmlPath = resolve(configDir, process.env.MISSKEY_CONFIG_YML); - const customJsonPath = customYmlPath.replace(/\.ya?ml$/i, '.json'); - convertYamlToJson(customYmlPath, customJsonPath); -} - -console.log('Configuration compiled'); -- cgit v1.2.3-freya