1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | var path = require("path");
|
5 | var fs = require("fs");
|
6 | var cwd = process.cwd();
|
7 | var down = [];
|
8 | var found = false;
|
9 | var exec;
|
10 | var execNameIndex = process.argv.indexOf('--exec-name');
|
11 | if (execNameIndex < 0) {
|
12 | exec = 'suman/cli.js';
|
13 | }
|
14 | else {
|
15 | exec = '.bin/' + process.argv[execNameIndex + 1];
|
16 | }
|
17 | try {
|
18 | fs.mkdirSync(path.resolve(process.env.HOME + '/.suman'));
|
19 | }
|
20 | catch (err) {
|
21 | }
|
22 | var debugLogPath = path.resolve(process.env.HOME + '/.suman/suman-debug.log');
|
23 | fs.writeFileSync(debugLogPath, '\n\n', { flag: 'a' });
|
24 | fs.writeFileSync(debugLogPath, ' => Date run => ' + new Date().toISOString(), { flag: 'a' });
|
25 | fs.writeFileSync(debugLogPath, ' => Running find-local-suman-executable.\n', { flag: 'a' });
|
26 | fs.writeFileSync(debugLogPath, ' => cwd => ' + cwd, { flag: 'a' });
|
27 | var p, cd;
|
28 | function stat(p) {
|
29 | try {
|
30 | return fs.statSync(p).isFile();
|
31 | }
|
32 | catch (err) {
|
33 | fs.writeFileSync(debugLogPath, '\n => stat error => ' + (err.stack || err), { flag: 'a' });
|
34 | if (!String(err.stack || err).match(/ENOENT: no such file or directory/i)) {
|
35 | throw err;
|
36 | }
|
37 | return false;
|
38 | }
|
39 | }
|
40 | while (true) {
|
41 | cd = path.resolve(cwd + down.join(''));
|
42 | if (String(cd) === String(path.sep)) {
|
43 | fs.writeFileSync(debugLogPath, '\n\n => Fail, (we went down to root "/") => cd => ' + cd, { flag: 'a' });
|
44 | break;
|
45 | }
|
46 | p = path.resolve(cd + '/node_modules/' + exec);
|
47 | fs.writeFileSync(debugLogPath, '\n Searching for suman executable at this path => ' + p, { flag: 'a' });
|
48 | if (stat(p)) {
|
49 | found = true;
|
50 | break;
|
51 | }
|
52 | down.push('/../');
|
53 | }
|
54 | if (found) {
|
55 | fs.writeFileSync(debugLogPath, '\n Found => ' + p, { flag: 'a' });
|
56 | console.log(p);
|
57 | process.exit(0);
|
58 | }
|
59 | else {
|
60 | fs.writeFileSync(debugLogPath, '\n * ! Not found * => ' + p, { flag: 'a' });
|
61 | process.exit(1);
|
62 | }
|