fluent-transpiler 0.3.2 → 0.4.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 CHANGED
@@ -1,6 +1,30 @@
1
- # fluent-transpiler
1
+ <div align="center">
2
2
 
3
- Transpile Fluent (ftl) files into optimized, tree-shakable, JavaScript EcmaScript Modules (esm).
3
+ <h1>fluent-transpiler</h1>
4
+ <p>Transpile Fluent (ftl) files into optimized, tree-shakable, JavaScript EcmaScript Modules (esm).</p>
5
+ <br />
6
+ <p>
7
+ <a href="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-unit.yml"><img src="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-unit.yml/badge.svg" alt="GitHub Actions unit test status"></a>
8
+ <a href="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-dast.yml"><img src="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-dast.yml/badge.svg" alt="GitHub Actions dast test status"></a>
9
+ <a href="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-perf.yml"><img src="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-perf.yml/badge.svg" alt="GitHub Actions perf test status"></a>
10
+ <a href="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-sast.yml"><img src="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-sast.yml/badge.svg" alt="GitHub Actions SAST test status"></a>
11
+ <a href="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-lint.yml"><img src="https://github.com/willfarrell/fluent-transpiler/actions/workflows/test-lint.yml/badge.svg" alt="GitHub Actions lint test status"></a>
12
+ <br/>
13
+ <a href="https://www.npmjs.com/package/fluent-transpiler"><img alt="npm version" src="https://img.shields.io/npm/v/fluent-transpiler.svg"></a>
14
+ <a href="https://packagephobia.com/result?p=fluent-transpiler"><img src="https://packagephobia.com/badge?p=fluent-transpiler" alt="npm install size"></a>
15
+ <a href="https://www.npmjs.com/package/fluent-transpiler">
16
+ <img alt="npm weekly downloads" src="https://img.shields.io/npm/dw/fluent-transpiler.svg"></a>
17
+ <a href="https://www.npmjs.com/package/fluent-transpiler#provenance">
18
+ <img alt="npm provenance" src="https://img.shields.io/badge/provenance-Yes-brightgreen"></a>
19
+ <br/>
20
+ <a href="https://scorecard.dev/viewer/?uri=github.com/willfarrell/fluent-transpiler"><img src="https://api.scorecard.dev/projects/github.com/willfarrell/fluent-transpiler/badge" alt="Open Source Security Foundation (OpenSSF) Scorecard"></a>
21
+ <a href="https://slsa.dev"><img src="https://slsa.dev/images/gh-badge-level3.svg" alt="SLSA 3"></a>
22
+ <a href="https://biomejs.dev"><img alt="Checked with Biome" src="https://img.shields.io/badge/Checked_with-Biome-60a5fa?style=flat&logo=biome"></a>
23
+ <a href="https://conventionalcommits.org"><img alt="Conventional Commits" src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white"></a>
24
+ <a href="https://github.com/willfarrell/fluent-transpiler/blob/main/package.json#L15">
25
+ <img alt="code coverage" src="https://img.shields.io/badge/code%20coverage-100%25-brightgreen"></a>
26
+ </p>
27
+ </div>
4
28
 
5
29
  ## Install
6
30
 
@@ -21,6 +45,9 @@ Arguments:
21
45
  Options:
22
46
  --locale <locale...> What locale(s) to be used. Multiple can be set to allow for fallback. i.e. en-CA
23
47
  --comments Include comments in output file.
48
+ --include-key <keys...> Allowed messages to be included. Default to include all.
49
+ --exclude-key <keys...> Ignored messages to be excluded. Default to exclude none.
50
+ --exclude-value <value> Set message to an empty string when it contains this value.
24
51
  --variable-notation <variableNotation> What variable notation to use with exports (choices: "camelCase", "pascalCase", "constantCase",
25
52
  "snakeCase", default: "camelCase")
26
53
  --disable-minify If disabled, all exported messages will have the same interface `(params) => ({value, attributes})`.
@@ -33,12 +60,15 @@ Options:
33
60
 
34
61
  | Option | Description |
35
62
  | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
36
- | locale | What locale(s) to be used. Multiple can be set to allow for fallback. i.e. en-CA |
37
- | comments | Include comments in output file. Default: true |
63
+ | locale | What locale(s) to be used. Multiple can be set to allow for fallback. i.e. `en-CA` |
64
+ | comments | Include comments in output file. Default: `true` |
65
+ | includeKey | Array of message keys to include. Default: `[]` (include all) |
66
+ | excludeKey | Array of message keys to exclude. Default: `[]` (exclude none) |
67
+ | excludeValue | Set message to an empty string when it contains this value. Default: `undefined` |
38
68
  | disableMinify | If disabled, all exported messages will have the same interface `(params) => ({value, attributes})`. Default: each exported message could be a different type based on what is needed to generate the message (`string`, `object`, `() => ''`, `() => ({})`) |
39
- | errorOnJunk | Throw error when `Junk` is parsed. Default: true |
40
- | variableNotation | What variable notation to use with exports. Default: `camelCase` |
41
- | useIsolating | Wrap placeable with \u2068 and \u2069. Default: false |
69
+ | errorOnJunk | Throw error when `Junk` is parsed. Default: `true` |
70
+ | variableNotation | What variable notation to use with exports. Choices: `camelCase`, `pascalCase`, `snakeCase`, `constantCase`. Default: `camelCase` |
71
+ | useIsolating | Wrap placeable with \u2068 and \u2069. Default: `false` |
42
72
  | exportDefault | Allows the overwriting of the `export default` to allow for custom uses. Default: See code |
43
73
 
44
74
  ```javascript
package/cli.js CHANGED
@@ -1,86 +1,89 @@
1
1
  #!/usr/bin/env node
2
+ // Copyright 2026 will Farrell, and fluent-transpiler contributors.
3
+ // SPDX-License-Identifier: MIT
2
4
 
3
- import { createReadStream } from 'fs'
4
- import { stat, readFile, writeFile } from 'node:fs/promises'
5
- import { Command, Option } from 'commander'
6
- import compile from './index.js'
5
+ import { readFile, stat, writeFile } from "node:fs/promises";
6
+ import { Command, Option } from "commander";
7
+ import compile from "./index.js";
7
8
 
8
9
  const fileExists = async (filepath) => {
9
- const stats = await stat(filepath)
10
- if (!stats.isFile()) {
11
- throw new Error(`${filepath} is not a file`)
12
- }
13
- }
10
+ const stats = await stat(filepath);
11
+ if (!stats.isFile()) {
12
+ throw new Error(`${filepath} is not a file`);
13
+ }
14
+ };
14
15
 
15
16
  new Command()
16
- .name('ftl')
17
- .description('Compile Fluent (.ftl) files to JavaScript (.js or .mjs)')
18
- //.version(package.version)
19
- .argument('<input>', 'Path to the Fluent file to compile')
20
- .requiredOption(
21
- '--locale <locale...>',
22
- 'What locale(s) to be used. Multiple can be set to allow for fallback. i.e. en-CA'
23
- )
24
- .addOption(
25
- new Option('--comments', 'Include comments in output file.').preset(true)
26
- )
27
- .addOption(
28
- new Option(
29
- '--include-key <includeMessageKey...>',
30
- 'Allowed messages to be included. Default to include all.'
31
- )
32
- )
33
- .addOption(
34
- new Option(
35
- '--exclude-key <excludeMessageKey...>',
36
- 'Ignored messages to be excluded. Default to exclude none.'
37
- )
38
- )
39
- .addOption(
40
- new Option(
41
- '--exclude-value <excludeMessageValue>',
42
- 'Set message to an empty string when it contains this value. Default to not allowing empty strings.'
43
- )
44
- )
45
- /*.addOption(new Option('--tree-shaking', 'Export all messages to allow tree shaking')
46
- .preset(true)
47
- )*/
48
- .addOption(
49
- new Option(
50
- '--variable-notation <variableNotation>',
51
- 'What variable notation to use with exports'
52
- )
53
- .choices(['camelCase', 'pascalCase', 'constantCase', 'snakeCase'])
54
- .default('camelCase')
55
- )
56
- .addOption(
57
- new Option(
58
- '--disable-minify',
59
- 'If disabled, all exported messages will have the same interface `(params) => ({value, attributes})`.'
60
- ).preset(true)
61
- )
62
- .addOption(
63
- new Option(
64
- '--use-isolating',
65
- 'Wrap placeable with \\u2068 and \\u2069.'
66
- ).preset(true)
67
- )
68
- .addOption(
69
- new Option(
70
- '-o, --output <output>',
71
- 'Path to store the resulting JavaScript file. Will be in ESM.'
72
- )
73
- )
74
- .action(async (input, options) => {
75
- await fileExists(input)
17
+ .name("ftl")
18
+ .description("Compile Fluent (.ftl) files to JavaScript (.js or .mjs)")
19
+ .argument("<input>", "Path to the Fluent file to compile")
20
+ .requiredOption(
21
+ "--locale <locale...>",
22
+ "What locale(s) to be used. Multiple can be set to allow for fallback. i.e. en-CA",
23
+ )
24
+ .addOption(
25
+ new Option("--comments", "Include comments in output file.").preset(true),
26
+ )
27
+ .addOption(
28
+ new Option(
29
+ "--include-key <includeMessageKey...>",
30
+ "Allowed messages to be included. Default to include all.",
31
+ ),
32
+ )
33
+ .addOption(
34
+ new Option(
35
+ "--exclude-key <excludeMessageKey...>",
36
+ "Ignored messages to be excluded. Default to exclude none.",
37
+ ),
38
+ )
39
+ .addOption(
40
+ new Option(
41
+ "--exclude-value <excludeMessageValue>",
42
+ "Set message to an empty string when it contains this value. Default to not allowing empty strings.",
43
+ ),
44
+ )
45
+ .addOption(
46
+ new Option(
47
+ "--variable-notation <variableNotation>",
48
+ "What variable notation to use with exports",
49
+ )
50
+ .choices(["camelCase", "pascalCase", "constantCase", "snakeCase"])
51
+ .default("camelCase"),
52
+ )
53
+ .addOption(
54
+ new Option(
55
+ "--disable-minify",
56
+ "If disabled, all exported messages will have the same interface `(params) => ({value, attributes})`.",
57
+ ).preset(true),
58
+ )
59
+ .addOption(
60
+ new Option(
61
+ "--use-isolating",
62
+ "Wrap placeable with \\u2068 and \\u2069.",
63
+ ).preset(true),
64
+ )
65
+ .addOption(
66
+ new Option(
67
+ "-o, --output <output>",
68
+ "Path to store the resulting JavaScript file. Will be in ESM.",
69
+ ),
70
+ )
71
+ .action(async (input, options) => {
72
+ options.comments = options.comments ?? false;
73
+ try {
74
+ await fileExists(input);
76
75
 
77
- const ftl = await readFile(input, { encoding: 'utf8' })
76
+ const ftl = await readFile(input, { encoding: "utf8" });
78
77
 
79
- const js = compile(ftl, options)
80
- if (options.output) {
81
- await writeFile(options.output, js, 'utf8')
82
- } else {
83
- console.log(js)
84
- }
85
- })
86
- .parse()
78
+ const js = compile(ftl, options);
79
+ if (options.output) {
80
+ await writeFile(options.output, js, "utf8");
81
+ } else {
82
+ console.log(js);
83
+ }
84
+ } catch (e) {
85
+ console.error(`Error: ${e.message}`);
86
+ process.exit(1);
87
+ }
88
+ })
89
+ .parse();
package/index.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ // Copyright 2026 will Farrell, and fluent-transpiler contributors.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ export interface CompileOptions {
5
+ /** What locale(s) to be used. Multiple can be set to allow for fallback. */
6
+ locale?: string | string[];
7
+ /** Include comments in output file. Default: `true` */
8
+ comments?: boolean;
9
+ /** Throw error when `Junk` is parsed. Default: `true` */
10
+ errorOnJunk?: boolean;
11
+ /** Array of message keys to include. Default: `[]` (include all) */
12
+ includeKey?: string | string[];
13
+ /** Array of message keys to exclude. Default: `[]` (exclude none) */
14
+ excludeKey?: string | string[];
15
+ /** Set message to an empty string when it contains this value. */
16
+ excludeValue?: string;
17
+ /** What variable notation to use with exports. Default: `"camelCase"` */
18
+ variableNotation?: "camelCase" | "pascalCase" | "snakeCase" | "constantCase";
19
+ /** If true, all exported messages will have the same interface `(params) => ({value, attributes})`. Default: `false` */
20
+ disableMinify?: boolean;
21
+ /** Wrap placeable with Unicode isolating characters. Default: `false` */
22
+ useIsolating?: boolean;
23
+ /** Parameter name used in generated functions. Default: `"params"` */
24
+ params?: string;
25
+ /** Allows overwriting the `export default` to allow for custom uses. */
26
+ exportDefault?: string;
27
+ }
28
+
29
+ /**
30
+ * Compile Fluent (.ftl) source into a JavaScript ESM string.
31
+ */
32
+ export declare function compile(src: string, opts?: CompileOptions): string;
33
+
34
+ export default compile;