UNPKG

2.76 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.executeCommand = executeCommand;
7exports.executeCommandAndGetStdout = executeCommandAndGetStdout;
8exports.isDirectoryExists = isDirectoryExists;
9exports.findMostNearestWord = findMostNearestWord;
10
11var fs = _interopRequireWildcard(require("fs"));
12
13var path = _interopRequireWildcard(require("path"));
14
15var shell = _interopRequireWildcard(require("shelljs"));
16
17function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
18
19function executeCommand(command, workingDirectory) {
20 if (workingDirectory != undefined) {
21 shell.pushd(workingDirectory);
22 }
23
24 try {
25 console.log(command);
26 shell.exec(command);
27 } finally {
28 if (workingDirectory != undefined) {
29 shell.popd();
30 }
31 }
32}
33
34function executeCommandAndGetStdout(command) {
35 console.log(command);
36 const result = shell.exec(command, {
37 silent: true
38 });
39
40 if (typeof result.stdout === "string") {
41 return result.stdout;
42 }
43
44 return "";
45}
46
47function isDirectoryExists(dirPath) {
48 try {
49 return fs.statSync(path.resolve(dirPath)).isDirectory();
50 } catch (e) {
51 return false;
52 }
53}
54
55function levenshtein(left, right) {
56 let a = left;
57 let b = right;
58
59 if (a.length === 0) {
60 return b.length;
61 }
62
63 if (b.length === 0) {
64 return a.length;
65 } // swap to save some memory O(min(a,b)) instead of O(a)
66
67
68 if (a.length > b.length) {
69 const tmp = a;
70 a = b;
71 b = tmp;
72 }
73
74 const row = []; // init the row
75
76 for (let i = 0; i <= a.length; i++) {
77 row[i] = i;
78 } // fill in the rest
79
80
81 for (let i = 1; i <= b.length; i++) {
82 let prev = i;
83
84 for (let j = 1; j <= a.length; j++) {
85 let val;
86
87 if (b.charAt(i - 1) === a.charAt(j - 1)) {
88 val = row[j - 1]; // match
89 } else {
90 val = Math.min(row[j - 1] + 1, // substitution
91 prev + 1, // insertion
92 row[j] + 1); // deletion
93 }
94
95 row[j - 1] = prev;
96 prev = val;
97 }
98
99 row[a.length] = prev;
100 }
101
102 return row[a.length];
103}
104
105function findMostNearestWord(list, word) {
106 let minValue = 4;
107 let resultWord;
108
109 for (const item of list) {
110 const distance = levenshtein(item, word);
111
112 if (distance <= minValue) {
113 minValue = distance;
114 resultWord = item;
115 }
116 }
117
118 return resultWord;
119}
120//# sourceMappingURL=scriptingUtils.js.map
\No newline at end of file