1- import axios from 'axios' ;
2- import { createWriteStream } from 'fs' ;
3- import path from 'path' ;
4- import fs from 'fs' ;
5- import { pipeline } from 'stream/promises' ;
6- import { exec } from 'child_process' ;
7- import { promisify } from 'util' ;
1+ import axios from 'axios'
2+ import { createWriteStream } from 'fs'
3+ import path from 'path'
4+ import fs from 'fs'
5+ import { pipeline } from 'stream/promises'
6+ import { exec } from 'child_process'
7+ import { promisify } from 'util'
88
99// Create a "promisified" version of exec
10- const execAsync = promisify ( exec ) ;
10+ const execAsync = promisify ( exec )
1111
1212/**
1313 * Downloads an image from a URL to a *specific* local file path.
1414 */
1515export async function downloadImage ( url : string , destinationPath : string ) : Promise < string > {
16- const response = await axios . get ( url , { responseType : 'stream' } ) ;
16+ const response = await axios . get ( url , {
17+ responseType : 'stream' ,
18+ headers : {
19+ 'User-Agent' :
20+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
21+ }
22+ } )
1723
1824 // Save the file to the *provided* path
19- await pipeline ( response . data , createWriteStream ( destinationPath ) ) ;
25+ await pipeline ( response . data , createWriteStream ( destinationPath ) )
2026
21- return destinationPath ;
27+ return destinationPath
2228}
2329
2430/**
2531 * Sets the desktop wallpaper from a local file path.
2632 */
2733
2834export async function setWallpaperExternal ( localPath : string ) : Promise < void > {
29- const platform = process . platform ;
30- let command : string | undefined ;
35+ const platform = process . platform
36+ let command : string | undefined
3137
3238 if ( platform === 'win32' ) {
3339 // Windows command (unchanged)
@@ -44,60 +50,56 @@ export async function setWallpaperExternal(localPath: string): Promise<void> {
4450 }
4551 Add-Type
4652 [SetWallpaper]::SystemParametersInfo(20, 0, "${ localPath } ", 3)
47- ` ;
48- command = `powershell.exe -Command "${ powershellCommand . replace ( / " / g, '\\"' ) } "` ;
49-
53+ `
54+ command = `powershell.exe -Command "${ powershellCommand . replace ( / " / g, '\\"' ) } "`
5055 } else if ( platform === 'darwin' ) {
5156 // macOS command (unchanged)
52- command = `osascript -e 'tell application "Finder" to set desktop picture to POSIX file "${ localPath } "'` ;
53-
57+ command = `osascript -e 'tell application "Finder" to set desktop picture to POSIX file "${ localPath } "'`
5458 } else if ( platform === 'linux' ) {
55- const cmd1 = `gsettings set org.gnome.desktop.background picture-uri "file://${ localPath } "` ;
56- const cmd2 = `gsettings set org.gnome.desktop.background picture-uri-dark "file://${ localPath } " 2>/dev/null` ;
57-
58- command = `{ ${ cmd1 } ; ${ cmd2 } ; } || true` ;
59+ const cmd1 = `gsettings set org.gnome.desktop.background picture-uri "file://${ localPath } "`
60+ const cmd2 = `gsettings set org.gnome.desktop.background picture-uri-dark "file://${ localPath } " 2>/dev/null`
5961
62+ command = `{ ${ cmd1 } ; ${ cmd2 } ; } || true`
6063 }
6164 if ( ! command ) {
62- throw new Error ( `Platform "${ platform } " is not supported.` ) ;
65+ throw new Error ( `Platform "${ platform } " is not supported.` )
6366 }
6467
6568 // Execute the command (unchanged)
6669 try {
67- const { stdout , stderr } = await execAsync ( command ) ;
70+ const { stderr } = await execAsync ( command )
6871 if ( stderr ) {
69- console . warn ( `[setWallpaperExternal] Warning: ${ stderr } ` ) ;
72+ console . warn ( `[setWallpaperExternal] Warning: ${ stderr } ` )
7073 }
7174 } catch ( error ) {
72- console . error ( `[setWallpaperExternal] Failed to execute command: ${ command } ` ) ;
73- throw error ;
75+ console . error ( `[setWallpaperExternal] Failed to execute command: ${ command } ` )
76+ throw error
7477 }
7578}
7679
7780export async function setWallpaper ( localPath : string ) : Promise < void > {
7881 try {
79- const absolutePath = path . resolve ( localPath ) ;
82+ const absolutePath = path . resolve ( localPath )
8083
8184 // First, let's verify the file 100% exists
8285 if ( ! fs . existsSync ( absolutePath ) ) {
83- console . error ( `❌ File does not exist at path: ${ absolutePath } ` ) ;
84- return ;
86+ console . error ( `❌ File does not exist at path: ${ absolutePath } ` )
87+ return
8588 }
8689
87- console . log ( `Setting wallpaper from absolute path: ${ absolutePath } ` ) ;
90+ console . log ( `Setting wallpaper from absolute path: ${ absolutePath } ` )
8891
8992 // Now we await the external package
90- await setWallpaperExternal ( absolutePath ) ;
91-
92- console . log ( '✅ setWallpaperExternal command finished.' ) ;
93+ await setWallpaperExternal ( absolutePath )
9394
95+ console . log ( '✅ setWallpaperExternal command finished.' )
9496 } catch ( err ) {
9597 // If the package fails, it should throw an error
96- console . error ( '❌ FAILED TO SET WALLPAPER:' ) ;
98+ console . error ( '❌ FAILED TO SET WALLPAPER:' )
9799 if ( err instanceof Error ) {
98- console . error ( err . message ) ;
100+ console . error ( err . message )
99101 } else {
100- console . error ( err ) ;
102+ console . error ( err )
101103 }
102104 }
103- }
105+ }
0 commit comments