This file, and all other .litcoffee files in this repo, flip the idea of coding around. Instead of seeing a code file with readable comments for documentation, you are now reading an English document with runnable code blocks. Sections set apart after a blank line and indented by four or more spaces will be executed:
print = console.log # alias the output function for clarity
print 'Running README...'However, anything after a hashtag is ignored, even if set apart and indented.
# print "This won't appear when running README.litcoffee"You will need node, npm, and coffeescript.
Once you have node, do npm install -g coffeescript to get coffee and cake.
Then, coffee README.litcoffee or cake bake will set up the node package!
To use the development server, you can run npm install now. This will download
devDependencies =
brunch: '2.10.17'
'coffee-script-brunch': '3.0.0'that can be used to host a local web server.
Node/npm requires a package.json file, and we create and save ours right here!
Lets create a function called runme.
Everything indented after the arrow -> will be part of this new function that
we are assigning to a variable called runme:
runme = ->
exports.package =
name: 'sketchscript'
version: '1.0.0'
description: 'literate programming playground'
main: 'README.litcoffee'
author: 'mdan'
license: 'AGPL-3.0'
scripts: arguments[0] # this will be passed in when run
devDependencies: devDependencies # this was defined on line 12, above
# de-indenting here says we're done writing the 'runme' functionThe word 'exports' above lets us treat this file as a module that can be used to get the data and code that is in this file from another document.
We have alreday done this in another file (scripts.litcoffee in the tools
directory) so that we can assign a variable here to reference the data there:
scripts = require './tools/scripts.litcoffee'Now, scripts contains all the exported code from the scripts.litcoffee file.
Until we execute runme(), exports.package does not exist.
Let's execute it now, making sure to send, or pass in, our scripts varible
as the first argument to the function call.
runme scripts- It would also work with explicit parentheses, as
runme(scripts). This is often helpful for spreading a function call over multiple lines of code. - Inside the function, the special
argumentsarray will have the data fromscriptsin array index 0. We can access this data with square brackets:arguments[0]. - We wouldn't have to use the
argumentsarray if we defined the function with a name to call this argument, for example, withrunme = (scriptSection) ->
Now that we have all the data stored in exports.package, let's make it into a
string of characters using the build-in JSON object's stringify method:
print '...serializing to pretty-printed json...'
formattedJSON = JSON.stringify(exports.package, null, ' ')The second argument is null to indicate that we want to include all
properties, rather than just some, in our output text.
In order for us to use our package.json, we must save this file using node's
core fs library:
print '...writing to disk...'
fs = require('fs')
fs.writeFileSync('package.json', formattedJSON)
print '...ready to use npm commands!'If you haven't already, now may be a good time to run this file with cake bake