|
1 | 1 | (function(undefined){ |
2 | 2 |
|
| 3 | + if (APP) |
| 4 | + { |
| 5 | + var fs = require('fs'); |
| 6 | + var path = require('path'); |
| 7 | + var glob = require('glob'); |
| 8 | + } |
| 9 | + |
3 | 10 | /** |
4 | 11 | * Class for managing the project data |
5 | 12 | * @class Project |
|
74 | 81 | { |
75 | 82 | this.close(); |
76 | 83 | this.dir = dir; |
77 | | - var self = this; |
78 | 84 |
|
79 | 85 | if (APP) |
80 | 86 | { |
81 | | - var path = require('path'); |
82 | | - var fs = require('fs'); |
| 87 | + var data; |
83 | 88 |
|
84 | 89 | // Convert the old project file to the new format |
85 | 90 | var oldFile = path.join(dir, OLD_PROJECT_FILE); |
86 | 91 | var file = path.join(dir, PROJECT_FILE); |
87 | 92 |
|
| 93 | + // backward compatibility with old project file |
88 | 94 | if (fs.existsSync(oldFile)) |
89 | 95 | { |
90 | | - // Read in the project json |
91 | | - var data = JSON.parse(fs.readFileSync(oldFile)); |
| 96 | + data = parseJSON(oldFile); |
| 97 | + |
| 98 | + if (!data) |
| 99 | + { |
| 100 | + callback(null); |
| 101 | + return; |
| 102 | + } |
92 | 103 |
|
93 | 104 | // Write the new project JSON file |
94 | | - fs.writeFileSync( |
95 | | - file, |
96 | | - JSON.stringify({ captions: data }, null, "\t") |
97 | | - ); |
| 105 | + writeJSON(file, { captions: data }); |
98 | 106 |
|
99 | 107 | // Remove the old project file |
100 | 108 | fs.unlinkSync(oldFile); |
101 | 109 | } |
102 | 110 |
|
| 111 | + // Check for a project file that doesn't exist |
103 | 112 | if (!fs.existsSync(file)) |
104 | 113 | { |
105 | 114 | if (DEBUG) |
|
110 | 119 | this.create(file, callback); |
111 | 120 | return; |
112 | 121 | } |
113 | | - fs.readFile(file, function(err, data){ |
114 | 122 |
|
115 | | - if (err) |
116 | | - { |
117 | | - callback(null); |
118 | | - throw err; |
119 | | - } |
120 | | - // Finish the loading |
121 | | - self.onProjectLoaded(JSON.parse(data), callback); |
122 | | - }); |
| 123 | + data = parseJSON(file); |
| 124 | + |
| 125 | + if (!data) |
| 126 | + { |
| 127 | + callback(null); |
| 128 | + return; |
| 129 | + } |
| 130 | + this.onProjectLoaded(data, callback); |
123 | 131 | } |
124 | 132 | if (WEB) |
125 | 133 | { |
|
128 | 136 | dir + "/"+PROJECT_FILE+"?cb=" + Math.random() * 10000, |
129 | 137 | function(data) |
130 | 138 | { |
131 | | - self.onProjectLoaded(data, callback); |
132 | | - } |
| 139 | + this.onProjectLoaded(data, callback); |
| 140 | + }.bind(this) |
133 | 141 | ); |
134 | 142 | } |
135 | 143 | }; |
136 | 144 |
|
| 145 | + /** |
| 146 | + * Handle the parse with an alert |
| 147 | + * @method parseJSON |
| 148 | + * @static |
| 149 | + * @private |
| 150 | + * @param {string} str The JSON string |
| 151 | + * @param {string} [create] Default contents if file doesn't exist |
| 152 | + * @return {object} The JSON object or null |
| 153 | + */ |
| 154 | + var parseJSON = function(file, create) |
| 155 | + { |
| 156 | + if (!fs.existsSync(file)) |
| 157 | + { |
| 158 | + if (create) |
| 159 | + { |
| 160 | + fs.writeFileSync(file, create); |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + alert("File " + file + " does not exist. Manually create it."); |
| 165 | + return; |
| 166 | + } |
| 167 | + } |
| 168 | + var str = fs.readFileSync(file); |
| 169 | + var data; |
| 170 | + try |
| 171 | + { |
| 172 | + data = JSON.parse(str); |
| 173 | + } |
| 174 | + catch(e) |
| 175 | + { |
| 176 | + alert("Unable to parse " + file + |
| 177 | + ". Manually fix JSON errors and try again."); |
| 178 | + return; |
| 179 | + } |
| 180 | + return data; |
| 181 | + }; |
| 182 | + |
| 183 | + /** |
| 184 | + * Write the JSON |
| 185 | + * @method writeJSON |
| 186 | + * @static |
| 187 | + * @private |
| 188 | + * @param {string} file Output file location |
| 189 | + * @param {*} data The output data |
| 190 | + */ |
| 191 | + var writeJSON = function(file, data) |
| 192 | + { |
| 193 | + fs.writeFileSync(file, JSON.stringify(data, null, "\t")); |
| 194 | + }; |
| 195 | + |
137 | 196 | /** |
138 | 197 | * The project is loadd |
139 | 198 | * @method onProjectLoaded |
|
164 | 223 | */ |
165 | 224 | p.create = function(file, callback) |
166 | 225 | { |
167 | | - var fs = require('fs'), |
168 | | - path = require('path'), |
169 | | - glob = require('glob'), |
170 | | - self = this, |
| 226 | + var self = this, |
171 | 227 | dir = path.dirname(file); |
172 | 228 |
|
173 | 229 | this.modal.open(dir, function(result){ |
|
226 | 282 | }; |
227 | 283 |
|
228 | 284 | // Write the project to file |
229 | | - fs.writeFileSync(file, JSON.stringify(data, null, "\t")); |
| 285 | + writeJSON(file, data); |
230 | 286 |
|
231 | 287 | // Continue with loading the new created project |
232 | 288 | self.onProjectLoaded(data, callback); |
|
259 | 315 | var exportPath; |
260 | 316 | if (APP) |
261 | 317 | { |
262 | | - var path = require('path'); |
263 | | - var fs = require('fs'); |
264 | 318 | exportPath = path.join(this.dir, locale.export); |
265 | | - this.captions = JSON.parse(fs.readFileSync(exportPath) || '{}'); |
| 319 | + |
| 320 | + this.captions = parseJSON(exportPath, '{}'); |
| 321 | + |
| 322 | + if (!this.captions) |
| 323 | + { |
| 324 | + callback(null); |
| 325 | + return; |
| 326 | + } |
266 | 327 | callback(this); |
267 | 328 | } |
268 | 329 | if (WEB) |
|
289 | 350 |
|
290 | 351 | if (APP) |
291 | 352 | { |
292 | | - var fs = require('fs'); |
293 | | - var path = require('path'); |
294 | 353 | var projectFile = path.join(this.dir, PROJECT_FILE); |
295 | 354 | var exportFile = path.join(this.dir, locale.export); |
296 | 355 |
|
297 | 356 | // Re-export the captions file |
298 | | - fs.writeFileSync( |
299 | | - exportFile, |
300 | | - JSON.stringify(this.captions, null, "\t") |
301 | | - ); |
| 357 | + writeJSON(exportFile, this.captions); |
302 | 358 |
|
303 | 359 | // Get the project JSON file and update it |
304 | | - var project = JSON.parse(fs.readFileSync(projectFile)); |
| 360 | + var project = parseJSON(projectFile); |
| 361 | + |
| 362 | + // Ignore if invalid project JSON |
| 363 | + if (!project) |
| 364 | + { |
| 365 | + return; |
| 366 | + } |
| 367 | + |
305 | 368 | project.captions.locales = this.locales; |
306 | 369 | project.captions.assets = this.assets; |
307 | 370 |
|
308 | 371 | // Update the project file |
309 | | - fs.writeFileSync(projectFile, JSON.stringify(project, null, "\t")); |
| 372 | + writeJSON(projectFile, project); |
310 | 373 | } |
311 | 374 |
|
312 | 375 | if (WEB && DEBUG) |
|
0 commit comments