@next-box/i18n 0.0.8 → 0.0.9

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@next-box/i18n",
3
3
  "description": "A type-safe content resolver with content path auto-complete and value preview.",
4
- "version": "0.0.8",
4
+ "version": "0.0.9",
5
5
  "author": "miami-man",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/badbatch/next-box",
@@ -0,0 +1 @@
1
+ export const CONFIG_FILENAME = '.i18n-macro.config.json';
@@ -0,0 +1,17 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { CONFIG_FILENAME } from '#constants.ts';
3
+ import { type MacrosConfig } from '#types.ts';
4
+
5
+ export const loadConfig = (): Partial<MacrosConfig> => {
6
+ let config: Partial<MacrosConfig> = {};
7
+
8
+ try {
9
+ // JSON.parse returns an any time
10
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
11
+ config = JSON.parse(readFileSync(CONFIG_FILENAME, { encoding: 'utf8' })) as Partial<MacrosConfig>;
12
+ } catch {
13
+ // no catch
14
+ }
15
+
16
+ return config;
17
+ };
@@ -1,4 +1,10 @@
1
- import { md } from '#macros.ts';
1
+ import { jest } from '@jest/globals';
2
+
3
+ jest.unstable_mockModule('#helpers/loadConfig.ts', () => ({
4
+ loadConfig: jest.fn().mockReturnValue({ markdownDir: './packages/i18n/src' }),
5
+ }));
6
+
7
+ const { md } = await import('#macros.ts');
2
8
 
3
9
  describe('md', () => {
4
10
  describe('when a valid path is provided', () => {
package/src/macros.ts CHANGED
@@ -1,19 +1,25 @@
1
- import callsites from 'callsites';
2
1
  import { readFileSync } from 'node:fs';
3
- import { dirname, resolve } from 'node:path';
2
+ import { resolve } from 'node:path';
3
+ import { loadConfig } from '#helpers/loadConfig.ts';
4
+
5
+ const { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;
4
6
 
5
7
  export const md = <P extends string>(path: P): P => {
6
8
  if (!path) {
7
9
  throw new Error('md expected a path, but received none');
8
10
  }
9
11
 
10
- const filename = callsites()[0]?.getFileName();
12
+ let dir = process.cwd();
13
+ let { markdownDir } = loadConfig();
14
+
15
+ if (markdownDir) {
16
+ if (NEXT_PUBLIC_LANGUAGE_CODE) {
17
+ markdownDir = markdownDir.replaceAll('{{NEXT_PUBLIC_LANGUAGE_CODE}}', NEXT_PUBLIC_LANGUAGE_CODE);
18
+ }
11
19
 
12
- if (!filename) {
13
- throw new Error('md could not derive the filename of the caller');
20
+ dir = resolve(dir, markdownDir);
14
21
  }
15
22
 
16
- const dir = dirname(filename);
17
23
  const fullPath = resolve(dir, path);
18
24
  // @ts-expect-error `md` function runs and build time and its return value
19
25
  // replaces the `md` invocation. We are forcing the return type to be the
package/src/types.ts CHANGED
@@ -17,3 +17,12 @@ export type CreateI18nResult<U extends object> = {
17
17
  // value, then it throws an error.
18
18
  tt: ConfigReader<U>['read'];
19
19
  };
20
+
21
+ export type MacrosConfig = {
22
+ // mardownDir should be a relative path from the current working
23
+ // directory. The relative path supports a template variable NEXT_PUBLIC_LANGUAGE_CODE,
24
+ // which is populated by setting an environment variable with the same name.
25
+ // You can use this if you support multiple languages like in the example below.
26
+ // `./content/{{NEXT_PUBLIC_LANGUAGE_CODE}}/markdown`
27
+ markdownDir?: string;
28
+ };