From 6db2cbcfcec423cd3c9f9084bfc51dda6e5286e8 Mon Sep 17 00:00:00 2001 From: Emile Nijssen Date: Tue, 16 Jul 2024 15:45:21 +0200 Subject: [PATCH] support `homey app translate --file` --- bin/cmds/app/translate.js | 5 +++ lib/App.js | 69 +++++++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/bin/cmds/app/translate.js b/bin/cmds/app/translate.js index 6bc22609..d9f9ccaa 100644 --- a/bin/cmds/app/translate.js +++ b/bin/cmds/app/translate.js @@ -22,6 +22,10 @@ exports.builder = yargs => { default: 'gpt-4o', type: 'string', description: 'OpenAI model to use.', + }) + .option('file', { + type: 'string', + description: 'Absolute path to a single file to translate, instead of automatically translating the entire folder.', }); }; exports.handler = async yargs => { @@ -31,6 +35,7 @@ exports.handler = async yargs => { languages: yargs.languages.split(',').map(lang => lang.trim()), apiKey: yargs.apiKey, model: yargs.model, + file: yargs.file, }); await app.preprocess(); await app.validate({ diff --git a/lib/App.js b/lib/App.js index 170ad9ac..c2923f18 100644 --- a/lib/App.js +++ b/lib/App.js @@ -2922,6 +2922,7 @@ $ sudo systemctl restart docker languages = [], apiKey = process.env.OPENAI_API_KEY, model = 'gpt-4o', + file = null, }) { // Validate languages if (languages.includes('en')) { @@ -2942,42 +2943,50 @@ You can create an API Key on https://platform.openai.com/api-keys.`); // Find & translate all .json files recursively const jsonFiles = new Set(); - if (App.hasHomeyCompose({ appPath: this.path })) { - jsonFiles.add(path.join(this.path, '.homeycompose', 'app.json')); + if (file) { + if (!file.endsWith('.json')) { + throw new Error('--file only supports a .json file.'); + } + + jsonFiles.add(file); } else { - jsonFiles.add(path.join(this.path, 'app.json')); - } + if (App.hasHomeyCompose({ appPath: this.path })) { + jsonFiles.add(path.join(this.path, '.homeycompose', 'app.json')); + } else { + jsonFiles.add(path.join(this.path, 'app.json')); + } - const walkSync = async dir => { - const files = await fs.promises.readdir(dir); - await Promise.all(files.map(async file => { - const filePath = `${dir}/${file}`; - const fileStats = await fs.promises.stat(filePath); + const walkSync = async dir => { + const files = await fs.promises.readdir(dir); + await Promise.all(files.map(async file => { + const filePath = `${dir}/${file}`; + const fileStats = await fs.promises.stat(filePath); + + if (fileStats.isDirectory()) { + if (file === 'locales') return; + if (file === 'node_modules') return; + if (file === '.homeybuild') return; + if (file === '.homeycompose') { + jsonFiles.add(path.normalize(`${filePath}/app.json`)); + } - if (fileStats.isDirectory()) { - if (file === 'locales') return; - if (file === 'node_modules') return; - if (file === '.homeybuild') return; - if (file === '.homeycompose') { - jsonFiles.add(path.normalize(`${filePath}/app.json`)); + await walkSync(filePath); } - await walkSync(filePath); - } - - if (fileStats.isFile() && filePath.endsWith('.json')) { - if (file.startsWith('.')) return; - if (file === 'app.json') return; - if (file === 'env.json') return; - if (file === 'package.json') return; - if (file === 'package-lock.json') return; + if (fileStats.isFile() && filePath.endsWith('.json')) { + if (file.startsWith('.')) return; + if (file === 'app.json') return; + if (file === 'env.json') return; + if (file === 'package.json') return; + if (file === 'package-lock.json') return; - jsonFiles.add(path.normalize(filePath)); - } - })); - }; + jsonFiles.add(path.normalize(filePath)); + } + })); + }; - await walkSync(this.path); + await walkSync(this.path); + } Log(`Found ${jsonFiles.size} JSON files to translate...`); @@ -3016,6 +3025,8 @@ ${content}`, }); })); + if (file) return; + // Translate README.txt const readme = await fs.promises.readFile(path.join(this.path, 'README.txt'), 'utf8'); await Promise.all(Object.values(languages).map(async language => {