|
| 1 | +/** |
| 2 | + * Helper functions |
| 3 | + * This allows us to give the console some colour when running in a terminal |
| 4 | + * |
| 5 | + */ |
| 6 | +import fs from 'fs'; |
| 7 | +import path from 'path'; |
| 8 | +import readline from 'readline'; |
| 9 | +import { fileURLToPath } from 'url'; |
| 10 | +import { execSync } from 'child_process'; |
| 11 | + |
| 12 | +export const getRootDir = (trajectory = '..') => { |
| 13 | + const filename = fileURLToPath(import.meta.url); |
| 14 | + const dirname = path.dirname(filename); |
| 15 | + return path.resolve(dirname, trajectory); |
| 16 | +}; |
| 17 | + |
| 18 | +export const askQuestion = (query) => { |
| 19 | + const rl = readline.createInterface({ |
| 20 | + input: process.stdin, |
| 21 | + output: process.stdout, |
| 22 | + }); |
| 23 | + |
| 24 | + return new Promise(resolve => rl.question(`\x1b[36m${query}\n> \x1b[0m`, (ans) => { |
| 25 | + rl.close(); |
| 26 | + resolve(ans); |
| 27 | + })); |
| 28 | +}; |
| 29 | + |
| 30 | +export function isDockerRunning() { |
| 31 | + try { |
| 32 | + execSync('docker info'); |
| 33 | + return true; |
| 34 | + } catch (e) { |
| 35 | + return false; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export function deleteNodeModules(dir) { |
| 40 | + const nodeModulesPath = path.join(dir, 'node_modules'); |
| 41 | + if (fs.existsSync(nodeModulesPath)) { |
| 42 | + console.purple(`Deleting node_modules in ${dir}`); |
| 43 | + fs.rmSync(nodeModulesPath, { recursive: true }); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export const silentExit = (code = 0) => { |
| 48 | + console.log = () => {}; |
| 49 | + process.exit(code); |
| 50 | +}; |
| 51 | + |
| 52 | +// Set the console colours |
| 53 | +console.orange = msg => console.log('\x1b[33m%s\x1b[0m', msg); |
| 54 | +console.green = msg => console.log('\x1b[32m%s\x1b[0m', msg); |
| 55 | +console.red = msg => console.log('\x1b[31m%s\x1b[0m', msg); |
| 56 | +console.blue = msg => console.log('\x1b[34m%s\x1b[0m', msg); |
| 57 | +console.purple = msg => console.log('\x1b[35m%s\x1b[0m', msg); |
| 58 | +console.cyan = msg => console.log('\x1b[36m%s\x1b[0m', msg); |
| 59 | +console.yellow = msg => console.log('\x1b[33m%s\x1b[0m', msg); |
| 60 | +console.white = msg => console.log('\x1b[37m%s\x1b[0m', msg); |
| 61 | +console.gray = msg => console.log('\x1b[90m%s\x1b[0m', msg); |
0 commit comments