haxtuple placeholder for 17921
[xtuple] / scripts / export_dictionary.js
1 #!/usr/bin/env node
2 /*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true,
3 latedef:true, newcap:true, noarg:true,
4 regexp:true, undef:true, strict:true, trailing:true, white:true */
5
6 //
7 // This file really just parses the arguments, and sends the real work
8 // off to scripts/lib/build_dictionary.js.
9 //
10 (function () {
11   "use strict";
12
13   var _ = require("underscore"),
14     async = require("async"),
15     fs = require('fs'),
16     path = require('path'),
17     program = require('commander'),
18     dictionaryToolkit = require("./lib/build_dictionary");
19
20   program
21     .option('-a, --apikey [api key]', 'Google Translate API key.')
22     .option('-d, --database [database name]', 'Database name to export from.')
23     .option('-l, --language [language]', 'Language name in form es_MX.')
24     .option('-t, --total', 'Export the totality of the language files en batch.')
25     .parse(process.argv);
26
27
28   if (!program.total) {
29     dictionaryToolkit.exportEnglish(
30     {
31       database: program.database,
32       apiKey: program.apikey,
33       language: program.language
34     },
35     function (err, res) {
36       if (err) {
37         console.log("Export failed", err);
38         return;
39       }
40       console.log("Success!");
41     });
42     return;
43   }
44
45
46   //
47   // Do a total batch export
48   //
49   if (program.database || program.language) {
50     console.log("Don't enter a database name or a language. I'll take care of that.");
51     return;
52   }
53   var buildAll = require('./lib/build_all');
54   program.database = "linguist";
55
56   //
57   // Step 1: Create a database with the appropriate extensions
58   //
59   var buildPostbooks = function (done) {
60     buildAll.build({
61       database: program.database,
62       initialize: true,
63       source: path.join(__dirname, "../foundation-database/postbooks_demo_data.sql")
64     }, done);
65   };
66   var buildExtensions = function (done) {
67     var extensions = ["inventory", "manufacturing", "distribution"/*, "bi"*/];
68     async.mapSeries(extensions, function (extension, next) {
69       buildAll.build({
70         database: program.database,
71         frozen: (extension !== "bi"),
72         extension: path.join(__dirname, "../../private-extensions/source", extension)
73       }, next)}, done);
74   };
75
76   //
77   // Step 2: Load all the language files that we have
78   //
79   var importExistingDictionaries = function (done) {
80     fs.readdir(path.join(__dirname, "../../xtuple-linguist/translations"), function (err, files) {
81       async.mapSeries(files, function (file, next) {
82         var fullFilename = path.join(__dirname, "../../xtuple-linguist/translations", file);
83         dictionaryToolkit.importDictionary(program.database, fullFilename, next);
84       }, done);
85     });
86   };
87
88   //
89   // Step 3: Export in every language
90   //
91   var supportedLanguages = [
92     {locale: "de_DE", label: "Deutch", english: "German"},
93     {locale: "es_MX", label: "Español", english: "Spanish"},
94     {locale: "fr_FR", label: "Français", english: "French"},
95     {locale: "te_IN", label: "తెలుగు", english: "Telugu"},
96     {locale: "zh_CN", label: "中国的", english: "Simplified Chinese"}
97   ];
98   var exportAllDictionaries = function (done) {
99     async.mapSeries(supportedLanguages, function (language, next) {
100       dictionaryToolkit.exportEnglish(
101       {
102         database: program.database,
103         apiKey: program.apikey,
104         language: language.locale,
105         directory: path.join(__dirname, "../../xtuple-linguist/translations")
106       }, next);
107     }, done);
108   };
109
110   async.series([
111     buildPostbooks,
112     buildExtensions,
113     importExistingDictionaries,
114     exportAllDictionaries
115   ], function (err, results) {
116     if (err) {
117       console.log("Export failed: ", err);
118       return;
119     }
120     console.log("Success! Don't forget to commit and push xtuple-linguist!");
121   });
122
123 }());