Skip to content

Instantly share code, notes, and snippets.

@MitMaro
Last active April 8, 2016 12:38
Show Gist options
  • Select an option

  • Save MitMaro/0ee2134e480a844c074bdc44678d58f6 to your computer and use it in GitHub Desktop.

Select an option

Save MitMaro/0ee2134e480a844c074bdc44678d58f6 to your computer and use it in GitHub Desktop.
Covert JSON to Node module
#!/bin/bash
SCRIPTROOT="$( cd "$(dirname "$0")" ; pwd -P )"
for f in "$@"; do
NEWFILE="${f:0:${#f} - 4}js"
gsed -f "$SCRIPTROOT/jsonToModule.script" "$f" > "$NEWFILE"
rm "$f"
done
# remove quotes around most identifiers
s/"\([a-zA-Z0-9_]*\)":/\1:/
# escape existing single quotes
s/'/\\'/g
# replace double quotes with single
s/"/'/g
# Add module loader
1s/^/'use strict';\n\nmodule.exports = /
# add trailing semicolon
$s/\(.*\)/\1;/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment