-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·133 lines (117 loc) · 3.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
"use strict";
const meow = require("meow");
const fs = require("fs");
const path = require("path");
const dotProp = require("dot-prop");
const isPlainObj = require("is-plain-obj");
const { Elm } = require("./codegen.js");
const cli = meow(
`
Usage
$ elm-translations
Options
--from, -f path to your translations file (JSON)
--module, -m custom Elm module name (default: Translations)
--root, -r key path to use as root (optional)
--out, -o path to a folder where the generated translations should be saved (optional)
--version show version
Examples
$ elm-translations --from en.json
$ elm-translations -f en.json -m I18n.Translations -o src
`,
{
flags: {
from: {
type: "string",
alias: "f"
},
module: {
type: "string",
alias: "m",
default: "Translations"
},
root: {
type: "string",
alias: "r",
default: ""
},
out: {
type: "string",
alias: "o",
default: ""
}
}
}
);
const codegen = (moduleName, jsonData) => {
const worker = Elm.Main.init({ flags: [moduleName, jsonData] });
return new Promise((resolve, reject) => {
worker.ports.succeed.subscribe(resolve);
worker.ports.fail.subscribe(reject);
});
};
const resolveTranslationsPath = (moduleName, baseDir = __dirname) => {
const filePath = path.resolve(baseDir, path.join(...moduleName.split(".")));
return `${filePath}.elm`;
};
const writeFile = (filePath, content) => {
const fileFolder = path.dirname(filePath);
if (!fs.existsSync(fileFolder)) {
fs.mkdirSync(fileFolder, { recursive: true });
}
return fs.writeFileSync(filePath, content, "utf8");
};
const run = async () => {
const { from, module: moduleName, root, out: outputFolder } = cli.flags;
if (from) {
let data;
let translations;
let elmCode;
try {
data = fs.readFileSync(from, "utf8");
} catch (err) {
console.error(`Cannot read file: ${from}`);
process.exit(3);
}
try {
translations = JSON.parse(data);
} catch (err) {
console.error(`Cannot parse file: ${from}\n- ${err}`);
process.exit(4);
}
if (root) {
const value = dotProp.get(translations, root);
if (isPlainObj(value)) {
translations = value;
} else {
console.error(
`Cannot use root: ${root}\n- Error: value of key '${root}' is not an object`
);
process.exit(5);
}
}
try {
elmCode = await codegen(moduleName, translations);
} catch (err) {
console.error(`Cannot generate Elm code\n- ${err}`);
process.exit(5);
}
if (outputFolder) {
try {
writeFile(
resolveTranslationsPath(moduleName, path.resolve(outputFolder)),
elmCode
);
} catch (err) {
console.error(`Cannot create translations file\n- ${err}`);
process.exit(6);
}
} else {
console.log(elmCode);
}
} else {
cli.showHelp();
}
};
run();