-
Notifications
You must be signed in to change notification settings - Fork 6
/
extractor.js
executable file
·123 lines (106 loc) · 4.56 KB
/
extractor.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
#!/usr/bin/env node
"use strict";
const argv = require("yargs")
.option("export", {alias: "e", describe: "If set all modules for the requested language found in root will be parsed and a export.csv will be generated."})
.option("exportOutput", {default: "export", describe: "The file path to which the export should be written."})
.option("format", {default: "csv", describe: "The format of the import/export operation. Supported formats are CSV and PO (case-insensitive)."})
.option("import", {alias: "i", describe: "A CSV file to be imported and to generate code from. Generate elm files will be placed in <importOutput>."})
.option("importOutput", {default: "import", describe: "The base directory to which the generated code should be written. Subdirectories will be created per language and submodule."})
.option("language", {alias: "l", describe: "The language code of the current operation. This should match the subdirectory of the language in root."})
.option("root", {default: "Translation", describe: "The root to the translation modules. This script expects this directory to contain a subdirectory for each language."})
.demand("language")
.boolean(["export"])
.help()
.argv;
const Elm = require("./dist/elm.js");
const fs = require("fs-extra");
const path = require("path");
const glob = require("glob");
if (!argv.export && !argv.import) {
console.error("Please provide import or export option");
process.exit(403);
}
if (argv.exportOutput == "export") {
argv.exportOutput = argv.exportOutput + "." + argv.format.toLowerCase();
}
const currentDir = process.cwd();
if (argv.export) {
let fullPath = path.join(currentDir, argv.root, argv.language);
console.log("Parsing from", fullPath);
let fileNames = glob.sync(fullPath + "/**/*.elm");
console.log("└── Found elm module files for export:", fileNames);
// read all files and store their content in an array
let fileContents = [];
fileNames.forEach(function(file) {
let data = fs.readFileSync(file);
let content = data.toString();
fileContents.push(content);
});
// pass the array of file contents to our elm worker
let worker = Elm.Main.worker({
"sources": fileContents,
"operation": "export",
"format": argv.format,
});
// subscribe to the port to handle completion callback
worker.ports.exportResult.subscribe(function(resultString) {
handleExport(resultString);
});
} else {
// ensure that import is a valid file path
if (!argv.import || argv.import == "" || argv.import == true) {
console.error("Please provide an import path");
process.exit(403);
}
let pathToImportFile = path.join(currentDir, argv.import);
if (!fs.existsSync(pathToImportFile)) {
console.error("Could not find " + argv.format + " file at", pathToImportFile);
process.exit(403);
}
let data = fs.readFileSync(pathToImportFile);
let csvContent = data.toString();
let worker = Elm.Main.worker({
"sources": [csvContent],
"operation": "import",
"format": argv.format,
});
worker.ports.importResult.subscribe(function(resultString) {
handleImport(resultString);
});
}
/**
* handleExport - Handles the csv string generated by elm and saves it to exportOutput.
*
* @param {String} resultString A CSV string of all translations.
*/
function handleExport(resultString) {
if (resultString === "") {
process.exit(500);
}
let targetPath = path.join(currentDir, argv.exportOutput);
fs.writeFileSync(targetPath, resultString);
console.log("Finished writing csv file to:", targetPath);
}
/**
* handleImport - Handles the code generated by elm from a CSV file and writes
* it to argv.importOutput/argv.language.
*
* @param {[[String]]} results A list of (module name, file content) tuples.
*/
function handleImport(results) {
let rootDir = path.normalize(argv.root);
let importDir = path.join(currentDir, argv.importOutput, argv.root, argv.language);
fs.ensureDirSync(importDir);
console.log("Writing elm-files files to:", importDir);
results.forEach(function(result) {
let moduleName = result[0];
if (moduleName.indexOf(rootDir) === 0) {
moduleName = moduleName.substr(rootDir.length)
}
let filePath = path.join(importDir, moduleName + ".elm");
fs.ensureDirSync(path.dirname(filePath));
fs.writeFileSync(filePath, result[1]);
console.log("└── Finished writing:", filePath);
});
console.log("Completed");
}