i18next / i18next-parser

Parse your code to extract translation keys/values and manage your catalog files
MIT License
486 stars 198 forks source link

throwing an error when using in a pnpm monorepo #1040

Open sibelius opened 3 months ago

sibelius commented 3 months ago

🐛 Bug Report

When running parser inside a monorepo, it is throwing an error

NODE_OPTIONS='--enable-source-maps --trace-warnings --trace-deprecation' i18next --config i18next-parser.config.js

i18next Parser
--------------
  Input:
Output: packages/i18n/src/locales/$LOCALE.json

node:events:497
throw er; // Unhandled 'error' event
^

Error: ENOENT: no such file or directory, stat 'app/packages/testutils/node_modules/.bin/nanoid'
Emitted 'error' event on Transform instance at:
  at ReadableState.afterDestroy (app/node_modules/streamx/index.js:500:19)
at Transform._destroy (app/node_modules/streamx/index.js:636:5)
at ReadableState.updateNonPrimary (app/node_modules/streamx/index.js:393:16)
at ReadableState.update (app/node_modules/streamx/index.js:374:71)
at ReadableState.updateReadNT (app/node_modules/streamx/index.js:543:10)
at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {

To Reproduce

try to run this inside a pnpm monorepo

module.exports = {
  input: [
    'packages/**/src/**/*.{ts,tsx}',
    '!packages/**/*.spec.{ts,tsx}',
    '!packages/**/*.graphql.{ts,tsx}',
    '!**/node_modules/**/*',
    '!**/.bin/**',
    // Use ! to filter out files or directories
    // '!app/**/*.spec.{js,jsx}',
    // '!app/i18n/**',
    // '!**/node_modules/**',
  ],
  output: 'packages/i18n/src/locales/$LOCALE.json',

  contextSeparator: '_',
  pluralSeparator: '_',
  namespaceSeparator: false,
  keySeparator: false,

  // resetDefaultValueLocale: 'en',
  resetDefaultValueLocale: null,
  verbose: true,
  keepRemoved: false,
  failOnWarnings: false,
  failOnUpdate: false,
  createOldCatalogs: false,

  locales: ['pt-BR', 'en', 'es'],

  indentation: 2,
  lineEnding: 'lf',

  defaultNamespace: 'translation',
  defaultValue: (lng, ns, key) => {
    if (process.env.I18N_KEY_DEFAULT === 'true') {
      return key;
    }

    if (lng === 'en') {
      // Return key as the default value for English language
      return key;
    }

    // Return the string '__NOT_TRANSLATED__' for other languages
    return '__STRING_NOT_TRANSLATED__';
  },

  lexers: {
    ts: [
      {
        lexer: 'JavascriptLexer',
        functions: ['t', 'context.t'],
      },
    ],
    tsx: [
      {
        lexer: 'JsxLexer',
        functions: ['t', 'context.t'],
      },
    ],
  },

  // sort: true,
  sort: (a, b) => {
    if (a > b) {
      return 1;
    }

    if (a < b) {
      return -1;
    }

    return 0;
  },
};

Expected behavior

A clear and concise description of what you expected to happen.

it should work

// Paste the expected results here

Your Environment

bobbonius commented 3 months ago

Experiencing a similar error in a monorepo

sibelius commented 3 months ago

here is a version that works well for now

import fs from 'fs/promises';
import { globSync } from 'glob';
import { transform as Transform } from 'i18next-parser';

import config from '../../i18next-parser.config';

const transform = new Transform({
  ...config,
});

const pattern = config.input.find((s) => !s.startsWith('!'));
const ignore = config.input
  .filter((s) => s.startsWith('!'))
  .map((s) => s.replace('!', ''));

(async () => {
  const files = globSync(pattern, { ignore });

  transform.on('data', (file) => {
    // eslint-disable-next-line
    console.log(file.path);
    fs.writeFile(file.path, file.contents);
  });

  await Promise.all(
    files.map((path) =>
      transform._transform({ path, isBuffer: () => false }, 'utf8', () => {}),
    ),
  );

  transform._flush(() => {});
})();