@@ -35,6 +35,43 @@ formats, and builds a JSON document suitable for use with CouchDB. The most
3535common application is for CouchDB design documents, that typically contain
3636embedded code.
3737
38+ ## Usage
39+
40+ ### Command line usage
41+
42+ couchdb-builder <source-path> > <destination-path>
43+
44+ ### Module usage
45+
46+ * CouchDB builder* implements a [ promise] -based interface:
47+
48+ ``` js
49+ var couchdbBuilder = require (' couchdb-builder' );
50+ var fs = require (' fs' );
51+ var jsonStringify = require (' json-stable-stringify' );
52+
53+ couchdbBuilder .build (sourcePath).then (
54+ function (result ) {
55+ fs .writeFile (
56+ targetPath,
57+ jsonStringify (result) + " \n " ,
58+ function (e ) {
59+ console .error (' Error writing file: ' + e);
60+ }
61+ );
62+ },
63+ function (e ) {
64+ console .error (' Error building document: ' + e);
65+ }
66+ );
67+ ```
68+
69+ Note that [ json-stable-stringify] is used instead of [ JSON.stringify] to ensure
70+ idempotent output, which helps reduce diff churn when the build product is
71+ committed to a version control repository.
72+
73+ [ promise ] : https://promisesaplus.com/
74+
3875## Differences to [ couchdb-compile]
3976
4077### Better support for CommonJS modules
@@ -102,6 +139,17 @@ once (it also simplifies the wrapper code).
102139
103140[ pull request ] : https://github.com/jo/couchdb-compile/pull/29
104141
142+ ### Idempotent output
143+
144+ When generating JSON output, [ couchdb-compile] internally uses [ JSON.stringify] ,
145+ which does not guarantee the order of properties in the output. If the resulting
146+ JSON is committed to a version control repository, this will sometimes produce
147+ "diff churn" (repeated commits to a file that do not change its content in a
148+ meaningful way).
149+
150+ * CouchDB builder* utilizes [ json-stable-stringify] instead of [ JSON.stringify] ,
151+ to ensure that its output is idempotent (always the same, given the same input).
152+
105153### Unsupported features
106154
107155The following features of [ couchdb-compile] are not currently supported. Please
@@ -113,4 +161,8 @@ open an issue if further discussion is warranted:
113161- Special treatment of ` index.js ` or ` index.coffee ` files as CommonJS modules.
114162 All ` .js ` and ` .coffee ` files are treated as modules in * CouchDB builder* .
115163
164+ <!-- References -->
165+
116166[ couchdb-compile ] : https://github.com/jo/couchdb-compile
167+ [ json-stable-stringify ] : https://github.com/substack/json-stable-stringify
168+ [ json.stringify ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
0 commit comments