UNPKG

4.93 kBJavaScriptView Raw
1#!/usr/bin/env node
2const fs = require('fs')
3const inspector = require('inspector')
4const path = require('path')
5const colors = require('./colors')
6const execSync = require('child_process').execSync
7
8const IS_DEBUGGING = process.argv[1] === 'inspect'
9const ARG_POSITION = IS_DEBUGGING ? 1 : 0
10
11if (IS_DEBUGGING) {
12 inspector.open(9229, 'localhost')
13}
14
15let ARGV = Array.from(process.argv)
16ARGV.shift()
17
18const PROFILER_JS_PATH = process.argv[ARG_POSITION + 1]
19const DIR_NAME = path.dirname(PROFILER_JS_PATH)
20const HOME = process.argv[ARG_POSITION + 5]
21
22if (HOME.indexOf('/root/') >= 0) {
23 HOME = require('os').homedir()
24}
25
26let useCustomSettings = 1
27let SETTINGS = {}
28
29try {
30 // let's try and use the user's settings at ~/.bash_profile.js
31 SETTINGS = require(HOME + '/.bash_profile.js')({})
32 useCustomSettings = 1
33} catch (e) {
34 // if the file DOES exist, but there was an error:
35 // if (e.message.toLowerCase().indexOf('enoent') < 0) {
36 if (e.message.toLowerCase().indexOf('enoent') < 0 && e.message.indexOf('Cannot find module') < 0) {
37 console.log(colors.red('[x] ') + 'Failed importing settings.\n' + e.message)
38 }
39 if (e.message.toLowerCase().indexOf('denied') >= 0) {
40 console.log(
41 colors.red('[x] '),
42 'Permission denied to read your customized file.\n' +
43 'Can you please set reading permissions for your ~/.bash_profile.js? \n' +
44 '\n chmod 555 ' + HOME + '/.bash_profile.js' + '\n\n')
45 }
46}
47
48// populating some aliases
49let aliases = fs.readFileSync(DIR_NAME + '/aliases.sh', 'utf8').toString()
50if (SETTINGS.aliases) {
51 for (let alias in SETTINGS.aliases) {
52 aliases += '\nalias ' + alias + '=' + JSON.stringify(SETTINGS.aliases[alias])
53 }
54}
55
56// adding git functions
57let git = fs.readFileSync(DIR_NAME + '/get-git.sh', 'utf8').toString()
58// battery manager
59let battery = fs.readFileSync(DIR_NAME + '/battery.sh', 'utf8').toString()
60
61let ps1 = `
62echo 0 > ~/.uis
63
64# the battery_charge function updates some variables with information about the battery
65battery_charge
66function buildPS1ForReal () {
67 # we will re-run battery_charge function every 10 seconds
68 if ((SECONDS % 10 == "0")); then
69 battery_charge
70 fi
71
72 # writtable checks if user has write access to the current directory
73 WRITTABLE=0
74 if [ -w \`pwd\` ]; then
75 WRITTABLE=1
76 fi
77
78 # every time PS1 is rendered, we trigger this node call
79 # ensuring it will bring its results up to date
80 node ${DIR_NAME}/get-ps1-parts.js ${ARGV.join(' ')} $(getGit) \$WRITTABLE \$BATT_CONNECTED \$BATT_PCT \$(now) $1 ${useCustomSettings} 2>/dev/null
81}
82
83# This function is called only ONCE, as soon as the profile is applied
84# it will write the PS1 in a way it will trigger buildPS1ForReal on new entries
85# and will also write a default output for sudo
86function buildPS1 () {
87 PS2c=1
88 PS1="\\$(if [ -n \\"\\$(type -t buildPS1ForReal)\\" ]; then echo \\"$(buildPS1ForReal $(whoami))\\"; else echo \\"$(cat ${DIR_NAME}/sudoed-ps1.txt 2>/dev/null)\\" ; fi)"
89}
90
91# here is where we use the current settings to generate the output for sudo
92# we do this only once, too
93node ${DIR_NAME}/get-ps1-parts.js ${ARGV.join(' ')} \$(now) root ${useCustomSettings} > ${DIR_NAME}/sudoed-ps1.txt 2>/dev/null
94
95# exporting the function
96export -f buildPS1
97
98# as PROMPT_COMMAND is called before PS1 is rendered, we can update it there
99PROMPT_COMMAND="buildPS1"
100
101# also, we will deal with PS2, showing some line numbers while you type
102# commands in multiple lines
103PS1="\${PS2c##*[$((PS2c=1))-9]}\$PS1"
104PS2="${colors.bgBlack.gray(" \\$((PS2c=PS2c+1)) ")}"
105`
106
107let nodeBin = ''// `export PATH="$HOME/.node/bin:$PATH"`
108
109// the characters we will try to use are these:
110// ⎇")
111// we should find a way to ensure the user can see them in their terminal
112
113// joining all the resulting content
114const exportedContent = '' +
115 `#!/bin/bash\n` +
116 `${aliases}\n` +
117 `${git}\n` +
118 `${battery}\n` +
119 `${ps1}\n` +
120 `${nodeBin}\n\n`
121
122// and finally printing it all into the real .sh file
123const exportedPath = DIR_NAME + '/exported.sh'
124
125try {
126 fs.writeFileSync(
127 exportedPath,
128 exportedContent
129 )
130
131 // let's set it as executable (trying it without sudo, and if failed, with sudo)
132 execSync(`chmod +x ${exportedPath} || sudo chmod +x ${exportedPath}`)
133} catch (e) {
134 if (e.message.toLowerCase().indexOf('denied') < 0) {
135 console.log(
136 colors.red('[x] '),
137 'Permission denied to write the output sh file.\n' +
138 'Can you please set writing permissions to the following file? \n' +
139 '\n ' + exportedPath + '\n\n')
140 console.log('\n'+
141 'Do you have problem with sudo and global instalations? try running:\n\n sudo chown -R $(whoami) ~/.npm\n\n')
142 }
143 // throw new Error(colors.red('[x] ') + 'Error when applying terminal tools!\n' + colors.bold(error))
144}