|
2 | 2 |
|
3 | 3 |
|
4 | 4 | /* tslint:disable */ |
5 | | -// check if we're running in dev mode |
6 | | -var devMode = require('fs').existsSync(`${__dirname}/../src`); |
7 | | -// or want to "force" running the compiled version with --compiled-build |
8 | | -var wantsCompiled = process.argv.indexOf('--compiled-build') >= 0; |
9 | | - |
10 | | -if (wantsCompiled || !devMode) { |
11 | | - // this runs from the compiled javascript source |
12 | | - require(`${__dirname}/../build/cli`).run(process.argv); |
13 | | -} else { |
14 | | - // this runs from the typescript source (for dev only) |
15 | | - // hook into ts-node so we can run typescript on the fly |
16 | | - require('ts-node').register({ project: `${__dirname}/../tsconfig.json` }); |
17 | | - // run the CLI with the current process arguments |
18 | | - require(`${__dirname}/../src/cli`).run(process.argv); |
| 5 | + |
| 6 | +// Handle case where current working directory no longer exists |
| 7 | +try { |
| 8 | + process.cwd(); |
| 9 | +} catch (err) { |
| 10 | + // Current directory doesn't exist - offer interactive solutions |
| 11 | + var fs = require('fs'); |
| 12 | + var path = require('path'); |
| 13 | + var os = require('os'); |
| 14 | + var readline = require('readline'); |
| 15 | + |
| 16 | + console.error('\nError: Current working directory no longer exists.'); |
| 17 | + console.error('The directory you were in has been deleted or is no longer accessible.\n'); |
| 18 | + |
| 19 | + // Try to find the next existing parent directory |
| 20 | + var originalPath = process.env.PWD || process.env.OLDPWD || ''; |
| 21 | + var parentDir = null; |
| 22 | + |
| 23 | + if (originalPath) { |
| 24 | + var testPath = path.dirname(originalPath); |
| 25 | + while (testPath && testPath !== '/' && testPath !== '.') { |
| 26 | + try { |
| 27 | + if (fs.existsSync(testPath) && fs.statSync(testPath).isDirectory()) { |
| 28 | + parentDir = testPath; |
| 29 | + break; |
| 30 | + } |
| 31 | + } catch (e) { |
| 32 | + // Continue searching |
| 33 | + } |
| 34 | + testPath = path.dirname(testPath); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Build options |
| 39 | + var options = []; |
| 40 | + if (parentDir) { |
| 41 | + options.push({ key: '1', label: 'Change to nearest existing parent directory: ' + parentDir, action: parentDir }); |
| 42 | + } |
| 43 | + options.push({ key: parentDir ? '2' : '1', label: 'Change to home directory: ' + os.homedir(), action: os.homedir() }); |
| 44 | + options.push({ key: parentDir ? '3' : '2', label: 'Enter a custom directory path', action: 'custom' }); |
| 45 | + options.push({ key: parentDir ? '4' : '3', label: 'Cancel and exit', action: 'exit' }); |
| 46 | + |
| 47 | + // Display options |
| 48 | + console.log('Please choose an option:\n'); |
| 49 | + options.forEach(function(opt) { |
| 50 | + console.log(' ' + opt.key + ') ' + opt.label); |
| 51 | + }); |
| 52 | + console.log(''); |
| 53 | + |
| 54 | + // Read user input |
| 55 | + var rl = readline.createInterface({ |
| 56 | + input: process.stdin, |
| 57 | + output: process.stdout |
| 58 | + }); |
| 59 | + |
| 60 | + rl.question('Enter your choice: ', function(answer) { |
| 61 | + var choice = options.find(function(opt) { return opt.key === answer.trim(); }); |
| 62 | + |
| 63 | + if (!choice) { |
| 64 | + console.error('\nInvalid choice. Exiting.\n'); |
| 65 | + rl.close(); |
| 66 | + process.exit(1); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (choice.action === 'exit') { |
| 71 | + console.log('\nCancelled.\n'); |
| 72 | + rl.close(); |
| 73 | + process.exit(0); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (choice.action === 'custom') { |
| 78 | + rl.question('Enter directory path (absolute or relative to ' + (parentDir || os.homedir()) + '): ', function(customPath) { |
| 79 | + var targetPath = customPath.trim(); |
| 80 | + |
| 81 | + // If relative path, resolve it relative to parent or home |
| 82 | + if (!path.isAbsolute(targetPath)) { |
| 83 | + targetPath = path.resolve(parentDir || os.homedir(), targetPath); |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + if (!fs.existsSync(targetPath)) { |
| 88 | + console.error('\nError: Directory does not exist: ' + targetPath + '\n'); |
| 89 | + rl.close(); |
| 90 | + process.exit(1); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + process.chdir(targetPath); |
| 95 | + console.log('\nChanged to: ' + targetPath + '\n'); |
| 96 | + rl.close(); |
| 97 | + continueExecution(); |
| 98 | + } catch (e) { |
| 99 | + console.error('\nError: Cannot change to directory: ' + targetPath); |
| 100 | + console.error(e.message + '\n'); |
| 101 | + rl.close(); |
| 102 | + process.exit(1); |
| 103 | + } |
| 104 | + }); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // Change to selected directory |
| 109 | + try { |
| 110 | + process.chdir(choice.action); |
| 111 | + console.log('\nChanged to: ' + choice.action + '\n'); |
| 112 | + rl.close(); |
| 113 | + continueExecution(); |
| 114 | + } catch (e) { |
| 115 | + console.error('\nError: Cannot change to directory: ' + choice.action); |
| 116 | + console.error(e.message + '\n'); |
| 117 | + rl.close(); |
| 118 | + process.exit(1); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + // Function to continue CLI execution after directory change |
| 123 | + function continueExecution() { |
| 124 | + runCLI(); |
| 125 | + } |
| 126 | + |
| 127 | + // Don't continue execution here - wait for user choice |
| 128 | + return; |
| 129 | +} |
| 130 | + |
| 131 | +// Normal execution path |
| 132 | +runCLI(); |
| 133 | + |
| 134 | +function runCLI() { |
| 135 | + // check if we're running in dev mode |
| 136 | + var devMode = require('fs').existsSync(`${__dirname}/../src`); |
| 137 | + // or want to "force" running the compiled version with --compiled-build |
| 138 | + var wantsCompiled = process.argv.indexOf('--compiled-build') >= 0; |
| 139 | + |
| 140 | + if (wantsCompiled || !devMode) { |
| 141 | + // this runs from the compiled javascript source |
| 142 | + require(`${__dirname}/../build/cli`).run(process.argv); |
| 143 | + } else { |
| 144 | + // this runs from the typescript source (for dev only) |
| 145 | + // hook into ts-node so we can run typescript on the fly |
| 146 | + require('ts-node').register({ project: `${__dirname}/../tsconfig.json` }); |
| 147 | + // run the CLI with the current process arguments |
| 148 | + require(`${__dirname}/../src/cli`).run(process.argv); |
| 149 | + } |
19 | 150 | } |
20 | 151 |
|
21 | 152 |
|
0 commit comments