<commit_before>{docopt} = require('docopt')
require! 'fs'

shelljs = require('shelljs')
server  = require('./lib/server')
config  = require('./lib/config')

exec = require('bluebird').promisify(shelljs.exec)

get-options = ->
    doc = shelljs.cat(__dirname+"/docs/usage.md")

    get-option = (a, b, def, o) ->
        if not o[a] and not o[b]
            return def
        else 
            return o[b]

    o = docopt(doc)

    port   = get-option('-p', '--port', 1666, o)
    code   = o['CODE']
    engine = get-option('-e', '--engine', 'javascript', o)
    dry    = get-option('-d', '--dry', false, o)

    if o['run'] 
        return { serve: false, run: true, code: code, engine: engine, dry: dry, +verbose }
    else
        return { serve: true, run: false, port: port , -verbose}

configure-server-dependencies = ->
    { configure } = require('./lib/das')
    configure({
            'fs': 'fs' 
            'shelljs': 'shelljs'
            'os': 'os'
            'uid': 'uid'
    })   

configure-cli-dependencies = (is-it-dry) ->
    { configure } = require('./lib/das')

    dryWrite = (name, content, mode, callback) ->
                        console.log "Writing to #name: #content"
                        callback(null, 'ok')

    dryExec = (command, opts, callback) ->
        console.log "Executing: #command"
        callback(0, 'ok')

    if is-it-dry
        if require('os').platform() != 'darwin'
            configure({
                'fs': { writeFile: dryWrite }
                'shelljs': { exec: dryExec }
                'os': 'os'
                'uid': 'uid'
            })
        else 
            configure({
                'fs': { writeFile: dryWrite }
                'shelljs': { exec: dryExec }
                'os': { tmpdir: -> "faketmp" }
                'uid': -> 'fakeuid'
            })        
    else 
        configure({
            'fs': 'fs' 
            'shelljs': 'shelljs'
            'os': 'os'
            'uid': 'uid'
        })        

main = ->
    # Main program configuration
    config.set(get-options!)

    # Parse commands
    { serve, run } = get-options!
    if serve 
        { port } = get-options!
        configure-server-dependencies()
        server.bringup(port)

    else 
        { code, engine, dry } = get-options!
        configure-cli-dependencies(dry)
        require('./lib/codejail').run(engine, code)


main!





<commit_msg>fix exit code when launched from command line<commit_after>{docopt} = require('docopt')
require! 'fs'

shelljs = require('shelljs')
server  = require('./lib/server')
config  = require('./lib/config')

exec = require('bluebird').promisify(shelljs.exec)

get-options = ->
    doc = shelljs.cat(__dirname+"/docs/usage.md")

    get-option = (a, b, def, o) ->
        if not o[a] and not o[b]
            return def
        else
            return o[b]

    o = docopt(doc)

    port   = get-option('-p', '--port', 1666, o)
    code   = o['CODE']
    engine = get-option('-e', '--engine', 'javascript', o)
    dry    = get-option('-d', '--dry', false, o)

    if o['run']
        return { serve: false, run: true, code: code, engine: engine, dry: dry, +verbose }
    else
        return { serve: true, run: false, port: port , -verbose}

configure-server-dependencies = ->
    { configure } = require('./lib/das')
    configure({
            'fs': 'fs'
            'shelljs': 'shelljs'
            'os': 'os'
            'uid': 'uid'
    })

configure-cli-dependencies = (is-it-dry) ->
    { configure } = require('./lib/das')

    dryWrite = (name, content, mode, callback) ->
                        console.log "Writing to #name: #content"
                        callback(null, 'ok')

    dryExec = (command, opts, callback) ->
        console.log "Executing: #command"
        callback(0, 'ok')

    if is-it-dry
        if require('os').platform() != 'darwin'
            configure({
                'fs': { writeFile: dryWrite }
                'shelljs': { exec: dryExec }
                'os': 'os'
                'uid': 'uid'
            })
        else
            configure({
                'fs': { writeFile: dryWrite }
                'shelljs': { exec: dryExec }
                'os': { tmpdir: -> "faketmp" }
                'uid': -> 'fakeuid'
            })
    else
        configure({
            'fs': 'fs'
            'shelljs': 'shelljs'
            'os': 'os'
            'uid': 'uid'
        })

main = ->
    # Main program configuration
    config.set(get-options!)

    # Parse commands
    { serve, run } = get-options!
    if serve
        { port } = get-options!
        configure-server-dependencies()
        server.bringup(port)

    else
        { code, engine, dry } = get-options!
        configure-cli-dependencies(dry)
        require('./lib/codejail').run(engine, code).catch ->
          process.exit(1)


main!

{'fraction_non_alphanumeric': 0.16190671871789603, 'fraction_numerical': 0.0022601191699198686, 'mean_word_length': 1.641345632121541, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '543882', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1477, 'n_words': 356}