-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contains core logic
- Loading branch information
Karanvir Kang
authored
Mar 17, 2017
1 parent
7764314
commit 9ec54fe
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict' | ||
|
||
var gutil = require('gulp-util'); | ||
|
||
module.exports = function(){ | ||
var cp = require('child_process'); | ||
return { | ||
clear: clearReadOnly | ||
}; | ||
|
||
/** | ||
* Clear readOnly flag from all files and sub folders. | ||
* @name clearReadOnly | ||
* @param {string} path - Folder from which read-only flag needs to be cleared | ||
* @param {function} callback - Callback function to invoke | ||
* @param {boolean} isFile - Whether the path is a file. Default: False | ||
*/ | ||
function clearReadOnly(path, callback, isFile) { | ||
gutil.log('Clearing read-only flag for path:' + path + '...'); | ||
var clearReadOnlyCommand = getCommand(path, !isFile); | ||
|
||
cp.exec(clearReadOnlyCommand, function(error) { | ||
// We only care about errors for this command | ||
if (error) { | ||
// Handle failure scenario | ||
gutil.log(error); | ||
throw new gutil.PluginError('clear:readonly', | ||
'Failed to clear read only flag.'); | ||
} else { | ||
// Mark this asynchronous task as completed | ||
gutil.log('Cleared read-only flag for path:' + path); | ||
// Invoke callback | ||
if (callback) { | ||
callback(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Get clear command | ||
* @name getCommand | ||
* @param {string} path - Path from which read-only flag needs to be cleared | ||
* @param {boolean} isFile - Whether the specified path is a file. Default: Path is a folder | ||
*/ | ||
function getCommand(path, isFile) { | ||
if (/^win/.test(process.platform)) { | ||
// Is Windows | ||
if (isFile) { | ||
command = 'attrib -r "' + path + '\\*.*" /s'; | ||
} else { | ||
command = 'attrib -r "' + path + '"'; | ||
} | ||
} else { | ||
// Is Linux or Unix | ||
if (isFile) { | ||
// Check if file exists before executing command | ||
command = '[ -f "' + path + '" ] && chmod 777 "' + path + '" -f'; | ||
} else { | ||
// Check if folder exists before executing command | ||
command = '[ -d "' + path + '" ] && chmod 777 "' + path + '" -f -R'; | ||
} | ||
} | ||
} | ||
}; |