Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
feat(index.ls): rewrite module in object-oriented way
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchentw committed Nov 9, 2014
1 parent 1276aa5 commit 6094485
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
1 change: 0 additions & 1 deletion .jscsrc
Expand Up @@ -20,7 +20,6 @@
"requireYodaConditions": true,
"requireParenthesesAroundIIFE": true,
"requireSpacesInConditionalExpression": true,
"requireMultipleVarDecl": "onevar",
"requireBlocksOnNewline": 1,
"requireCommaBeforeLineBreak": true,
"requireSpaceBeforeBinaryOperators": true,
Expand Down
83 changes: 48 additions & 35 deletions src/index.ls
Expand Up @@ -2,50 +2,63 @@
require! {
through2
LiveScript
path
path: Path
"gulp-util": gutil
}

/* jshint -W030 */
module.exports = (options || {}) ->
!function modifyLS (file, enc, done)
return done null, file if file.isNull!
module.exports = (options) ->
through2.obj(
new VinylLSConverter(options || {}).transformFn
)

if file.isStream!
error = "Streaming not supported"
else
try
input = file.contents.toString "utf8"

dirname = path.dirname(file.path)
filename = path.basename(file.path, '.ls')
if options.json or '.json' is path.extname(filename)
file.path = path.join do
dirname,
path.basename(filename, '.json') + '.json'
json = true
else
file.path = path.join(dirname, filename + '.js')
json = false
/* jshint -W004 */
/* jshint -W014 */
/* jshint -W030 */
/* jshint -W033 */
/* jshint -W116 */
class VinylLSConverter
(@options) ->
@isJson = delete options.json

t = {input, options}
t.tokens = LiveScript.tokens t.input, raw: options.lex
t.ast = LiveScript.ast t.tokens
transformFn: !(file, enc, done) ~>
[error, clonedFile] = @_convert(file)
error = new gutil.PluginError "gulp-livescript", error if error
done(error, clonedFile)

options.bare ||= json
_convert: (file) ->
if file.isNull!
[null, file]
else if file.isStream!
["Streaming not supported", null]
else
@_tryConvertToJS(file.clone!)

t.ast.make-return! if json
t.output = t.ast.compile-root options
_tryConvertToJS: (clonedFile) ->
try
json = @_convertFilepath(clonedFile)
input = clonedFile.contents.toString("utf8")
options = {} <<< @options
options.bare ||= json

if json
t.result = LiveScript.run t.output, options, true
t.output = JSON.stringify(t.result, null, 2) + '\n'
tokens = LiveScript.tokens(input, raw: options.lex)
ast = LiveScript.ast(tokens)
ast.make-return! if json
output = ast.compile-root(options)

file.contents = new Buffer t.output
if json
result = LiveScript.run(output, options, true)
output = JSON.stringify(result, null, 2) + "\n"

catch error
clonedFile.contents = new Buffer(output)
catch error
clonedFile = null
[error, clonedFile]

error = new gutil.PluginError "gulp-livescript", error if error
done error, file
_convertFilepath: (clonedFile) ->
dirname = Path.dirname(clonedFile.path)
filename = Path.basename(clonedFile.path, ".ls")
json = @isJson or ".json" is Path.extname(filename)

through2.obj modifyLS
newFilename = if json then Path.basename(filename, ".json") + ".json" else filename + ".js"
clonedFile.path = Path.join(dirname, newFilename)
json

0 comments on commit 6094485

Please sign in to comment.