1 | #!/usr/bin/env node
|
2 | const fs = require('fs')
|
3 | const inspector = require('inspector')
|
4 | const path = require('path')
|
5 | const colors = require('./colors')
|
6 | const execSync = require('child_process').execSync
|
7 |
|
8 | const IS_DEBUGGING = process.argv[1] === 'inspect'
|
9 | const ARG_POSITION = IS_DEBUGGING ? 1 : 0
|
10 |
|
11 | if (IS_DEBUGGING) {
|
12 | inspector.open(9229, 'localhost')
|
13 | }
|
14 |
|
15 | let ARGV = Array.from(process.argv)
|
16 | ARGV.shift()
|
17 |
|
18 | const PROFILER_JS_PATH = process.argv[ARG_POSITION + 1]
|
19 | const DIR_NAME = path.dirname(PROFILER_JS_PATH)
|
20 | const HOME = process.argv[ARG_POSITION + 5]
|
21 |
|
22 | if (HOME.indexOf('/root/') >= 0) {
|
23 | HOME = require('os').homedir()
|
24 | }
|
25 |
|
26 | let useCustomSettings = 1
|
27 | let SETTINGS = {}
|
28 |
|
29 | try {
|
30 |
|
31 | SETTINGS = require(HOME + '/.bash_profile.js')({})
|
32 | useCustomSettings = 1
|
33 | } catch (e) {
|
34 |
|
35 |
|
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 |
|
49 | let aliases = fs.readFileSync(DIR_NAME + '/aliases.sh', 'utf8').toString()
|
50 | if (SETTINGS.aliases) {
|
51 | for (let alias in SETTINGS.aliases) {
|
52 | aliases += '\nalias ' + alias + '=' + JSON.stringify(SETTINGS.aliases[alias])
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 | let git = fs.readFileSync(DIR_NAME + '/get-git.sh', 'utf8').toString()
|
58 |
|
59 | let battery = fs.readFileSync(DIR_NAME + '/battery.sh', 'utf8').toString()
|
60 |
|
61 | let ps1 = `
|
62 | echo 0 > ~/.uis
|
63 |
|
64 | # the battery_charge function updates some variables with information about the battery
|
65 | battery_charge
|
66 | function 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
|
86 | function 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
|
93 | node ${DIR_NAME}/get-ps1-parts.js ${ARGV.join(' ')} \$(now) root ${useCustomSettings} > ${DIR_NAME}/sudoed-ps1.txt 2>/dev/null
|
94 |
|
95 | # exporting the function
|
96 | export -f buildPS1
|
97 |
|
98 | # as PROMPT_COMMAND is called before PS1 is rendered, we can update it there
|
99 | PROMPT_COMMAND="buildPS1"
|
100 |
|
101 | # also, we will deal with PS2, showing some line numbers while you type
|
102 | # commands in multiple lines
|
103 | PS1="\${PS2c##*[$((PS2c=1))-9]}\$PS1"
|
104 | PS2="${colors.bgBlack.gray(" \\$((PS2c=PS2c+1)) ")}"
|
105 | `
|
106 |
|
107 | let nodeBin = ''
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | const exportedContent = '' +
|
115 | `#!/bin/bash\n` +
|
116 | `${aliases}\n` +
|
117 | `${git}\n` +
|
118 | `${battery}\n` +
|
119 | `${ps1}\n` +
|
120 | `${nodeBin}\n\n`
|
121 |
|
122 |
|
123 | const exportedPath = DIR_NAME + '/exported.sh'
|
124 |
|
125 | try {
|
126 | fs.writeFileSync(
|
127 | exportedPath,
|
128 | exportedContent
|
129 | )
|
130 |
|
131 |
|
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 |
|
144 | }
|