pulsar/build/tasks/task-helpers.coffee

69 lines
2.4 KiB
CoffeeScript
Raw Normal View History

fs = require 'fs-plus'
2013-06-27 20:07:33 +04:00
path = require 'path'
module.exports = (grunt) ->
cp: (source, destination, {filter}={}) ->
2013-10-29 00:53:51 +04:00
unless grunt.file.exists(source)
grunt.fatal("Cannot copy non-existent #{source.cyan} to #{destination.cyan}")
copyFile = (sourcePath, destinationPath) ->
return if filter?(sourcePath) or filter?.test?(sourcePath)
stats = fs.lstatSync(sourcePath)
if stats.isSymbolicLink()
grunt.file.mkdir(path.dirname(destinationPath))
fs.symlinkSync(fs.readlinkSync(sourcePath), destinationPath)
else if stats.isFile()
grunt.file.copy(sourcePath, destinationPath)
if grunt.file.exists(destinationPath)
fs.chmodSync(destinationPath, fs.statSync(sourcePath).mode)
if grunt.file.isFile(source)
copyFile(source, destination)
else
try
onFile = (sourcePath) ->
destinationPath = path.join(destination, path.relative(source, sourcePath))
copyFile(sourcePath, destinationPath)
onDirectory = (sourcePath) ->
if fs.isSymbolicLinkSync(sourcePath)
destinationPath = path.join(destination, path.relative(source, sourcePath))
copyFile(sourcePath, destinationPath)
false
else
true
fs.traverseTreeSync source, onFile, onDirectory
catch error
grunt.fatal(error)
2013-06-27 20:07:33 +04:00
grunt.verbose.writeln("Copied #{source.cyan} to #{destination.cyan}.")
2013-06-27 20:07:33 +04:00
mkdir: (args...) ->
grunt.file.mkdir(args...)
rm: (args...) ->
grunt.file.delete(args..., force: true) if grunt.file.exists(args...)
spawn: (options, callback) ->
2013-11-14 22:54:25 +04:00
childProcess = require 'child_process'
stdout = []
stderr = []
error = null
proc = childProcess.spawn(options.cmd, options.args, options.opts)
proc.stdout.on 'data', (data) -> stdout.push(data.toString())
proc.stderr.on 'data', (data) -> stderr.push(data.toString())
proc.on 'error', (processError) -> error ?= processError
proc.on 'close', (exitCode, signal) ->
2015-04-07 07:05:19 +03:00
error ?= new Error(signal) if exitCode isnt 0
2013-11-14 22:54:25 +04:00
results = {stderr: stderr.join(''), stdout: stdout.join(''), code: exitCode}
2015-04-07 07:05:19 +03:00
grunt.log.error results.stderr if exitCode isnt 0
2013-11-14 22:54:25 +04:00
callback(error, results, exitCode)
2013-10-11 20:43:37 +04:00
isAtomPackage: (packagePath) ->
try
{engines} = grunt.file.readJSON(path.join(packagePath, 'package.json'))
engines?.atom?
catch error
false