UNPKG

1.19 kBPlain TextView Raw
1#!/usr/bin/env node
2'use strict';
3
4//core
5import path = require('path');
6import fs = require('fs');
7
8//project
9const cwd = process.cwd();
10const down = [];
11let exec, found = false;
12const execNameIndex = process.argv.indexOf('--exec-name');
13
14if(execNameIndex < 0){
15 exec = 'suman/dist/cli.js';
16}
17else{
18 exec = '.bin/' + process.argv[execNameIndex + 1];
19}
20
21try{
22 fs.mkdirSync(path.resolve(process.env.HOME + '/.suman'));
23}
24catch(err){
25
26}
27
28const debugLogPath = path.resolve(process.env.HOME + '/.suman/suman-debug.log');
29let p, cd;
30
31function 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 //explicit for your pleasure
40 return false;
41 }
42}
43
44while (true) {
45
46 cd = path.resolve(cwd + down.join(''));
47
48 if (String(cd) === String(path.sep)) {
49 // We are down to the root => fail
50 break;
51 }
52
53 p = path.resolve(cd + '/node_modules/' + exec);
54
55 if (stat(p)) {
56 // Found Suman installation path
57 found = true;
58 break;
59 }
60
61 down.push('/../');
62
63}
64
65if (found) {
66 console.log(p);
67 process.exit(0);
68}
69else {
70 process.exit(1);
71}