-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgstoi18n.js
executable file
·64 lines (53 loc) · 1.87 KB
/
gstoi18n.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
#!/usr/bin/env node
var fs = require('fs');
var resolve = require('path').resolve;
var app = require('commander');
var Promise = require('bluebird');
var out = require('cli-output');
var helper = require('./helper');
var packageData = require('./package.json');
function collect(value, previous) {
return previous.concat([value]);
}
app
.version(packageData.version)
.usage('<spreadsheet-id> <saveas>')
.option('-w, --worksheet <value>', 'specify worksheet', collect, [])
.option('-b, --beautify', 'Beautify final JSON')
.option('-e, --esmodule', 'Export with es module')
.option('-p, --property [propertyMode]', 'File name case', /^(camel|pascal|nospace|default)$/i)
.parse(process.argv);
if (app.args.length < 1) {
app.help();
}
app.spreadsheetId = app.args[0] || app.spreadsheetId;
app.beautify = typeof app.beautify !== 'undefined' ? app.beautify : true;
app.propertyMode = app.property;
var saveas = app.args[1];
helper
.spreadsheetToJson(app)
.then(function(result) {
if (saveas) {
saveas = resolve(saveas);
if (!fs.existsSync(saveas)) {
fs.mkdirSync(saveas);
}
Object.keys(result).forEach(function(value) {
var content = JSON.stringify(result[value], null, app.beautify ? 4 : null);
var extension = '.json';
if (app.esmodule) {
extension = '.js';
content = 'export default ' + content + ';\r\n';
}
var file = saveas + '/' + value + extension;
out.info('Creating ' + file);
return Promise.promisify(fs.writeFile)(file, content, 'utf-8');
});
out.success('Finished!');
} else {
out.prettyJSON(result);
}
})
.catch(function(error) {
out.error(error);
});