Skip to content

Commit

Permalink
support homey app translate --file
Browse files Browse the repository at this point in the history
  • Loading branch information
WeeJeWel committed Jul 16, 2024
1 parent bec776c commit 6db2cbc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
5 changes: 5 additions & 0 deletions bin/cmds/app/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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({
Expand Down
69 changes: 40 additions & 29 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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...`);

Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 6db2cbc

Please sign in to comment.