UNPKG

805 Btext/coffeescriptView Raw
1mkdirp = require 'mkdirp'
2fs = require 'fs'
3
4#
5# Convenient method for writing a file on
6# a path that might not exist. This function
7# will create all folders provided in the
8# path to the file.
9#
10#
11# writeToFile('/tmp/hi/folder/file.js', "console.log('hi')")
12#
13# will create a file at /tmp/hi/folder/file.js with provided content
14#
15writeToFile = (file, content) ->
16 try
17 fs.writeFileSync(file, content)
18 catch e
19 if e.code == 'ENOENT' or e.code == 'EBADF'
20 splitted = file.split('/')
21 mkdirp.sync(splitted.splice(0, splitted.length-1).join('/'), 0777)
22
23 # Retry!
24 writeToFile(file, content)
25 else
26 console.log e
27
28#
29# This is fancy
30#
31normalizeUrl = (url) ->
32 return url.replace('//', '/')
33
34exports.writeToFile = writeToFile
35exports.normalizeUrl = normalizeUrl