1 | 'use strict';
|
2 | var 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 |
|
5 | iced = require('iced-runtime');
|
6 | __iced_k = __iced_k_noop = function() {};
|
7 |
|
8 | fs = require('fs');
|
9 |
|
10 | path = require('path');
|
11 |
|
12 | yaml = require('js-yaml');
|
13 |
|
14 | PARSER_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 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | normalizePath = 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 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | exports.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 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | exports.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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | exports.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 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | exports.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 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | exports.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 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | exports.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 |
|
258 | exports.Schema = yaml.Schema;
|