-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstaging.js
More file actions
66 lines (52 loc) · 1.29 KB
/
staging.js
File metadata and controls
66 lines (52 loc) · 1.29 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
var fs = require('fs'),
util = require('util'),
extend = util._extend;
function POEditorStaging(defaultLanguage){
this.stagingFile = "./.poeditor-staging";
this.stagingData = {};
this.defaultLanguage = defaultLanguage || 'en-us';
this.readStagingCache();
}
extend(POEditorStaging.prototype, {
readStagingCache: function(){
try {
fs.accessSync(this.stagingFile, fs.F_OK | fs.R_OK | fs.W_OK);
var rawStagingData = fs.readFileSync(this.stagingFile, "utf8");
this.stagingData = JSON.parse(rawStagingData);
} catch(e) {}
},
writeStagingCache: function(){
var out = fs.createWriteStream(this.stagingFile);
out.write(JSON.stringify(this.stagingData));
out.end();
},
clearStaging: function(){
this.stagingData = {};
this.writeStagingCache();
},
addTerm: function(term, defaultTranslation, context, tags, update){
if (!term) {
throw {
code: 1,
message: 'Term is required'
};
}
if (!update && this.stagingData[term]) {
throw {
code: 2,
message: 'Term already exists'
};
}
this.stagingData[term] = {
term: term,
context: context || '',
defaultTranslation: defaultTranslation,
tags: tags || []
};
this.writeStagingCache();
},
getStagingData: function(){
return this.stagingData;
}
});
module.exports = POEditorStaging;