-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.js
More file actions
116 lines (96 loc) · 2.45 KB
/
utils.js
File metadata and controls
116 lines (96 loc) · 2.45 KB
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
var fs = require('fs'),
ConfigStore = require('configstore'),
util = require('util'),
inquirer = require('inquirer'),
api = require('./api'),
extend = util._extend,
aWrite = require('atomic-write');
var config = {
targetDir: null,
apiToken: null,
projectId: null,
projectLanguages: null,
defaultLanguage: null
};
const GLOBAL_CONFIG = 'poeditor-config';
const LOCAL_CONFIG = './.poeditor-config';
module.exports = {
getGlobalConfiguration: function(){
var globalConfig = new ConfigStore(GLOBAL_CONFIG);
return extend(config, globalConfig.all);
},
getLocalConfiguration: function(){
var localConfig = {};
try {
fs.accessSync(LOCAL_CONFIG, fs.F_OK | fs.R_OK | fs.W_OK);
var data = fs.readFileSync(LOCAL_CONFIG, "utf8");
localConfig = JSON.parse(data);
} catch(e) {}
return extend(config, localConfig);
},
setGlobalConfiguration: function(key, val){
var cfg = new ConfigStore(GLOBAL_CONFIG);
if(val === undefined){
cfg.del(key);
} else {
cfg.set(key, val);
}
},
setLocalConfiguration: function(key, val){
var cfg = this.getLocalConfiguration();
if(val === undefined){
delete cfg[key];
} else {
cfg[key] = val;
}
var out = fs.createWriteStream(LOCAL_CONFIG);
out.write(JSON.stringify(cfg));
out.end();
/*aWrite.writeFile(LOCAL_CONFIG, JSON.stringify(cfg), function(err, result){
if(err){
return console.log(err);
}
});*/
},
chooseTargetDir: function(){
return new Promise(function(resolve, reject){
inquirer.prompt([
{
type: "input",
name: "target",
message: "Target directory for translation files"
}
], function(answers){
resolve(answers.target);
});
})
},
chooseProject: function(apiToken){
return new Promise(function(resolve, reject){
api.getProjects(apiToken).then(function(res){
var list = res[1];
var choices = list.map(function(item){
return item.name
});
inquirer.prompt([{
type: 'list',
name: 'project',
message: 'Which project should be used?',
choices: choices
}], function(answers){
var index = choices.indexOf(answers.project);
var id = list[index].id;
/*project = list[index];
conf.set('api_project', list[index]);*/
api.getProjectLanguages(apiToken, id).then(function(res){
var l = res[1];
var languages = l.map(function(item){
return item.code;
});
resolve([id, languages]);
}, reject);
});
}, reject);
});
}
};