├── test ├── fixtures │ ├── foo.md │ ├── test.txt │ ├── test.yaml │ └── test.json ├── actual │ ├── a.yml │ └── b.yml └── test.js ├── .gitattributes ├── .travis.yml ├── .editorconfig ├── .gitignore ├── .verb.md ├── LICENSE ├── package.json ├── index.js ├── .eslintrc.json └── README.md /test/fixtures/foo.md: -------------------------------------------------------------------------------- 1 | FOOOO! -------------------------------------------------------------------------------- /test/fixtures/test.txt: -------------------------------------------------------------------------------- 1 | FILE CONTENTS!!! -------------------------------------------------------------------------------- /test/fixtures/test.yaml: -------------------------------------------------------------------------------- 1 | foo: 2 | bar: baz -------------------------------------------------------------------------------- /test/actual/a.yml: -------------------------------------------------------------------------------- 1 | a: 2 | b: 3 | c: 4 | d: e 5 | -------------------------------------------------------------------------------- /test/actual/b.yml: -------------------------------------------------------------------------------- 1 | a: 2 | b: 3 | c: 4 | d: e 5 | -------------------------------------------------------------------------------- /test/fixtures/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "foo": { 3 | "bar": "baz" 4 | } 5 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '7' 9 | - '6' 10 | - '5' 11 | - '4' 12 | - '0.12' 13 | - '0.10' 14 | matrix: 15 | allow_failures: [] 16 | fast_finish: true 17 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | Add to your javascript/node.js application with the following line of code: 4 | 5 | ```js 6 | var yaml = require('{%= name %}'); 7 | ``` 8 | 9 | ### async 10 | 11 | ```js 12 | var data = {language: 'node_js', node_js: ['0.10', '0.11']}; 13 | 14 | yaml('.travis.yml', data, function(err) { 15 | // do stuff with err 16 | }); 17 | ``` 18 | 19 | Would write `.travis.yml` to disk with the following contents: 20 | 21 | ```yaml 22 | language: node_js 23 | node_js: 24 | - "0.10" 25 | - "0.11" 26 | ``` 27 | 28 | 29 | ### sync 30 | 31 | ```js 32 | yaml.sync('.travis.yml', data); 33 | ``` 34 | 35 | Would write `.travis.yml` to disk with the following contents: 36 | 37 | ```yaml 38 | language: node_js 39 | node_js: 40 | - "0.10" 41 | - "0.11" 42 | ``` 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015, 2017, Jon Schlinkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "write-yaml", 3 | "description": "Write YAML. Converts JSON to YAML writes it to the specified file.", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/jonschlinkert/write-yaml", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 9 | "Shinnosuke Watanabe (https://shinnn.github.io)" 10 | ], 11 | "repository": "jonschlinkert/write-yaml", 12 | "bugs": { 13 | "url": "https://github.com/jonschlinkert/write-yaml/issues" 14 | }, 15 | "license": "MIT", 16 | "files": [ 17 | "index.js" 18 | ], 19 | "main": "index.js", 20 | "engines": { 21 | "node": ">=0.10.0" 22 | }, 23 | "scripts": { 24 | "test": "mocha" 25 | }, 26 | "dependencies": { 27 | "extend-shallow": "^2.0.1", 28 | "js-yaml": "^3.8.3", 29 | "write": "^0.3.3" 30 | }, 31 | "devDependencies": { 32 | "gulp-format-md": "^0.1.12", 33 | "mocha": "^3.2.0", 34 | "read-yaml": "^1.1.0", 35 | "should": "^11.2.1" 36 | }, 37 | "keywords": [ 38 | "data", 39 | "format", 40 | "json", 41 | "read", 42 | "write", 43 | "yaml" 44 | ], 45 | "verb": { 46 | "toc": false, 47 | "layout": "default", 48 | "tasks": [ 49 | "readme" 50 | ], 51 | "plugins": [ 52 | "gulp-format-md" 53 | ], 54 | "related": { 55 | "list": [ 56 | "read-data", 57 | "read-yaml", 58 | "write-data", 59 | "write-json" 60 | ] 61 | }, 62 | "lint": { 63 | "reflinks": true 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * write-yaml 3 | * 4 | * Copyright (c) 2014-2015, 2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var yaml = require('js-yaml'); 11 | var extend = require('extend-shallow'); 12 | var write = require('write'); 13 | 14 | module.exports = function(dest, data, options, cb) { 15 | if (typeof options === 'function') { 16 | cb = options; 17 | options = undefined; 18 | } 19 | 20 | if (typeof cb !== 'function') { 21 | throw new TypeError('expected callback to be a function'); 22 | } 23 | if (typeof dest !== 'string') { 24 | cb(new TypeError('expected dest to be a string')); 25 | return; 26 | } 27 | if (typeof data !== 'object') { 28 | cb(new TypeError('expected data to be an object')); 29 | return; 30 | } 31 | 32 | write(dest, toYaml(data, options), cb); 33 | }; 34 | 35 | /** 36 | * Sync method 37 | */ 38 | 39 | module.exports.sync = function(dest, data, options) { 40 | if (typeof dest !== 'string') { 41 | throw new TypeError('expected dest to be a string'); 42 | } 43 | if (typeof data !== 'object') { 44 | throw new TypeError('expected data to be an object'); 45 | } 46 | 47 | try { 48 | return write.sync(dest, toYaml(data, options)); 49 | } catch (err) { 50 | err.reason = 'write-yaml: failed to write "' + dest + '": '; 51 | throw err; 52 | } 53 | }; 54 | 55 | /** 56 | * Convert data to yaml with the specified 57 | * defaults and user-defined options 58 | */ 59 | 60 | function toYaml(data, options) { 61 | var defaults = {indent: 2, skipInvalid: false, flowLevel: -1}; 62 | var opts = extend(defaults, options); 63 | var fn = opts.safe ? yaml.safeDump : yaml.dump; 64 | return fn(data, opts); 65 | } 66 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * write-yaml 3 | * 4 | * Copyright (c) 2014-2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | require('mocha'); 11 | var fs = require('fs'); 12 | var path = require('path'); 13 | var should = require('should'); 14 | var readYaml = require('read-yaml'); 15 | var writeYaml = require('..'); 16 | var should = require('should'); 17 | 18 | var files = ['tmp/a.md', 'tmp/b.md', 'tmp/c.md', 'tmp/d.md', 'tmp/e.md']; 19 | 20 | describe('sync', function() { 21 | it('should write a yaml file synchronously', function() { 22 | writeYaml.sync('test/actual/a.yml', {a: {b: {c: {d: 'e'}}}}); 23 | var json = readYaml.sync('test/actual/a.yml'); 24 | json.a.should.have.property('b'); 25 | json.a.b.should.have.property('c'); 26 | }); 27 | 28 | it('should use specified indentation.', function() { 29 | writeYaml.sync('test/actual/b.yml', {a: {b: {c: {d: 'e'}}}}, {indent: 4}); 30 | var str = fs.readFileSync('test/actual/b.yml', 'utf8'); 31 | var secondLine = str.split('\n')[1]; 32 | var indent = secondLine.match(/^(\s+)/)[0].split('').length; 33 | indent.should.equal(4); 34 | }); 35 | }); 36 | 37 | describe('async', function() { 38 | it('should write a yaml file asynchronously', function(cb) { 39 | writeYaml('test/actual/a.yml', {a: {b: {c: {d: 'e'}}}}, function(err) { 40 | if (err) { 41 | console.log(err); 42 | return cb(err); 43 | } 44 | var json = readYaml.sync('test/actual/a.yml'); 45 | json.a.should.have.property('b'); 46 | json.a.b.should.have.property('c'); 47 | cb(); 48 | }); 49 | }); 50 | 51 | it('should use specified indentation.', function(cb) { 52 | writeYaml('test/actual/b.yml', {a: {b: {c: {d: 'e'}}}}, {indent: 4}, function(err) { 53 | if (err) { 54 | console.log(err); 55 | return cb(err); 56 | } 57 | var str = fs.readFileSync('test/actual/b.yml', 'utf8'); 58 | var secondLine = str.split('\n')[1]; 59 | var indent = secondLine.match(/^(\s+)/)[0].split('').length; 60 | indent.should.equal(4); 61 | cb(); 62 | }); 63 | }); 64 | }); 65 | 66 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # write-yaml [![NPM version](https://img.shields.io/npm/v/write-yaml.svg?style=flat)](https://www.npmjs.com/package/write-yaml) [![NPM monthly downloads](https://img.shields.io/npm/dm/write-yaml.svg?style=flat)](https://npmjs.org/package/write-yaml) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/write-yaml.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/write-yaml) 2 | 3 | > Write YAML. Converts JSON to YAML writes it to the specified file. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save write-yaml 11 | ``` 12 | 13 | Install with [yarn](https://yarnpkg.com): 14 | 15 | ```sh 16 | $ yarn add write-yaml 17 | ``` 18 | 19 | ## Usage 20 | 21 | Add to your javascript/node.js application with the following line of code: 22 | 23 | ```js 24 | var yaml = require('write-yaml'); 25 | ``` 26 | 27 | ### async 28 | 29 | ```js 30 | var data = {language: 'node_js', node_js: ['0.10', '0.11']}; 31 | 32 | yaml('.travis.yml', data, function(err) { 33 | // do stuff with err 34 | }); 35 | ``` 36 | 37 | Would write `.travis.yml` to disk with the following contents: 38 | 39 | ```yaml 40 | language: node_js 41 | node_js: 42 | - "0.10" 43 | - "0.11" 44 | ``` 45 | 46 | ### sync 47 | 48 | ```js 49 | yaml.sync('.travis.yml', data); 50 | ``` 51 | 52 | Would write `.travis.yml` to disk with the following contents: 53 | 54 | ```yaml 55 | language: node_js 56 | node_js: 57 | - "0.10" 58 | - "0.11" 59 | ``` 60 | 61 | ## About 62 | 63 | ### Related projects 64 | 65 | * [read-data](https://www.npmjs.com/package/read-data): Read JSON or YAML files. | [homepage](https://github.com/jonschlinkert/read-data "Read JSON or YAML files.") 66 | * [read-yaml](https://www.npmjs.com/package/read-yaml): Very thin wrapper around js-yaml for directly reading in YAML files. | [homepage](https://github.com/jonschlinkert/read-yaml "Very thin wrapper around js-yaml for directly reading in YAML files.") 67 | * [write-data](https://www.npmjs.com/package/write-data): Write a YAML or JSON file to disk. Automatically detects the format to write based… [more](https://github.com/jonschlinkert/write-data) | [homepage](https://github.com/jonschlinkert/write-data "Write a YAML or JSON file to disk. Automatically detects the format to write based on extension. Or pass `ext` on the options.") 68 | * [write-json](https://www.npmjs.com/package/write-json): Write a JSON file to disk, also creates intermediate directories in the destination path if… [more](https://github.com/jonschlinkert/write-json) | [homepage](https://github.com/jonschlinkert/write-json "Write a JSON file to disk, also creates intermediate directories in the destination path if they don't already exist.") 69 | 70 | ### Contributing 71 | 72 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 73 | 74 | ### Contributors 75 | 76 | | **Commits** | **Contributor** | 77 | | --- | --- | 78 | | 12 | [jonschlinkert](https://github.com/jonschlinkert) | 79 | | 2 | [shinnn](https://github.com/shinnn) | 80 | 81 | ### Building docs 82 | 83 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 84 | 85 | To generate the readme, run the following command: 86 | 87 | ```sh 88 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 89 | ``` 90 | 91 | ### Running tests 92 | 93 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 94 | 95 | ```sh 96 | $ npm install && npm test 97 | ``` 98 | 99 | ### Author 100 | 101 | **Jon Schlinkert** 102 | 103 | * [github/jonschlinkert](https://github.com/jonschlinkert) 104 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 105 | 106 | ### License 107 | 108 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 109 | Released under the [MIT License](LICENSE). 110 | 111 | *** 112 | 113 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 12, 2017._ --------------------------------------------------------------------------------