@fredlackey/devutils 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/package.json +1 -1
- package/src/commands/install.js +374 -36
- package/src/installs/adobe-creative-cloud.js +527 -25
- package/src/installs/adobe-creative-cloud.md +605 -0
- package/src/installs/appcleaner.js +303 -26
- package/src/installs/appcleaner.md +699 -0
- package/src/installs/apt-transport-https.js +390 -0
- package/src/installs/apt-transport-https.md +678 -0
- package/src/installs/atomicparsley.js +624 -26
- package/src/installs/atomicparsley.md +795 -0
- package/src/installs/aws-cli.js +779 -26
- package/src/installs/aws-cli.md +727 -0
- package/src/installs/balena-etcher.js +688 -26
- package/src/installs/balena-etcher.md +761 -0
- package/src/installs/bambu-studio.js +912 -26
- package/src/installs/bambu-studio.md +780 -0
- package/src/installs/bash-completion.js +554 -23
- package/src/installs/bash-completion.md +833 -0
- package/src/installs/bash.js +399 -26
- package/src/installs/bash.md +993 -0
- package/src/installs/beyond-compare.js +585 -26
- package/src/installs/beyond-compare.md +813 -0
- package/src/installs/build-essential.js +511 -26
- package/src/installs/build-essential.md +977 -0
- package/src/installs/ca-certificates.js +618 -0
- package/src/installs/ca-certificates.md +937 -0
- package/src/installs/caffeine.js +490 -26
- package/src/installs/caffeine.md +839 -0
- package/src/installs/camtasia.js +577 -25
- package/src/installs/camtasia.md +762 -0
- package/src/installs/chatgpt.js +458 -26
- package/src/installs/chatgpt.md +814 -0
- package/src/installs/chocolatey.js +447 -0
- package/src/installs/chocolatey.md +661 -0
- package/src/installs/chrome-canary.js +472 -26
- package/src/installs/chrome-canary.md +641 -0
- package/src/installs/chromium.js +645 -26
- package/src/installs/chromium.md +838 -0
- package/src/installs/claude-code.js +558 -26
- package/src/installs/claude-code.md +1173 -0
- package/src/installs/curl.js +361 -26
- package/src/installs/curl.md +714 -0
- package/src/installs/cursor.js +561 -26
- package/src/installs/cursor.md +970 -0
- package/src/installs/dbschema.js +674 -26
- package/src/installs/dbschema.md +925 -0
- package/src/installs/dependencies.md +435 -0
- package/src/installs/development-tools.js +600 -0
- package/src/installs/development-tools.md +977 -0
- package/src/installs/docker.js +1010 -25
- package/src/installs/docker.md +1109 -0
- package/src/installs/drawio.js +1001 -26
- package/src/installs/drawio.md +795 -0
- package/src/installs/elmedia-player.js +328 -25
- package/src/installs/elmedia-player.md +556 -0
- package/src/installs/ffmpeg.js +870 -25
- package/src/installs/ffmpeg.md +852 -0
- package/src/installs/file.js +464 -0
- package/src/installs/file.md +987 -0
- package/src/installs/gemini-cli.js +793 -26
- package/src/installs/gemini-cli.md +1153 -0
- package/src/installs/git.js +382 -26
- package/src/installs/git.md +907 -0
- package/src/installs/gitego.js +931 -26
- package/src/installs/gitego.md +1172 -0
- package/src/installs/go.js +913 -26
- package/src/installs/go.md +958 -0
- package/src/installs/google-chrome.js +801 -25
- package/src/installs/google-chrome.md +862 -0
- package/src/installs/gpg.js +412 -73
- package/src/installs/gpg.md +1056 -0
- package/src/installs/homebrew.js +1015 -26
- package/src/installs/homebrew.md +988 -0
- package/src/installs/imageoptim.js +950 -26
- package/src/installs/imageoptim.md +1119 -0
- package/src/installs/installers.json +2297 -0
- package/src/installs/jq.js +382 -26
- package/src/installs/jq.md +809 -0
- package/src/installs/keyboard-maestro.js +701 -26
- package/src/installs/keyboard-maestro.md +825 -0
- package/src/installs/latex.js +771 -26
- package/src/installs/latex.md +1095 -0
- package/src/installs/lftp.js +338 -26
- package/src/installs/lftp.md +907 -0
- package/src/installs/lsb-release.js +346 -0
- package/src/installs/lsb-release.md +814 -0
- package/src/installs/messenger.js +829 -26
- package/src/installs/messenger.md +900 -0
- package/src/installs/microsoft-office.js +550 -26
- package/src/installs/microsoft-office.md +760 -0
- package/src/installs/microsoft-teams.js +782 -25
- package/src/installs/microsoft-teams.md +886 -0
- package/src/installs/node.js +886 -26
- package/src/installs/node.md +1153 -0
- package/src/installs/nordpass.js +698 -26
- package/src/installs/nordpass.md +921 -0
- package/src/installs/nvm.js +977 -26
- package/src/installs/nvm.md +1057 -0
- package/src/installs/openssh.js +734 -64
- package/src/installs/openssh.md +1056 -0
- package/src/installs/pandoc.js +644 -26
- package/src/installs/pandoc.md +1036 -0
- package/src/installs/pinentry.js +492 -26
- package/src/installs/pinentry.md +1142 -0
- package/src/installs/pngyu.js +851 -26
- package/src/installs/pngyu.md +896 -0
- package/src/installs/postman.js +781 -26
- package/src/installs/postman.md +940 -0
- package/src/installs/procps.js +425 -0
- package/src/installs/procps.md +851 -0
- package/src/installs/safari-tech-preview.js +355 -25
- package/src/installs/safari-tech-preview.md +533 -0
- package/src/installs/sfnt2woff.js +640 -26
- package/src/installs/sfnt2woff.md +795 -0
- package/src/installs/shellcheck.js +463 -26
- package/src/installs/shellcheck.md +1005 -0
- package/src/installs/slack.js +722 -25
- package/src/installs/slack.md +865 -0
- package/src/installs/snagit.js +566 -25
- package/src/installs/snagit.md +844 -0
- package/src/installs/software-properties-common.js +372 -0
- package/src/installs/software-properties-common.md +805 -0
- package/src/installs/spotify.js +858 -25
- package/src/installs/spotify.md +901 -0
- package/src/installs/studio-3t.js +803 -26
- package/src/installs/studio-3t.md +918 -0
- package/src/installs/sublime-text.js +780 -25
- package/src/installs/sublime-text.md +914 -0
- package/src/installs/superwhisper.js +687 -25
- package/src/installs/superwhisper.md +630 -0
- package/src/installs/tailscale.js +727 -26
- package/src/installs/tailscale.md +1100 -0
- package/src/installs/tar.js +389 -0
- package/src/installs/tar.md +946 -0
- package/src/installs/termius.js +780 -26
- package/src/installs/termius.md +844 -0
- package/src/installs/terraform.js +761 -26
- package/src/installs/terraform.md +899 -0
- package/src/installs/tidal.js +752 -25
- package/src/installs/tidal.md +864 -0
- package/src/installs/tmux.js +328 -26
- package/src/installs/tmux.md +1030 -0
- package/src/installs/tree.js +393 -26
- package/src/installs/tree.md +833 -0
- package/src/installs/unzip.js +460 -0
- package/src/installs/unzip.md +879 -0
- package/src/installs/vim.js +403 -26
- package/src/installs/vim.md +1040 -0
- package/src/installs/vlc.js +803 -26
- package/src/installs/vlc.md +927 -0
- package/src/installs/vscode.js +825 -26
- package/src/installs/vscode.md +1002 -0
- package/src/installs/wget.js +415 -0
- package/src/installs/wget.md +791 -0
- package/src/installs/whatsapp.js +710 -25
- package/src/installs/whatsapp.md +854 -0
- package/src/installs/winpty.js +352 -0
- package/src/installs/winpty.md +620 -0
- package/src/installs/woff2.js +535 -26
- package/src/installs/woff2.md +977 -0
- package/src/installs/wsl.js +572 -0
- package/src/installs/wsl.md +699 -0
- package/src/installs/xcode-clt.js +520 -0
- package/src/installs/xcode-clt.md +351 -0
- package/src/installs/xcode.js +542 -26
- package/src/installs/xcode.md +573 -0
- package/src/installs/yarn.js +806 -26
- package/src/installs/yarn.md +1074 -0
- package/src/installs/yq.js +636 -26
- package/src/installs/yq.md +944 -0
- package/src/installs/yt-dlp.js +683 -26
- package/src/installs/yt-dlp.md +946 -0
- package/src/installs/yum-utils.js +297 -0
- package/src/installs/yum-utils.md +648 -0
- package/src/installs/zoom.js +740 -25
- package/src/installs/zoom.md +884 -0
- package/src/scripts/README.md +567 -45
- package/src/scripts/STATUS.md +208 -0
- package/src/scripts/afk.js +395 -7
- package/src/scripts/backup-all.js +731 -9
- package/src/scripts/backup-source.js +711 -8
- package/src/scripts/brewd.js +373 -7
- package/src/scripts/brewi.js +505 -9
- package/src/scripts/brewr.js +512 -9
- package/src/scripts/brews.js +462 -9
- package/src/scripts/brewu.js +488 -7
- package/src/scripts/c.js +185 -7
- package/src/scripts/ccurl.js +325 -8
- package/src/scripts/certbot-crontab-init.js +488 -8
- package/src/scripts/certbot-init.js +641 -9
- package/src/scripts/ch.js +339 -7
- package/src/scripts/claude-danger.js +253 -8
- package/src/scripts/clean-dev.js +419 -8
- package/src/scripts/clear-dns-cache.js +525 -7
- package/src/scripts/clone.js +417 -7
- package/src/scripts/code-all.js +420 -7
- package/src/scripts/count-files.js +195 -8
- package/src/scripts/count-folders.js +195 -8
- package/src/scripts/count.js +248 -8
- package/src/scripts/d.js +203 -7
- package/src/scripts/datauri.js +373 -8
- package/src/scripts/delete-files.js +363 -7
- package/src/scripts/docker-clean.js +410 -8
- package/src/scripts/dp.js +426 -7
- package/src/scripts/e.js +375 -9
- package/src/scripts/empty-trash.js +497 -7
- package/src/scripts/evm.js +428 -9
- package/src/scripts/fetch-github-repos.js +441 -10
- package/src/scripts/get-channel.js +329 -8
- package/src/scripts/get-course.js +384 -11
- package/src/scripts/get-dependencies.js +290 -9
- package/src/scripts/get-folder.js +783 -10
- package/src/scripts/get-tunes.js +411 -10
- package/src/scripts/get-video.js +352 -9
- package/src/scripts/git-backup.js +561 -9
- package/src/scripts/git-clone.js +477 -9
- package/src/scripts/git-pup.js +303 -7
- package/src/scripts/git-push.js +380 -8
- package/src/scripts/h.js +607 -9
- package/src/scripts/hide-desktop-icons.js +483 -7
- package/src/scripts/hide-hidden-files.js +522 -7
- package/src/scripts/install-dependencies-from.js +440 -9
- package/src/scripts/ips.js +647 -10
- package/src/scripts/iso.js +354 -8
- package/src/scripts/killni.js +561 -7
- package/src/scripts/ll.js +451 -8
- package/src/scripts/local-ip.js +310 -8
- package/src/scripts/m.js +508 -8
- package/src/scripts/map.js +293 -8
- package/src/scripts/mkd.js +287 -7
- package/src/scripts/ncu-update-all.js +441 -8
- package/src/scripts/nginx-init.js +702 -12
- package/src/scripts/npmi.js +366 -7
- package/src/scripts/o.js +495 -8
- package/src/scripts/org-by-date.js +321 -7
- package/src/scripts/p.js +208 -7
- package/src/scripts/packages.js +313 -8
- package/src/scripts/path.js +209 -7
- package/src/scripts/ports.js +582 -8
- package/src/scripts/q.js +290 -8
- package/src/scripts/refresh-files.js +378 -10
- package/src/scripts/remove-smaller-files.js +500 -8
- package/src/scripts/rename-files-with-date.js +517 -9
- package/src/scripts/resize-image.js +523 -9
- package/src/scripts/rm-safe.js +653 -8
- package/src/scripts/s.js +525 -9
- package/src/scripts/set-git-public.js +349 -7
- package/src/scripts/show-desktop-icons.js +459 -7
- package/src/scripts/show-hidden-files.js +456 -7
- package/src/scripts/tpa.js +265 -8
- package/src/scripts/tpo.js +264 -7
- package/src/scripts/u.js +489 -7
- package/src/scripts/vpush.js +422 -8
- package/src/scripts/y.js +267 -7
- package/src/utils/common/os.js +94 -2
- package/src/utils/ubuntu/apt.js +13 -7
- package/src/utils/windows/choco.js +82 -26
- package/src/utils/windows/winget.js +89 -27
package/src/scripts/d.js
CHANGED
|
@@ -1,23 +1,219 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* d - Navigate to the Desktop directory
|
|
5
|
+
*
|
|
6
|
+
* Migrated from legacy dotfiles alias.
|
|
7
|
+
* Original: alias d="cd ~/Desktop"
|
|
8
|
+
*
|
|
9
|
+
* This script outputs the path to the user's Desktop folder, which can be used
|
|
10
|
+
* with shell integration (e.g., `cd $(d)`). The Desktop folder location varies
|
|
11
|
+
* by operating system:
|
|
12
|
+
* - macOS: ~/Desktop
|
|
13
|
+
* - Linux: ~/Desktop (XDG standard)
|
|
14
|
+
* - Windows: %USERPROFILE%\Desktop
|
|
15
|
+
*
|
|
5
16
|
* @module scripts/d
|
|
6
17
|
*/
|
|
7
18
|
|
|
19
|
+
const os = require('../utils/common/os');
|
|
20
|
+
const path = require('path');
|
|
21
|
+
const fs = require('fs');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Pure Node.js implementation that works on any platform.
|
|
25
|
+
*
|
|
26
|
+
* This function determines the Desktop folder path using Node.js APIs.
|
|
27
|
+
* The Desktop folder is typically in the user's home directory, but the
|
|
28
|
+
* exact location can vary. We check for the folder's existence and provide
|
|
29
|
+
* helpful feedback if it doesn't exist.
|
|
30
|
+
*
|
|
31
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
32
|
+
* @returns {Promise<void>}
|
|
33
|
+
*/
|
|
34
|
+
async function do_d_nodejs(args) {
|
|
35
|
+
// Get the user's home directory using Node.js built-in os module
|
|
36
|
+
const homeDir = os.getHomeDir();
|
|
37
|
+
|
|
38
|
+
// Construct the path to the Desktop folder
|
|
39
|
+
// On all major platforms, the Desktop folder is named "Desktop" in the home directory
|
|
40
|
+
const desktopPath = path.join(homeDir, 'Desktop');
|
|
41
|
+
|
|
42
|
+
// Check if the Desktop folder exists
|
|
43
|
+
if (!fs.existsSync(desktopPath)) {
|
|
44
|
+
// The Desktop folder doesn't exist - this is unusual but can happen
|
|
45
|
+
// on headless servers or minimal installations
|
|
46
|
+
console.error(`Error: Desktop folder not found at ${desktopPath}`);
|
|
47
|
+
console.error('');
|
|
48
|
+
console.error('This might happen if:');
|
|
49
|
+
console.error(' - You are on a headless server without a desktop environment');
|
|
50
|
+
console.error(' - The Desktop folder was deleted or renamed');
|
|
51
|
+
console.error(' - Your system uses a non-standard Desktop location');
|
|
52
|
+
console.error('');
|
|
53
|
+
console.error(`You can create it with: mkdir -p "${desktopPath}"`);
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Output the path so it can be used with shell integration
|
|
58
|
+
// Example usage: cd $(d)
|
|
59
|
+
console.log(desktopPath);
|
|
60
|
+
}
|
|
61
|
+
|
|
8
62
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
63
|
+
* Navigate to the Desktop folder on macOS.
|
|
64
|
+
*
|
|
65
|
+
* On macOS, the Desktop folder is always at ~/Desktop.
|
|
66
|
+
* This function delegates to the pure Node.js implementation since
|
|
67
|
+
* the logic is identical across platforms.
|
|
11
68
|
*
|
|
12
69
|
* @param {string[]} args - Command line arguments (unused)
|
|
13
70
|
* @returns {Promise<void>}
|
|
14
71
|
*/
|
|
15
|
-
async function
|
|
16
|
-
|
|
72
|
+
async function do_d_macos(args) {
|
|
73
|
+
return do_d_nodejs(args);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Navigate to the Desktop folder on Ubuntu.
|
|
78
|
+
*
|
|
79
|
+
* On Ubuntu and other Linux distributions following the XDG Base Directory
|
|
80
|
+
* Specification, the Desktop folder is typically at ~/Desktop.
|
|
81
|
+
* This function delegates to the pure Node.js implementation.
|
|
82
|
+
*
|
|
83
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
84
|
+
* @returns {Promise<void>}
|
|
85
|
+
*/
|
|
86
|
+
async function do_d_ubuntu(args) {
|
|
87
|
+
return do_d_nodejs(args);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Navigate to the Desktop folder on Raspberry Pi OS.
|
|
92
|
+
*
|
|
93
|
+
* Raspberry Pi OS follows the same convention as other Debian-based systems.
|
|
94
|
+
* The Desktop folder is at ~/Desktop.
|
|
95
|
+
* This function delegates to the pure Node.js implementation.
|
|
96
|
+
*
|
|
97
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
98
|
+
* @returns {Promise<void>}
|
|
99
|
+
*/
|
|
100
|
+
async function do_d_raspbian(args) {
|
|
101
|
+
return do_d_nodejs(args);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Navigate to the Desktop folder on Amazon Linux.
|
|
106
|
+
*
|
|
107
|
+
* On Amazon Linux (typically used in server environments), a Desktop folder
|
|
108
|
+
* may not exist. This function delegates to the pure Node.js implementation,
|
|
109
|
+
* which will provide helpful feedback if the folder doesn't exist.
|
|
110
|
+
*
|
|
111
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
112
|
+
* @returns {Promise<void>}
|
|
113
|
+
*/
|
|
114
|
+
async function do_d_amazon_linux(args) {
|
|
115
|
+
return do_d_nodejs(args);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Navigate to the Desktop folder on Windows Command Prompt.
|
|
120
|
+
*
|
|
121
|
+
* On Windows, the Desktop folder is at %USERPROFILE%\Desktop.
|
|
122
|
+
* Node.js's os.homedir() correctly returns the USERPROFILE path on Windows,
|
|
123
|
+
* so the pure Node.js implementation works correctly.
|
|
124
|
+
* This function delegates to the pure Node.js implementation.
|
|
125
|
+
*
|
|
126
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
127
|
+
* @returns {Promise<void>}
|
|
128
|
+
*/
|
|
129
|
+
async function do_d_cmd(args) {
|
|
130
|
+
return do_d_nodejs(args);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Navigate to the Desktop folder on Windows PowerShell.
|
|
135
|
+
*
|
|
136
|
+
* On Windows, the Desktop folder is at %USERPROFILE%\Desktop.
|
|
137
|
+
* This function delegates to the pure Node.js implementation.
|
|
138
|
+
*
|
|
139
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
140
|
+
* @returns {Promise<void>}
|
|
141
|
+
*/
|
|
142
|
+
async function do_d_powershell(args) {
|
|
143
|
+
return do_d_nodejs(args);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Navigate to the Desktop folder on Git Bash (Windows).
|
|
148
|
+
*
|
|
149
|
+
* Git Bash runs on Windows, so the Desktop folder is at %USERPROFILE%\Desktop.
|
|
150
|
+
* This function delegates to the pure Node.js implementation.
|
|
151
|
+
*
|
|
152
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
153
|
+
* @returns {Promise<void>}
|
|
154
|
+
*/
|
|
155
|
+
async function do_d_gitbash(args) {
|
|
156
|
+
return do_d_nodejs(args);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Main entry point - detects environment and executes appropriate implementation.
|
|
161
|
+
*
|
|
162
|
+
* The "d" command outputs the path to the user's Desktop folder.
|
|
163
|
+
* This is designed to be used with shell integration:
|
|
164
|
+
*
|
|
165
|
+
* cd $(d) # Change to Desktop folder
|
|
166
|
+
* ls $(d) # List Desktop contents
|
|
167
|
+
* cp file $(d) # Copy file to Desktop
|
|
168
|
+
*
|
|
169
|
+
* The original alias "cd ~/Desktop" directly changed directories, but since
|
|
170
|
+
* Node.js scripts run in a subprocess, they cannot change the parent shell's
|
|
171
|
+
* working directory. Instead, this script outputs the path for the shell to use.
|
|
172
|
+
*
|
|
173
|
+
* @param {string[]} args - Command line arguments (unused)
|
|
174
|
+
* @returns {Promise<void>}
|
|
175
|
+
*/
|
|
176
|
+
async function do_d(args) {
|
|
177
|
+
const platform = os.detect();
|
|
178
|
+
|
|
179
|
+
const handlers = {
|
|
180
|
+
'macos': do_d_macos,
|
|
181
|
+
'ubuntu': do_d_ubuntu,
|
|
182
|
+
'debian': do_d_ubuntu,
|
|
183
|
+
'raspbian': do_d_raspbian,
|
|
184
|
+
'amazon_linux': do_d_amazon_linux,
|
|
185
|
+
'rhel': do_d_amazon_linux,
|
|
186
|
+
'fedora': do_d_ubuntu,
|
|
187
|
+
'linux': do_d_ubuntu,
|
|
188
|
+
'wsl': do_d_ubuntu,
|
|
189
|
+
'cmd': do_d_cmd,
|
|
190
|
+
'windows': do_d_cmd,
|
|
191
|
+
'powershell': do_d_powershell,
|
|
192
|
+
'gitbash': do_d_gitbash
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const handler = handlers[platform.type];
|
|
196
|
+
if (!handler) {
|
|
197
|
+
console.error(`Platform '${platform.type}' is not supported for this command.`);
|
|
198
|
+
process.exit(1);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
await handler(args);
|
|
17
202
|
}
|
|
18
203
|
|
|
19
|
-
module.exports = {
|
|
204
|
+
module.exports = {
|
|
205
|
+
main: do_d,
|
|
206
|
+
do_d,
|
|
207
|
+
do_d_nodejs,
|
|
208
|
+
do_d_macos,
|
|
209
|
+
do_d_ubuntu,
|
|
210
|
+
do_d_raspbian,
|
|
211
|
+
do_d_amazon_linux,
|
|
212
|
+
do_d_cmd,
|
|
213
|
+
do_d_powershell,
|
|
214
|
+
do_d_gitbash
|
|
215
|
+
};
|
|
20
216
|
|
|
21
217
|
if (require.main === module) {
|
|
22
|
-
|
|
218
|
+
do_d(process.argv.slice(2));
|
|
23
219
|
}
|
package/src/scripts/datauri.js
CHANGED
|
@@ -1,24 +1,389 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* datauri - Convert a file to a base64-encoded data URI string
|
|
5
|
+
*
|
|
6
|
+
* Migrated from legacy dotfiles function.
|
|
7
|
+
* Original:
|
|
8
|
+
* datauri() {
|
|
9
|
+
* local mimeType=""
|
|
10
|
+
* if [ ! -f "$1" ]; then
|
|
11
|
+
* printf "%s is not a file.\n" "$1"
|
|
12
|
+
* return
|
|
13
|
+
* fi
|
|
14
|
+
* mimeType=$(file --brief --mime-type "$1")
|
|
15
|
+
* if [[ $mimeType == text/* ]]; then
|
|
16
|
+
* mimeType="$mimeType;charset=utf-8"
|
|
17
|
+
* fi
|
|
18
|
+
* printf "data:%s;base64,%s" \
|
|
19
|
+
* "$mimeType" \
|
|
20
|
+
* "$(openssl base64 -in "$1" | tr -d "\n")"
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* This script converts any file to a data URI format that can be embedded
|
|
24
|
+
* directly in HTML, CSS, or other documents. Data URIs are useful for:
|
|
25
|
+
* - Embedding small images directly in HTML/CSS
|
|
26
|
+
* - Avoiding additional HTTP requests
|
|
27
|
+
* - Creating self-contained HTML files
|
|
28
|
+
*
|
|
5
29
|
* @module scripts/datauri
|
|
6
30
|
*/
|
|
7
31
|
|
|
32
|
+
const fs = require('fs');
|
|
33
|
+
const path = require('path');
|
|
34
|
+
const os = require('../utils/common/os');
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A mapping of common file extensions to their MIME types.
|
|
38
|
+
* This allows pure Node.js MIME type detection without external dependencies.
|
|
39
|
+
*
|
|
40
|
+
* The mapping covers the most common file types used in web development,
|
|
41
|
+
* images, documents, and media files.
|
|
42
|
+
*/
|
|
43
|
+
const MIME_TYPES = {
|
|
44
|
+
// Images
|
|
45
|
+
'.png': 'image/png',
|
|
46
|
+
'.jpg': 'image/jpeg',
|
|
47
|
+
'.jpeg': 'image/jpeg',
|
|
48
|
+
'.gif': 'image/gif',
|
|
49
|
+
'.webp': 'image/webp',
|
|
50
|
+
'.svg': 'image/svg+xml',
|
|
51
|
+
'.ico': 'image/x-icon',
|
|
52
|
+
'.bmp': 'image/bmp',
|
|
53
|
+
'.tiff': 'image/tiff',
|
|
54
|
+
'.tif': 'image/tiff',
|
|
55
|
+
'.avif': 'image/avif',
|
|
56
|
+
|
|
57
|
+
// Audio
|
|
58
|
+
'.mp3': 'audio/mpeg',
|
|
59
|
+
'.wav': 'audio/wav',
|
|
60
|
+
'.ogg': 'audio/ogg',
|
|
61
|
+
'.m4a': 'audio/mp4',
|
|
62
|
+
'.flac': 'audio/flac',
|
|
63
|
+
'.aac': 'audio/aac',
|
|
64
|
+
'.weba': 'audio/webm',
|
|
65
|
+
|
|
66
|
+
// Video
|
|
67
|
+
'.mp4': 'video/mp4',
|
|
68
|
+
'.webm': 'video/webm',
|
|
69
|
+
'.ogv': 'video/ogg',
|
|
70
|
+
'.avi': 'video/x-msvideo',
|
|
71
|
+
'.mov': 'video/quicktime',
|
|
72
|
+
'.mkv': 'video/x-matroska',
|
|
73
|
+
|
|
74
|
+
// Fonts
|
|
75
|
+
'.woff': 'font/woff',
|
|
76
|
+
'.woff2': 'font/woff2',
|
|
77
|
+
'.ttf': 'font/ttf',
|
|
78
|
+
'.otf': 'font/otf',
|
|
79
|
+
'.eot': 'application/vnd.ms-fontobject',
|
|
80
|
+
|
|
81
|
+
// Text/Code
|
|
82
|
+
'.html': 'text/html',
|
|
83
|
+
'.htm': 'text/html',
|
|
84
|
+
'.css': 'text/css',
|
|
85
|
+
'.js': 'text/javascript',
|
|
86
|
+
'.mjs': 'text/javascript',
|
|
87
|
+
'.json': 'application/json',
|
|
88
|
+
'.xml': 'application/xml',
|
|
89
|
+
'.txt': 'text/plain',
|
|
90
|
+
'.md': 'text/markdown',
|
|
91
|
+
'.csv': 'text/csv',
|
|
92
|
+
'.yaml': 'text/yaml',
|
|
93
|
+
'.yml': 'text/yaml',
|
|
94
|
+
'.ts': 'text/typescript',
|
|
95
|
+
'.tsx': 'text/typescript',
|
|
96
|
+
'.jsx': 'text/javascript',
|
|
97
|
+
|
|
98
|
+
// Documents
|
|
99
|
+
'.pdf': 'application/pdf',
|
|
100
|
+
'.doc': 'application/msword',
|
|
101
|
+
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
102
|
+
'.xls': 'application/vnd.ms-excel',
|
|
103
|
+
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
104
|
+
'.ppt': 'application/vnd.ms-powerpoint',
|
|
105
|
+
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
106
|
+
|
|
107
|
+
// Archives
|
|
108
|
+
'.zip': 'application/zip',
|
|
109
|
+
'.tar': 'application/x-tar',
|
|
110
|
+
'.gz': 'application/gzip',
|
|
111
|
+
'.rar': 'application/vnd.rar',
|
|
112
|
+
'.7z': 'application/x-7z-compressed',
|
|
113
|
+
|
|
114
|
+
// Other
|
|
115
|
+
'.wasm': 'application/wasm',
|
|
116
|
+
'.bin': 'application/octet-stream'
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Get the MIME type for a file based on its extension.
|
|
121
|
+
*
|
|
122
|
+
* This function uses a built-in mapping of file extensions to MIME types,
|
|
123
|
+
* which is faster and more portable than calling external tools like `file`.
|
|
124
|
+
* Falls back to 'application/octet-stream' for unknown extensions.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} filePath - The path to the file
|
|
127
|
+
* @returns {string} The MIME type for the file
|
|
128
|
+
*/
|
|
129
|
+
function getMimeType(filePath) {
|
|
130
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
131
|
+
return MIME_TYPES[ext] || 'application/octet-stream';
|
|
132
|
+
}
|
|
133
|
+
|
|
8
134
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
135
|
+
* Check if a MIME type represents text content.
|
|
136
|
+
*
|
|
137
|
+
* Text content should have charset=utf-8 appended to the MIME type
|
|
138
|
+
* in data URIs for proper character encoding.
|
|
139
|
+
*
|
|
140
|
+
* @param {string} mimeType - The MIME type to check
|
|
141
|
+
* @returns {boolean} True if the MIME type is text-based
|
|
142
|
+
*/
|
|
143
|
+
function isTextMimeType(mimeType) {
|
|
144
|
+
// Text types start with 'text/'
|
|
145
|
+
if (mimeType.startsWith('text/')) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Some application types are also text-based
|
|
150
|
+
const textApplicationTypes = [
|
|
151
|
+
'application/json',
|
|
152
|
+
'application/xml',
|
|
153
|
+
'application/javascript',
|
|
154
|
+
'application/x-javascript'
|
|
155
|
+
];
|
|
156
|
+
|
|
157
|
+
return textApplicationTypes.includes(mimeType);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Pure Node.js implementation that works on any platform.
|
|
162
|
+
*
|
|
163
|
+
* This function reads a file, detects its MIME type based on extension,
|
|
164
|
+
* encodes the content as base64, and outputs a complete data URI string.
|
|
165
|
+
*
|
|
166
|
+
* The data URI format is: data:[<mediatype>][;base64],<data>
|
|
167
|
+
*
|
|
168
|
+
* For text content, we append ;charset=utf-8 to the MIME type to ensure
|
|
169
|
+
* proper character encoding, matching the original bash function behavior.
|
|
170
|
+
*
|
|
171
|
+
* @param {string[]} args - Command line arguments, expects file path as first element
|
|
172
|
+
* @returns {Promise<void>}
|
|
173
|
+
*/
|
|
174
|
+
async function do_datauri_nodejs(args) {
|
|
175
|
+
// Check if a file path was provided
|
|
176
|
+
if (!args || args.length === 0) {
|
|
177
|
+
console.error('Usage: datauri <file>');
|
|
178
|
+
console.error('');
|
|
179
|
+
console.error('Convert a file to a base64-encoded data URI string.');
|
|
180
|
+
console.error('');
|
|
181
|
+
console.error('Example:');
|
|
182
|
+
console.error(' datauri image.png');
|
|
183
|
+
console.error(' datauri styles.css');
|
|
184
|
+
process.exit(1);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const filePath = args[0];
|
|
188
|
+
|
|
189
|
+
// Resolve the file path (handles relative paths)
|
|
190
|
+
const resolvedPath = path.resolve(filePath);
|
|
191
|
+
|
|
192
|
+
// Check if the file exists
|
|
193
|
+
if (!fs.existsSync(resolvedPath)) {
|
|
194
|
+
console.error(`${filePath} is not a file.`);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Check if it's actually a file (not a directory)
|
|
199
|
+
const stats = fs.statSync(resolvedPath);
|
|
200
|
+
if (!stats.isFile()) {
|
|
201
|
+
console.error(`${filePath} is not a file.`);
|
|
202
|
+
process.exit(1);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Get the MIME type based on file extension
|
|
206
|
+
let mimeType = getMimeType(resolvedPath);
|
|
207
|
+
|
|
208
|
+
// For text content, append charset=utf-8 (matching original bash behavior)
|
|
209
|
+
if (isTextMimeType(mimeType)) {
|
|
210
|
+
mimeType = `${mimeType};charset=utf-8`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Read the file contents as a Buffer
|
|
214
|
+
const fileBuffer = fs.readFileSync(resolvedPath);
|
|
215
|
+
|
|
216
|
+
// Convert to base64 string
|
|
217
|
+
const base64Data = fileBuffer.toString('base64');
|
|
218
|
+
|
|
219
|
+
// Output the complete data URI
|
|
220
|
+
// Using process.stdout.write to avoid trailing newline (matching original printf behavior)
|
|
221
|
+
process.stdout.write(`data:${mimeType};base64,${base64Data}`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Convert a file to data URI on macOS.
|
|
226
|
+
*
|
|
227
|
+
* macOS can use the pure Node.js implementation since file reading,
|
|
228
|
+
* MIME type detection, and base64 encoding all work identically.
|
|
11
229
|
*
|
|
12
230
|
* @param {string[]} args - Command line arguments
|
|
13
|
-
* @param {string} args.0 - Path to the file to convert
|
|
14
231
|
* @returns {Promise<void>}
|
|
15
232
|
*/
|
|
16
|
-
async function
|
|
17
|
-
|
|
233
|
+
async function do_datauri_macos(args) {
|
|
234
|
+
return do_datauri_nodejs(args);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Convert a file to data URI on Ubuntu.
|
|
239
|
+
*
|
|
240
|
+
* Ubuntu can use the pure Node.js implementation since file reading,
|
|
241
|
+
* MIME type detection, and base64 encoding all work identically.
|
|
242
|
+
*
|
|
243
|
+
* @param {string[]} args - Command line arguments
|
|
244
|
+
* @returns {Promise<void>}
|
|
245
|
+
*/
|
|
246
|
+
async function do_datauri_ubuntu(args) {
|
|
247
|
+
return do_datauri_nodejs(args);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Convert a file to data URI on Raspberry Pi OS.
|
|
252
|
+
*
|
|
253
|
+
* Raspberry Pi OS can use the pure Node.js implementation since file reading,
|
|
254
|
+
* MIME type detection, and base64 encoding all work identically.
|
|
255
|
+
*
|
|
256
|
+
* @param {string[]} args - Command line arguments
|
|
257
|
+
* @returns {Promise<void>}
|
|
258
|
+
*/
|
|
259
|
+
async function do_datauri_raspbian(args) {
|
|
260
|
+
return do_datauri_nodejs(args);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Convert a file to data URI on Amazon Linux.
|
|
265
|
+
*
|
|
266
|
+
* Amazon Linux can use the pure Node.js implementation since file reading,
|
|
267
|
+
* MIME type detection, and base64 encoding all work identically.
|
|
268
|
+
*
|
|
269
|
+
* @param {string[]} args - Command line arguments
|
|
270
|
+
* @returns {Promise<void>}
|
|
271
|
+
*/
|
|
272
|
+
async function do_datauri_amazon_linux(args) {
|
|
273
|
+
return do_datauri_nodejs(args);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Convert a file to data URI on Windows Command Prompt.
|
|
278
|
+
*
|
|
279
|
+
* Windows CMD can use the pure Node.js implementation since file reading,
|
|
280
|
+
* MIME type detection, and base64 encoding all work identically.
|
|
281
|
+
* Node.js handles path separators and file system differences automatically.
|
|
282
|
+
*
|
|
283
|
+
* @param {string[]} args - Command line arguments
|
|
284
|
+
* @returns {Promise<void>}
|
|
285
|
+
*/
|
|
286
|
+
async function do_datauri_cmd(args) {
|
|
287
|
+
return do_datauri_nodejs(args);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Convert a file to data URI on Windows PowerShell.
|
|
292
|
+
*
|
|
293
|
+
* Windows PowerShell can use the pure Node.js implementation since file reading,
|
|
294
|
+
* MIME type detection, and base64 encoding all work identically.
|
|
295
|
+
* Node.js handles path separators and file system differences automatically.
|
|
296
|
+
*
|
|
297
|
+
* @param {string[]} args - Command line arguments
|
|
298
|
+
* @returns {Promise<void>}
|
|
299
|
+
*/
|
|
300
|
+
async function do_datauri_powershell(args) {
|
|
301
|
+
return do_datauri_nodejs(args);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Convert a file to data URI on Git Bash.
|
|
306
|
+
*
|
|
307
|
+
* Git Bash can use the pure Node.js implementation since file reading,
|
|
308
|
+
* MIME type detection, and base64 encoding all work identically.
|
|
309
|
+
* Node.js handles path separators and file system differences automatically.
|
|
310
|
+
*
|
|
311
|
+
* @param {string[]} args - Command line arguments
|
|
312
|
+
* @returns {Promise<void>}
|
|
313
|
+
*/
|
|
314
|
+
async function do_datauri_gitbash(args) {
|
|
315
|
+
return do_datauri_nodejs(args);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Main entry point - detects environment and executes appropriate implementation.
|
|
320
|
+
*
|
|
321
|
+
* The "datauri" command converts any file to a base64-encoded data URI string.
|
|
322
|
+
* Data URIs embed file contents directly in the string, useful for:
|
|
323
|
+
* - Embedding small images in HTML img src or CSS background-image
|
|
324
|
+
* - Avoiding additional HTTP requests for small resources
|
|
325
|
+
* - Creating self-contained HTML documents
|
|
326
|
+
*
|
|
327
|
+
* Usage: datauri <file>
|
|
328
|
+
*
|
|
329
|
+
* Example output:
|
|
330
|
+
* datauri image.png
|
|
331
|
+
* => data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
|
|
332
|
+
*
|
|
333
|
+
* datauri styles.css
|
|
334
|
+
* => data:text/css;charset=utf-8;base64,LyogU3R5bGVz...
|
|
335
|
+
*
|
|
336
|
+
* @param {string[]} args - Command line arguments, expects file path as first element
|
|
337
|
+
* @returns {Promise<void>}
|
|
338
|
+
*/
|
|
339
|
+
async function do_datauri(args) {
|
|
340
|
+
const platform = os.detect();
|
|
341
|
+
|
|
342
|
+
const handlers = {
|
|
343
|
+
'macos': do_datauri_macos,
|
|
344
|
+
'ubuntu': do_datauri_ubuntu,
|
|
345
|
+
'debian': do_datauri_ubuntu,
|
|
346
|
+
'raspbian': do_datauri_raspbian,
|
|
347
|
+
'amazon_linux': do_datauri_amazon_linux,
|
|
348
|
+
'rhel': do_datauri_amazon_linux,
|
|
349
|
+
'fedora': do_datauri_ubuntu,
|
|
350
|
+
'linux': do_datauri_ubuntu,
|
|
351
|
+
'wsl': do_datauri_ubuntu,
|
|
352
|
+
'cmd': do_datauri_cmd,
|
|
353
|
+
'windows': do_datauri_cmd,
|
|
354
|
+
'powershell': do_datauri_powershell,
|
|
355
|
+
'gitbash': do_datauri_gitbash
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const handler = handlers[platform.type];
|
|
359
|
+
if (!handler) {
|
|
360
|
+
console.error(`Platform '${platform.type}' is not supported for this command.`);
|
|
361
|
+
console.error('');
|
|
362
|
+
console.error('Supported platforms:');
|
|
363
|
+
console.error(' - macOS');
|
|
364
|
+
console.error(' - Ubuntu, Debian, and other Linux distributions');
|
|
365
|
+
console.error(' - Raspberry Pi OS');
|
|
366
|
+
console.error(' - Amazon Linux');
|
|
367
|
+
console.error(' - Windows (CMD, PowerShell, Git Bash)');
|
|
368
|
+
process.exit(1);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
await handler(args);
|
|
18
372
|
}
|
|
19
373
|
|
|
20
|
-
module.exports = {
|
|
374
|
+
module.exports = {
|
|
375
|
+
main: do_datauri,
|
|
376
|
+
do_datauri,
|
|
377
|
+
do_datauri_nodejs,
|
|
378
|
+
do_datauri_macos,
|
|
379
|
+
do_datauri_ubuntu,
|
|
380
|
+
do_datauri_raspbian,
|
|
381
|
+
do_datauri_amazon_linux,
|
|
382
|
+
do_datauri_cmd,
|
|
383
|
+
do_datauri_powershell,
|
|
384
|
+
do_datauri_gitbash
|
|
385
|
+
};
|
|
21
386
|
|
|
22
387
|
if (require.main === module) {
|
|
23
|
-
|
|
388
|
+
do_datauri(process.argv.slice(2));
|
|
24
389
|
}
|