UNPKG

1.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var fs = require("fs");
4/**
5 * game.json をファイルとして取り扱うモジュール。
6 */
7var ConfigurationFile;
8(function (ConfigurationFile) {
9 /**
10 * game.json ファイルを読み込む。
11 * なければ作成する。
12 *
13 * @param confPath game.jsonがある、または作成するディレクトリ。絶対パスであることを期待する。
14 * @param logger ログ出力に用いるロガー。
15 */
16 function read(confPath, logger) {
17 return new Promise(function (resolve, reject) {
18 fs.readFile(confPath, "utf8", function (err, data) {
19 if (err) {
20 if (err.code !== "ENOENT")
21 return void reject(err);
22 logger.info("No game.json found. Create a new one.");
23 data = "{}";
24 }
25 try {
26 resolve(JSON.parse(data));
27 }
28 catch (e) {
29 reject(e);
30 }
31 });
32 });
33 }
34 ConfigurationFile.read = read;
35 /**
36 * game.json をファイルに書き込む。
37 *
38 * @param confPath game.jsonを保存するディレクトリ。絶対パスであることを期待する。
39 * @param logger ログ出力に用いるロガー。
40 */
41 function write(content, confPath, logger) {
42 return new Promise(function (resolve, reject) {
43 var text = JSON.stringify(content, null, "\t");
44 fs.writeFile(confPath, text, { encoding: "utf8" }, function (err) {
45 if (err) {
46 reject(err);
47 }
48 else {
49 resolve();
50 }
51 });
52 });
53 }
54 ConfigurationFile.write = write;
55})(ConfigurationFile = exports.ConfigurationFile || (exports.ConfigurationFile = {}));