@next-box/i18n 0.0.7 → 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/README.md +22 -4
- package/dist/cjs/macros.cjs +1 -1
- package/dist/cjs/macros.cjs.map +1 -1
- package/dist/esm/macros.mjs +1 -1
- package/dist/esm/macros.mjs.map +1 -1
- package/dist/production.analysis.txt +27 -9
- package/dist/types/cjs/constants.d.cts +2 -0
- package/dist/types/cjs/constants.d.cts.map +1 -0
- package/dist/types/cjs/helpers/loadConfig.d.cts +3 -0
- package/dist/types/cjs/helpers/loadConfig.d.cts.map +1 -0
- package/dist/types/cjs/macros.d.cts +1 -1
- package/dist/types/cjs/macros.d.cts.map +1 -1
- package/dist/types/cjs/types.d.cts +3 -0
- package/dist/types/cjs/types.d.cts.map +1 -1
- package/dist/types/esm/constants.d.ts +2 -0
- package/dist/types/esm/constants.d.ts.map +1 -0
- package/dist/types/esm/helpers/loadConfig.d.ts +3 -0
- package/dist/types/esm/helpers/loadConfig.d.ts.map +1 -0
- package/dist/types/esm/macros.d.ts +1 -1
- package/dist/types/esm/macros.d.ts.map +1 -1
- package/dist/types/esm/types.d.ts +3 -0
- package/dist/types/esm/types.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/src/constants.ts +1 -0
- package/src/helpers/loadConfig.ts +17 -0
- package/src/macros.test.ts +7 -1
- package/src/macros.ts +15 -5
- package/src/types.ts +9 -0
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.
|
|
4
|
+
"version": "0.0.9",
|
|
5
5
|
"author": "miami-man",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/badbatch/next-box",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
+
"callsites": "^4.2.0",
|
|
51
52
|
"markdown-to-jsx": "^7.7.6",
|
|
52
53
|
"type-fest": "^4.41.0",
|
|
53
54
|
"zcb": "^0.3.0"
|
package/src/constants.ts
ADDED
|
@@ -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
|
+
};
|
package/src/macros.test.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
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,15 +1,25 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { loadConfig } from '#helpers/loadConfig.ts';
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;
|
|
6
6
|
|
|
7
|
-
export const md = <P extends string>(path: P
|
|
7
|
+
export const md = <P extends string>(path: P): P => {
|
|
8
8
|
if (!path) {
|
|
9
9
|
throw new Error('md expected a path, but received none');
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
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
|
+
}
|
|
19
|
+
|
|
20
|
+
dir = resolve(dir, markdownDir);
|
|
21
|
+
}
|
|
22
|
+
|
|
13
23
|
const fullPath = resolve(dir, path);
|
|
14
24
|
// @ts-expect-error `md` function runs and build time and its return value
|
|
15
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
|
+
};
|