Skip to content

Commit 582a788

Browse files
author
Maximilian Wehrstedt
committed
fixed not working cmd command for uploading and executing local scripts
1 parent 106f949 commit 582a788

File tree

1 file changed

+140
-28
lines changed

1 file changed

+140
-28
lines changed

src/cmd.ts

Lines changed: 140 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as os from 'os';
22
import * as config from './config';
33
import { SDSConnection } from 'node-sds';
44
import * as sdsAccess from './sdsAccess';
5-
5+
import * as fs from 'fs';
6+
import * as path from 'path';
67

78
export let cmdvar = 0;
89

@@ -13,27 +14,43 @@ var program = require('commander');
1314
// set up sdsAccess
1415

1516

16-
async function uploadAndRunAll(loginData: config.LoginData, folder: string, prefix: string): Promise<sdsAccess.scriptT[]> {
17+
async function uploadAndRunAll(loginData: config.LoginData, files: sdsAccess.scriptT[]): Promise<sdsAccess.scriptT[]> {
1718
return new Promise<sdsAccess.scriptT[]>((resolve, reject) => {
18-
let scripts: sdsAccess.scriptT[] = [];
19-
sdsAccess.getScriptsFromFolder(folder).then((_upscripts) => {
20-
return sdsAccess.sdsSession(loginData, _upscripts, sdsAccess.uploadAll).then(() => {
21-
return sdsAccess.getScriptsFromFolder(folder, prefix).then((_runscripts) => {
22-
return sdsAccess.sdsSession(loginData, _runscripts, sdsAccess.runAll).then((retval) => {
23-
for(let i=0; i<retval.length; i++) {
24-
scripts.push(retval[i]);
25-
console.log("script " + i + ":" + os.EOL + retval[i].output);
26-
}
27-
resolve(scripts);
28-
});
29-
});
19+
return sdsAccess.sdsSession(loginData, files, sdsAccess.uploadAll).then(() => {
20+
return sdsAccess.sdsSession(loginData, files, sdsAccess.runAll).then((retval) => {
21+
let scripts: sdsAccess.scriptT[] = [];
22+
23+
for (let i = 0; i < retval.length; i++) {
24+
scripts.push(retval[i]);
25+
console.log("script " + i + ":" + os.EOL + retval[i].output);
26+
}
27+
28+
resolve(scripts);
29+
}).catch((reason) => {
30+
reject(reason);
3031
});
3132
}).catch((reason) => {
32-
reject();
33+
reject(reason);
3334
});
3435
});
3536
}
3637

38+
function readDirSync(dir: string, rec: boolean = true): string[] {
39+
var results: string[] = [];
40+
var list = fs.readdirSync(dir);
41+
42+
list.forEach(function (elem) {
43+
elem = path.join(dir, elem);
44+
45+
if (fs.statSync(elem).isDirectory() && rec) {
46+
results = results.concat(readDirSync(elem, rec));
47+
} else {
48+
results.push(elem);
49+
}
50+
});
51+
52+
return results;
53+
}
3754

3855
// program
3956
// .version('0.0.1')
@@ -44,22 +61,117 @@ async function uploadAndRunAll(loginData: config.LoginData, folder: string, pref
4461
program
4562
.version('0.0.1')
4663
.command('test <json> [dir...]')
47-
.action(function (json: string, dir: string, filter: string) {
48-
console.log('test json %s', json);
49-
if (dir) {
50-
console.log('test ' + dir[0]);
51-
let loginData: config.LoginData = new config.LoginData();
52-
// dir[1] == name-prefix
53-
let params = [dir[0], dir[1]];
54-
uploadAndRunAll(loginData, dir[0], dir[1]);
55-
// dir.forEach(function (dir_i) {
56-
// console.log('test ' + dir_i);
57-
// });
64+
.action(function (json: string, dir: string[]) {
65+
console.log("Note:\nIf you you use wildcards in the path to the dir to upload, set the path in quoatmarks.\n");
66+
console.log('Params: ' + JSON.stringify({
67+
configFile: json,
68+
path: dir
69+
}, null, "\t"));
70+
71+
// the commander module resolves wildcards (more or less)
72+
// so we have to sourround paths with wildcards with quoatmarks to suppress this behaviour
73+
let dirsToUpload: string[] = [];
74+
75+
if (dir.length < 1) {
76+
throw new Error("Dir/file to run/upload missing");
77+
} else if (dir.length > 1) {
78+
throw new Error("Something went wrong. If you you use wildcards in the path to the dir to upload, set the path in quoatmarks.");
5879
} else {
59-
console.log('test dir missing');
80+
// we have only one element in the dir-array:
81+
// [0] => path (with or without) wildcards to upload (can be a directory or a file)
82+
let uploadPath = dir[0].split("\\").join("/");
83+
84+
// resolve wildcards in the upload path
85+
let indexOf = uploadPath.indexOf("/**/");
86+
let recursive = indexOf > -1;
87+
88+
if (recursive) {
89+
uploadPath = uploadPath.replace("/**", "");
90+
}
91+
92+
// if the path contains a wildcard, we have to make sure that wildcards can only be used at the end of a path
93+
indexOf = uploadPath.lastIndexOf("/");
94+
let wildcardIndex = uploadPath.indexOf("*");
95+
let wildcard = "";
96+
let wildcardReg: RegExp;
97+
98+
if (wildcardIndex > -1 && wildcardIndex < indexOf) {
99+
throw new Error("Wildcards can only be used at the end of the path: " + wildcard);
100+
} else {
101+
wildcard = uploadPath.substr(indexOf + 1).split(".").join("\\.").split("*").join(".*");
102+
uploadPath = uploadPath.substr(0, indexOf);
103+
104+
try {
105+
wildcardReg = new RegExp("^" + wildcard + "$");
106+
} catch (err) {
107+
throw new Error("Invalid wildcard expression: " + wildcard);
108+
}
109+
}
110+
111+
let files = readDirSync(uploadPath, true);
112+
let filesToUpload: sdsAccess.scriptT[] = [];
113+
114+
files.forEach(function (file) {
115+
let parsedFile = path.parse(file);
116+
117+
if (wildcardReg.test(parsedFile.base)) {
118+
filesToUpload.push({
119+
name: parsedFile.name,
120+
path: file,
121+
sourceCode: fs.readFileSync(file).toString()
122+
});
123+
}
124+
});
125+
126+
if (filesToUpload.length < 0) {
127+
console.log("No files to upload found.");
128+
} else {
129+
let loginData: config.LoginData = new config.LoginData();
130+
loginData.loadConfigFile(json);
131+
uploadAndRunAll(loginData, filesToUpload);
132+
}
60133
}
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
// if (dir.length > 0) {
149+
// let loginData: config.LoginData = new config.LoginData();
150+
// loginData.loadConfigFile(json)
151+
// let dirToUpload = dir[0];
152+
// let execPrefix = '';
153+
154+
// // dirToUpload has to be a directory
155+
// if (fs.statSync(dirToUpload).isFile()) {
156+
// if (!path.isAbsolute(dirToUpload)) {
157+
// dirToUpload = path.join(process.cwd(), dirToUpload);
158+
// }
159+
160+
// let parsedFile = path.parse(dirToUpload);
161+
// dirToUpload = parsedFile.dir;
162+
// execPrefix = parsedFile.base;
163+
// } else if (dir.length > 1) {
164+
// execPrefix = dir[1];
165+
// }
166+
167+
// // upload and execute
168+
// console.log(`upload and execute scripts from dir '${dirToUpload}' with file prefix '${execPrefix}'`);
169+
// uploadAndRunAll(loginData, dirToUpload, execPrefix);
170+
// } else {
171+
// console.log('Dir/file to run/upload missing');
172+
// }
61173
});
62-
174+
63175
program.parse(process.argv);
64176

65177
// if (process.argv.length > 2) {

0 commit comments

Comments
 (0)