2014-02-04 16:29:44 +04:00
|
|
|
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}")
|
|
|
|
|
2014-02-04 16:29:44 +04:00
|
|
|
copyFile = (sourcePath, destinationPath) ->
|
2014-07-15 20:17:50 +04:00
|
|
|
return if filter?(sourcePath) or filter?.test?(sourcePath)
|
2014-02-04 16:29:44 +04:00
|
|
|
|
|
|
|
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)
|
2014-02-06 05:34:14 +04:00
|
|
|
onDirectory = (sourcePath) ->
|
|
|
|
if fs.isSymbolicLinkSync(sourcePath)
|
|
|
|
destinationPath = path.join(destination, path.relative(source, sourcePath))
|
|
|
|
copyFile(sourcePath, destinationPath)
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
2014-02-04 16:29:44 +04:00
|
|
|
fs.traverseTreeSync source, onFile, onDirectory
|
|
|
|
catch error
|
|
|
|
grunt.fatal(error)
|
2013-06-27 20:07:33 +04:00
|
|
|
|
2013-11-14 05:24:49 +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())
|
2014-09-22 21:57:42 +04:00
|
|
|
proc.on 'error', (processError) -> error ?= processError
|
2014-01-10 01:47:51 +04:00
|
|
|
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
|