UNPKG

6.91 kBtext/coffeescriptView Raw
1return if global.window
2
3# require '../runtime/sugar'
4fs = require 'fs'
5np = require 'path'
6cp = require 'child_process'
7
8isWindows = process.platform is 'win32'
9fixCommand = (command) ->
10 if not isWindows
11 command = command.replace /\.cmd\b/, ""
12 command
13
14module.exports = exports =
15 spawn: spawn = (command, options, callback) ->
16 originalCommand = command
17 return callback?() unless command?
18 command = fixCommand command
19 if typeof options is 'function'
20 callback = options
21 options = null
22 options ?= {}
23 options.stdio ?= 'inherit'
24 args = command.split /\s+/
25 command = args.shift()
26 try
27 child = cp.spawn command, args, options
28 child.on 'exit', callback if callback?
29 child.on 'error', (error) ->
30 console.log "Error running #{originalCommand}\n#{error}"
31 callback?()
32 catch e
33 console.log originalCommand
34 throw e
35 return child
36 exec: exec = (command, options, callback) ->
37 originalCommand = command
38 return callback?() unless command?
39 command = fixCommand command
40 if typeof options is 'function'
41 callback = options
42 options = null
43 options ?= {}
44 try
45 cp.exec command, options, (err, stdout, stderr) ->
46 console.log err if err?
47 console.log stdout.toString() if stdout?
48 console.log stderr.toString() if stderr?
49 callback?()
50 catch e
51 console.log originalCommand
52 throw e
53 copyMetadata: copyMetadata = (source, target) ->
54 for file in ["package.json", "README.md"]
55 from = np.join source, file
56 to = np.join target, file
57 if fs.existsSync from
58 copy from, to
59 buildCoffee: buildCoffee = (input, output, callback) ->
60 spawn "coffee.cmd -c -o #{output} #{input}", callback
61 watchCoffee: watchCoffee = (input, output) ->
62 spawn "coffee.cmd -w -c -o #{output} #{input}"
63 isMatch: isMatch = (value, match, defaultValue=false) ->
64 return defaultValue unless match?
65 return match value if 'function' is typeof match
66 if Array.isArray match
67 # see if it matches any subitem in the array
68 for item in match
69 if isMatch value, item
70 return true
71 return false
72 value = normalizePath value
73 if typeof match is 'string'
74 return value.startsWith(match) or value.endsWith(match)
75 # return value.substring(value.length-match.length) is match
76 value = value.split(/[\/\\]/g).pop()
77 return match.test?(value)
78 defaultFileExclude: ["node_modules","www"]
79 removeExtension: removeExtension = (file) ->
80 dot = file.lastIndexOf '.'
81 if dot > 0
82 return file.substring 0, dot
83 else
84 return file
85 changeExtension: changeExtension = (file, ext) -> removeExtension(file) + ext
86 touch: touch = (file) ->
87 now = new Date()
88 fs.utimesSync file, now, now
89 getModified: getModified = (path) ->
90 try
91 if fs.existsSync path
92 stats = fs.statSync path
93 if stats.mtime?
94 date = new Date(stats.mtime)
95 time = date.getTime()
96 return time
97 else
98 return 0
99 catch e
100 console.warn e
101 return 0
102 isFile: isFile = (file) -> fs.statSync(file)?.isFile?() is true
103 isDirectory: isDirectory = (file) -> fs.statSync(file)?.isDirectory?() is true
104 list: list = (dir, options={}, files=[]) ->
105 exclude = options.exclude ? exports.defaultFileExclude
106 recursive = options.recursive ? true
107 if fs.existsSync dir
108 for file in fs.readdirSync(dir)
109 file = np.join dir, file
110 if not isMatch file, exclude, false
111 files.push file if isMatch file, options.include, true
112 if recursive and isDirectory file
113 list file, options, files
114 files
115 makeDirectories: makeDirectories = (dir) ->
116 if typeof dir isnt 'string'
117 throw new Error "dir is not a string: #{JSON.stringify dir}"
118 if not fs.existsSync dir
119 # make parent first
120 makeDirectories np.dirname dir
121 # make self
122 fs.mkdirSync dir
123 makeParentDirectories: makeParentDirectories = (file) ->
124 makeDirectories np.dirname file
125 read: read = (file, encoding) ->
126 if encoding == undefined
127 encoding = 'utf8'
128 fs.readFileSync(file, encoding)
129 write: write = (file, content, encoding) ->
130 makeParentDirectories file
131 if content?
132 if encoding == undefined and typeof content is 'string'
133 encoding = 'utf8'
134 fs.writeFileSync(file, content, encoding)
135 else if fs.existsSync file
136 fs.unlinkSync(file)
137 # copies files or folders
138 copy: copy = (source, target, include) ->
139 target = np.normalize target
140 if isFile(source)
141 if isMatch(source, include, true)
142 content = read source
143 write target, content
144 console.log "Copied: #{np.normalize target}"
145 else if isDirectory source
146 files = fs.readdirSync source
147 for file in files
148 copy np.join(source, file), np.join(target, file), include
149 normalizePath: normalizePath = (path) -> path?.replace /\\/g,"\/"
150 watchCopy: (input, output, include) ->
151 watcher = require './watcher'
152 watcher.watchDirectory input, {include:include}, (inputFile) ->
153 outputFile = np.join(output, np.relative(input, inputFile))
154 copy inputFile, outputFile
155 getMatches: (s, regex, group) ->
156 if not regex.global
157 throw 'regex must be declared with global modifier /trailing/g'
158 results = []
159 while match = regex.exec s
160 results.push if group > 0 then match[group] else match
161 results
162
163# if typeof describe is 'function'
164# assert = require 'assert'
165# describe 'glass.build.utility', ->
166# describe 'isMatch', ->
167# it "should work", ->
168# assert isMatch "foo.js", ".js"
169# assert isMatch "foo.js", ["foo.bar","foo.js"]
170# assert isMatch "foo.js", /\.js$/
171# assert isMatch "foo.js", (x) -> x is "foo.js"
172# assert not isMatch "foo.jsp", ".js"
173# assert not isMatch "foo.jsp", ["foo.bar","foo.js"]
174# assert not isMatch "foo.jsp", /\.js$/
175# assert not isMatch "foo.jsp", (x) -> x is "foo.js"
176
\No newline at end of file