Giter VIP home page Giter VIP logo

season's Introduction

Atom and all repositories under Atom will be archived on December 15, 2022. Learn more in our official announcement

season - CSON Node module

macOS Build Status Windows Build Status Dependency Status

Read and write CSON/JSON files seamlessly.

Installing

npm install season

Building

  • Clone the repository
  • Run npm install
  • Run grunt to compile the CoffeeScript code
  • Run grunt test to run the specs

Compiling CSON to JSON

This module comes with a csonc executable that allows you to compile a CSON file to JSON.

To use:

npm install -g season
echo "this: 'is cson'" > file.cson
csonc file.cson --output file.json
cat file.json
{
  "this": "is cson"
}

Docs

CSON = require 'season'

CSON.setCacheDir(cacheDirectory)

Set the cache directory to use for storing compiled CSON files.

cacheDirectory - Root directory path for storing compiled CSON.

CSON.stringify(object)

Convert the object to a CSON string.

object - The object to convert to CSON.

Returns the CSON string representation of the given object.

CSON.readFile(objectPath, callback)

Read the CSON or JSON object at the given path and return it to the callback once it is read and parsed.

objectPath - The string path to a JSON or CSON object file.

callback - The function to call with the error or object once the path is read and parsed.

CSON.readFileSync(objectPath)

Synchronous version of CSON.readFile(objectPath, callback).

Returns the object read from the path or throws an error if reading fails.

CSON.writeFile(objectPath, object, callback)

Write the object to the given path as either JSON or CSON depending on the path's extension.

objectPath - The string path to a JSON or CSON object file.

object - The object to convert to a string and write to the path.

callback - The function to with an error object on failures.

CSON.writeFileSync(objectPath, object)

Synchronous version of CSON.writeFile(objectPath, object, callback)

CSON.isObjectPath(objectPath)

Is the given path a valid object path?

Returns true if the path has a .json or .cson file extension, false otherwise.

CSON.resolve(objectPath)

Resolve the path to an existent file that has a .json or .cson extension.

objectPath - The string path to a JSON or CSON object file with or without an extension.

Returns the path to an existent CSON or JSON file or null if none found.

season's People

Contributors

damieng avatar darangi avatar edjubuh avatar johan avatar kevinsawicki avatar maxbrunsfeld avatar smashwilson avatar strd6 avatar torn4dom4n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

season's Issues

Preserve cson formatting, order and quoting.

I stumbled upon atom/atom#7763 when looking for an existing issue on this. It seems like the original issue never got to be recreated here, so I would like to do after all.

As season is used by atom for parsing of and writing to config.cson, this means no formatting is preserved after safe. Over time I've hacked on my config quite a lot, and I would really like to add some comments and whitespace here and there to provide some readability.

Example (taken from original issue):

Edited config.cson:

#
# Config
#

'*':
    # --- MAIN ---

    # WELCOME
    welcome:
        showOnStartup: false

    # CORE
    core:
        disabledPackages: [
            'autocomplete-plus'
            'wrap-guide'
        ]
        themes: [
            'one-dark-ui'
            'solarized-dark-syntax'
        ]

    # EDITOR
    editor:
        invisibles: {}
        tabLength: 4


    # --- PACKAGES ---

    # REPORTING
    'exception-reporting':
        userId: 'ddaafd07-dffd-b252-48db-612909a97759'

    # BRACKET MATCHER
    'bracket-matcher':
        autocompleteBrackets: false
        autocompleteSmartQuotes: false

    # WHITESPACE
    whitespace:
        ensureSingleTrailingNewline: false

After saving:

"*":
  welcome:
    showOnStartup: false
  core:
    disabledPackages: [
      "autocomplete-plus"
      "wrap-guide"
    ]
    themes: [
      "one-dark-ui"
      "solarized-dark-syntax"
    ]
  editor:
    invisibles: {}
    tabLength: 4
  "exception-reporting":
    userId: "ddaafd07-dffd-b252-48db-612909a97759"
  "bracket-matcher":
    autocompleteBrackets: false
    autocompleteSmartQuotes: false
  whitespace:
    ensureSingleTrailingNewline: false

Object __proto__ can be hijacked

Running CSON.readFile on the following CSON results in the callback object's __proto__ being hijacked:

'__proto__':
  toString: 1

season-incorrect-proto

Any calls to the object's .toString() method will fail as toString is now an object.

This also has the side effect of preventing season from properly returning __proto__ keys (which can be useful in, for example, snippets).

catching callback errors

season is catching an error thrown by one of my callbacks which made debugging a pain.
The offending function is:

parseContents = (objectPath, cachePath, contents, callback) ->
  try
    object = parseObject(objectPath, contents)
    writeCacheFile(cachePath, object) if cachePath
    callback?(null, object)
  catch parseError
    callback?(parseError)

try/catches should be extremely limited in their scope to they only catch errors they want to.
Thus I believe this function needs to be updated to:

parseContents = (objectPath, cachePath, contents, callback) ->
  try
    object = parseObject(objectPath, contents)
  catch parseError
    return callback?(parseError)

  writeCacheFile(cachePath, object) if cachePath
  callback?(null, object)

I'm happy to create a PR with this change and test verifying this behavior if you agree

error on simultaneous write from two atom instances

Due to a problem with my package mocha-ui, I encountered a problem with CSON.writeFile.

I have two watchers on a file from two different atom instances, on change, a cson file is written, potential with different content. Quite often, the resulting file is a combination of both, which is of course invalid cson ;)

One of the files is larger, if this gets written first, then a fragment of the larger file is tailing the smaller file which is written a little bit later.

Maybe rather a problem of fs-plus or fs ?

updateFile

Inspired by the fact that I have this in my code

CSON.readFile filePath, (err, data) ->
  if err then return done err
  # manipulate data
  CSON.writeFile filePath, data, done

and would like to be able to just write

CSON.updateFile(
  filePath 
  (data) -> # manipulate data
  done
)

CSON.updateFile(objectPath, transform, callback)

Updates the CSON or JSON at the given path.

objectPath - The string path to a JSON or CSON object file.

transform - The function called with the read object and should return the object to be written.

callback - The function to with an error object on failures.

Add feature to write all keys as strings

file.json:

{
  "a": 5,
  "b": 6,
  "c-d": 7
}

index.js:

var CSON = require('season');
CSON.writeFileSync('file.cson', CSON.readFileSync('file.json'));

file.cson:

a: 5
b: 6
"c-d": 7

I would prefer to have all keys written as strings, it makes bigger files much easier to read.

CSON.writeFile writing json by default

CSON.writeFile only writes cson if the file extension ends with .cson and write json otherwise.
I'd prefer it to be the other way around and that is only writes json when the file extension is .json and cson otherwise.

This is a cson library so I think that makes sense. I like the idea of writing being able to convert json to cson (to make it easier for users to convert over) but otherwise don't see any reason to have all this support for json.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.