1 |
|
2 | Parser = require './Parser'
|
3 | Dumper = require './Dumper'
|
4 | Utils = require './Utils'
|
5 |
|
6 | # Yaml offers convenience methods to load and dump YAML.
|
7 | #
|
8 | class Yaml
|
9 |
|
10 | # Parses YAML into a JavaScript object.
|
11 | #
|
12 | # The parse method, when supplied with a YAML string,
|
13 | # will do its best to convert YAML in a file into a JavaScript object.
|
14 | #
|
15 | # Usage:
|
16 | # myObject = Yaml.parse('some: yaml');
|
17 | # console.log(myObject);
|
18 | #
|
19 | # @param [String] input A string containing YAML
|
20 | # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise
|
21 | # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise
|
22 | #
|
23 | # @return [Object] The YAML converted to a JavaScript object
|
24 | #
|
25 | # @throw [ParseException] If the YAML is not valid
|
26 | #
|
27 | @parse: (input, exceptionOnInvalidType = false, objectDecoder = null) ->
|
28 | return new Parser().parse(input, exceptionOnInvalidType, objectDecoder)
|
29 |
|
30 |
|
31 | # Parses YAML from file path into a JavaScript object.
|
32 | #
|
33 | # The parseFile method, when supplied with a YAML file,
|
34 | # will do its best to convert YAML in a file into a JavaScript object.
|
35 | #
|
36 | # Usage:
|
37 | # myObject = Yaml.parseFile('config.yml');
|
38 | # console.log(myObject);
|
39 | #
|
40 | # @param [String] path A file path pointing to a valid YAML file
|
41 | # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise
|
42 | # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise
|
43 | #
|
44 | # @return [Object] The YAML converted to a JavaScript object or null if the file doesn't exist.
|
45 | #
|
46 | # @throw [ParseException] If the YAML is not valid
|
47 | #
|
48 | @parseFile: (path, callback = null, exceptionOnInvalidType = false, objectDecoder = null) ->
|
49 | if callback?
|
50 | # Async
|
51 | Utils.getStringFromFile path, (input) =>
|
52 | result = null
|
53 | if input?
|
54 | result = @parse input, exceptionOnInvalidType, objectDecoder
|
55 | callback result
|
56 | return
|
57 | else
|
58 | # Sync
|
59 | input = Utils.getStringFromFile path
|
60 | if input?
|
61 | return @parse input, exceptionOnInvalidType, objectDecoder
|
62 | return null
|
63 |
|
64 |
|
65 | # Dumps a JavaScript object to a YAML string.
|
66 | #
|
67 | # The dump method, when supplied with an object, will do its best
|
68 | # to convert the object into friendly YAML.
|
69 | #
|
70 | # @param [Object] input JavaScript object
|
71 | # @param [Integer] inline The level where you switch to inline YAML
|
72 | # @param [Integer] indent The amount of spaces to use for indentation of nested nodes.
|
73 | # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types (a JavaScript resource or object), false otherwise
|
74 | # @param [Function] objectEncoder A function to serialize custom objects, null otherwise
|
75 | #
|
76 | # @return [String] A YAML string representing the original JavaScript object
|
77 | #
|
78 | @dump: (input, inline = 2, indent = 4, exceptionOnInvalidType = false, objectEncoder = null) ->
|
79 | yaml = new Dumper()
|
80 | yaml.indentation = indent
|
81 |
|
82 | return yaml.dump(input, inline, 0, exceptionOnInvalidType, objectEncoder)
|
83 |
|
84 |
|
85 | # Registers .yml extension to work with node's require() function.
|
86 | #
|
87 | @register: ->
|
88 | require_handler = (module, filename) ->
|
89 | # Fill in result
|
90 | module.exports = YAML.parseFile filename
|
91 |
|
92 | # Register require extensions only if we're on node.js
|
93 | # hack for browserify
|
94 | if require?.extensions?
|
95 | require.extensions['.yml'] = require_handler
|
96 | require.extensions['.yaml'] = require_handler
|
97 |
|
98 |
|
99 | # Alias of dump() method for compatibility reasons.
|
100 | #
|
101 | @stringify: (input, inline, indent, exceptionOnInvalidType, objectEncoder) ->
|
102 | return @dump input, inline, indent, exceptionOnInvalidType, objectEncoder
|
103 |
|
104 |
|
105 | # Alias of parseFile() method for compatibility reasons.
|
106 | #
|
107 | @load: (path, callback, exceptionOnInvalidType, objectDecoder) ->
|
108 | return @parseFile path, callback, exceptionOnInvalidType, objectDecoder
|
109 |
|
110 |
|
111 | # Expose YAML namespace to browser
|
112 | window?.YAML = Yaml
|
113 |
|
114 | # Not in the browser?
|
115 | unless window?
|
116 | @YAML = Yaml
|
117 |
|
118 | module.exports = Yaml
|