Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new directives for including files #14

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/snockets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,38 @@ module.exports = class Snockets
requireTree itemPath
q.unwaitFor itemPath

escapeFile = (string) ->
string = string.replace(/\\/g, '\\\\').replace(/(['"])/g, '\\$1')
string = string.replace(/\n/, "\\\n") unless filePath.match(/\.coffee$/)
return string

include = (relPath, substr, escape=off) =>
# i'm setting random holder (just in case of unwanted collisions)
q.waitFor hold = Math.random()
onFound = (incPath) =>
@readFile incPath, flags, (err, fileChanged) =>
return callback err if err
if fileChanged then graphChanged = true
replaceTo = @cache[incPath].data.toString('utf8')
replaceTo = escapeFile(replaceTo) if escape
# here we are casting buffer to utf8... any need to cast it back?
@cache[filePath].data = @cache[filePath].data.toString('utf8').
replace(substr, replaceTo)
q.unwaitFor hold

relName = stripExt relPath
if relName.match EXPLICIT_PATH
onFound relPath
else
incName = path.join path.dirname(filePath), relName
@findMatchingFile incName, flags, (err, incPath) ->
return callback err if err
onFound incPath

@readFile filePath, flags, (err, fileChanged) =>
return callback err if err
if fileChanged then graphChanged = true
for directive in parseDirectives(@cache[filePath].data.toString 'utf8')
for [substr, directive] in parseDirectives(@cache[filePath].data.toString 'utf8')
words = directive.replace(/['"]/g, '').split /\s+/
[command, relPaths...] = words

Expand All @@ -163,6 +191,11 @@ module.exports = class Snockets
when 'require_tree'
for relPath in relPaths
requireTree path.join path.dirname(filePath), relPath
when 'include'
# multiple file including is not supported :(
include relPaths[0], substr
when 'include_str'
include relPaths[0], substr, on

q.finalize()

Expand Down Expand Up @@ -290,7 +323,7 @@ parseDirectives = (code) ->
code = code.replace /[\r\t ]+$/gm, '\n' # fix for issue #2
return [] unless match = HEADER.exec(code)
header = match[0]
match[1] while match = DIRECTIVE.exec header
match while match = DIRECTIVE.exec header

stripExt = (filePath) ->
if path.extname(filePath) in jsExts()
Expand All @@ -310,4 +343,4 @@ minify = (js) ->
pro.gen_code ast

timeEq = (date1, date2) ->
date1? and date2? and date1.getTime() is date2.getTime()
date1? and date2? and date1.getTime() is date2.getTime()