-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserializers.js
More file actions
167 lines (134 loc) · 3.62 KB
/
serializers.js
File metadata and controls
167 lines (134 loc) · 3.62 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";
// ===================================================================
const findKey = require("lodash/findKey");
const formatJson = JSON.stringify;
const parseJson = (function () {
try {
return require("json5").parse;
} catch (_) {
/* empty */
}
try {
const stripJsonComments = require("strip-json-comments");
return function parseJson(json) {
return JSON.parse(stripJsonComments(json));
};
} catch (_) {
/* empty */
}
return JSON.parse;
})();
const UnknownFormatError = require("./unknown-format-error");
// ===================================================================
const serializers = Object.create(null);
serializers.json = {
// Test whether this “file” seems to be on the correct format.
test: function (file) {
return file.path && /\.json5?$/i.test(file.path);
},
// Unserialize the content of the file.
unserialize: function (file) {
return parseJson(String(file.content));
},
// Serialize the value to a string/buffer.
serialize: function (value, opts) {
opts || (opts = {});
const indent = "indent" in opts ? opts.indent : 2;
return formatJson(value, null, indent);
},
};
// Optional dependency.
try {
const CSON = require("cson-parse");
serializers.cson = {
test: function (file) {
return file.path && /\.cson$/i.test(file.path);
},
unserialize: function (file) {
return CSON.parse(String(file.content));
},
serialize: function (value) {
return CSON.stringify(value);
},
};
} catch (_) {
/* empty */
}
// Optional dependency.
try {
const ini = require("ini");
serializers.ini = {
test: function (file) {
return file.path && /\.ini$/i.test(file.path);
},
unserialize: function (file) {
return ini.decode(String(file.content));
},
serialize: function (value) {
return ini.encode(value);
},
};
} catch (_) {
/* empty */
}
try {
const { parse, stringify } = require("@iarna/toml");
serializers.toml = {
serialize: stringify,
test: ({ path }) => path !== undefined && /\.toml$/i.test(path),
unserialize: ({ content }) => parse(String(content)),
};
} catch (_) {
/* empty */
}
// Optional dependency.
try {
const yaml = require("js-yaml");
let { dump, load } = yaml;
// compat with js-yaml < 4
if ("DEFAULT_SAFE_SCHEMA" in yaml) {
dump = yaml.safeDump;
load = yaml.safeLoad;
}
serializers.yaml = {
test: function (file) {
return file.path && /\.yaml$/i.test(file.path);
},
unserialize: function (file) {
return load(String(file.content));
},
serialize: function (value, opts) {
opts || (opts = {});
const indent = "indent" in opts ? opts.indent : 2;
return dump(value, {
indent,
});
},
};
} catch (_) {
/* empty */
}
// --------------------------------------------------------------------
const detectFormat = function (file) {
return findKey(serializers, function (serializer) {
return serializer.test(file);
});
};
const serialize = function (value, format, opts) {
let buffer = serializers[format].serialize(value, opts);
if (!(buffer instanceof Buffer)) {
buffer = Buffer.from(buffer);
}
return buffer;
};
const unserialize = function (file, format) {
format || (format = detectFormat(file));
if (!format) {
throw new UnknownFormatError("no compatible format found for " + file.path);
}
return serializers[format].unserialize(file);
};
// ===================================================================
exports.detectFormat = detectFormat;
exports.serialize = serialize;
exports.unserialize = unserialize;