From bdf240857e1fbb0d7b13482cbbd39eedfe768119 Mon Sep 17 00:00:00 2001 From: Yassine Doghri Date: Sat, 20 Aug 2022 17:00:27 +0000 Subject: [PATCH] feat: move astro-i18next config in a standalone file to load it for CLI commands update config properties for consistency BREAKING CHANGE: config is now a standalone file + some property names have changed for better clarity and consistency - `baseLocale` is now `defaultLanguage` - `supportedLocales` is now `supportedLanguages`` --- build.cjs | 4 +- example/astro-i18next.config.ts | 12 + example/astro.config.mjs | 13 +- example/package-lock.json | 2 + package-lock.json | 620 ++++++++++++++++++++++++++++++-- package.json | 2 + src/cli/generate.ts | 8 +- src/cli/index.ts | 78 ++-- src/cli/middlewares.ts | 32 ++ src/cli/types.ts | 11 + src/index.mts | 113 ------ src/index.ts | 110 ++++++ src/types.ts | 47 +++ src/utils.ts | 31 ++ 14 files changed, 870 insertions(+), 213 deletions(-) create mode 100644 example/astro-i18next.config.ts create mode 100644 src/cli/middlewares.ts create mode 100644 src/cli/types.ts delete mode 100644 src/index.mts create mode 100644 src/index.ts create mode 100644 src/types.ts diff --git a/build.cjs b/build.cjs index 8af4547..65224df 100755 --- a/build.cjs +++ b/build.cjs @@ -4,7 +4,7 @@ require("esbuild") .build({ bundle: true, entryPoints: [ - "src/index.mts", + "src/index.ts", "src/utils.ts", "src/components/index.mts", "src/cli/index.ts", @@ -21,6 +21,8 @@ require("esbuild") "fs-extra", "iso-639-1", "iso-3166-1-alpha-2", + "@proload/core", + "@proload/plugin-typescript", ], minify: false, format: "esm", diff --git a/example/astro-i18next.config.ts b/example/astro-i18next.config.ts new file mode 100644 index 0000000..23dd2a5 --- /dev/null +++ b/example/astro-i18next.config.ts @@ -0,0 +1,12 @@ +export default { + defaultLanguage: "en", + supportedLanguages: ["en", "fr"], + i18next: { + debug: true, + initImmediate: false, + backend: { + loadPath: "./src/locales/{{lng}}.json", + }, + }, + i18nextPlugins: { fsBackend: "i18next-fs-backend" }, +}; diff --git a/example/astro.config.mjs b/example/astro.config.mjs index a52fa60..09d15f2 100644 --- a/example/astro.config.mjs +++ b/example/astro.config.mjs @@ -20,17 +20,6 @@ export default defineConfig({ }, }), tailwind(), - astroI18next({ - baseLocale: "en", - supportedLocales: ["en", "fr"], - i18next: { - debug: true, - initImmediate: false, - backend: { - loadPath: "./src/locales/{{lng}}.json", - }, - }, - i18nextPlugins: { fsBackend: "i18next-fs-backend" }, - }), + astroI18next(), ], }); diff --git a/example/package-lock.json b/example/package-lock.json index f511440..f44d223 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -21,6 +21,7 @@ "dev": true, "license": "MIT", "dependencies": { + "@proload/core": "^0.3.2", "fdir": "^5.2.0", "fs-extra": "^10.1.0", "i18next": "^21.9.1", @@ -7000,6 +7001,7 @@ "requires": { "@commitlint/cli": "^17.0.3", "@commitlint/config-conventional": "^17.0.3", + "@proload/core": "^0.3.2", "@semantic-release/changelog": "^6.0.1", "@semantic-release/git": "^10.0.1", "@types/fs-extra": "^9.0.13", diff --git a/package-lock.json b/package-lock.json index 4796779..ff2b6d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,8 @@ "version": "1.0.0-beta.3", "license": "MIT", "dependencies": { + "@proload/core": "^0.3.2", + "@proload/plugin-typescript": "^0.2.1", "fdir": "^5.2.0", "fs-extra": "^10.1.0", "i18next": "^21.9.1", @@ -2023,7 +2025,6 @@ "version": "0.3.2", "resolved": "https://registry.npmjs.org/@proload/core/-/core-0.3.2.tgz", "integrity": "sha512-4ga4HpS0ieVYWVMS+F62W++6SNACBu0lkw8snw3tEdH6AeqZu8i8262n3I81jWAWXVcg3sMfhb+kBexrfGrTUQ==", - "dev": true, "dependencies": { "deepmerge": "^4.2.2", "escalade": "^3.1.1" @@ -2041,6 +2042,18 @@ "@proload/core": "^0.3.2" } }, + "node_modules/@proload/plugin-typescript": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@proload/plugin-typescript/-/plugin-typescript-0.2.1.tgz", + "integrity": "sha512-pvlYjq1pg6XRCR8rEsSGBRe7ZfuUZWCyXeEZvoCgROBRtglw5IYOWJ29ib65jhK2LZLtVFBv4x430VJR0bdl3g==", + "dependencies": { + "@swc-node/register": "^1.3.7", + "typescript": "^4.1.3" + }, + "peerDependencies": { + "@proload/core": "^0.3.2" + } + }, "node_modules/@semantic-release/changelog": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/@semantic-release/changelog/-/changelog-6.0.1.tgz", @@ -2239,6 +2252,351 @@ "@sinonjs/commons": "^1.7.0" } }, + "node_modules/@swc-node/core": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@swc-node/core/-/core-1.9.0.tgz", + "integrity": "sha512-vRnvsMtL9OxybA/Wun1ZhlDvB6MNs4Zujnina0VKdGk+yI6s87KUhdTcbAY6dQMZhQTLFiC1Lnv/BuwCKcCEug==", + "dependencies": { + "@swc/core": "^1.2.172" + }, + "engines": { + "node": ">= 10" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + } + }, + "node_modules/@swc-node/register": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/@swc-node/register/-/register-1.5.1.tgz", + "integrity": "sha512-6IL5s4QShKGs08qAeNou3rDA3gbp2WHk6fo0XnJXQn/aC9k6FnVBbj/thGOIEDtgNhC/DKpZT8tCY1LpQnOZFg==", + "dependencies": { + "@swc-node/core": "^1.9.0", + "@swc-node/sourcemap-support": "^0.2.0", + "colorette": "^2.0.16", + "debug": "^4.3.4", + "pirates": "^4.0.5", + "tslib": "^2.4.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + }, + "peerDependencies": { + "typescript": ">= 4.3" + } + }, + "node_modules/@swc-node/register/node_modules/tslib": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", + "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" + }, + "node_modules/@swc-node/sourcemap-support": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@swc-node/sourcemap-support/-/sourcemap-support-0.2.0.tgz", + "integrity": "sha512-FNrxdI6XMYfoNt81L8eFKEm1d8P82I1nPwS3MrnBGzZoMWB+seQhQK+iN6M5RreJxXbfZw5lF86LRjHEQeGMqg==", + "dependencies": { + "source-map-support": "^0.5.21" + } + }, + "node_modules/@swc-node/sourcemap-support/node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/@swc-node/sourcemap-support/node_modules/source-map-support": { + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "dependencies": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, + "node_modules/@swc/core": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.2.241.tgz", + "integrity": "sha512-zDUpW3ffFllBi2c5ui9JXl7zUjzMOOZGwy9JCAsodWo7DXWjw5pJF4GsTCzaYHDf62XQzQWuL7zGyRnJyMiyAA==", + "hasInstallScript": true, + "bin": { + "swcx": "run_swcx.js" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/swc" + }, + "optionalDependencies": { + "@swc/core-android-arm-eabi": "1.2.241", + "@swc/core-android-arm64": "1.2.241", + "@swc/core-darwin-arm64": "1.2.241", + "@swc/core-darwin-x64": "1.2.241", + "@swc/core-freebsd-x64": "1.2.241", + "@swc/core-linux-arm-gnueabihf": "1.2.241", + "@swc/core-linux-arm64-gnu": "1.2.241", + "@swc/core-linux-arm64-musl": "1.2.241", + "@swc/core-linux-x64-gnu": "1.2.241", + "@swc/core-linux-x64-musl": "1.2.241", + "@swc/core-win32-arm64-msvc": "1.2.241", + "@swc/core-win32-ia32-msvc": "1.2.241", + "@swc/core-win32-x64-msvc": "1.2.241" + } + }, + "node_modules/@swc/core-android-arm-eabi": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm-eabi/-/core-android-arm-eabi-1.2.241.tgz", + "integrity": "sha512-VfbyFAQ+JT4kl4a7kPFM4pUSLHXnJ/bKIW0gAsVngBIcu73cz59HlylKiOtmx3UtXPsYu0Ort/qfC/UJfeEgrQ==", + "cpu": [ + "arm" + ], + "optional": true, + "os": [ + "android" + ], + "dependencies": { + "@swc/wasm": "1.2.122" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-android-arm64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm64/-/core-android-arm64-1.2.241.tgz", + "integrity": "sha512-WAJW542fxtO5iTP/vrBrf64dWfBq6rmWgL0HpM+ENFbqO4ME0xO49ky+5rMRAQdtwnJ5ZNkCvb49J+iIIY6yaw==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "android" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-android-arm64/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + }, + "node_modules/@swc/core-darwin-arm64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.2.241.tgz", + "integrity": "sha512-5lQaguosciAN6kOfmNY1UeitrwMyPUt4d/Z70A1ac5e1ZFuYlhOxGHuhkz6abEewLkS/b1CGruSAtphEEVGLmw==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-darwin-x64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-x64/-/core-darwin-x64-1.2.241.tgz", + "integrity": "sha512-VtcCBdhOktYPDnEEL0f+pfGmvjIlmXWMZKIb48WTYunxwsehxQk79ZkLXc+TwZ3ur9GEoZHh31RaKqOj4QDHpQ==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-freebsd-x64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-freebsd-x64/-/core-freebsd-x64-1.2.241.tgz", + "integrity": "sha512-i12GxWnm1LuvZ9T0HVB8+CFIhcFzTxu3u2U97LZNb7vbHGHehUwIb6GmTwUbF+wEdFkwsIKWTf3RpvnEejWUsA==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "freebsd" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-freebsd-x64/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + }, + "node_modules/@swc/core-linux-arm-gnueabihf": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.2.241.tgz", + "integrity": "sha512-lTSiPkfEscfYEZxsKLbVqISRvCcatB+h7eENy0+Qdqqyio0yTOMfG7837jZhfy1hCjAwT8x2sh77fbvfQD4dRA==", + "cpu": [ + "arm" + ], + "optional": true, + "os": [ + "linux" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-arm-gnueabihf/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + }, + "node_modules/@swc/core-linux-arm64-gnu": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.2.241.tgz", + "integrity": "sha512-H6lTvd6nm4eaOi4Ledo5z1a6LXzJ2WpHTRsf3FssM9qqwFmbvNIz9vCTI4jCR5Y3Ed3jlmQli+znzmWJ/qzLLQ==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-arm64-musl": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.2.241.tgz", + "integrity": "sha512-K8bXA+JtoD0g+w9wDyI3R0VkFaxFokF9KI0ioDVRfwDDNoFWq3slQWyN9fkj0dI9XagK15OcSuMGTH+h9B7veQ==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-x64-gnu": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.2.241.tgz", + "integrity": "sha512-jLr+mtNhHMcSRz0xZ9/R9g59kVmgekcz9RyXIFkO7RzJOGVzXxGxfO3pSsQ+u2tCpYbK9M6rMiaNoRYnQj3yNQ==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-x64-musl": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.2.241.tgz", + "integrity": "sha512-yXkhlxTSH6ddcBCxwRHTnpj5TA0GXbWADjPIhhXG8KlM4KGjnEvfSBa1xtSNbJcYT8kBYM1n+jYf0dIX2je5eg==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-arm64-msvc": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.2.241.tgz", + "integrity": "sha512-/f3ylWLHfUtRgHFER3FdH5QwDhO7siQ6h5ug0yVKXIDfNJhJVt9Hd+ZjMGJhNGTkzrl+uZmwXWBiklMcaMCtbQ==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "win32" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-arm64-msvc/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + }, + "node_modules/@swc/core-win32-ia32-msvc": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.2.241.tgz", + "integrity": "sha512-HC1T9sWC9zuZ6C/WWTFMHdgKYv+qaOfWduIvNVqhECa+FXRcBTPtDgNBhMTc2lpt4biKf5iPHhAVZkP6Za3OOw==", + "cpu": [ + "ia32" + ], + "optional": true, + "os": [ + "win32" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-ia32-msvc/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + }, + "node_modules/@swc/core-win32-x64-msvc": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.2.241.tgz", + "integrity": "sha512-BW1MHKdmi+DDBH+Z/XlhluIjZj9SMkMheeN95G71Z2Pim5LrvzIHf31UD0kYh6ZWWphP06Jlpzl0oi4stxeETw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/wasm": { + "version": "1.2.122", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.122.tgz", + "integrity": "sha512-sM1VCWQxmNhFtdxME+8UXNyPNhxNu7zdb6ikWpz0YKAQQFRGT5ThZgJrubEpah335SUToNg8pkdDF7ibVCjxbQ==", + "optional": true + }, "node_modules/@tootallnate/once": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz", @@ -4308,9 +4666,7 @@ "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", - "dev": true, - "peer": true + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" }, "node_modules/builtins": { "version": "5.0.1", @@ -4698,8 +5054,7 @@ "node_modules/colorette": { "version": "2.0.17", "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.17.tgz", - "integrity": "sha512-hJo+3Bkn0NCHybn9Tu35fIeoOKGOk5OCC32y4Hz2It+qlCO2Q3DeQ1hRn/tDDMQKRYUEzqsl7jbF6dYKjlE60g==", - "dev": true + "integrity": "sha512-hJo+3Bkn0NCHybn9Tu35fIeoOKGOk5OCC32y4Hz2It+qlCO2Q3DeQ1hRn/tDDMQKRYUEzqsl7jbF6dYKjlE60g==" }, "node_modules/comma-separated-tokens": { "version": "2.0.2", @@ -5101,7 +5456,6 @@ "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -5183,7 +5537,6 @@ "version": "4.2.2", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -11242,8 +11595,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/mute-stream": { "version": "0.0.8", @@ -14365,8 +14717,6 @@ "version": "4.0.5", "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz", "integrity": "sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ==", - "dev": true, - "peer": true, "engines": { "node": ">= 6" } @@ -17255,7 +17605,6 @@ "version": "4.7.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz", "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==", - "dev": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -20047,7 +20396,6 @@ "version": "0.3.2", "resolved": "https://registry.npmjs.org/@proload/core/-/core-0.3.2.tgz", "integrity": "sha512-4ga4HpS0ieVYWVMS+F62W++6SNACBu0lkw8snw3tEdH6AeqZu8i8262n3I81jWAWXVcg3sMfhb+kBexrfGrTUQ==", - "dev": true, "requires": { "deepmerge": "^4.2.2", "escalade": "^3.1.1" @@ -20062,6 +20410,15 @@ "tsm": "^2.1.4" } }, + "@proload/plugin-typescript": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@proload/plugin-typescript/-/plugin-typescript-0.2.1.tgz", + "integrity": "sha512-pvlYjq1pg6XRCR8rEsSGBRe7ZfuUZWCyXeEZvoCgROBRtglw5IYOWJ29ib65jhK2LZLtVFBv4x430VJR0bdl3g==", + "requires": { + "@swc-node/register": "^1.3.7", + "typescript": "^4.1.3" + } + }, "@semantic-release/changelog": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/@semantic-release/changelog/-/changelog-6.0.1.tgz", @@ -20222,6 +20579,220 @@ "@sinonjs/commons": "^1.7.0" } }, + "@swc-node/core": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@swc-node/core/-/core-1.9.0.tgz", + "integrity": "sha512-vRnvsMtL9OxybA/Wun1ZhlDvB6MNs4Zujnina0VKdGk+yI6s87KUhdTcbAY6dQMZhQTLFiC1Lnv/BuwCKcCEug==", + "requires": { + "@swc/core": "^1.2.172" + } + }, + "@swc-node/register": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/@swc-node/register/-/register-1.5.1.tgz", + "integrity": "sha512-6IL5s4QShKGs08qAeNou3rDA3gbp2WHk6fo0XnJXQn/aC9k6FnVBbj/thGOIEDtgNhC/DKpZT8tCY1LpQnOZFg==", + "requires": { + "@swc-node/core": "^1.9.0", + "@swc-node/sourcemap-support": "^0.2.0", + "colorette": "^2.0.16", + "debug": "^4.3.4", + "pirates": "^4.0.5", + "tslib": "^2.4.0" + }, + "dependencies": { + "tslib": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", + "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" + } + } + }, + "@swc-node/sourcemap-support": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@swc-node/sourcemap-support/-/sourcemap-support-0.2.0.tgz", + "integrity": "sha512-FNrxdI6XMYfoNt81L8eFKEm1d8P82I1nPwS3MrnBGzZoMWB+seQhQK+iN6M5RreJxXbfZw5lF86LRjHEQeGMqg==", + "requires": { + "source-map-support": "^0.5.21" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" + }, + "source-map-support": { + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "requires": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + } + } + }, + "@swc/core": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.2.241.tgz", + "integrity": "sha512-zDUpW3ffFllBi2c5ui9JXl7zUjzMOOZGwy9JCAsodWo7DXWjw5pJF4GsTCzaYHDf62XQzQWuL7zGyRnJyMiyAA==", + "requires": { + "@swc/core-android-arm-eabi": "1.2.241", + "@swc/core-android-arm64": "1.2.241", + "@swc/core-darwin-arm64": "1.2.241", + "@swc/core-darwin-x64": "1.2.241", + "@swc/core-freebsd-x64": "1.2.241", + "@swc/core-linux-arm-gnueabihf": "1.2.241", + "@swc/core-linux-arm64-gnu": "1.2.241", + "@swc/core-linux-arm64-musl": "1.2.241", + "@swc/core-linux-x64-gnu": "1.2.241", + "@swc/core-linux-x64-musl": "1.2.241", + "@swc/core-win32-arm64-msvc": "1.2.241", + "@swc/core-win32-ia32-msvc": "1.2.241", + "@swc/core-win32-x64-msvc": "1.2.241" + } + }, + "@swc/core-android-arm-eabi": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm-eabi/-/core-android-arm-eabi-1.2.241.tgz", + "integrity": "sha512-VfbyFAQ+JT4kl4a7kPFM4pUSLHXnJ/bKIW0gAsVngBIcu73cz59HlylKiOtmx3UtXPsYu0Ort/qfC/UJfeEgrQ==", + "optional": true, + "requires": { + "@swc/wasm": "1.2.122" + } + }, + "@swc/core-android-arm64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm64/-/core-android-arm64-1.2.241.tgz", + "integrity": "sha512-WAJW542fxtO5iTP/vrBrf64dWfBq6rmWgL0HpM+ENFbqO4ME0xO49ky+5rMRAQdtwnJ5ZNkCvb49J+iIIY6yaw==", + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + } + } + }, + "@swc/core-darwin-arm64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.2.241.tgz", + "integrity": "sha512-5lQaguosciAN6kOfmNY1UeitrwMyPUt4d/Z70A1ac5e1ZFuYlhOxGHuhkz6abEewLkS/b1CGruSAtphEEVGLmw==", + "optional": true + }, + "@swc/core-darwin-x64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-x64/-/core-darwin-x64-1.2.241.tgz", + "integrity": "sha512-VtcCBdhOktYPDnEEL0f+pfGmvjIlmXWMZKIb48WTYunxwsehxQk79ZkLXc+TwZ3ur9GEoZHh31RaKqOj4QDHpQ==", + "optional": true + }, + "@swc/core-freebsd-x64": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-freebsd-x64/-/core-freebsd-x64-1.2.241.tgz", + "integrity": "sha512-i12GxWnm1LuvZ9T0HVB8+CFIhcFzTxu3u2U97LZNb7vbHGHehUwIb6GmTwUbF+wEdFkwsIKWTf3RpvnEejWUsA==", + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + } + } + }, + "@swc/core-linux-arm-gnueabihf": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.2.241.tgz", + "integrity": "sha512-lTSiPkfEscfYEZxsKLbVqISRvCcatB+h7eENy0+Qdqqyio0yTOMfG7837jZhfy1hCjAwT8x2sh77fbvfQD4dRA==", + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + } + } + }, + "@swc/core-linux-arm64-gnu": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.2.241.tgz", + "integrity": "sha512-H6lTvd6nm4eaOi4Ledo5z1a6LXzJ2WpHTRsf3FssM9qqwFmbvNIz9vCTI4jCR5Y3Ed3jlmQli+znzmWJ/qzLLQ==", + "optional": true + }, + "@swc/core-linux-arm64-musl": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.2.241.tgz", + "integrity": "sha512-K8bXA+JtoD0g+w9wDyI3R0VkFaxFokF9KI0ioDVRfwDDNoFWq3slQWyN9fkj0dI9XagK15OcSuMGTH+h9B7veQ==", + "optional": true + }, + "@swc/core-linux-x64-gnu": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.2.241.tgz", + "integrity": "sha512-jLr+mtNhHMcSRz0xZ9/R9g59kVmgekcz9RyXIFkO7RzJOGVzXxGxfO3pSsQ+u2tCpYbK9M6rMiaNoRYnQj3yNQ==", + "optional": true + }, + "@swc/core-linux-x64-musl": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.2.241.tgz", + "integrity": "sha512-yXkhlxTSH6ddcBCxwRHTnpj5TA0GXbWADjPIhhXG8KlM4KGjnEvfSBa1xtSNbJcYT8kBYM1n+jYf0dIX2je5eg==", + "optional": true + }, + "@swc/core-win32-arm64-msvc": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.2.241.tgz", + "integrity": "sha512-/f3ylWLHfUtRgHFER3FdH5QwDhO7siQ6h5ug0yVKXIDfNJhJVt9Hd+ZjMGJhNGTkzrl+uZmwXWBiklMcaMCtbQ==", + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + } + } + }, + "@swc/core-win32-ia32-msvc": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.2.241.tgz", + "integrity": "sha512-HC1T9sWC9zuZ6C/WWTFMHdgKYv+qaOfWduIvNVqhECa+FXRcBTPtDgNBhMTc2lpt4biKf5iPHhAVZkP6Za3OOw==", + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "optional": true + } + } + }, + "@swc/core-win32-x64-msvc": { + "version": "1.2.241", + "resolved": "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.2.241.tgz", + "integrity": "sha512-BW1MHKdmi+DDBH+Z/XlhluIjZj9SMkMheeN95G71Z2Pim5LrvzIHf31UD0kYh6ZWWphP06Jlpzl0oi4stxeETw==", + "optional": true + }, + "@swc/wasm": { + "version": "1.2.122", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.122.tgz", + "integrity": "sha512-sM1VCWQxmNhFtdxME+8UXNyPNhxNu7zdb6ikWpz0YKAQQFRGT5ThZgJrubEpah335SUToNg8pkdDF7ibVCjxbQ==", + "optional": true + }, "@tootallnate/once": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz", @@ -21675,9 +22246,7 @@ "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", - "dev": true, - "peer": true + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" }, "builtins": { "version": "5.0.1", @@ -21955,8 +22524,7 @@ "colorette": { "version": "2.0.17", "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.17.tgz", - "integrity": "sha512-hJo+3Bkn0NCHybn9Tu35fIeoOKGOk5OCC32y4Hz2It+qlCO2Q3DeQ1hRn/tDDMQKRYUEzqsl7jbF6dYKjlE60g==", - "dev": true + "integrity": "sha512-hJo+3Bkn0NCHybn9Tu35fIeoOKGOk5OCC32y4Hz2It+qlCO2Q3DeQ1hRn/tDDMQKRYUEzqsl7jbF6dYKjlE60g==" }, "comma-separated-tokens": { "version": "2.0.2", @@ -22274,7 +22842,6 @@ "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dev": true, "requires": { "ms": "2.1.2" } @@ -22333,8 +22900,7 @@ "deepmerge": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", - "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", - "dev": true + "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==" }, "defaults": { "version": "1.0.3", @@ -26629,8 +27195,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "mute-stream": { "version": "0.0.8", @@ -28804,9 +29369,7 @@ "pirates": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz", - "integrity": "sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ==", - "dev": true, - "peer": true + "integrity": "sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ==" }, "pkg-conf": { "version": "2.1.0", @@ -30841,8 +31404,7 @@ "typescript": { "version": "4.7.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz", - "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==", - "dev": true + "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==" }, "uglify-js": { "version": "3.15.4", diff --git a/package.json b/package.json index 29186eb..e951f79 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,8 @@ "astro": "^1.0.0" }, "dependencies": { + "@proload/core": "^0.3.2", + "@proload/plugin-typescript": "^0.2.1", "fdir": "^5.2.0", "fs-extra": "^10.1.0", "i18next": "^21.9.1", diff --git a/src/cli/generate.ts b/src/cli/generate.ts index 1f8423e..ec4cdb8 100644 --- a/src/cli/generate.ts +++ b/src/cli/generate.ts @@ -12,19 +12,17 @@ import { * Reads all files inside inputPath * * @param inputPath - * @param languages + * @param supportedLanguages * @param outputPath */ export const generate = ( inputPath: string, defaultLanguage: string, - languages: string[], + supportedLanguages: string[], outputPath: string = inputPath ) => { const files = crawlInputDirectory(inputPath); - const allLanguages = [defaultLanguage, ...languages]; - const filesToGenerate: FileToGenerate[] = []; files.forEach(async function (file) { @@ -38,7 +36,7 @@ export const generate = ( const parsedFrontmatter = parseFrontmatter(fileContentsString); - allLanguages.forEach((language) => { + supportedLanguages.forEach((language) => { const frontmatterCode = generateLocalizedFrontmatter( parsedFrontmatter, language, diff --git a/src/cli/index.ts b/src/cli/index.ts index 47cf68f..006914c 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -3,14 +3,8 @@ import yargs from "yargs"; import { hideBin } from "yargs/helpers"; import { generate } from "./generate"; -import { isLocale } from "./utils"; - -interface GenerateArgs { - path: string; - defaultLanguage: string; - languages: string[]; - output: string; -} +import { loadConfig, normalizePath } from "./middlewares"; +import { GenerateArgs } from "./types"; yargs(hideBin(process.argv)) .usage("usage: $0 ") @@ -21,61 +15,39 @@ yargs(hideBin(process.argv)) return yargs .positional("path", { type: "string", - description: "Path to base pages folder", - default: "./src/pages", - }) - .options({ - "default-language": { - alias: "d", - type: "string", - description: "Set the default language", - demandOption: true, - }, - languages: { - alias: "l", - type: "array", - string: true, - description: "Provide a list of locales to generate", - demandOption: true, - }, - output: { - alias: "o", - type: "string", - description: - "Set the output of the generated pages if different from input", - }, + description: "Path to the Astro project folder", + default: "./", }) - .check((argv) => { - const { "default-language": defaultLanguage, languages } = argv; - - const errors = []; - - if (!isLocale(defaultLanguage)) { - errors.push(`"${defaultLanguage}" is not a valid locale!`); - } - - languages.forEach((language) => { - if (!isLocale(language)) { - errors.push(`"${language}" is not a valid locale!`); - } - }); - - if (errors.length > 0) { - throw new Error(errors.join("\n")); - } - - return true; + .option("output", { + alias: "o", + type: "string", + description: + "Set the output of the generated pages if different from input", }); }, - (argv) => { + async (argv) => { if (argv.verbose) { console.info(`Generating localized pages: ${argv.languages}`); } - generate(argv.path, argv.defaultLanguage, argv.languages, argv.output); + const pagesPath = argv.path + "src/pages"; + + generate( + pagesPath, + argv.config.defaultLanguage, + argv.config.supportedLanguages, + argv.output + ); } ) + .middleware([normalizePath, loadConfig], true) .options({ + config: { + alias: "c", + type: "string", + description: + "Set the output of the generated pages if different from input", + }, verbose: { alias: "v", type: "boolean", diff --git a/src/cli/middlewares.ts b/src/cli/middlewares.ts new file mode 100644 index 0000000..8961e38 --- /dev/null +++ b/src/cli/middlewares.ts @@ -0,0 +1,32 @@ +import { pathToFileURL } from "url"; +import { getUserConfig } from "utils"; +import { MiddlewareFunction } from "yargs"; +import { GenerateArgs, GlobalArgs } from "./types"; + +// @ts-ignore +export const loadConfig: MiddlewareFunction = async ( + argv +): Promise => { + const { path, config } = argv; + + const userConfig = await getUserConfig(pathToFileURL(path), config as any); + + if (path && !userConfig?.value) { + throw new Error( + `Could not find a config file at ${JSON.stringify( + path + )}. Does the file exist?` + ); + } + + return { ...argv, config: userConfig?.value }; +}; + +// @ts-ignore +export const normalizePath: MiddlewareFunction< + GlobalArgs & GenerateArgs +> = async (argv): Promise => { + const { path } = argv; + + return { ...argv, path: path.endsWith("/") ? path : path + "/" }; +}; diff --git a/src/cli/types.ts b/src/cli/types.ts new file mode 100644 index 0000000..f1e16b0 --- /dev/null +++ b/src/cli/types.ts @@ -0,0 +1,11 @@ +import { AstroI18nextConfig } from "types"; + +export interface GlobalArgs { + verbose: boolean; +} + +export interface GenerateArgs { + path: string; + config: AstroI18nextConfig; + output: string; +} diff --git a/src/index.mts b/src/index.mts deleted file mode 100644 index b31e6d8..0000000 --- a/src/index.mts +++ /dev/null @@ -1,113 +0,0 @@ -import { AstroIntegration } from "astro"; -import i18next, { i18n, InitOptions } from "i18next"; -import { moveBaseLanguageToFirstIndex, deeplyStringifyObject } from "./utils"; - -export interface AstroI18nextOptions { - /** - * The default language for your website. - * - * @default undefined - */ - baseLocale: string; - - /** - * The locales that are supported by your website. - * - * @default undefined - */ - supportedLocales: string[]; - - /** - * i18next config. See https://www.i18next.com/overview/configuration-options - * - * @default undefined - */ - i18next: InitOptions; - - /** - * i18next plugins. See https://www.i18next.com/overview/plugins-and-utils - * - * Include the plugins with the key being the import name and the value being the plugin name. - * - * Eg.: - * ``` - * { - * "Backend": "i18next-fs-backend", - * } - * ``` - */ - i18nextPlugins: { - [key: string]: string; - }; -} - -export const aI18next: i18n = i18next; - -export default (options: AstroI18nextOptions): AstroIntegration => { - return { - name: "astro-i18next", - hooks: { - "astro:config:setup": async ({ injectScript }) => { - /** - * 1. Validate and prepare config - */ - if (!options.baseLocale || options.baseLocale === "") { - throw new Error( - "[astro-i18next]: you must set a `baseLanguage` in your config!" - ); - } - - if (!options.supportedLocales) { - options.supportedLocales = [options.baseLocale]; - } - - if (!options.supportedLocales.includes(options.baseLocale)) { - options.supportedLocales.unshift(options.baseLocale); - } - - // make sure to have base language set as first element in supportedLngs - if (options.supportedLocales[0] !== options.baseLocale) { - moveBaseLanguageToFirstIndex( - options.supportedLocales as string[], - options.baseLocale - ); - } - - // set i18next supported and fallback languages (same as supportedLocales) - options.i18next.supportedLngs = [ - ...(options.supportedLocales as string[]), - ]; - options.i18next.fallbackLng = [ - ...(options.supportedLocales as string[]), - ]; - - let imports = `import i18next from "i18next";`; - let i18nextInit = `i18next`; - if ( - options.i18nextPlugins && - Object.keys(options.i18nextPlugins).length > 0 - ) { - // loop through plugins to import them - for (const key of Object.keys(options.i18nextPlugins)) { - imports += `import ${key} from "${options.i18nextPlugins[key]}";`; - } - - // loop through plugins to use them - for (const key of Object.keys(options.i18nextPlugins)) { - i18nextInit += `.use(${key})`; - } - } - i18nextInit += `.init(${deeplyStringifyObject(options.i18next)});`; - - injectScript("page-ssr", imports + i18nextInit); - }, - }, - }; -}; - -export { - interpolate, - localizePath, - localizeUrl, - detectLocaleFromPath, -} from "./utils"; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..8d3b431 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,110 @@ +import { AstroIntegration } from "astro"; +import { AstroI18nextConfig, AstroI18nextOptions } from "types"; +import { + moveBaseLanguageToFirstIndex, + deeplyStringifyObject, + getUserConfig, +} from "./utils"; + +export default (options?: AstroI18nextOptions): AstroIntegration => { + const customConfigPath = options?.configPath; + + return { + name: "astro-i18next", + hooks: { + "astro:config:setup": async ({ config, injectScript }) => { + /** + * 0. Get user config + */ + const userConfig = await getUserConfig(config.root, customConfigPath); + + if (customConfigPath && !userConfig?.value) { + throw new Error( + `[astro-i18next]: Could not find a config file at ${JSON.stringify( + customConfigPath + )}. Does the file exist?` + ); + } + + const astroI18nextConfig: AstroI18nextConfig = + userConfig?.value as AstroI18nextConfig; + + /** + * 1. Validate and prepare config + */ + if ( + !astroI18nextConfig.defaultLanguage || + astroI18nextConfig.defaultLanguage === "" + ) { + throw new Error( + "[astro-i18next]: you must set a `baseLanguage` in your astroI18nextConfig!" + ); + } + + if (!astroI18nextConfig.supportedLanguages) { + astroI18nextConfig.supportedLanguages = [ + astroI18nextConfig.defaultLanguage, + ]; + } + + if ( + !astroI18nextConfig.supportedLanguages.includes( + astroI18nextConfig.defaultLanguage + ) + ) { + astroI18nextConfig.supportedLanguages.unshift( + astroI18nextConfig.defaultLanguage + ); + } + + // make sure to have base language set as first element in supportedLngs + if ( + astroI18nextConfig.supportedLanguages[0] !== + astroI18nextConfig.defaultLanguage + ) { + moveBaseLanguageToFirstIndex( + astroI18nextConfig.supportedLanguages as string[], + astroI18nextConfig.defaultLanguage + ); + } + + // set i18next supported and fallback languages (same as supportedLocales) + astroI18nextConfig.i18next.supportedLngs = [ + ...(astroI18nextConfig.supportedLanguages as string[]), + ]; + astroI18nextConfig.i18next.fallbackLng = [ + ...(astroI18nextConfig.supportedLanguages as string[]), + ]; + + let imports = `import i18next from "i18next";`; + let i18nextInit = `i18next`; + if ( + astroI18nextConfig.i18nextPlugins && + Object.keys(astroI18nextConfig.i18nextPlugins).length > 0 + ) { + // loop through plugins to import them + for (const key of Object.keys(astroI18nextConfig.i18nextPlugins)) { + imports += `import ${key} from "${astroI18nextConfig.i18nextPlugins[key]}";`; + } + + // loop through plugins to use them + for (const key of Object.keys(astroI18nextConfig.i18nextPlugins)) { + i18nextInit += `.use(${key})`; + } + } + i18nextInit += `.init(${deeplyStringifyObject( + astroI18nextConfig.i18next + )});`; + + injectScript("page-ssr", imports + i18nextInit); + }, + }, + }; +}; + +export { + interpolate, + localizePath, + localizeUrl, + detectLocaleFromPath, +} from "./utils"; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..9defac9 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,47 @@ +import { InitOptions } from "i18next"; + +export interface AstroI18nextOptions { + /** + * Path to your astro-i18next config file + * + * @default 'astro-i18next.config.js' + */ + configPath?: string; +} + +export interface AstroI18nextConfig { + /** + * The default language for your website. + * + * @default undefined + */ + defaultLanguage: string; + + /** + * The locales that are supported by your website. + * + * @default undefined + */ + supportedLanguages: string[]; + + /** + * i18next config. See https://www.i18next.com/overview/configuration-options + */ + i18next?: InitOptions; + + /** + * i18next plugins. See https://www.i18next.com/overview/plugins-and-utils + * + * Include the plugins with the key being the import name and the value being the plugin name. + * + * Eg.: + * ``` + * { + * "Backend": "i18next-fs-backend", + * } + * ``` + */ + i18nextPlugins?: { + [key: string]: string; + }; +} diff --git a/src/utils.ts b/src/utils.ts index 5d6dab0..a510d06 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,35 @@ import i18next, { t } from "i18next"; +import { fileURLToPath } from "url"; +import load from "@proload/core"; +import { AstroI18nextConfig } from "types"; +import typescript from "@proload/plugin-typescript"; + +/** + * Adapted from astro's tailwind integration: + * https://github.com/withastro/astro/tree/main/packages/integrations/tailwind + */ +export const getUserConfig = async ( + root: URL, + configPath?: string +): Promise> => { + const resolvedRoot = fileURLToPath(root); + let userConfigPath: string | undefined; + + if (configPath) { + const configPathWithLeadingSlash = /^\.*\//.test(configPath) + ? configPath + : `./${configPath}`; + + userConfigPath = fileURLToPath(new URL(configPathWithLeadingSlash, root)); + } + + load.use([typescript]); + return (await load("astro-i18next", { + mustExist: false, + cwd: resolvedRoot, + filePath: userConfigPath, + })) as load.Config; +}; /** * Moves the default locale in the first index