Skip to content

Commit

Permalink
Throw an error when sku translations is run without languages con…
Browse files Browse the repository at this point in the history
…figured, suggest `vocab` CLI if a vocab config file is detected (#1090)
  • Loading branch information
askoufis authored Nov 20, 2024
1 parent c8bff6f commit 43e641e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .changeset/hot-fans-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'sku': patch
---

`sku translations`: Throw an error when `languages` has not been configured

Previously, if `languages` was not configured, the `sku translations` command would log a message and continue to run the command, which would eventually error due to the lack of a vocab config (generated from the `languages` config).
We now throw an error sooner to make it clear that the `languages` configuration is required to run any `sku translations` commands.
5 changes: 5 additions & 0 deletions .changeset/sixty-bats-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sku': minor
---

`sku translations`: Suggest using the `vocab` CLI when `languages` is not configured and a vocab config file is detected
5 changes: 5 additions & 0 deletions .changeset/sour-terms-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sku': minor
---

`sku translations`: When `languages` is configured in sku config _and_ a vocab config file is found, a warning will be shown telling the user that the vocab config file will be ignored
28 changes: 20 additions & 8 deletions packages/sku/scripts/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
watch,
'delete-unused-keys': deleteUnusedKeys,
} = require('../config/args');
const { compile, validate } = require('@vocab/core');
const { compile, validate, resolveConfig } = require('@vocab/core');
const { push, pull } = require('@vocab/phrase');
const { getVocabConfig } = require('../config/vocab/vocab');

Expand Down Expand Up @@ -40,14 +40,26 @@ const log = (message) => console.log(chalk.cyan(message));
(async () => {
const translationSubCommand = commandArguments[0];

const vocabConfig = getVocabConfig();
const vocabConfigFromSkuConfig = getVocabConfig();
const resolvedVocabConfig = await resolveConfig();

if (!vocabConfig) {
if (vocabConfigFromSkuConfig && resolvedVocabConfig) {
console.log(
'No languages configured. Please set languages in before running translation commands',
`Ignoring vocab config file in ${resolvedVocabConfig.projectRoot}. Sku only supports multi-language applications by configuring the "languages" property in your sku config.`,
);
}

if (!vocabConfigFromSkuConfig) {
let errorMessage =
'No "languages" configured. Please configure "languages" in your sku config before running translation commands.';

if (resolvedVocabConfig) {
errorMessage += `\nIt looks like you have a vocab config file in ${resolvedVocabConfig.projectRoot}. Perhaps you intended to run "vocab ${translationSubCommand}" instead?`;
}

throw new Error(errorMessage);
}

if (!translationSubCommands.includes(translationSubCommand)) {
throw new Error(
`Unknown sub-command ${translationSubCommand}. Available options are ${translationSubCommands.join(
Expand All @@ -64,7 +76,7 @@ const log = (message) => console.log(chalk.cyan(message));
log('Watching for changes to translations');
}

await compile({ watch }, vocabConfig);
await compile({ watch }, vocabConfigFromSkuConfig);

if (!watch) {
log('Successfully compiled translations');
Expand All @@ -73,23 +85,23 @@ const log = (message) => console.log(chalk.cyan(message));

if (translationSubCommand === 'validate') {
log('Validating translations...');
await validate(vocabConfig);
await validate(vocabConfigFromSkuConfig);
log('Successfully validated translations');
}

if (translationSubCommand === 'push') {
const branch = await ensureBranch();

log('Pushing translations to Phrase...');
await push({ branch, deleteUnusedKeys }, vocabConfig);
await push({ branch, deleteUnusedKeys }, vocabConfigFromSkuConfig);
log('Successfully pushed translations to Phrase');
}

if (translationSubCommand === 'pull') {
const branch = await ensureBranch();

log('Pulling translations from Phrase...');
await pull({ branch }, vocabConfig);
await pull({ branch }, vocabConfigFromSkuConfig);
log('Successfully pulled translations from Phrase');
}
} catch (e) {
Expand Down

0 comments on commit 43e641e

Please sign in to comment.