This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (95 loc) · 2.58 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const shell = require('shelljs');
const os = require('os');
const exe = {
err: null,
output: null
};
const getShellOutput = (cmd, replace) => {
let output = null;
let error = null;
try {
output = shell.exec(cmd, {
silent: true
}).stdout.trim();
} catch (error) {
err = error;
output = null;
}
if (null !== error) {
return {
error,
output
};
}
return {
output: replace ? output.replace(/[\n\r]/g, '') : output,
error: null
};
}
const getFtypeArguments = (line) => {
const ftypeRegEx = /(["'])(?:(?=(\\?))\2.)*?\1/g;
const regExMatch = line.match(ftypeRegEx);
if (regExMatch.length === 2) {
// Found cmd + input arg
return {
cmd : regExMatch[0].replace(/\"/g,''),
arg : regExMatch[1].replace(/\"/g,'').replace('%1','{path}')
};
} else {
return null;
}
}
const findModeler = () => {
let findGeneric = true;
const assoc = getShellOutput('assoc .mpr', true);
// Find association
if (assoc.error) {
exe.err = assoc.error;
} else if (assoc.output.indexOf('not found') !== -1) {
exe.err = 'No file association found for .mpr, are you sure you installed Mendix?';
} else if (assoc.output.indexOf('.mpr=') === 0) {
// Found association, getting the Version Selector
const association = assoc.output.split('=')[1];
const ftypeShell = getShellOutput('ftype', false);
if (ftypeShell.error !== null) {
exe.err = assoc.error;
} else {
const ftype = ftypeShell.output.split('\n').filter(function (line) {
return line.indexOf(association) !== -1;
});
if (ftype.length === 1) {
// ftype found, getting arguments
const ftypeArgs = getFtypeArguments(ftype[0]);
if (ftypeArgs !== null) {
exe.output = ftypeArgs;
findGeneric = false;
}
}
}
} else {
exe.err = 'Unknown error, cannot find the association for .MPR (Mendix Project) files. Are you on Windows?';
findGeneric = false;
}
if (findGeneric) {
const ftypeMendixShell = getShellOutput('ftype mendix', true);
if (ftypeMendixShell.error !== null) {
exe.err = ftypeMendixShell.error;
} else if (ftypeMendixShell.output.indexOf('not found') !== -1) {
exe.err = 'No file association found for .mpr, are you sure you installed Mendix?';
} else {
// ftype found, getting arguments
const ftypeArgs = getFtypeArguments(ftypeMendix);
if (ftypeArgs !== null) {
exe.output = ftypeArgs;
} else {
exe.err = 'No file association found for .mpr, are you sure you installed Mendix?';
}
}
}
}
if (os.platform() !== 'win32') {
exe.err = 'Unfortunately this feature only works in Windows...';
} else {
findModeler();
}
module.exports = exe;