1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 |
|
5 | import path = require('path');
|
6 | import fs = require('fs');
|
7 |
|
8 |
|
9 | const cwd = process.cwd();
|
10 | const down = [];
|
11 | let exec, found = false;
|
12 | const execNameIndex = process.argv.indexOf('--exec-name');
|
13 |
|
14 | if(execNameIndex < 0){
|
15 | exec = 'suman/dist/cli.js';
|
16 | }
|
17 | else{
|
18 | exec = '.bin/' + process.argv[execNameIndex + 1];
|
19 | }
|
20 |
|
21 | try{
|
22 | fs.mkdirSync(path.resolve(process.env.HOME + '/.suman'));
|
23 | }
|
24 | catch(err){
|
25 |
|
26 | }
|
27 |
|
28 | const debugLogPath = path.resolve(process.env.HOME + '/.suman/suman-debug.log');
|
29 | let p, cd;
|
30 |
|
31 | function stat (p: string) {
|
32 | try {
|
33 | return fs.statSync(p).isFile();
|
34 | }
|
35 | catch (err) {
|
36 | if (!String(err.stack || err).match(/ENOENT: no such file or directory/i)) {
|
37 | throw err;
|
38 | }
|
39 |
|
40 | return false;
|
41 | }
|
42 | }
|
43 |
|
44 | while (true) {
|
45 |
|
46 | cd = path.resolve(cwd + down.join(''));
|
47 |
|
48 | if (String(cd) === String(path.sep)) {
|
49 |
|
50 | break;
|
51 | }
|
52 |
|
53 | p = path.resolve(cd + '/node_modules/' + exec);
|
54 |
|
55 | if (stat(p)) {
|
56 |
|
57 | found = true;
|
58 | break;
|
59 | }
|
60 |
|
61 | down.push('/../');
|
62 |
|
63 | }
|
64 |
|
65 | if (found) {
|
66 | console.log(p);
|
67 | process.exit(0);
|
68 | }
|
69 | else {
|
70 | process.exit(1);
|
71 | }
|