UNPKG

7.03 kBJavaScriptView Raw
1'use strict';
2var PARSER_SCHEMA, dump, fs, iced, normalizePath, parse, path, read, readSync, write, writeSync, yaml, __iced_k, __iced_k_noop,
3 __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
4
5iced = require('iced-runtime');
6__iced_k = __iced_k_noop = function() {};
7
8fs = require('fs');
9
10path = require('path');
11
12yaml = require('js-yaml');
13
14PARSER_SCHEMA = {
15 defaultSafe: yaml.DEFAULT_SAFE_SCHEMA,
16 defaultFull: yaml.DEFAULT_FULL_SCHEMA,
17 failsafe: yaml.FAILSAFE_SCHEMA,
18 json: yaml.JSON_SCHEMA,
19 core: yaml.CORE_SCHEMA
20};
21
22
23/*
24 * Normalize path to YAML file
25 *
26 * @param string sPath Path to YAML file
27 */
28
29normalizePath = function(sPath) {
30 var YAML_EXT, dirname, file, files, sBasename, _i, _len, _ref;
31 YAML_EXT = ['.yml', '.yaml'];
32 sBasename = path.basename(sPath);
33 dirname = path.dirname(sPath);
34 files = fs.readdirSync(dirname);
35 for (_i = 0, _len = files.length; _i < _len; _i++) {
36 file = files[_i];
37 if (sBasename === path.basename(file, YAML_EXT[0]) || sBasename === path.basename(file, YAML_EXT[1]) && (_ref = path.extname(file), __indexOf.call(YAML_EXT, _ref) >= 0)) {
38 return dirname + path.sep + file;
39 }
40 }
41 return sPath;
42};
43
44
45/*
46 * Parse yaml
47 *
48 * @param string sString YAML string to parse
49 * @param object|null oOptions options for parser:
50 * - schema: object Schema. More information here: https://github.com/nodeca/js-yaml#safeload-string---options-
51 * note: defaultSafe schema used by default because is that recomended loading way.
52 *
53 * @return JSON
54 */
55
56exports.parse = parse = function(sString, oOptions) {
57 if (oOptions == null) {
58 oOptions = null;
59 }
60 if (oOptions != null) {
61 oOptions.schema = Object.keys(oOptions).length > 0 ? PARSER_SCHEMA[oOptions.schema] : PARSER_SCHEMA['defaultSafe'];
62 }
63 return yaml.load(sString, oOptions);
64};
65
66
67/*
68 * Convert JSON into YAML
69 *
70 * @param object JSON to dump
71 *
72 * @return string YAML
73 */
74
75exports.dump = dump = function(oJson, oOptions) {
76 if (oOptions == null) {
77 oOptions = null;
78 }
79 if (oOptions != null) {
80 oOptions.schema = Object.keys(oOptions).length > 0 ? PARSER_SCHEMA[oOptions.schema] : PARSER_SCHEMA['defaultSafe'];
81 }
82 return yaml.dump(oJson, oOptions);
83};
84
85
86/*
87 * Read and parse YAML file
88 *
89 * @param string sPath Path to YAML file (It can be provided without extname)
90 * @param null|string|object mOptions
91 * @param Callback
92 */
93
94exports.read = read = function(sPath, mOptions, cb) {
95 var e, encoding, err, filename, mData, ___iced_passed_deferral, __iced_deferrals, __iced_k, _ref;
96 __iced_k = __iced_k_noop;
97 ___iced_passed_deferral = iced.findDeferral(arguments);
98 if (typeof mOptions === 'function') {
99 cb = mOptions;
100 } else if (typeof mOptions === 'string') {
101 _ref = [mOptions, null], encoding = _ref[0], mOptions = _ref[1];
102 } else if (typeof mOptions === 'object' && Array.isArray !== true) {
103 if (mOptions.encoding != null) {
104 encoding = mOptions.encoding;
105 delete mOptions.encoding;
106 }
107 }
108 filename = normalizePath(sPath);
109 (function(_this) {
110 return (function(__iced_k) {
111 __iced_deferrals = new iced.Deferrals(__iced_k, {
112 parent: ___iced_passed_deferral,
113 filename: "/Users/octetstream/sandbox/projects/node-yaml/index.iced"
114 });
115 fs.readFile(filename, encoding, __iced_deferrals.defer({
116 assign_fn: (function() {
117 return function() {
118 err = arguments[0];
119 return mData = arguments[1];
120 };
121 })(),
122 lineno: 69
123 }));
124 __iced_deferrals._fulfill();
125 });
126 })(this)((function(_this) {
127 return function() {
128 if (typeof err !== "undefined" && err !== null) {
129 return cb(err);
130 }
131 try {
132 mData = parse(mData, mOptions);
133 } catch (_error) {
134 e = _error;
135 return cb(e);
136 }
137 return cb(null, mData);
138 };
139 })(this));
140};
141
142
143/*
144 * Synchronous version of yaml.read
145 *
146 * @return JSON
147 */
148
149exports.readSync = readSync = function(sPath, mOptions) {
150 var encoding, filename, mData, _ref;
151 if (mOptions == null) {
152 mOptions = null;
153 }
154 if (typeof mOptions === 'string') {
155 _ref = [mOptions, null], encoding = _ref[0], mOptions = _ref[1];
156 } else if ((mOptions != null) && typeof mOptions === 'object' && Array.isArray !== true) {
157 if (mOptions.encoding != null) {
158 encoding = mOptions.encoding;
159 delete mOptions.encoding;
160 }
161 }
162 filename = normalizePath(sPath);
163 mData = fs.readFileSync(filename, encoding);
164 mData = parse(mData, mOptions);
165 return mData;
166};
167
168
169/*
170 * Parse and write YAML to file
171 *
172 * @param string sPath Path to YAML file
173 * @param string|Buffer Data to write
174 * @param null|string|options mOptions
175 * @param Callback
176 */
177
178exports.write = write = function(sPath, mData, mOptions, cb) {
179 var e, encoding, err, ___iced_passed_deferral, __iced_deferrals, __iced_k, _ref;
180 __iced_k = __iced_k_noop;
181 ___iced_passed_deferral = iced.findDeferral(arguments);
182 if (mData == null) {
183 mData = '';
184 }
185 if (mOptions == null) {
186 mOptions = null;
187 }
188 if (typeof mOptions === 'function') {
189 cb = mOptions;
190 } else if (typeof mOptions === 'string') {
191 _ref = [mOptions, null], encoding = _ref[0], mOptions = _ref[1];
192 } else if (typeof mOptions === 'object' && Array.isArray !== true) {
193 if (mOptions.encoding != null) {
194 encoding = mOptions.encoding;
195 delete mOptions.encoding;
196 }
197 }
198 try {
199 mData = dump(mData, mOptions);
200 } catch (_error) {
201 e = _error;
202 return cb(e);
203 }
204 (function(_this) {
205 return (function(__iced_k) {
206 __iced_deferrals = new iced.Deferrals(__iced_k, {
207 parent: ___iced_passed_deferral,
208 filename: "/Users/octetstream/sandbox/projects/node-yaml/index.iced"
209 });
210 fs.writeFile(sPath, mData, encoding, __iced_deferrals.defer({
211 assign_fn: (function() {
212 return function() {
213 return err = arguments[0];
214 };
215 })(),
216 lineno: 114
217 }));
218 __iced_deferrals._fulfill();
219 });
220 })(this)((function(_this) {
221 return function() {
222 if (typeof err !== "undefined" && err !== null) {
223 return cb(err);
224 }
225 return cb(null);
226 };
227 })(this));
228};
229
230
231/*
232 * Synchronous version of yaml.write
233 *
234 * @return Returns null if file has successfully written
235 */
236
237exports.writeSync = writeSync = function(sPath, mData, mOptions, cb) {
238 var encoding, _ref;
239 if (mData == null) {
240 mData = '';
241 }
242 if (mOptions == null) {
243 mOptions = null;
244 }
245 if (typeof mOptions === 'string') {
246 _ref = [mOptions, null], encoding = _ref[0], mOptions = _ref[1];
247 } else if ((mOptions != null) && typeof mOptions === 'object' && Array.isArray !== true) {
248 if (mOptions.encoding != null) {
249 encoding = mOptions.encoding;
250 delete mOptions.encoding;
251 }
252 }
253 mData = dump(mData);
254 fs.writeFileSync(sPath, mData, encoding);
255 return null;
256};
257
258exports.Schema = yaml.Schema;