@inlang/paraglide-js 2.26.0 → 2.28.0
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 +11 -1
- package/dist/bundler-plugins/per-locale-build/index.js +14 -7
- package/dist/bundler-plugins/per-locale-build/index.js.map +1 -1
- package/dist/bundler-plugins/per-locale-build/vite-environments.test.js +237 -4
- package/dist/bundler-plugins/per-locale-build/vite-environments.test.js.map +1 -1
- package/dist/bundler-plugins/unplugin.d.ts +1 -1
- package/dist/bundler-plugins/unplugin.js +79 -10
- package/dist/bundler-plugins/unplugin.js.map +1 -1
- package/dist/bundler-plugins/unplugin.test.js +334 -1
- package/dist/bundler-plugins/unplugin.test.js.map +1 -1
- package/dist/bundler-plugins/vite.js +2 -1
- package/dist/bundler-plugins/vite.js.map +1 -1
- package/dist/bundler-plugins/vite.test.js +1 -1
- package/dist/bundler-plugins/vite.test.js.map +1 -1
- package/dist/cli/commands/compile/command.js +13 -0
- package/dist/cli/commands/compile/command.js.map +1 -1
- package/dist/cli/commands/compile/command.test.js +87 -3
- package/dist/cli/commands/compile/command.test.js.map +1 -1
- package/dist/cli/commands/init/command.d.ts +5 -0
- package/dist/cli/commands/init/command.js +216 -44
- package/dist/cli/commands/init/command.js.map +1 -1
- package/dist/cli/commands/init/command.test.d.ts +1 -0
- package/dist/cli/commands/init/command.test.js +123 -0
- package/dist/cli/commands/init/command.test.js.map +1 -0
- package/dist/cli/defaults.js +2 -2
- package/dist/cli/defaults.js.map +1 -1
- package/dist/cli/init-options.d.ts +11 -0
- package/dist/cli/init-options.js +2 -0
- package/dist/cli/init-options.js.map +1 -0
- package/dist/cli/steps/add-inlang-check.d.ts +19 -0
- package/dist/cli/steps/add-inlang-check.js +60 -0
- package/dist/cli/steps/add-inlang-check.js.map +1 -0
- package/dist/cli/steps/add-inlang-check.test.d.ts +1 -0
- package/dist/cli/steps/add-inlang-check.test.js +88 -0
- package/dist/cli/steps/add-inlang-check.test.js.map +1 -0
- package/dist/cli/steps/add-message-lint.d.ts +34 -0
- package/dist/cli/steps/add-message-lint.js +105 -0
- package/dist/cli/steps/add-message-lint.js.map +1 -0
- package/dist/cli/steps/add-message-lint.test.d.ts +1 -0
- package/dist/cli/steps/add-message-lint.test.js +74 -0
- package/dist/cli/steps/add-message-lint.test.js.map +1 -0
- package/dist/cli/steps/add-vite-plugin.d.ts +18 -0
- package/dist/cli/steps/add-vite-plugin.js +34 -6
- package/dist/cli/steps/add-vite-plugin.js.map +1 -1
- package/dist/cli/steps/add-vite-plugin.test.js +25 -9
- package/dist/cli/steps/add-vite-plugin.test.js.map +1 -1
- package/dist/cli/steps/agents-md-suggestion.d.ts +11 -0
- package/dist/cli/steps/agents-md-suggestion.js +19 -0
- package/dist/cli/steps/agents-md-suggestion.js.map +1 -0
- package/dist/cli/steps/agents-md-suggestion.test.d.ts +1 -0
- package/dist/cli/steps/agents-md-suggestion.test.js +36 -0
- package/dist/cli/steps/agents-md-suggestion.test.js.map +1 -0
- package/dist/cli/steps/check-for-uncomitted-changes.d.ts +3 -0
- package/dist/cli/steps/check-for-uncomitted-changes.js +15 -3
- package/dist/cli/steps/check-for-uncomitted-changes.js.map +1 -1
- package/dist/cli/steps/detect-bundler.d.ts +1 -0
- package/dist/cli/steps/detect-bundler.js +7 -1
- package/dist/cli/steps/detect-bundler.js.map +1 -1
- package/dist/cli/steps/initialize-inlang-project.d.ts +4 -3
- package/dist/cli/steps/initialize-inlang-project.js +60 -157
- package/dist/cli/steps/initialize-inlang-project.js.map +1 -1
- package/dist/cli/steps/maybe-add-machine-translation.d.ts +4 -0
- package/dist/cli/steps/maybe-add-machine-translation.js +14 -8
- package/dist/cli/steps/maybe-add-machine-translation.js.map +1 -1
- package/dist/cli/steps/maybe-add-sherlock.d.ts +2 -0
- package/dist/cli/steps/maybe-add-sherlock.js +13 -7
- package/dist/cli/steps/maybe-add-sherlock.js.map +1 -1
- package/dist/cli/steps/maybe-add-sherlock.test.d.ts +1 -0
- package/dist/cli/steps/maybe-add-sherlock.test.js +44 -0
- package/dist/cli/steps/maybe-add-sherlock.test.js.map +1 -0
- package/dist/cli/steps/prompt-for-outdir.d.ts +2 -0
- package/dist/cli/steps/prompt-for-outdir.js +4 -0
- package/dist/cli/steps/prompt-for-outdir.js.map +1 -1
- package/dist/cli/steps/update-package-json.js +8 -13
- package/dist/cli/steps/update-package-json.js.map +1 -1
- package/dist/cli/steps/update-ts-config.d.ts +6 -0
- package/dist/cli/steps/update-ts-config.js +27 -4
- package/dist/cli/steps/update-ts-config.js.map +1 -1
- package/dist/cli/utils.d.ts +11 -0
- package/dist/cli/utils.js +21 -0
- package/dist/cli/utils.js.map +1 -1
- package/dist/cli/utils.test.d.ts +1 -0
- package/dist/cli/utils.test.js +16 -0
- package/dist/cli/utils.test.js.map +1 -0
- package/dist/compiler/compile-annotation.d.ts +48 -1
- package/dist/compiler/compile-annotation.js +243 -10
- package/dist/compiler/compile-annotation.js.map +1 -1
- package/dist/compiler/compile-bundle.d.ts +7 -2
- package/dist/compiler/compile-bundle.js +97 -3
- package/dist/compiler/compile-bundle.js.map +1 -1
- package/dist/compiler/compile-bundle.test.js +232 -19
- package/dist/compiler/compile-bundle.test.js.map +1 -1
- package/dist/compiler/compile-lifecycle.test.d.ts +1 -0
- package/dist/compiler/compile-lifecycle.test.js +69 -0
- package/dist/compiler/compile-lifecycle.test.js.map +1 -0
- package/dist/compiler/compile-local-variable.d.ts +3 -0
- package/dist/compiler/compile-local-variable.js +5 -2
- package/dist/compiler/compile-local-variable.js.map +1 -1
- package/dist/compiler/compile-local-variable.test.js +55 -15
- package/dist/compiler/compile-local-variable.test.js.map +1 -1
- package/dist/compiler/compile-message.d.ts +29 -1
- package/dist/compiler/compile-message.exact-number.test.d.ts +1 -0
- package/dist/compiler/compile-message.exact-number.test.js +786 -0
- package/dist/compiler/compile-message.exact-number.test.js.map +1 -0
- package/dist/compiler/compile-message.js +162 -66
- package/dist/compiler/compile-message.js.map +1 -1
- package/dist/compiler/compile-message.locals.test.d.ts +1 -0
- package/dist/compiler/compile-message.locals.test.js +498 -0
- package/dist/compiler/compile-message.locals.test.js.map +1 -0
- package/dist/compiler/compile-message.test.js +113 -22
- package/dist/compiler/compile-message.test.js.map +1 -1
- package/dist/compiler/compile-pattern.d.ts +5 -0
- package/dist/compiler/compile-pattern.js +6 -18
- package/dist/compiler/compile-pattern.js.map +1 -1
- package/dist/compiler/compile-pattern.test.js +169 -5
- package/dist/compiler/compile-pattern.test.js.map +1 -1
- package/dist/compiler/compile-project.js +5 -1
- package/dist/compiler/compile-project.js.map +1 -1
- package/dist/compiler/compile-project.test.js +1260 -185
- package/dist/compiler/compile-project.test.js.map +1 -1
- package/dist/compiler/compile.js +36 -17
- package/dist/compiler/compile.js.map +1 -1
- package/dist/compiler/compile.test.js +71 -1
- package/dist/compiler/compile.test.js.map +1 -1
- package/dist/compiler/compiler-options.d.ts +41 -0
- package/dist/compiler/compiler-options.js.map +1 -1
- package/dist/compiler/create-readme.d.ts +4 -1
- package/dist/compiler/create-readme.js +33 -125
- package/dist/compiler/create-readme.js.map +1 -1
- package/dist/compiler/emit-ts-declarations.interop.test.js +2 -2
- package/dist/compiler/emit-ts-declarations.interop.test.js.map +1 -1
- package/dist/compiler/emit-ts-declarations.test.js +4 -4
- package/dist/compiler/emit-ts-declarations.test.js.map +1 -1
- package/dist/compiler/index.d.ts +1 -1
- package/dist/compiler/index.js +1 -1
- package/dist/compiler/index.js.map +1 -1
- package/dist/compiler/input-alias.d.ts +21 -0
- package/dist/compiler/input-alias.js +42 -0
- package/dist/compiler/input-alias.js.map +1 -0
- package/dist/compiler/input-alias.test.d.ts +1 -0
- package/dist/compiler/input-alias.test.js +58 -0
- package/dist/compiler/input-alias.test.js.map +1 -0
- package/dist/compiler/message-locals.d.ts +46 -0
- package/dist/compiler/message-locals.js +129 -0
- package/dist/compiler/message-locals.js.map +1 -0
- package/dist/compiler/output-structure/locale-modules.js +7 -1
- package/dist/compiler/output-structure/locale-modules.js.map +1 -1
- package/dist/compiler/output-structure/locale-modules.test.js +35 -0
- package/dist/compiler/output-structure/locale-modules.test.js.map +1 -1
- package/dist/compiler/output-structure/message-modules.js +6 -4
- package/dist/compiler/output-structure/message-modules.js.map +1 -1
- package/dist/compiler/output-structure/message-modules.test.js +35 -0
- package/dist/compiler/output-structure/message-modules.test.js.map +1 -1
- package/dist/compiler/registry.d.ts +12 -0
- package/dist/compiler/registry.js +158 -31
- package/dist/compiler/registry.js.map +1 -1
- package/dist/compiler/registry.test.d.ts +1 -0
- package/dist/compiler/registry.test.js +291 -0
- package/dist/compiler/registry.test.js.map +1 -0
- package/dist/compiler/runtime/create-runtime.d.ts +2 -0
- package/dist/compiler/runtime/create-runtime.js +9 -0
- package/dist/compiler/runtime/create-runtime.js.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-cookie.js +8 -1
- package/dist/compiler/runtime/extract-locale-from-cookie.js.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-cookie.test.js +26 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.test.js.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-header.js +47 -5
- package/dist/compiler/runtime/extract-locale-from-header.js.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-header.test.js +58 -0
- package/dist/compiler/runtime/extract-locale-from-header.test.js.map +1 -1
- package/dist/compiler/runtime/get-locale.js +7 -1
- package/dist/compiler/runtime/get-locale.js.map +1 -1
- package/dist/compiler/runtime/get-locale.test.js +95 -0
- package/dist/compiler/runtime/get-locale.test.js.map +1 -1
- package/dist/compiler/runtime/localize-href.d.ts +8 -0
- package/dist/compiler/runtime/localize-href.js +60 -0
- package/dist/compiler/runtime/localize-href.js.map +1 -1
- package/dist/compiler/runtime/localize-href.test.js +227 -1
- package/dist/compiler/runtime/localize-href.test.js.map +1 -1
- package/dist/compiler/runtime/set-locale.js +24 -6
- package/dist/compiler/runtime/set-locale.js.map +1 -1
- package/dist/compiler/runtime/set-locale.test.js +182 -0
- package/dist/compiler/runtime/set-locale.test.js.map +1 -1
- package/dist/compiler/runtime/type.d.ts +2 -0
- package/dist/compiler/runtime/type.js.map +1 -1
- package/dist/compiler/runtime/type.test.js +34 -0
- package/dist/compiler/runtime/type.test.js.map +1 -1
- package/dist/compiler/runtime/variables.d.ts +2 -0
- package/dist/compiler/runtime/variables.js +7 -0
- package/dist/compiler/runtime/variables.js.map +1 -1
- package/dist/compiler/server/create-server-file.js +24 -0
- package/dist/compiler/server/create-server-file.js.map +1 -1
- package/dist/compiler/server/create-server-file.test.js +8 -2
- package/dist/compiler/server/create-server-file.test.js.map +1 -1
- package/dist/compiler/server/middleware.d.ts +8 -2
- package/dist/compiler/server/middleware.js +204 -11
- package/dist/compiler/server/middleware.js.map +1 -1
- package/dist/compiler/server/middleware.test.js +750 -3
- package/dist/compiler/server/middleware.test.js.map +1 -1
- package/dist/eslint/index.d.ts +4 -0
- package/dist/eslint/index.js +8 -0
- package/dist/eslint/index.js.map +1 -0
- package/dist/eslint/no-dynamic-message-access.d.ts +3 -0
- package/dist/eslint/no-dynamic-message-access.js +150 -0
- package/dist/eslint/no-dynamic-message-access.js.map +1 -0
- package/dist/eslint/no-dynamic-message-access.test.d.ts +1 -0
- package/dist/eslint/no-dynamic-message-access.test.js +126 -0
- package/dist/eslint/no-dynamic-message-access.test.js.map +1 -0
- package/dist/eslint/oxlint.test.d.ts +1 -0
- package/dist/eslint/oxlint.test.js +64 -0
- package/dist/eslint/oxlint.test.js.map +1 -0
- package/dist/services/config/config-schema.d.ts +2 -0
- package/dist/services/config/config-schema.js +2 -0
- package/dist/services/config/config-schema.js.map +1 -1
- package/dist/services/config/load-paraglide-config.test.js +12 -0
- package/dist/services/config/load-paraglide-config.test.js.map +1 -1
- package/dist/services/env-variables/index.js +1 -1
- package/dist/services/env-variables/index.js.map +1 -1
- package/dist/services/file-handling/write-output.bench.js +51 -11
- package/dist/services/file-handling/write-output.bench.js.map +1 -1
- package/dist/services/file-handling/write-output.js +60 -20
- package/dist/services/file-handling/write-output.js.map +1 -1
- package/dist/services/file-handling/write-output.test.js +175 -0
- package/dist/services/file-handling/write-output.test.js.map +1 -1
- package/package.json +15 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"maybe-add-machine-translation.js","sourceRoot":"","sources":["../../../src/cli/steps/maybe-add-machine-translation.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"maybe-add-machine-translation.js","sourceRoot":"","sources":["../../../src/cli/steps/maybe-add-machine-translation.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AAEjE,MAAM,CAAC,MAAM,0BAA0B,GAmBnC,KAAK,EAAE,GAAG,EAAE,EAAE;IACjB,iCAAiC;IACjC,MAAM,qBAAqB,GAC1B,GAAG,CAAC,kBAAkB;QACtB,CAAC,GAAG,CAAC,WAAW,KAAK,KAAK;YACzB,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,MAAM,MAAM,CAAC,6CAA6C,EAAE;gBAC5D,IAAI,EAAE,SAAS;gBACf,OAAO,EAAE,IAAI;aACb,CAAC,CAAC,CAAC;IAEP,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAC5B,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,IAAI,CAAC;QACJ,mDAAmD;QACnD,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;QAE9D,2FAA2F;QAC3F,MAAM,iBAAiB,CAAC;YACvB,eAAe,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;gBAClC,iBAAiB;gBACjB,IAAI,OAAO,CAAC,aAAa,CAAC;oBAAE,OAAO,OAAO,CAAC;gBAC3C,OAAO;oBACN,GAAG,OAAO;oBACV,aAAa,EAAE,wBAAwB;iBACvC,CAAC;YACH,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;gBAC1B,iBAAiB;gBACjB,IAAI,OAAO,CAAC,mBAAmB,CAAC;oBAAE,OAAO,OAAO,CAAC;gBACjD,OAAO;oBACN,GAAG,OAAO;oBACV,mBAAmB,EAAE,sCAAsC,aAAa,CAAC,YAAY,CAAC,EAAE;iBACxF,CAAC;YACH,CAAC;SACD,CAAC,CAAC,GAAG,CAAC,CAAC;QAER,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,mDAAmD,CAAC,CAAC;IACzE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,GAAG,CAAC,MAAM,CAAC,KAAK,CACf;YACC,+DAA+D;YAC/D,uCAAuC;YACvC,mDAAmD;YACnD,KAAK;SACL,CAAC,IAAI,CAAC,IAAI,CAAC,CACZ,CAAC;QACF,IAAI,GAAG,CAAC,WAAW,KAAK,KAAK;YAAE,MAAM,KAAK,CAAC;IAC5C,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC,CAAC","sourcesContent":["import type { Logger } from \"../../services/logger/index.js\";\nimport type { CliStep } from \"../utils.js\";\nimport { prompt, shellArgument } from \"../utils.js\";\nimport { updatePackageJson } from \"./update-package-json.js\";\nimport path from \"node:path\";\nimport { INLANG_CLI_VERSION_RANGE } from \"./add-inlang-check.js\";\n\nexport const maybeAddMachineTranslation: CliStep<\n\t{\n\t\tfs: typeof import(\"node:fs/promises\");\n\t\tlogger: Logger;\n\t\tprojectPath: string;\n\t\troot: string;\n\t\tpackageJsonPath: string;\n\t\tinteractive?: boolean;\n\t\tmachineTranslation?: boolean;\n\t},\n\t{\n\t\tfs: typeof import(\"node:fs/promises\");\n\t\tlogger: Logger;\n\t\tprojectPath: string;\n\t\troot: string;\n\t\tpackageJsonPath: string;\n\t\tinteractive?: boolean;\n\t\tmachineTranslation?: boolean;\n\t}\n> = async (ctx) => {\n\t// Prompt for machine translation\n\tconst useMachineTranslation =\n\t\tctx.machineTranslation ??\n\t\t(ctx.interactive === false\n\t\t\t? false\n\t\t\t: await prompt(`Do you want to set up machine translations?`, {\n\t\t\t\t\ttype: \"confirm\",\n\t\t\t\t\tinitial: true,\n\t\t\t\t}));\n\n\tif (!useMachineTranslation) {\n\t\treturn ctx;\n\t}\n\n\ttry {\n\t\t// Determine the relative path from root to project\n\t\tconst relativePath = path.relative(ctx.root, ctx.projectPath);\n\n\t\t// Use updatePackageJson to add @inlang/cli to devDependencies and machine-translate script\n\t\tawait updatePackageJson({\n\t\t\tdevDependencies: async (devDeps) => {\n\t\t\t\t// exists already\n\t\t\t\tif (devDeps[\"@inlang/cli\"]) return devDeps;\n\t\t\t\treturn {\n\t\t\t\t\t...devDeps,\n\t\t\t\t\t\"@inlang/cli\": INLANG_CLI_VERSION_RANGE,\n\t\t\t\t};\n\t\t\t},\n\t\t\tscripts: async (scripts) => {\n\t\t\t\t// exists already\n\t\t\t\tif (scripts[\"machine-translate\"]) return scripts;\n\t\t\t\treturn {\n\t\t\t\t\t...scripts,\n\t\t\t\t\t\"machine-translate\": `inlang machine translate --project ${shellArgument(relativePath)}`,\n\t\t\t\t};\n\t\t\t},\n\t\t})(ctx);\n\n\t\tctx.logger.success(\"Added a machine-translate script to package.json.\");\n\t} catch (error) {\n\t\tctx.logger.error(\n\t\t\t[\n\t\t\t\t\"Failed to set up machine translation. Please add it manually:\",\n\t\t\t\t\"1. Add @inlang/cli to devDependencies\",\n\t\t\t\t\"2. Add a machine-translate script to package.json\",\n\t\t\t\terror,\n\t\t\t].join(\"\\n\")\n\t\t);\n\t\tif (ctx.interactive === false) throw error;\n\t}\n\n\treturn ctx;\n};\n"]}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { prompt } from "../utils.js";
|
|
2
2
|
import * as Sherlock from "@inlang/recommend-sherlock";
|
|
3
3
|
export const maybeAddSherlock = async (ctx) => {
|
|
4
|
+
if (ctx.sherlock === false ||
|
|
5
|
+
(ctx.interactive === false && ctx.sherlock !== true))
|
|
6
|
+
return ctx;
|
|
4
7
|
const isCertainlyVsCode = process?.env?.TERM_PROGRAM === "vscode";
|
|
5
|
-
let isUsingVsCode = isCertainlyVsCode;
|
|
6
|
-
if (!isCertainlyVsCode) {
|
|
8
|
+
let isUsingVsCode = ctx.sherlock === true || isCertainlyVsCode;
|
|
9
|
+
if (!isCertainlyVsCode && ctx.sherlock !== true) {
|
|
7
10
|
isUsingVsCode = await prompt(`Are you using Visual Studio Code or a compatible fork (like Cursor)?`, {
|
|
8
11
|
type: "confirm",
|
|
9
12
|
initial: true,
|
|
@@ -11,10 +14,11 @@ export const maybeAddSherlock = async (ctx) => {
|
|
|
11
14
|
}
|
|
12
15
|
if (isUsingVsCode === false)
|
|
13
16
|
return ctx;
|
|
14
|
-
const shouldAddSherlock =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
const shouldAddSherlock = ctx.sherlock ??
|
|
18
|
+
(await prompt(`Do you want to use the Sherlock VSCode (or Cursor) extension for a better DX?`, {
|
|
19
|
+
type: "confirm",
|
|
20
|
+
initial: true,
|
|
21
|
+
}));
|
|
18
22
|
if (shouldAddSherlock === false)
|
|
19
23
|
return ctx;
|
|
20
24
|
const settings = await ctx.project.settings.get();
|
|
@@ -22,7 +26,7 @@ export const maybeAddSherlock = async (ctx) => {
|
|
|
22
26
|
if (settings.modules?.some((m) => m.includes("plugin-m-function-matcher")) ===
|
|
23
27
|
false) {
|
|
24
28
|
// add the m function matcher plugin
|
|
25
|
-
settings.modules.push("https://cdn.jsdelivr.net/npm/@inlang/plugin-m-function-matcher@2.
|
|
29
|
+
settings.modules.push("https://cdn.jsdelivr.net/npm/@inlang/plugin-m-function-matcher@2.3.0/dist/index.js");
|
|
26
30
|
await ctx.project.settings.set(settings);
|
|
27
31
|
}
|
|
28
32
|
try {
|
|
@@ -36,6 +40,8 @@ export const maybeAddSherlock = async (ctx) => {
|
|
|
36
40
|
"Failed to add the Sherlock VSCode extension. Please open an issue",
|
|
37
41
|
error,
|
|
38
42
|
].join("\n"));
|
|
43
|
+
if (ctx.interactive === false)
|
|
44
|
+
throw error;
|
|
39
45
|
}
|
|
40
46
|
return ctx;
|
|
41
47
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"maybe-add-sherlock.js","sourceRoot":"","sources":["../../../src/cli/steps/maybe-add-sherlock.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,OAAO,KAAK,QAAQ,MAAM,4BAA4B,CAAC;AAEvD,MAAM,CAAC,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"maybe-add-sherlock.js","sourceRoot":"","sources":["../../../src/cli/steps/maybe-add-sherlock.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,OAAO,KAAK,QAAQ,MAAM,4BAA4B,CAAC;AAEvD,MAAM,CAAC,MAAM,gBAAgB,GASzB,KAAK,EAAE,GAAG,EAAE,EAAE;IACjB,IACC,GAAG,CAAC,QAAQ,KAAK,KAAK;QACtB,CAAC,GAAG,CAAC,WAAW,KAAK,KAAK,IAAI,GAAG,CAAC,QAAQ,KAAK,IAAI,CAAC;QAEpD,OAAO,GAAG,CAAC;IACZ,MAAM,iBAAiB,GAAG,OAAO,EAAE,GAAG,EAAE,YAAY,KAAK,QAAQ,CAAC;IAClE,IAAI,aAAa,GAAG,GAAG,CAAC,QAAQ,KAAK,IAAI,IAAI,iBAAiB,CAAC;IAE/D,IAAI,CAAC,iBAAiB,IAAI,GAAG,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;QACjD,aAAa,GAAG,MAAM,MAAM,CAC3B,sEAAsE,EACtE;YACC,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,IA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K,CAAC;IAC5C,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC,CAAC","sourcesContent":["import type { Logger } from \"../../services/logger/index.js\";\nimport { prompt } from \"../utils.js\";\nimport type { CliStep } from \"../utils.js\";\nimport type { InlangProject } from \"@inlang/sdk\";\nimport * as Sherlock from \"@inlang/recommend-sherlock\";\n\nexport const maybeAddSherlock: CliStep<\n\t{\n\t\tfs: typeof import(\"node:fs/promises\");\n\t\tlogger: Logger;\n\t\tproject: InlangProject;\n\t\tinteractive?: boolean;\n\t\tsherlock?: boolean;\n\t},\n\tunknown\n> = async (ctx) => {\n\tif (\n\t\tctx.sherlock === false ||\n\t\t(ctx.interactive === false && ctx.sherlock !== true)\n\t)\n\t\treturn ctx;\n\tconst isCertainlyVsCode = process?.env?.TERM_PROGRAM === \"vscode\";\n\tlet isUsingVsCode = ctx.sherlock === true || isCertainlyVsCode;\n\n\tif (!isCertainlyVsCode && ctx.sherlock !== true) {\n\t\tisUsingVsCode = await prompt(\n\t\t\t`Are you using Visual Studio Code or a compatible fork (like Cursor)?`,\n\t\t\t{\n\t\t\t\ttype: \"confirm\",\n\t\t\t\tinitial: true,\n\t\t\t}\n\t\t);\n\t}\n\tif (isUsingVsCode === false) return ctx;\n\n\tconst shouldAddSherlock =\n\t\tctx.sherlock ??\n\t\t(await prompt(\n\t\t\t`Do you want to use the Sherlock VSCode (or Cursor) extension for a better DX?`,\n\t\t\t{\n\t\t\t\ttype: \"confirm\",\n\t\t\t\tinitial: true,\n\t\t\t}\n\t\t));\n\tif (shouldAddSherlock === false) return ctx;\n\n\tconst settings = await ctx.project.settings.get();\n\n\t// m function matcher is not installed\n\tif (\n\t\tsettings.modules?.some((m) => m.includes(\"plugin-m-function-matcher\")) ===\n\t\tfalse\n\t) {\n\t\t// add the m function matcher plugin\n\t\tsettings.modules.push(\n\t\t\t\"https://cdn.jsdelivr.net/npm/@inlang/plugin-m-function-matcher@2.3.0/dist/index.js\"\n\t\t);\n\t\tawait ctx.project.settings.set(settings);\n\t}\n\n\ttry {\n\t\tif (!(await Sherlock.isAdopted({ fs: ctx.fs }))) {\n\t\t\tawait Sherlock.add({ fs: ctx.fs });\n\t\t\tctx.logger.success(\n\t\t\t\t\"Added the Sherlock VSCode extension to the workspace recommendations.\"\n\t\t\t);\n\t\t}\n\t} catch (error) {\n\t\tctx.logger.error(\n\t\t\t[\n\t\t\t\t\"Failed to add the Sherlock VSCode extension. Please open an issue\",\n\t\t\t\terror,\n\t\t\t].join(\"\\n\")\n\t\t);\n\t\tif (ctx.interactive === false) throw error;\n\t}\n\n\treturn ctx;\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { test, expect, vi, afterEach } from "vitest";
|
|
2
|
+
import { memfs } from "memfs";
|
|
3
|
+
import * as Sherlock from "@inlang/recommend-sherlock";
|
|
4
|
+
import { Logger } from "../../services/logger/index.js";
|
|
5
|
+
import { getNewProjectTemplate } from "../defaults.js";
|
|
6
|
+
import { maybeAddSherlock } from "./maybe-add-sherlock.js";
|
|
7
|
+
vi.mock("@inlang/recommend-sherlock", () => ({
|
|
8
|
+
isAdopted: vi.fn().mockResolvedValue(false),
|
|
9
|
+
add: vi.fn().mockResolvedValue(undefined),
|
|
10
|
+
}));
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
vi.unstubAllEnvs();
|
|
13
|
+
vi.clearAllMocks();
|
|
14
|
+
});
|
|
15
|
+
test("headless setup skips editor recommendations unless explicitly requested", async () => {
|
|
16
|
+
vi.stubEnv("TERM_PROGRAM", "vscode");
|
|
17
|
+
const fs = memfs().fs;
|
|
18
|
+
const project = {
|
|
19
|
+
settings: { get: vi.fn().mockResolvedValue(getNewProjectTemplate()) },
|
|
20
|
+
};
|
|
21
|
+
await maybeAddSherlock({
|
|
22
|
+
fs: fs.promises,
|
|
23
|
+
project,
|
|
24
|
+
logger: new Logger({ silent: true, prefix: false }),
|
|
25
|
+
interactive: false,
|
|
26
|
+
});
|
|
27
|
+
expect(Sherlock.add).not.toHaveBeenCalled();
|
|
28
|
+
});
|
|
29
|
+
test("explicit editor opt-in works headlessly outside a VSCode terminal", async () => {
|
|
30
|
+
vi.stubEnv("TERM_PROGRAM", "");
|
|
31
|
+
const fs = memfs().fs;
|
|
32
|
+
const project = {
|
|
33
|
+
settings: { get: vi.fn().mockResolvedValue(getNewProjectTemplate()) },
|
|
34
|
+
};
|
|
35
|
+
await maybeAddSherlock({
|
|
36
|
+
fs: fs.promises,
|
|
37
|
+
project,
|
|
38
|
+
logger: new Logger({ silent: true, prefix: false }),
|
|
39
|
+
interactive: false,
|
|
40
|
+
sherlock: true,
|
|
41
|
+
});
|
|
42
|
+
expect(Sherlock.add).toHaveBeenCalledOnce();
|
|
43
|
+
});
|
|
44
|
+
//# sourceMappingURL=maybe-add-sherlock.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maybe-add-sherlock.test.js","sourceRoot":"","sources":["../../../src/cli/steps/maybe-add-sherlock.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B,OAAO,KAAK,QAAQ,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,EAAE,CAAC,IAAI,CAAC,4BAA4B,EAAE,GAAG,EAAE,CAAC,CAAC;IAC5C,SAAS,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,KAAK,CAAC;IAC3C,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;CACz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AM,EAAE,KAAK,EAAE,CAAC;QACnD,WAAW,EAAE,KAAK;QAClB,QAAQ,EAAE,IAAI;KACd,CAAC,CAAC;IACH,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,oBAAoB,EAAE,CAAC;AAC7C,CAAC,CAAC,CAAC","sourcesContent":["import { test, expect, vi, afterEach } from \"vitest\";\nimport { memfs } from \"memfs\";\nimport type { InlangProject } from \"@inlang/sdk\";\nimport * as Sherlock from \"@inlang/recommend-sherlock\";\nimport { Logger } from \"../../services/logger/index.js\";\nimport { getNewProjectTemplate } from \"../defaults.js\";\nimport { maybeAddSherlock } from \"./maybe-add-sherlock.js\";\n\nvi.mock(\"@inlang/recommend-sherlock\", () => ({\n\tisAdopted: vi.fn().mockResolvedValue(false),\n\tadd: vi.fn().mockResolvedValue(undefined),\n}));\nafterEach(() => {\n\tvi.unstubAllEnvs();\n\tvi.clearAllMocks();\n});\n\ntest(\"headless setup skips editor recommendations unless explicitly requested\", async () => {\n\tvi.stubEnv(\"TERM_PROGRAM\", \"vscode\");\n\tconst fs = memfs().fs as unknown as typeof import(\"node:fs\");\n\tconst project = {\n\t\tsettings: { get: vi.fn().mockResolvedValue(getNewProjectTemplate()) },\n\t} as unknown as InlangProject;\n\tawait maybeAddSherlock({\n\t\tfs: fs.promises,\n\t\tproject,\n\t\tlogger: new Logger({ silent: true, prefix: false }),\n\t\tinteractive: false,\n\t});\n\texpect(Sherlock.add).not.toHaveBeenCalled();\n});\n\ntest(\"explicit editor opt-in works headlessly outside a VSCode terminal\", async () => {\n\tvi.stubEnv(\"TERM_PROGRAM\", \"\");\n\tconst fs = memfs().fs as unknown as typeof import(\"node:fs\");\n\tconst project = {\n\t\tsettings: { get: vi.fn().mockResolvedValue(getNewProjectTemplate()) },\n\t} as unknown as InlangProject;\n\tawait maybeAddSherlock({\n\t\tfs: fs.promises,\n\t\tproject,\n\t\tlogger: new Logger({ silent: true, prefix: false }),\n\t\tinteractive: false,\n\t\tsherlock: true,\n\t});\n\texpect(Sherlock.add).toHaveBeenCalledOnce();\n});\n"]}
|
|
@@ -2,6 +2,8 @@ import type { Logger } from "../../services/logger/index.js";
|
|
|
2
2
|
import type { CliStep } from "../utils.js";
|
|
3
3
|
export declare const promptForOutdir: CliStep<{
|
|
4
4
|
logger: Logger;
|
|
5
|
+
interactive?: boolean;
|
|
6
|
+
outdir?: string;
|
|
5
7
|
}, {
|
|
6
8
|
/** Relative path to the output directory */
|
|
7
9
|
outdir: string;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { DEFAULT_OUTDIR } from "../../services/config/defaults.js";
|
|
2
2
|
import { prompt } from "../utils.js";
|
|
3
3
|
export const promptForOutdir = async (ctx) => {
|
|
4
|
+
if (ctx.outdir !== undefined)
|
|
5
|
+
return { ...ctx, outdir: ctx.outdir };
|
|
6
|
+
if (ctx.interactive === false)
|
|
7
|
+
return { ...ctx, outdir: DEFAULT_OUTDIR };
|
|
4
8
|
const response = await prompt("Which outdir should Paraglide write generated files to?", {
|
|
5
9
|
type: "text",
|
|
6
10
|
initial: DEFAULT_OUTDIR,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt-for-outdir.js","sourceRoot":"","sources":["../../../src/cli/steps/prompt-for-outdir.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"prompt-for-outdir.js","sourceRoot":"","sources":["../../../src/cli/steps/prompt-for-outdir.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,eAAe,GAUxB,KAAK,EAAE,GAAG,EAAE,EAAE;IACjB,IAAI,GAAG,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC;IACpE,IAAI,GAAG,CAAC,WAAW,KAAK,KAAK;QAAE,OAAO,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC;IACzE,MAAM,QAAQ,GAAG,MAAM,MAAM,CAC5B,yDAAyD,EACzD;QACC,IAAI,EAAE,MAAM;QACZ,OAAO,EAAE,cAAc;QACvB,OAAO,EAAE,cAAc;QACvB,WAAW,EACV,8EAA8E;KAC/E,CACD,CAAC;IAEF,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,GAAG,CAAC,MAAM,CAAC,IAAI,CACd,sEAAsE,CACtE,CAAC;QACF,OAAO,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;IAED,OAAO;QACN,GAAG,GAAG;QACN,MAAM,EAAE,QAAQ;KAChB,CAAC;AACH,CAAC,CAAC","sourcesContent":["import type { Logger } from \"../../services/logger/index.js\";\nimport { DEFAULT_OUTDIR } from \"../../services/config/defaults.js\";\nimport { prompt } from \"../utils.js\";\nimport type { CliStep } from \"../utils.js\";\n\nexport const promptForOutdir: CliStep<\n\t{\n\t\tlogger: Logger;\n\t\tinteractive?: boolean;\n\t\toutdir?: string;\n\t},\n\t{\n\t\t/** Relative path to the output directory */\n\t\toutdir: string;\n\t}\n> = async (ctx) => {\n\tif (ctx.outdir !== undefined) return { ...ctx, outdir: ctx.outdir };\n\tif (ctx.interactive === false) return { ...ctx, outdir: DEFAULT_OUTDIR };\n\tconst response = await prompt(\n\t\t\"Which outdir should Paraglide write generated files to?\",\n\t\t{\n\t\t\ttype: \"text\",\n\t\t\tinitial: DEFAULT_OUTDIR,\n\t\t\tdefault: DEFAULT_OUTDIR,\n\t\t\tplaceholder:\n\t\t\t\t\"Relative path from the package root to the desired compiler output directory\",\n\t\t}\n\t);\n\n\tif (!response.startsWith(\"./\")) {\n\t\tctx.logger.warn(\n\t\t\t\"You must enter a valid relative path starting from the package root.\"\n\t\t);\n\t\treturn await promptForOutdir(ctx);\n\t}\n\n\treturn {\n\t\t...ctx,\n\t\toutdir: response,\n\t};\n};\n"]}
|
|
@@ -14,20 +14,15 @@ export function updatePackageJson(opt) {
|
|
|
14
14
|
}
|
|
15
15
|
catch {
|
|
16
16
|
ctx.logger.error(`Your ./package.json does not contain valid JSON. Please fix it and try again.`);
|
|
17
|
-
|
|
17
|
+
throw new Error("package.json must contain a valid JSON object.");
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
catch {
|
|
28
|
-
return ctx;
|
|
29
|
-
}
|
|
30
|
-
await ctx.fs.writeFile("./package.json", stringify(pkg));
|
|
19
|
+
if (opt.dependencies)
|
|
20
|
+
pkg.dependencies = await opt.dependencies(pkg.dependencies || {});
|
|
21
|
+
if (opt.devDependencies)
|
|
22
|
+
pkg.devDependencies = await opt.devDependencies(pkg.devDependencies || {});
|
|
23
|
+
if (opt.scripts)
|
|
24
|
+
pkg.scripts = await opt.scripts(pkg.scripts || {});
|
|
25
|
+
await ctx.fs.writeFile(ctx.packageJsonPath, stringify(pkg));
|
|
31
26
|
return ctx;
|
|
32
27
|
};
|
|
33
28
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-package-json.js","sourceRoot":"","sources":["../../../src/cli/steps/update-package-json.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AAEjF,MAAM,UAAU,iBAAiB,CAAC,GAUjC;IAQA,OAAO,KAAK,EAAE,GAAG,EAAE,EAAE;QACpB,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,eAAe,EAAE;YACvD,QAAQ,EAAE,OA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G,CAAC,YAAY,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC;QACnE,IAAI,GAAG,CAAC,eAAe;YACtB,GAAG,CAAC,eAAe,GAAG,MAAM,GAAG,CAAC,eAAe,CAC9C,GAAG,CAAC,eAAe,IAAI,EAAE,CACzB,CAAC;QACH,IAAI,GAAG,CAAC,OAAO;YAAE,GAAG,CAAC,OAAO,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;QACpE,MAAM,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,eAAe,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;QAC5D,OAAO,GAAG,CAAC;IACZ,CAAC,CAAC;AACH,CAAC","sourcesContent":["import type { Logger } from \"../../services/logger/index.js\";\nimport type { CliStep } from \"../utils.js\";\nimport { detectJsonFormatting } from \"../../utilities/detect-json-formatting.js\";\n\nexport function updatePackageJson(opt: {\n\tdependencies?: (\n\t\tdeps: Record<string, string>\n\t) => Promise<Record<string, string>>;\n\tdevDependencies?: (\n\t\tdevDeps: Record<string, string>\n\t) => Promise<Record<string, string>>;\n\tscripts?: (\n\t\tscripts: Record<string, string>\n\t) => Promise<Record<string, string>>;\n}): CliStep<\n\t{\n\t\tpackageJsonPath: string;\n\t\tfs: typeof import(\"node:fs/promises\");\n\t\tlogger: Logger;\n\t},\n\tunknown\n> {\n\treturn async (ctx) => {\n\t\tconst file = await ctx.fs.readFile(ctx.packageJsonPath, {\n\t\t\tencoding: \"utf-8\",\n\t\t});\n\n\t\tconst stringify = detectJsonFormatting(file);\n\n\t\tlet pkg;\n\t\ttry {\n\t\t\tpkg = JSON.parse(file);\n\t\t\tif (typeof pkg !== \"object\" || pkg === null) {\n\t\t\t\tthrow new Error();\n\t\t\t}\n\t\t} catch {\n\t\t\tctx.logger.error(\n\t\t\t\t`Your ./package.json does not contain valid JSON. Please fix it and try again.`\n\t\t\t);\n\t\t\tthrow new Error(\"package.json must contain a valid JSON object.\");\n\t\t}\n\n\t\tif (opt.dependencies)\n\t\t\tpkg.dependencies = await opt.dependencies(pkg.dependencies || {});\n\t\tif (opt.devDependencies)\n\t\t\tpkg.devDependencies = await opt.devDependencies(\n\t\t\t\tpkg.devDependencies || {}\n\t\t\t);\n\t\tif (opt.scripts) pkg.scripts = await opt.scripts(pkg.scripts || {});\n\t\tawait ctx.fs.writeFile(ctx.packageJsonPath, stringify(pkg));\n\t\treturn ctx;\n\t};\n}\n"]}
|
|
@@ -3,6 +3,8 @@ import type { Logger } from "../../services/logger/index.js";
|
|
|
3
3
|
export declare const maybeUpdateTsConfig: CliStep<{
|
|
4
4
|
fs: typeof import("node:fs/promises");
|
|
5
5
|
logger: Logger;
|
|
6
|
+
interactive?: boolean;
|
|
7
|
+
root?: string;
|
|
6
8
|
}, unknown>;
|
|
7
9
|
/**
|
|
8
10
|
* Paraligde JS compiles to JS with JSDoc comments. TypeScript doesn't allow JS files by default.
|
|
@@ -10,6 +12,8 @@ export declare const maybeUpdateTsConfig: CliStep<{
|
|
|
10
12
|
export declare const maybeUpdateTsConfigAllowJs: CliStep<{
|
|
11
13
|
fs: typeof import("node:fs/promises");
|
|
12
14
|
logger: Logger;
|
|
15
|
+
interactive?: boolean;
|
|
16
|
+
root?: string;
|
|
13
17
|
}, unknown>;
|
|
14
18
|
/**
|
|
15
19
|
* Recursively checks whether allowJs is enabled in the provided tsconfig or any
|
|
@@ -24,3 +28,5 @@ export declare const maybeUpdateTsConfigAllowJs: CliStep<{
|
|
|
24
28
|
* ```
|
|
25
29
|
*/
|
|
26
30
|
export declare const hasAllowJsEnabled: (tsconfigPath: string, fs: typeof import("node:fs/promises"), visited?: Set<string>) => Promise<boolean>;
|
|
31
|
+
/** Edit one property while retaining comments and unrelated configuration text. */
|
|
32
|
+
export declare function enableAllowJs(text: string): string;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { prompt } from "../utils.js";
|
|
2
2
|
import JSON5 from "json5";
|
|
3
|
+
import { parseTree, modify, applyEdits } from "jsonc-parser";
|
|
3
4
|
import { pathExists } from "../../services/file-handling/exists.js";
|
|
4
5
|
import nodePath from "node:path";
|
|
5
6
|
import { createRequire } from "node:module";
|
|
@@ -13,13 +14,20 @@ export const maybeUpdateTsConfig = async (ctx) => {
|
|
|
13
14
|
* Paraligde JS compiles to JS with JSDoc comments. TypeScript doesn't allow JS files by default.
|
|
14
15
|
*/
|
|
15
16
|
export const maybeUpdateTsConfigAllowJs = async (ctx) => {
|
|
16
|
-
|
|
17
|
+
const configPath = nodePath.resolve(ctx.root ?? process.cwd(), "tsconfig.json");
|
|
18
|
+
if ((await pathExists(configPath, ctx.fs)) === false) {
|
|
17
19
|
return ctx;
|
|
18
20
|
}
|
|
19
|
-
if (await hasAllowJsEnabled(
|
|
21
|
+
if (await hasAllowJsEnabled(configPath, ctx.fs)) {
|
|
20
22
|
// all clear, allowJs is already set to true
|
|
21
23
|
return ctx;
|
|
22
24
|
}
|
|
25
|
+
if (ctx.interactive === false) {
|
|
26
|
+
const text = await ctx.fs.readFile(configPath, "utf8");
|
|
27
|
+
await ctx.fs.writeFile(configPath, enableAllowJs(text));
|
|
28
|
+
ctx.logger.success("Enabled compilerOptions.allowJs in tsconfig.json.");
|
|
29
|
+
return ctx;
|
|
30
|
+
}
|
|
23
31
|
ctx.logger.info(`The option \`compilerOptions.allowJs\` needs to be set to \`true\` in the \`tsconfig.json\` file:
|
|
24
32
|
|
|
25
33
|
\`{
|
|
@@ -37,7 +45,7 @@ export const maybeUpdateTsConfigAllowJs = async (ctx) => {
|
|
|
37
45
|
ctx.logger.warn("Continuing without adjusting the tsconfig.json. This may lead to type errors.");
|
|
38
46
|
return ctx;
|
|
39
47
|
}
|
|
40
|
-
if (await hasAllowJsEnabled(
|
|
48
|
+
if (await hasAllowJsEnabled(configPath, ctx.fs)) {
|
|
41
49
|
isValid = true;
|
|
42
50
|
return ctx;
|
|
43
51
|
}
|
|
@@ -180,7 +188,7 @@ const resolveReferenceConfig = async (referenceSpecifier, baseDir, fs) => {
|
|
|
180
188
|
// * errors with Paraglide-JS.
|
|
181
189
|
// */
|
|
182
190
|
// const maybeUpdateTsConfigModuleResolution: CliStep<
|
|
183
|
-
// { fs: typeof import("node:fs/promises"); logger: Logger },
|
|
191
|
+
// { fs: typeof import("node:fs/promises"); logger: Logger; interactive?: boolean; root?: string },
|
|
184
192
|
// unknown
|
|
185
193
|
// > = async (ctx) => {
|
|
186
194
|
// if ((await pathExists("./tsconfig.json", ctx.fs)) === false) {
|
|
@@ -267,4 +275,19 @@ const resolveReferenceConfig = async (referenceSpecifier, baseDir, fs) => {
|
|
|
267
275
|
// }
|
|
268
276
|
// return ctx;
|
|
269
277
|
// };
|
|
278
|
+
/** Edit one property while retaining comments and unrelated configuration text. */
|
|
279
|
+
export function enableAllowJs(text) {
|
|
280
|
+
const errors = [];
|
|
281
|
+
const tree = parseTree(text, errors, { allowTrailingComma: true });
|
|
282
|
+
if (errors.length || tree?.type !== "object")
|
|
283
|
+
throw new Error("tsconfig.json must be a valid JSON-with-comments object.");
|
|
284
|
+
const indent = text.match(/\n([ \t]+)"/)?.[1] ?? " ";
|
|
285
|
+
return applyEdits(text, modify(text, ["compilerOptions", "allowJs"], true, {
|
|
286
|
+
formattingOptions: {
|
|
287
|
+
insertSpaces: !indent.includes("\t"),
|
|
288
|
+
tabSize: indent.length,
|
|
289
|
+
eol: text.includes("\r\n") ? "\r\n" : "\n",
|
|
290
|
+
},
|
|
291
|
+
}));
|
|
292
|
+
}
|
|
270
293
|
//# sourceMappingURL=update-ts-config.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-ts-config.js","sourceRoot":"","sources":["../../../src/cli/steps/update-ts-config.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,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AACvC,qCAAqC;AAErC,wCAAwC;AAExC,2BAA2B;AAC3B,UAAU;AACV,kDAAkD;AAClD,qBAAqB;AACrB,uBAAuB;AACvB,QAAQ;AACR,4EAA4E;AAC5E,yBAAyB;AACzB,SAAS;AACT,uDAAuD;AACvD,cAAc;AACd,sBAAsB;AACtB,4MAA4M;AAC5M,QAAQ;AACR,MAAM;AACN,KAAK;AAEL,iDAAiD;AACjD,yDAAyD;AACzD,4BAA4B;AAC5B,kDAAkD;AAClD,uDAAuD;AAEvD,QAAQ;AACR,wBAAwB;AACxB,sEAAsE;AACtE,OAAO;AACP,iEAAiE;AACjE,gBAAgB;AAChB,KAAK;AAEL,oBAAoB;AACpB,8GAA8G;AAE9G,MAAM;AACN,wBAAwB;AACxB,mCAAmC;AACnC,KAAK;AACL,OAAO;AACP,MAAM;AACN,wBAAwB;AACxB,+BAA+B;AAC/B,mCAAmC;AACnC,kEAAkE;AAClE,OAAO;AACP,uBAAuB;AACvB,qBAAqB;AACrB,OAAO;AACP,OAAO;AACP,8BAA8B;AAC9B,sBAAsB;AACtB,sFAAsF;AACtF,QAAQ;AACR,iBAAiB;AACjB,MAAM;AAEN,kFAAkF;AAClF,qCAAqC;AACrC,4BAA4B;AAC5B,qBAAqB;AACrB,iBAAiB;AACjB,MAAM;AAEN,4DAA4D;AAC5D,wBAAwB;AACxB,QAAQ;AACR,kCAAkC;AAClC,SAAS;AACT,oDAAoD;AACpD,2EAA2E;AAC3E,QAAQ;AACR,qBAAqB;AACrB,iBAAiB;AACjB,aAAa;AACb,uBAAuB;AACvB,qHAAqH;AACrH,QAAQ;AACR,MAAM;AACN,KAAK;AAEL,eAAe;AACf,KAAK","sourcesContent":["import type { CliStep } from \"../utils.js\";\nimport type { Logger } from \"../../services/logger/index.js\";\nimport { prompt } from \"../utils.js\";\nimport JSON5 from \"json5\";\nimport { pathExists } from \"../../services/file-handling/exists.js\";\nimport nodePath from \"node:path\";\nimport { createRequire } from \"node:module\";\n\nconst require = createRequire(import.meta.url);\n\nexport const maybeUpdateTsConfig: CliStep<\n\t{ fs: typeof import(\"node:fs/promises\"); logger: Logger },\n\tunknown\n> = async (ctx) => {\n\tconst ctx1 = await maybeUpdateTsConfigAllowJs(ctx);\n\treturn ctx1;\n\t// return await maybeUpdateTsConfigModuleResolution(ctx1);\n};\n\n/**\n * Paraligde JS compiles to JS with JSDoc comments. TypeScript doesn't allow JS files by default.\n */\nexport const maybeUpdateTsConfigAllowJs: CliStep<\n\t{ fs: typeof import(\"node:fs/promises\"); logger: Logger },\n\tunknown\n> = async (ctx) => {\n\tif ((await pathExists(\"./tsconfig.json\", ctx.fs)) === false) {\n\t\treturn ctx;\n\t}\n\tif (await hasAllowJsEnabled(\"./tsconfig.json\", ctx.fs)) {\n\t\t// all clear, allowJs is already set to true\n\t\treturn ctx;\n\t}\n\n\tctx.logger.info(\n\t\t`The option \\`compilerOptions.allowJs\\` needs to be set to \\`true\\` in the \\`tsconfig.json\\` file:\n\n\\`{\n \"compilerOptions\": {\n \"allowJs\": true\n }\n}\\``\n\t);\n\tlet isValid = false;\n\twhile (isValid === false) {\n\t\tconst response = await prompt(\n\t\t\t`Is \\`compilerOptions.allowJs\\` set to \\`true\\`?`,\n\t\t\t{\n\t\t\t\ttype: \"confirm\",\n\t\t\t\tinitial: true,\n\t\t\t}\n\t\t);\n\t\tif (response === false) {\n\t\t\tctx.logger.warn(\n\t\t\t\t\"Continuing without adjusting the tsconfig.json. This may lead to type errors.\"\n\t\t\t);\n\t\t\treturn ctx;\n\t\t}\n\n\t\tif (await hasAllowJsEnabled(\"./tsconfig.json\", ctx.fs)) {\n\t\t\tisValid = true;\n\t\t\treturn ctx;\n\t\t} else {\n\t\t\tctx.logger.error(\n\t\t\t\t\"The compiler options has not been adjusted. Please sets `compilerOptions.allowJs` to `true`.\"\n\t\t\t);\n\t\t}\n\t}\n\treturn ctx;\n};\n\n/**\n * Recursively checks whether allowJs is enabled in the provided tsconfig or any\n * referenced configuration files.\n *\n * @param tsconfigPath The path to the tsconfig to inspect.\n * @param fs The file system used to read the configs.\n * @param visited A set of already inspected files to avoid circular lookups.\n * @example\n * ```ts\n * await hasAllowJsEnabled(\"./tsconfig.json\", fs);\n * ```\n */\nexport const hasAllowJsEnabled = async (\n\ttsconfigPath: string,\n\tfs: typeof import(\"node:fs/promises\"),\n\tvisited: Set<string> = new Set()\n): Promise<boolean> => {\n\tconst normalizedPath = normalizeConfigPath(tsconfigPath);\n\n\tif (visited.has(normalizedPath)) {\n\t\treturn false;\n\t}\n\n\tvisited.add(normalizedPath);\n\n\tconst file = await fs.readFile(normalizedPath, { encoding: \"utf-8\" });\n\tconst tsconfig = JSON5.parse(file);\n\n\tif (tsconfig?.compilerOptions?.allowJs === true) {\n\t\treturn true;\n\t}\n\n\tconst baseDir = nodePath.dirname(normalizedPath);\n\n\tconst extendCandidates = Array.isArray(tsconfig?.extends)\n\t\t? tsconfig.extends\n\t\t: tsconfig?.extends\n\t\t\t? [tsconfig.extends]\n\t\t\t: [];\n\n\tfor (const candidate of extendCandidates) {\n\t\tif (typeof candidate !== \"string\") continue;\n\t\tconst resolved = await resolveExtendedConfig(candidate, baseDir, fs);\n\t\tif (resolved && (await hasAllowJsEnabled(resolved, fs, visited))) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tif (Array.isArray(tsconfig?.references)) {\n\t\tfor (const reference of tsconfig.references) {\n\t\t\tconst referencePath = reference?.path;\n\t\t\tif (typeof referencePath !== \"string\") continue;\n\t\t\tconst resolved = await resolveReferenceConfig(referencePath, baseDir, fs);\n\t\t\tif (resolved && (await hasAllowJsEnabled(resolved, fs, visited))) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn false;\n};\n\n/**\n * Normalizes a tsconfig path to an absolute path.\n */\nconst normalizeConfigPath = (configPath: string): string => {\n\treturn nodePath.isAbsolute(configPath)\n\t\t? configPath\n\t\t: nodePath.resolve(process.cwd(), configPath);\n};\n\n/**\n * Resolves the extended tsconfig path relative to the base config.\n */\nconst resolveExtendedConfig = async (\n\textendsSpecifier: string,\n\tbaseDir: string,\n\tfs: typeof import(\"node:fs/promises\")\n): Promise<string | undefined> => {\n\tconst candidates = new Set<string>();\n\tconst resolvedBase = nodePath.isAbsolute(extendsSpecifier)\n\t\t? extendsSpecifier\n\t\t: nodePath.resolve(baseDir, extendsSpecifier);\n\tcandidates.add(resolvedBase);\n\n\tif (nodePath.extname(resolvedBase) === \"\") {\n\t\tcandidates.add(`${resolvedBase}.json`);\n\t\tcandidates.add(nodePath.join(resolvedBase, \"tsconfig.json\"));\n\t}\n\n\tfor (const candidate of candidates) {\n\t\tif (await pathExists(candidate, fs)) {\n\t\t\treturn candidate;\n\t\t}\n\t}\n\n\ttry {\n\t\treturn require.resolve(extendsSpecifier, { paths: [baseDir] });\n\t} catch {\n\t\tif (extendsSpecifier.endsWith(\".json\") === false) {\n\t\t\ttry {\n\t\t\t\treturn require.resolve(`${extendsSpecifier}.json`, {\n\t\t\t\t\tpaths: [baseDir],\n\t\t\t\t});\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn undefined;\n};\n\n/**\n * Resolves the tsconfig referenced through the `references` property.\n */\nconst resolveReferenceConfig = async (\n\treferenceSpecifier: string,\n\tbaseDir: string,\n\tfs: typeof import(\"node:fs/promises\")\n): Promise<string | undefined> => {\n\tconst candidates = new Set<string>();\n\tconst resolvedBase = nodePath.isAbsolute(referenceSpecifier)\n\t\t? referenceSpecifier\n\t\t: nodePath.resolve(baseDir, referenceSpecifier);\n\tcandidates.add(resolvedBase);\n\n\tif (nodePath.extname(resolvedBase) === \"\") {\n\t\tcandidates.add(`${resolvedBase}.json`);\n\t\tcandidates.add(nodePath.join(resolvedBase, \"tsconfig.json\"));\n\t}\n\n\tfor (const candidate of candidates) {\n\t\tif (await pathExists(candidate, fs)) {\n\t\t\ttry {\n\t\t\t\tconst stats = await fs.stat(candidate);\n\t\t\t\tif (stats.isDirectory()) {\n\t\t\t\t\tconst directoryConfig = nodePath.join(candidate, \"tsconfig.json\");\n\t\t\t\t\tif (await pathExists(directoryConfig, fs)) {\n\t\t\t\t\t\treturn directoryConfig;\n\t\t\t\t\t}\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// ignore, we'll continue checking other candidates\n\t\t\t}\n\t\t\treturn candidate;\n\t\t}\n\t}\n\n\treturn undefined;\n};\n\n// /**\n// * Ensures that the moduleResolution compiler option is set to \"bundler\" or similar in the tsconfig.json.\n// *\n// * Otherwise, types defined in `package.exports` are not resolved by TypeScript. Leading to type\n// * errors with Paraglide-JS.\n// */\n// const maybeUpdateTsConfigModuleResolution: CliStep<\n// \t{ fs: typeof import(\"node:fs/promises\"); logger: Logger },\n// \tunknown\n// > = async (ctx) => {\n// \tif ((await pathExists(\"./tsconfig.json\", ctx.fs)) === false) {\n// \t\treturn ctx;\n// \t}\n// \tconst file = await ctx.fs.readFile(\"./tsconfig.json\", {\n// \t\tencoding: \"utf-8\",\n// \t});\n// \t// tsconfig allows comments ... FML\n// \tlet tsconfig = JSON5.parse(file);\n\n// \tlet parentTsConfig: any | undefined;\n\n// \tif (tsconfig.extends) {\n// \t\ttry {\n// \t\t\tconst parentTsConfigPath = nodePath.resolve(\n// \t\t\t\tprocess.cwd(),\n// \t\t\t\ttsconfig.extends\n// \t\t\t);\n// \t\t\tconst parentTsConfigFile = await ctx.fs.readFile(parentTsConfigPath, {\n// \t\t\t\tencoding: \"utf-8\",\n// \t\t\t});\n// \t\t\tparentTsConfig = JSON5.parse(parentTsConfigFile);\n// \t\t} catch {\n// \t\t\tctx.logger.warn(\n// \t\t\t\t`The tsconfig.json is extended from a tsconfig that couldn't be read. Maybe the file doesn't exist yet or is a NPM package. Continuing without taking the extended from tsconfig into consideration.`\n// \t\t\t);\n// \t\t}\n// \t}\n\n// \t// options that don't support package.exports\n// \tconst invalidOptions = [\"classic\", \"node\", \"node10\"];\n// \tconst moduleResolution =\n// \t\ttsconfig.compilerOptions?.moduleResolution ??\n// \t\tparentTsConfig?.compilerOptions?.moduleResolution;\n\n// \tif (\n// \t\tmoduleResolution &&\n// \t\tinvalidOptions.includes(moduleResolution.toLowerCase()) === false\n// \t) {\n// \t\t// the moduleResolution is already set to bundler or similar\n// \t\treturn ctx;\n// \t}\n\n// \tctx.logger.info(\n// \t\t`The \\`compilerOptions.moduleResolution\\` options must be set to \"Bundler\" in the \\`tsconfig.json\\` file:\n\n// \\`{\n// \"compilerOptions\": {\n// \"moduleResolution\": \"Bundler\"\n// }\n// }\\``\n// \t);\n// \tlet isValid = false;\n// \twhile (isValid === false) {\n// \t\tconst response = await prompt(\n// \t\t\t`Is \\`compilerOptions.moduleResolution\\` set to \"Bundler\"?`,\n// \t\t\t{\n// \t\t\t\ttype: \"confirm\",\n// \t\t\t\tinitial: true,\n// \t\t\t}\n// \t\t);\n// \t\tif (response === false) {\n// \t\t\tctx.logger.warn(\n// \t\t\t\t\"Continuing without adjusting the tsconfig.json. This may lead to type errors.\"\n// \t\t\t);\n// \t\t\treturn ctx;\n// \t\t}\n\n// \t\t// don't re-ask the question if there is an `extends` present in the tsconfig\n// \t\t// just trust that it's correct.\n// \t\tif (tsconfig.extends) {\n// \t\t\tisValid = true;\n// \t\t\treturn ctx;\n// \t\t}\n\n// \t\tconst file = await ctx.fs.readFile(\"./tsconfig.json\", {\n// \t\t\tencoding: \"utf-8\",\n// \t\t});\n// \t\ttsconfig = JSON5.parse(file);\n// \t\tif (\n// \t\t\ttsconfig?.compilerOptions?.moduleResolution &&\n// \t\t\ttsconfig.compilerOptions.moduleResolution.toLowerCase() === \"bundler\"\n// \t\t) {\n// \t\t\tisValid = true;\n// \t\t\treturn ctx;\n// \t\t} else {\n// \t\t\tctx.logger.error(\n// \t\t\t\t\"The compiler options have not been adjusted. Please set the `compilerOptions.moduleResolution` to `Bundler`.\"\n// \t\t\t);\n// \t\t}\n// \t}\n\n// \treturn ctx;\n// };\n"]}
|
|
1
|
+
{"version":3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C,CAAC,IAAI;SAC1C;KACD,CAAC,CACF,CAAC;AACH,CAAC","sourcesContent":["import type { CliStep } from \"../utils.js\";\nimport type { Logger } from \"../../services/logger/index.js\";\nimport { prompt } from \"../utils.js\";\nimport JSON5 from \"json5\";\nimport { parseTree, modify, applyEdits, type ParseError } from \"jsonc-parser\";\nimport { pathExists } from \"../../services/file-handling/exists.js\";\nimport nodePath from \"node:path\";\nimport { createRequire } from \"node:module\";\n\nconst require = createRequire(import.meta.url);\n\nexport const maybeUpdateTsConfig: CliStep<\n\t{\n\t\tfs: typeof import(\"node:fs/promises\");\n\t\tlogger: Logger;\n\t\tinteractive?: boolean;\n\t\troot?: string;\n\t},\n\tunknown\n> = async (ctx) => {\n\tconst ctx1 = await maybeUpdateTsConfigAllowJs(ctx);\n\treturn ctx1;\n\t// return await maybeUpdateTsConfigModuleResolution(ctx1);\n};\n\n/**\n * Paraligde JS compiles to JS with JSDoc comments. TypeScript doesn't allow JS files by default.\n */\nexport const maybeUpdateTsConfigAllowJs: CliStep<\n\t{\n\t\tfs: typeof import(\"node:fs/promises\");\n\t\tlogger: Logger;\n\t\tinteractive?: boolean;\n\t\troot?: string;\n\t},\n\tunknown\n> = async (ctx) => {\n\tconst configPath = nodePath.resolve(\n\t\tctx.root ?? process.cwd(),\n\t\t\"tsconfig.json\"\n\t);\n\tif ((await pathExists(configPath, ctx.fs)) === false) {\n\t\treturn ctx;\n\t}\n\tif (await hasAllowJsEnabled(configPath, ctx.fs)) {\n\t\t// all clear, allowJs is already set to true\n\t\treturn ctx;\n\t}\n\n\tif (ctx.interactive === false) {\n\t\tconst text = await ctx.fs.readFile(configPath, \"utf8\");\n\t\tawait ctx.fs.writeFile(configPath, enableAllowJs(text));\n\t\tctx.logger.success(\"Enabled compilerOptions.allowJs in tsconfig.json.\");\n\t\treturn ctx;\n\t}\n\n\tctx.logger.info(\n\t\t`The option \\`compilerOptions.allowJs\\` needs to be set to \\`true\\` in the \\`tsconfig.json\\` file:\n\n\\`{\n \"compilerOptions\": {\n \"allowJs\": true\n }\n}\\``\n\t);\n\tlet isValid = false;\n\twhile (isValid === false) {\n\t\tconst response = await prompt(\n\t\t\t`Is \\`compilerOptions.allowJs\\` set to \\`true\\`?`,\n\t\t\t{\n\t\t\t\ttype: \"confirm\",\n\t\t\t\tinitial: true,\n\t\t\t}\n\t\t);\n\t\tif (response === false) {\n\t\t\tctx.logger.warn(\n\t\t\t\t\"Continuing without adjusting the tsconfig.json. This may lead to type errors.\"\n\t\t\t);\n\t\t\treturn ctx;\n\t\t}\n\n\t\tif (await hasAllowJsEnabled(configPath, ctx.fs)) {\n\t\t\tisValid = true;\n\t\t\treturn ctx;\n\t\t} else {\n\t\t\tctx.logger.error(\n\t\t\t\t\"The compiler options has not been adjusted. Please sets `compilerOptions.allowJs` to `true`.\"\n\t\t\t);\n\t\t}\n\t}\n\treturn ctx;\n};\n\n/**\n * Recursively checks whether allowJs is enabled in the provided tsconfig or any\n * referenced configuration files.\n *\n * @param tsconfigPath The path to the tsconfig to inspect.\n * @param fs The file system used to read the configs.\n * @param visited A set of already inspected files to avoid circular lookups.\n * @example\n * ```ts\n * await hasAllowJsEnabled(\"./tsconfig.json\", fs);\n * ```\n */\nexport const hasAllowJsEnabled = async (\n\ttsconfigPath: string,\n\tfs: typeof import(\"node:fs/promises\"),\n\tvisited: Set<string> = new Set()\n): Promise<boolean> => {\n\tconst normalizedPath = normalizeConfigPath(tsconfigPath);\n\n\tif (visited.has(normalizedPath)) {\n\t\treturn false;\n\t}\n\n\tvisited.add(normalizedPath);\n\n\tconst file = await fs.readFile(normalizedPath, { encoding: \"utf-8\" });\n\tconst tsconfig = JSON5.parse(file);\n\n\tif (tsconfig?.compilerOptions?.allowJs === true) {\n\t\treturn true;\n\t}\n\n\tconst baseDir = nodePath.dirname(normalizedPath);\n\n\tconst extendCandidates = Array.isArray(tsconfig?.extends)\n\t\t? tsconfig.extends\n\t\t: tsconfig?.extends\n\t\t\t? [tsconfig.extends]\n\t\t\t: [];\n\n\tfor (const candidate of extendCandidates) {\n\t\tif (typeof candidate !== \"string\") continue;\n\t\tconst resolved = await resolveExtendedConfig(candidate, baseDir, fs);\n\t\tif (resolved && (await hasAllowJsEnabled(resolved, fs, visited))) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tif (Array.isArray(tsconfig?.references)) {\n\t\tfor (const reference of tsconfig.references) {\n\t\t\tconst referencePath = reference?.path;\n\t\t\tif (typeof referencePath !== \"string\") continue;\n\t\t\tconst resolved = await resolveReferenceConfig(referencePath, baseDir, fs);\n\t\t\tif (resolved && (await hasAllowJsEnabled(resolved, fs, visited))) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn false;\n};\n\n/**\n * Normalizes a tsconfig path to an absolute path.\n */\nconst normalizeConfigPath = (configPath: string): string => {\n\treturn nodePath.isAbsolute(configPath)\n\t\t? configPath\n\t\t: nodePath.resolve(process.cwd(), configPath);\n};\n\n/**\n * Resolves the extended tsconfig path relative to the base config.\n */\nconst resolveExtendedConfig = async (\n\textendsSpecifier: string,\n\tbaseDir: string,\n\tfs: typeof import(\"node:fs/promises\")\n): Promise<string | undefined> => {\n\tconst candidates = new Set<string>();\n\tconst resolvedBase = nodePath.isAbsolute(extendsSpecifier)\n\t\t? extendsSpecifier\n\t\t: nodePath.resolve(baseDir, extendsSpecifier);\n\tcandidates.add(resolvedBase);\n\n\tif (nodePath.extname(resolvedBase) === \"\") {\n\t\tcandidates.add(`${resolvedBase}.json`);\n\t\tcandidates.add(nodePath.join(resolvedBase, \"tsconfig.json\"));\n\t}\n\n\tfor (const candidate of candidates) {\n\t\tif (await pathExists(candidate, fs)) {\n\t\t\treturn candidate;\n\t\t}\n\t}\n\n\ttry {\n\t\treturn require.resolve(extendsSpecifier, { paths: [baseDir] });\n\t} catch {\n\t\tif (extendsSpecifier.endsWith(\".json\") === false) {\n\t\t\ttry {\n\t\t\t\treturn require.resolve(`${extendsSpecifier}.json`, {\n\t\t\t\t\tpaths: [baseDir],\n\t\t\t\t});\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn undefined;\n};\n\n/**\n * Resolves the tsconfig referenced through the `references` property.\n */\nconst resolveReferenceConfig = async (\n\treferenceSpecifier: string,\n\tbaseDir: string,\n\tfs: typeof import(\"node:fs/promises\")\n): Promise<string | undefined> => {\n\tconst candidates = new Set<string>();\n\tconst resolvedBase = nodePath.isAbsolute(referenceSpecifier)\n\t\t? referenceSpecifier\n\t\t: nodePath.resolve(baseDir, referenceSpecifier);\n\tcandidates.add(resolvedBase);\n\n\tif (nodePath.extname(resolvedBase) === \"\") {\n\t\tcandidates.add(`${resolvedBase}.json`);\n\t\tcandidates.add(nodePath.join(resolvedBase, \"tsconfig.json\"));\n\t}\n\n\tfor (const candidate of candidates) {\n\t\tif (await pathExists(candidate, fs)) {\n\t\t\ttry {\n\t\t\t\tconst stats = await fs.stat(candidate);\n\t\t\t\tif (stats.isDirectory()) {\n\t\t\t\t\tconst directoryConfig = nodePath.join(candidate, \"tsconfig.json\");\n\t\t\t\t\tif (await pathExists(directoryConfig, fs)) {\n\t\t\t\t\t\treturn directoryConfig;\n\t\t\t\t\t}\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// ignore, we'll continue checking other candidates\n\t\t\t}\n\t\t\treturn candidate;\n\t\t}\n\t}\n\n\treturn undefined;\n};\n\n// /**\n// * Ensures that the moduleResolution compiler option is set to \"bundler\" or similar in the tsconfig.json.\n// *\n// * Otherwise, types defined in `package.exports` are not resolved by TypeScript. Leading to type\n// * errors with Paraglide-JS.\n// */\n// const maybeUpdateTsConfigModuleResolution: CliStep<\n// \t{ fs: typeof import(\"node:fs/promises\"); logger: Logger; interactive?: boolean; root?: string },\n// \tunknown\n// > = async (ctx) => {\n// \tif ((await pathExists(\"./tsconfig.json\", ctx.fs)) === false) {\n// \t\treturn ctx;\n// \t}\n// \tconst file = await ctx.fs.readFile(\"./tsconfig.json\", {\n// \t\tencoding: \"utf-8\",\n// \t});\n// \t// tsconfig allows comments ... FML\n// \tlet tsconfig = JSON5.parse(file);\n\n// \tlet parentTsConfig: any | undefined;\n\n// \tif (tsconfig.extends) {\n// \t\ttry {\n// \t\t\tconst parentTsConfigPath = nodePath.resolve(\n// \t\t\t\tprocess.cwd(),\n// \t\t\t\ttsconfig.extends\n// \t\t\t);\n// \t\t\tconst parentTsConfigFile = await ctx.fs.readFile(parentTsConfigPath, {\n// \t\t\t\tencoding: \"utf-8\",\n// \t\t\t});\n// \t\t\tparentTsConfig = JSON5.parse(parentTsConfigFile);\n// \t\t} catch {\n// \t\t\tctx.logger.warn(\n// \t\t\t\t`The tsconfig.json is extended from a tsconfig that couldn't be read. Maybe the file doesn't exist yet or is a NPM package. Continuing without taking the extended from tsconfig into consideration.`\n// \t\t\t);\n// \t\t}\n// \t}\n\n// \t// options that don't support package.exports\n// \tconst invalidOptions = [\"classic\", \"node\", \"node10\"];\n// \tconst moduleResolution =\n// \t\ttsconfig.compilerOptions?.moduleResolution ??\n// \t\tparentTsConfig?.compilerOptions?.moduleResolution;\n\n// \tif (\n// \t\tmoduleResolution &&\n// \t\tinvalidOptions.includes(moduleResolution.toLowerCase()) === false\n// \t) {\n// \t\t// the moduleResolution is already set to bundler or similar\n// \t\treturn ctx;\n// \t}\n\n// \tctx.logger.info(\n// \t\t`The \\`compilerOptions.moduleResolution\\` options must be set to \"Bundler\" in the \\`tsconfig.json\\` file:\n\n// \\`{\n// \"compilerOptions\": {\n// \"moduleResolution\": \"Bundler\"\n// }\n// }\\``\n// \t);\n// \tlet isValid = false;\n// \twhile (isValid === false) {\n// \t\tconst response = await prompt(\n// \t\t\t`Is \\`compilerOptions.moduleResolution\\` set to \"Bundler\"?`,\n// \t\t\t{\n// \t\t\t\ttype: \"confirm\",\n// \t\t\t\tinitial: true,\n// \t\t\t}\n// \t\t);\n// \t\tif (response === false) {\n// \t\t\tctx.logger.warn(\n// \t\t\t\t\"Continuing without adjusting the tsconfig.json. This may lead to type errors.\"\n// \t\t\t);\n// \t\t\treturn ctx;\n// \t\t}\n\n// \t\t// don't re-ask the question if there is an `extends` present in the tsconfig\n// \t\t// just trust that it's correct.\n// \t\tif (tsconfig.extends) {\n// \t\t\tisValid = true;\n// \t\t\treturn ctx;\n// \t\t}\n\n// \t\tconst file = await ctx.fs.readFile(\"./tsconfig.json\", {\n// \t\t\tencoding: \"utf-8\",\n// \t\t});\n// \t\ttsconfig = JSON5.parse(file);\n// \t\tif (\n// \t\t\ttsconfig?.compilerOptions?.moduleResolution &&\n// \t\t\ttsconfig.compilerOptions.moduleResolution.toLowerCase() === \"bundler\"\n// \t\t) {\n// \t\t\tisValid = true;\n// \t\t\treturn ctx;\n// \t\t} else {\n// \t\t\tctx.logger.error(\n// \t\t\t\t\"The compiler options have not been adjusted. Please set the `compilerOptions.moduleResolution` to `Bundler`.\"\n// \t\t\t);\n// \t\t}\n// \t}\n\n// \treturn ctx;\n// };\n\n/** Edit one property while retaining comments and unrelated configuration text. */\nexport function enableAllowJs(text: string): string {\n\tconst errors: ParseError[] = [];\n\tconst tree = parseTree(text, errors, { allowTrailingComma: true });\n\tif (errors.length || tree?.type !== \"object\")\n\t\tthrow new Error(\"tsconfig.json must be a valid JSON-with-comments object.\");\n\tconst indent = text.match(/\\n([ \\t]+)\"/)?.[1] ?? \" \";\n\treturn applyEdits(\n\t\ttext,\n\t\tmodify(text, [\"compilerOptions\", \"allowJs\"], true, {\n\t\t\tformattingOptions: {\n\t\t\t\tinsertSpaces: !indent.includes(\"\\t\"),\n\t\t\t\ttabSize: indent.length,\n\t\t\t\teol: text.includes(\"\\r\\n\") ? \"\\r\\n\" : \"\\n\",\n\t\t\t},\n\t\t})\n\t);\n}\n"]}
|
package/dist/cli/utils.d.ts
CHANGED
|
@@ -34,3 +34,14 @@ export declare const promptSelection: <T extends string>(message: string, option
|
|
|
34
34
|
value: T;
|
|
35
35
|
}[];
|
|
36
36
|
}) => Promise<T>;
|
|
37
|
+
/**
|
|
38
|
+
* A path relative to `root` with forward slashes and without a leading `./`,
|
|
39
|
+
* for messages and package.json scripts.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* toRelativeDisplayPath("/app", "./project.inlang") // "project.inlang"
|
|
43
|
+
* toRelativeDisplayPath("/app", "/app/src/paraglide") // "src/paraglide"
|
|
44
|
+
*/
|
|
45
|
+
export declare function toRelativeDisplayPath(root: string, path: string): string;
|
|
46
|
+
/** Quote package-script arguments containing shell metacharacters. */
|
|
47
|
+
export declare function shellArgument(value: string): string;
|
package/dist/cli/utils.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import consola from "consola";
|
|
2
|
+
import nodePath from "node:path";
|
|
2
3
|
/**
|
|
3
4
|
* Wrapper to exit the process if the user presses CTRL+C.
|
|
4
5
|
*/
|
|
@@ -17,4 +18,24 @@ export const promptSelection = async (message, options = {
|
|
|
17
18
|
...options,
|
|
18
19
|
});
|
|
19
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* A path relative to `root` with forward slashes and without a leading `./`,
|
|
23
|
+
* for messages and package.json scripts.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* toRelativeDisplayPath("/app", "./project.inlang") // "project.inlang"
|
|
27
|
+
* toRelativeDisplayPath("/app", "/app/src/paraglide") // "src/paraglide"
|
|
28
|
+
*/
|
|
29
|
+
export function toRelativeDisplayPath(root, path) {
|
|
30
|
+
return nodePath
|
|
31
|
+
.relative(root, nodePath.resolve(root, path))
|
|
32
|
+
.split(nodePath.sep)
|
|
33
|
+
.join("/");
|
|
34
|
+
}
|
|
35
|
+
/** Quote package-script arguments containing shell metacharacters. */
|
|
36
|
+
export function shellArgument(value) {
|
|
37
|
+
return /^[A-Za-z0-9_./-]+$/.test(value)
|
|
38
|
+
? value
|
|
39
|
+
: "'" + value.replaceAll("'", `'"'"'`) + "'";
|
|
40
|
+
}
|
|
20
41
|
//# sourceMappingURL=utils.js.map
|
package/dist/cli/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/cli/utils.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/cli/utils.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,QAAQ,MAAM,WAAW,CAAC;AA6BjC;;GAEG;AACH,MAAM,CAAC,MAAM,MAAM,GAA0B,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;IACvE,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACxD,IAAI,QAAQ,EAAE,QAAQ,EAAE,KAAK,sBAAsB,EAAE,CAAC;QACrD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACjB,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EACnC,OAAe,EACf,OAAO,GAA4D;IAClE,OAAO,EAAE,EAAE;CACX,EACY,EAAE;IACf,OAAO,MAAM,CAAC,OAAO,EAAE;QACtB,IAAI,EAAE,QAAQ;QACd,GAAG,OAAO;KACV,CAA0B,CAAC;AAC7B,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY,EAAE,IAAY;IAC/D,OAAO,QAAQ;SACb,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;SAC5C,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC;SACnB,IAAI,CAAC,GAAG,CAAC,CAAC;AACb,CAAC;AAED,sEAAsE;AACtE,MAAM,UAAU,aAAa,CAAC,KAAa;IAC1C,OAAO,oBAAoB,CAAC,IAAI,CAAC,KAAK,CAAC;QACtC,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,GAAG,CAAC;AAC/C,CAAC","sourcesContent":["import consola from \"consola\";\nimport nodePath from \"node:path\";\n\n/**\n * One step in a CLI chain.\n * Defines which types the context needs to extend and how it extends the context\n *\n * Cli Steps can be chained to slowly build up the context over time\n *\n * @example\n * ```ts\n * const step1: CliStep<unknown, { foo: string }> = async (ctx) => {\n * return {...ctx, foo: \"hello\" }\n * }\n *\n * const step2: CliStep<{ foo: string }, { bar: number }> = async (ctx) => {\n * return { ...ctx, bar: 42 }\n * }\n *\n * const initial = { baz: \"baz\" } as const;\n * const ctx1 = await step1(initial);\n * const ctx2 = await step2(ctx1);\n *\n * ctx2 // Has type { foo: string, bar: number, baz: \"baz\" }\n * ```\n */\nexport type CliStep<In extends object, Out> = <Ctx extends In>(\n\tctx: Ctx\n) => Promise<Ctx & Out>;\n\n/**\n * Wrapper to exit the process if the user presses CTRL+C.\n */\nexport const prompt: typeof consola.prompt = async (message, options) => {\n\tconst response = await consola.prompt(message, options);\n\tif (response?.toString() === \"Symbol(clack:cancel)\") {\n\t\tprocess.exit(0);\n\t}\n\treturn response;\n};\n\nexport const promptSelection = async <T extends string>(\n\tmessage: string,\n\toptions: { initial?: T; options: { label: string; value: T }[] } = {\n\t\toptions: [],\n\t}\n): Promise<T> => {\n\treturn prompt(message, {\n\t\ttype: \"select\",\n\t\t...options,\n\t}) as unknown as Promise<T>;\n};\n\n/**\n * A path relative to `root` with forward slashes and without a leading `./`,\n * for messages and package.json scripts.\n *\n * @example\n * toRelativeDisplayPath(\"/app\", \"./project.inlang\") // \"project.inlang\"\n * toRelativeDisplayPath(\"/app\", \"/app/src/paraglide\") // \"src/paraglide\"\n */\nexport function toRelativeDisplayPath(root: string, path: string): string {\n\treturn nodePath\n\t\t.relative(root, nodePath.resolve(root, path))\n\t\t.split(nodePath.sep)\n\t\t.join(\"/\");\n}\n\n/** Quote package-script arguments containing shell metacharacters. */\nexport function shellArgument(value: string): string {\n\treturn /^[A-Za-z0-9_./-]+$/.test(value)\n\t\t? value\n\t\t: \"'\" + value.replaceAll(\"'\", `'\"'\"'`) + \"'\";\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import { shellArgument } from "./utils.js";
|
|
5
|
+
test.each([
|
|
6
|
+
"./src/paraglide",
|
|
7
|
+
"space directory",
|
|
8
|
+
"quote'path",
|
|
9
|
+
"literal$word",
|
|
10
|
+
"semi;colon",
|
|
11
|
+
"back`tick",
|
|
12
|
+
])("package scripts preserve literal path %s", async (value) => {
|
|
13
|
+
const { stdout } = await promisify(execFile)("/bin/sh", ["-c", "printf '%s' " + shellArgument(value)], { env: {} });
|
|
14
|
+
expect(stdout).toBe(value);
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=utils.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.test.js","sourceRoot":"","sources":["../../src/cli/utils.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,IAAI,CAAC,IAAI,CAAC;IACT,iBAAiB;IACjB,iBAAiB;IACjB,YAAY;IACZ,cAAc;IACd,YAAY;IACZ,WAAW;CACX,CAAC,CAAC,0CAA0C,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;IAC9D,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,CAC3C,SAAS,EACT,CAAC,IAAI,EAAE,cAAc,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC,EAC7C,EAAE,GAAG,EAAE,EAAE,EAAE,CACX,CAAC;IACF,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC,CAAC,CAAC","sourcesContent":["import { test, expect } from \"vitest\";\nimport { execFile } from \"node:child_process\";\nimport { promisify } from \"node:util\";\nimport { shellArgument } from \"./utils.js\";\n\ntest.each([\n\t\"./src/paraglide\",\n\t\"space directory\",\n\t\"quote'path\",\n\t\"literal$word\",\n\t\"semi;colon\",\n\t\"back`tick\",\n])(\"package scripts preserve literal path %s\", async (value) => {\n\tconst { stdout } = await promisify(execFile)(\n\t\t\"/bin/sh\",\n\t\t[\"-c\", \"printf '%s' \" + shellArgument(value)],\n\t\t{ env: {} }\n\t);\n\texpect(stdout).toBe(value);\n});\n"]}
|
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import type { Declaration, FunctionReference } from "@inlang/sdk";
|
|
2
2
|
export declare function isRegistryFunction(name: string): boolean;
|
|
3
3
|
export declare function registryFunctionNamesForDisplay(): string;
|
|
4
|
+
/**
|
|
5
|
+
* Returns the value unformatted, and warns once per formatter name.
|
|
6
|
+
*
|
|
7
|
+
* Unknown annotations, on pattern expressions and on local variables, fall
|
|
8
|
+
* back to plain interpolation to avoid breaking compilation of messages
|
|
9
|
+
* imported from other i18n libraries (e.g. i18next's `{{value, customFormat}}`).
|
|
10
|
+
*/
|
|
11
|
+
export declare function ignoreUnknownFormatter(name: string, value: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* The names of the registry functions a message calls, collected while it is
|
|
14
|
+
* compiled. The output decides from it whether to import the registry.
|
|
15
|
+
*/
|
|
16
|
+
export type RegistryUsage = Set<string>;
|
|
4
17
|
/**
|
|
5
18
|
* Wraps a compiled expression value in a `registry.*` call if an
|
|
6
19
|
* annotation is present.
|
|
@@ -9,4 +22,38 @@ export declare function registryFunctionNamesForDisplay(): string;
|
|
|
9
22
|
* compileAnnotation("i?.count", "en", { type: "function-reference", name: "number", options: [] })
|
|
10
23
|
* >> 'registry.number("en", i?.count, {})'
|
|
11
24
|
*/
|
|
12
|
-
export declare function compileAnnotation(str: string, locale: string, annotation?: FunctionReference, declarations?: Declaration[]): string;
|
|
25
|
+
export declare function compileAnnotation(str: string, locale: string, annotation?: FunctionReference, declarations?: Declaration[], registryUsage?: RegistryUsage): string;
|
|
26
|
+
/**
|
|
27
|
+
* Translates the ICU MessageFormat 1 `style` of an annotation to the options
|
|
28
|
+
* of the registry function.
|
|
29
|
+
*
|
|
30
|
+
* The ICU1 plugin imports `{n, number, integer}` as `{$n :number style=integer}`
|
|
31
|
+
* and `{d, date, short}` as `{$d :date style=short}`. Intl knows the number
|
|
32
|
+
* styles "decimal", "percent", "currency" (with a `currency`) and "unit", and
|
|
33
|
+
* throws for others. The date and time styles are Intl's `dateStyle` and
|
|
34
|
+
* `timeStyle`. A style without an Intl equivalent, like an ICU skeleton
|
|
35
|
+
* (`::currency/EUR`), is left out with a warning.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* icuStyleAnnotation({ name: "date", options: [{ name: "style", value: "short" }] })
|
|
39
|
+
* >> { name: "date", options: [{ name: "dateStyle", value: "short" }] }
|
|
40
|
+
*/
|
|
41
|
+
export declare function icuStyleAnnotation(annotation: FunctionReference): FunctionReference;
|
|
42
|
+
/**
|
|
43
|
+
* Adds the default styles of `date`, `time` and `datetime` when the options
|
|
44
|
+
* pick no fields. Options like `timeZone` or `hour12` are kept.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* withDefaultDateTimeStyles({ name: "datetime", options: [] })
|
|
48
|
+
* >> { name: "datetime", options: [{ name: "dateStyle", value: "medium" }, { name: "timeStyle", value: "short" }] }
|
|
49
|
+
*/
|
|
50
|
+
export declare function withDefaultDateTimeStyles(annotation: FunctionReference): FunctionReference;
|
|
51
|
+
/**
|
|
52
|
+
* Returns the names of the variables the compiled options of an annotation
|
|
53
|
+
* read.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // {$date :relativetime unit=$unit}
|
|
57
|
+
* annotationVariableReferences(annotation) // ["unit"]
|
|
58
|
+
*/
|
|
59
|
+
export declare function annotationVariableReferences(annotation: FunctionReference): string[];
|