@panthus/elm-xgettext 1.0.1

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/index.js ADDED
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { Elm } = require('./dist/xgettext.js');
4
+ const fs = require('node:fs/promises');
5
+ const path = require('node:path');
6
+ const { parseArgs } = require('node:util');
7
+ const packageJson = require('./package.json');
8
+
9
+ const app = Elm.Main.init();
10
+
11
+ const options = {
12
+ help: { short: 'h', long: 'help', type: 'boolean' },
13
+ output: { short: 'o', long: 'output', type: 'string' },
14
+ };
15
+
16
+ let values, positionals;
17
+ try {
18
+ ({ values, positionals } = parseArgs({
19
+ options,
20
+ allowPositionals: true,
21
+ strict: true,
22
+ }));
23
+ } catch (err) {
24
+ console.error(err.message);
25
+ process.exit(1);
26
+ }
27
+
28
+ if (values.help || positionals.length === 0) {
29
+ console.log(`Version: ${packageJson.version}
30
+
31
+ Usage:
32
+ \telm-xgettext [FILE] [OPTIONS]
33
+
34
+ Dependening on the input file type, this tool can be used in two ways:
35
+
36
+ 1. To convert a PO file to a MO file:
37
+ Parses the translations from the given PO file and outputs a MO file.
38
+
39
+ 2. To extract translations from Elm files and outputs a POT file.
40
+ The translations must be defined using the panthus/elm-gettext Elm library.
41
+
42
+ FILE The glob pattern for the input Elm/PO files, for example "src/**/*.elm".
43
+
44
+ Arguments:
45
+ -h, --help\t\tDisplay this help text.
46
+ -o, --output\tSpecify the name of the resulting POT file.
47
+ \tNote for PO to MO the MO file is always put next to the PO file.`);
48
+ } else if (positionals.length > 0) {
49
+ parse(positionals, values.output);
50
+ } else {
51
+ console.error("Invalid arguments, see --help for details.");
52
+ process.exit(1);
53
+ }
54
+
55
+ async function parse(inputs, output) {
56
+ const extInput = path.extname(inputs[0]);
57
+
58
+ for (const input of inputs) {
59
+ if (path.extname(input) !== extInput) {
60
+ console.error("Invalid arguments, all input files must have the same extension. See --help for details.");
61
+ process.exit(1);
62
+ }
63
+ }
64
+
65
+ app.ports.logError.subscribe(function(error) {
66
+ console.error(error);
67
+ process.exit(1);
68
+ });
69
+
70
+ app.ports.saveFile.subscribe(async function(data) {
71
+ try {
72
+ await fs.writeFile(data.outputPath, data.content, { encoding: data.encoding });
73
+ } catch (err) {
74
+ console.error(`Could not save file: ${output}. ${err}`);
75
+ process.exit(1);
76
+ }
77
+ });
78
+
79
+ if (extInput === ".po") {
80
+ if (output) {
81
+ console.error("Invalid arguments, output is not supported for PO input. See --help for details.");
82
+ process.exit(1);
83
+ }
84
+
85
+ for (const input of inputs) {
86
+ for await (const entry of fs.glob(input)) {
87
+ const content = await fs.readFile(entry, { encoding: "utf8" });
88
+ app.ports.parsePoFile.send({ content, outputPath: entry.replace(/\.po$/, ".mo") });
89
+ }
90
+ }
91
+ } else if (extInput === ".elm") {
92
+ if (!output) {
93
+ console.error("Invalid arguments, specify the output POT file. See --help for details.");
94
+ process.exit(1);
95
+ }
96
+ if (path.extname(output) !== ".pot") {
97
+ console.error("Invalid arguments, the output must be a POT file. See --help for details.");
98
+ process.exit(1);
99
+ }
100
+
101
+ for (const input of inputs) {
102
+ for await (const entry of fs.glob(input)) {
103
+ const content = await fs.readFile(entry, { encoding: "utf8" });
104
+ app.ports.parseElmFile.send({ content });
105
+ }
106
+ }
107
+
108
+ app.ports.generatePotFile.send({ outputPath: output });
109
+ } else {
110
+ console.error(
111
+ `Unsupported file type: ${extInput}. Only .elm and .po files are supported. See --help for details.`);
112
+ process.exit(1);
113
+ }
114
+ }
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@panthus/elm-xgettext",
3
+ "version": "1.0.1",
4
+ "license": "MIT",
5
+ "type": "commonjs",
6
+ "bin": {
7
+ "elm-xgettext": "index.js"
8
+ },
9
+ "files": [
10
+ "dist",
11
+ "index.js"
12
+ ],
13
+ "scripts": {
14
+ "build": "elm make --optimize src/Main.elm --output dist/xgettext.js",
15
+ "test": "elm-test-rs"
16
+ },
17
+ "devDependencies": {
18
+ "elm": "^0.19.1-6",
19
+ "elm-format": "^0.8.8",
20
+ "elm-test-rs": "^3.0.1-0"
21
+ }
22
+ }