-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (83 loc) · 2.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var gutil = require('gulp-util');
var cp = require('child_process');
var syncExec = require('sync-exec');
var moduleName = 'gulp-clear-readonly';
var debug = gutil.log;
/**
* 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 {Object} opts - Plugin options { isFile, debug }
* @return {Object} callback result if any
*/
function clearReadOnly(path, callback, providedOptions) {
var options = getOptions(providedOptions);
debug = options.debug ? gutil.log : function () {};
debug(moduleName, 'Clearing read-only flag for path: ' + path + '...');
var callbackResult;
var clearReadOnlyCommand = getCommand(path, options.isFile);
try {
// Use the native execSync if it is available on current node
// platform, else use an external library
if (cp.execSync) {
cp.execSync(clearReadOnlyCommand);
} else {
syncExec(clearReadOnlyCommand);
}
// Mark this asynchronous task as completed
debug(moduleName, 'Cleared read-only flag for path: ' + path);
} catch (error) {
// Handle failure scenario
debug(moduleName, error);
throw new gutil.PluginError(moduleName, 'Failed to clear read only flag.');
}
if (typeof(callback) == 'function') {
try {
callbackResult = callback();
} catch (error) {
debug(moduleName, error);
throw new gutil.PluginError(moduleName, 'An error occurred while trying to execute callback.');
}
}
return callbackResult;
}
/**
* Get clear read-only flag 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
* @return {string} Clear read-only flag command
*/
function getCommand(path, isFile) {
var command;
if (/^win/.test(process.platform)) {
debug(moduleName, 'Detected platform: Windows');
// Is Windows
if (isFile) {
command = 'attrib -r "' + path + '\\*.*" /s';
} else {
command = 'attrib -r "' + path + '"';
}
} else {
debug(moduleName, 'Detected platform: Non-Windows');
// Is Linux or Unix
command = '[ -e "' + path + '" ] && chmod 777 "' + path + '" -f -R';
}
debug(moduleName, 'Command: ' + command);
return command;
}
/**
* Merge provided options with default options
* @name getCommand
* @param {object} options - Plugin options
* @return {object} Merged options
*/
function getOptions(options) {
options = options || {};
options.isFile = 'isFile' in options ? options.isFile : false;
options.debug = 'debug' in options ? options.debug : false;
return options;
}
// Export this module
module.exports = clearReadOnly;