-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
166 lines (151 loc) · 4.43 KB
/
Copy pathfile.js
File metadata and controls
166 lines (151 loc) · 4.43 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const fs = require('fs');
const path = require('path');
// from stackoverflow
exports.copyRecursiveSync = function (src, dest) {
var exists = fs.existsSync(src);
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (isDirectory)
{
fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src).forEach(function(childItemName)
{
exports.copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
});
}
else
{
fs.copyFileSync(src, dest);
}
};
exports.cp_r = function(/*String*/ from, /*String*/ to)
{
if (fs.existsSync(to))
exports.rm_rf(to);
if (fs.lstatSync(from).isDirectory())
exports.copyRecursiveSync(from, to);
else
{
try
{
fs.copyFileSync(from, to);
}
catch (e)
{
console.log(e + fs.existsSync(from) + " " + fs.existsSync(path.dirname(to)));
}
}
};
exports.cp = function(/*String*/ from, /*String*/ to)
{
fs.copyFileSync(from, to)
// FILE.chmod(to, FILE.mod(from));
};
exports.mv = function(/*String*/ from, /*String*/ to)
{
fs.renameSync(from, to);
};
exports.rm_rf = function(/*String*/ aFilename)
{
try { fs.rmSync(aFilename, {recursive: true, force: true}); }
catch (anException)
{
console.log(anException);
}
};
exports.enquote = function(word)
{
return "'" + String(word).replace(/'/g, "'\"'\"'") + "'";
}
/**
* parses command line arguments
* @param command {String} a command composed of space delimited,
* quoted, or backslash escaped arguments.
* @returns an Array of unquoted arguments.
*
* /!\ WARNING: this does not handle all of the edge cases
* of command line argument parsing, nor is suitable for
* general purpose argument enquoting on all platforms. It
* also will never be able to handle environment variable
* interpolation or other forms of shell quote expansion.
* This utility is used by Cappuccino to parse arguments from
* system.env.OBJJ_OPT.
*/
var STATE_NORMAL = 0; // waiting for non whitespace/quote
var STATE_ARG = 1; // nextArg is an argument, even if empty
var STATE_IN_QUOTE = 2; // within a ' or " quote
exports.parse = function (argString) {
var args = [];
var nextArg = "";
var state = STATE_NORMAL;
var escapeNext = false;
var delimiter;
var tokens = argString.split("");
while (tokens.length > 0) {
var token = tokens.shift();
if (state === STATE_NORMAL || state === STATE_ARG) {
if (!escapeNext && token === "\\") {
escapeNext = true;
}
else if (escapeNext) {
state = STATE_ARG;
escapeNext = false;
nextArg += token;
}
else if (token === "'" || token === '"') {
delimiter = token;
state = STATE_IN_QUOTE;
}
else if (token === " ") {
if (state === STATE_ARG) {
args.push(nextArg);
nextArg = "";
}
state = STATE_NORMAL;
}
else {
nextArg += token;
state = STATE_ARG;
}
}
else if (state === STATE_IN_QUOTE) {
if (!escapeNext && token === "\\") {
escapeNext = true;
}
else if (delimiter === token) {
if (escapeNext) {
nextArg += token;
escapeNext = false;
} else {
state = STATE_ARG;
}
}
else {
if (escapeNext) {
// if not a quote (above) or other special character that needs to be escaped then include the backslash
if (token !== "\\")
nextArg += "\\";
nextArg += token;
escapeNext = false;
} else {
nextArg += token;
}
}
}
else {
throw "wtf " + state;
}
}
if (state === STATE_IN_QUOTE) {
if (token === delimiter) {
args.push(nextArg.slice(0,-1) + "\\");
}
else {
// throw "Invalid or not yet implemented case"
}
}
else if (state === STATE_ARG) {
args.push(nextArg);
}
return args;
};