@magmonium/cli 0.8.17 → 0.8.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/bin/cli.js +2 -0
  2. package/bin/commands/assets.js +42 -0
  3. package/bin/commands/fix-i18n.js +27 -12
  4. package/bin/commands/i18n-baseline.js +33 -0
  5. package/codegen/index.js +320 -46
  6. package/package.json +1 -1
  7. package/src/compilers/logo-compiler.d.ts.map +1 -1
  8. package/src/compilers/logo-compiler.js +3 -1
  9. package/src/compilers/logo-compiler.js.map +1 -1
  10. package/src/compilers/web-component/lib/web-component-wrapper.d.ts.map +1 -1
  11. package/src/compilers/web-component/lib/web-component-wrapper.js +62 -48
  12. package/src/compilers/web-component/lib/web-component-wrapper.js.map +1 -1
  13. package/src/compilers/web-component/model/type.d.ts +1 -0
  14. package/src/compilers/web-component/model/type.d.ts.map +1 -1
  15. package/src/index.d.ts +1 -0
  16. package/src/index.d.ts.map +1 -1
  17. package/src/index.js +1 -0
  18. package/src/index.js.map +1 -1
  19. package/src/utils/ai-provider-translator.d.ts +10 -5
  20. package/src/utils/ai-provider-translator.d.ts.map +1 -1
  21. package/src/utils/ai-provider-translator.js +37 -31
  22. package/src/utils/ai-provider-translator.js.map +1 -1
  23. package/src/utils/app-logo-svg.d.ts +4 -3
  24. package/src/utils/app-logo-svg.d.ts.map +1 -1
  25. package/src/utils/app-logo-svg.js +30 -4
  26. package/src/utils/app-logo-svg.js.map +1 -1
  27. package/src/utils/i18n-fixer.d.ts +2 -3
  28. package/src/utils/i18n-fixer.d.ts.map +1 -1
  29. package/src/utils/i18n-fixer.js +24 -56
  30. package/src/utils/i18n-fixer.js.map +1 -1
  31. package/src/utils/i18n-gate.d.ts +28 -0
  32. package/src/utils/i18n-gate.d.ts.map +1 -0
  33. package/src/utils/i18n-gate.js +66 -0
  34. package/src/utils/i18n-gate.js.map +1 -0
package/bin/cli.js CHANGED
@@ -19,6 +19,7 @@ const refractorCommand = require('./commands/refractor');
19
19
  const fixI18nCommand = require('./commands/fix-i18n');
20
20
  const sanitizeI18nCommand = require('./commands/sanitize-i18n');
21
21
  const compileI18nCommand = require('./commands/compile-i18n');
22
+ const i18nBaselineCommand = require('./commands/i18n-baseline');
22
23
  const generateCommand = require('./commands/generate');
23
24
  const patchWcEnvCommand = require('./commands/patch-wc-env');
24
25
  const genCodeCommand = require('./commands/gen-code');
@@ -51,6 +52,7 @@ refractorCommand(program);
51
52
  fixI18nCommand(program);
52
53
  sanitizeI18nCommand(program);
53
54
  compileI18nCommand(program);
55
+ i18nBaselineCommand(program);
54
56
  generateCommand(program);
55
57
  patchWcEnvCommand(program);
56
58
  genCodeCommand(program);
@@ -15,11 +15,51 @@ const {
15
15
  DEFAULT_PATHS_CONFIG,
16
16
  WebComponentBuilder,
17
17
  collectDocs,
18
+ runI18nGate,
19
+ I18N_BASELINE_FILE,
18
20
  } = require('../../src/index.js');
19
21
 
20
22
  const fs = require('fs').promises;
21
23
  const path = require('path');
22
24
 
25
+ const I18N_GATE_WHY = {
26
+ new: 'not in baseline',
27
+ 'more-missing': 'more langs missing than baseline',
28
+ 'en-changed': 'en changed since baseline',
29
+ };
30
+
31
+ /**
32
+ * Stop the run when an i18n key lacks langs it may not lack: a key the
33
+ * baseline does not grandfather, one missing more than recorded, or one whose
34
+ * `en` changed since. Runs before cleaning, so a stopped run leaves the last
35
+ * good assets in place for a running app.
36
+ */
37
+ async function assertI18nComplete(root, verbose) {
38
+ const { failures, tightenable } = await runI18nGate(root, verbose);
39
+ if (tightenable > 0) {
40
+ console.log(
41
+ `ℹ️ ${tightenable} baseline key(s) translated further — npm run i18n:baseline to tighten ${I18N_BASELINE_FILE}`
42
+ );
43
+ }
44
+ if (failures.length === 0) return;
45
+
46
+ console.error(
47
+ `\n❌ ${failures.length} i18n key(s) missing translations — compile stopped:`
48
+ );
49
+ for (const failure of failures) {
50
+ const where = failure.file
51
+ ? path.relative(root, failure.file)
52
+ : `${failure.key} (no i18n yml)`;
53
+ console.error(
54
+ ` ${where}: ${failure.missingLangs.length} lang(s) missing, ${I18N_GATE_WHY[failure.reason]}`
55
+ );
56
+ }
57
+ console.error(
58
+ `\nFix: npm run translate:fix -- --key ${failures.map((f) => f.key).join(' ')}`
59
+ );
60
+ process.exit(1);
61
+ }
62
+
23
63
  /**
24
64
  * Create a YamlCompiler with validation rules for a specific asset type
25
65
  * @param {string} assetType - The asset type (Button, Card, etc.)
@@ -299,6 +339,8 @@ function assetsCommand(program) {
299
339
  );
300
340
  }
301
341
 
342
+ await assertI18nComplete(process.cwd(), options.verbose);
343
+
302
344
  // Function to resolve paths from config or command line options
303
345
  const resolvePathsConfig = () => {
304
346
  // Priority: config.paths > command line -p option > defaults
@@ -1,29 +1,45 @@
1
1
  const path = require('path');
2
+ const inquirer = require('inquirer');
3
+ const { FREELLM_BASE_URL, FREELLM_MODEL } = require('../../src/index.js');
4
+
5
+ /** The token from env, else asked for once — a terminal only, never on a dry run (which translates nothing). */
6
+ async function resolveAiToken(baseUrl, dryRun) {
7
+ const fromEnv = process.env.MAG_AI_TOKEN || process.env.FREELLMAPI_API_KEY;
8
+ if (fromEnv || dryRun || !process.stdin.isTTY) return fromEnv;
9
+ const { token } = await inquirer.prompt([
10
+ {
11
+ type: 'password',
12
+ name: 'token',
13
+ mask: '*',
14
+ message: `🔑 AI provider token for ${baseUrl}:`,
15
+ },
16
+ ]);
17
+ return token.trim() || undefined;
18
+ }
2
19
 
3
20
  function fixI18nCommand(program) {
4
21
  program
5
22
  .command('fix-i18n')
6
23
  .description(
7
- 'Scan templates and TypeScript for missing i18n keys and auto-translate them. An AI provider (the OpenAI-compatible endpoint m-one-ui uses, or a llama-server if one answers) translates first; web APIs fill what it misses.'
24
+ 'Scan templates and TypeScript for missing i18n keys and auto-translate them. FreeLLMAPI (or another OpenAI-compatible endpoint m-one-ui uses) is the only translator: every language of a key in one ask, existing translations re-done.'
8
25
  )
9
26
  .option('--dry-run', 'Preview what would be created/deleted without writing files', false)
10
27
  .option('-v, --verbose', 'Log per-language translation results', false)
11
28
  .option('--root <path>', 'Workspace root (defaults to cwd)', process.cwd())
12
29
  .option('--limit <n>', 'Process at most N keys per run (for batching large workspaces)', parseInt)
13
30
  .option('--key <keys...>', 'Only these keys (i18n file names, e.g. nav-delete-references-left); skips stale-copy cleanup')
14
- .option('--ai-base-url <url>', 'AI provider base URL, ending in /v1 (env MAG_AI_BASE_URL); token from env MAG_AI_TOKEN', process.env.MAG_AI_BASE_URL)
15
- .option('--ai-model <id>', 'AI provider model id (env MAG_AI_MODEL)', process.env.MAG_AI_MODEL)
16
- .option('--llama-host <url>', 'llama-server tried when no AI provider is given', 'http://127.0.0.1:8080')
31
+ .option('--ai-base-url <url>', 'AI provider base URL, ending in /v1 (env MAG_AI_BASE_URL); token from env MAG_AI_TOKEN or FREELLMAPI_API_KEY, else asked for', process.env.MAG_AI_BASE_URL || FREELLM_BASE_URL)
32
+ .option('--ai-model <id>', 'AI provider model id (env MAG_AI_MODEL)', process.env.MAG_AI_MODEL || FREELLM_MODEL)
33
+ .option('--ai-reasoning-effort <level>', 'reasoning_effort sent to the provider (env MAG_AI_REASONING_EFFORT); unset sends none', process.env.MAG_AI_REASONING_EFFORT)
17
34
  .action(async (options) => {
18
35
  const root = path.resolve(options.root);
19
36
 
20
- if (options.aiBaseUrl && !options.aiModel) {
21
- console.error('fix-i18n failed: --ai-base-url needs --ai-model (or MAG_AI_MODEL)');
22
- process.exit(1);
23
- }
24
- const aiProvider = options.aiBaseUrl
25
- ? { baseUrl: options.aiBaseUrl, model: options.aiModel, token: process.env.MAG_AI_TOKEN }
26
- : undefined;
37
+ const aiProvider = {
38
+ baseUrl: options.aiBaseUrl,
39
+ model: options.aiModel,
40
+ token: await resolveAiToken(options.aiBaseUrl, options.dryRun),
41
+ reasoningEffort: options.aiReasoningEffort,
42
+ };
27
43
 
28
44
  try {
29
45
  const { I18nFixer } = require('../../src/index.js');
@@ -33,7 +49,6 @@ function fixI18nCommand(program) {
33
49
  dryRun: options.dryRun,
34
50
  verbose: options.verbose,
35
51
  limit: options.limit,
36
- llamaHost: options.llamaHost,
37
52
  aiProvider,
38
53
  keys: options.key,
39
54
  });
@@ -0,0 +1,33 @@
1
+ const path = require('path');
2
+
3
+ function i18nBaselineCommand(program) {
4
+ program
5
+ .command('i18n-baseline')
6
+ .description(
7
+ 'Record every i18n key still missing langs into i18n-baseline.json — the only incomplete keys `assets` lets through. Run after translate:fix to tighten it.'
8
+ )
9
+ .option('--root <path>', 'Workspace root (defaults to cwd)', process.cwd())
10
+ .option('-v, --verbose', 'Verbose logging', false)
11
+ .action(async (options) => {
12
+ const {
13
+ I18nFixer,
14
+ buildI18nBaseline,
15
+ readI18nBaseline,
16
+ writeI18nBaseline,
17
+ I18N_BASELINE_FILE,
18
+ } = require('../../src/index.js');
19
+ const root = path.resolve(options.root);
20
+
21
+ const before = Object.keys(await readI18nBaseline(root)).length;
22
+ const baseline = buildI18nBaseline(
23
+ await new I18nFixer(root, options.verbose).scan()
24
+ );
25
+ await writeI18nBaseline(root, baseline);
26
+
27
+ console.log(
28
+ `${I18N_BASELINE_FILE}: ${before} → ${Object.keys(baseline).length} incomplete key(s)`
29
+ );
30
+ });
31
+ }
32
+
33
+ module.exports = i18nBaselineCommand;