-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.coffee
50 lines (40 loc) · 1.31 KB
/
index.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
through = require("through2")
gutil = require("gulp-util")
# gulp-indent
# Indents piped files
# Options: (key/default)
# * tabs: false
# * amount: 2
isEmptyLine = (string)->
return string is "" or string is "\r"
module.exports = (options={}) ->
options.tabs ?= false
options.amount ?= 2
indent = (file, enc, done) ->
#jshint validthis:true
# Do nothing if no contents
if file.isNull()
@push file
return done()
# Error if file is a stream
if file.isStream()
@emit "error", new gutil.PluginError("gulp-indent", "Stream content is not supported")
return done()
# Indent the file
if file.isBuffer()
# Tabs or spaces
character = if options.tabs then "\t" else " "
# Create the correct amount of indentation
indentation = ""
indentation += character for number in [0...options.amount]
# Split the file into lines
lines = file.contents.toString().split "\n"
# Add the indentation to the lines, if they aren't empty.
lines = ((if !isEmptyLine(line) then indentation+line else line) for line in lines )
# Replace the file contents
file.contents = new Buffer(lines.join "\n")
# Return the file to the stream
@push file
#Return control to the stream
done()
through.obj indent