From 9b52f210dbff1e71088cde2276461ed69ccf3fee Mon Sep 17 00:00:00 2001 From: Ronan Jouchet Date: Thu, 25 Feb 2021 18:11:10 -0500 Subject: [PATCH] Get rid of dependency "shelljs" We don't need a fancy _"portable (Windows/Linux/macOS) implementation of Unix shell commands on top of the Node.js API"_, we just want to run a simple script. Replacing with using stdlib `child_process.spawnSync`. --- package.json | 2 - src/helpers/iconShellHelpers.ts | 66 +++++++++++++++------------------ 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index c93fa8e..8900ea3 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "ncp": "^2.0.0", "page-icon": "^0.3.4", "sanitize-filename": "^1.6.3", - "shelljs": "^0.8.4", "source-map-support": "^0.5.19", "tmp": "^0.2.1" }, @@ -71,7 +70,6 @@ "@types/ncp": "^2.0.4", "@types/node": "^10.17.54", "@types/page-icon": "^0.3.3", - "@types/shelljs": "^0.8.8", "@types/tmp": "^0.2.0", "@typescript-eslint/eslint-plugin": "^4.15.2", "@typescript-eslint/parser": "^4.15.2", diff --git a/src/helpers/iconShellHelpers.ts b/src/helpers/iconShellHelpers.ts index 059aca9..48f17c2 100644 --- a/src/helpers/iconShellHelpers.ts +++ b/src/helpers/iconShellHelpers.ts @@ -1,6 +1,5 @@ import * as path from 'path'; - -import * as shell from 'shelljs'; +import { spawnSync } from 'child_process'; import { isWindows, isOSX, getTempDir } from './helpers'; import * as log from 'loglevel'; @@ -15,44 +14,39 @@ const SCRIPT_PATHS = { /** * Executes a shell script with the form "./pathToScript param1 param2" */ -async function iconShellHelper( +function iconShellHelper( shellScriptPath: string, icoSource: string, icoDestination: string, -): Promise { - return new Promise((resolve, reject) => { - if (isWindows()) { - reject( - new Error( - 'Icon conversion only supported on macOS or Linux. ' + - 'If building for Windows, download/create a .ico and pass it with --icon favicon.ico . ' + - 'If building for macOS/Linux, do it from macOS/Linux', - ), - ); - return; - } - - const shellCommand = `"${shellScriptPath}" "${icoSource}" "${icoDestination}"`; - log.debug( - `Converting icon ${icoSource} to ${icoDestination}.`, - `Calling: ${shellCommand}`, +): string { + if (isWindows()) { + throw new Error( + 'Icon conversion only supported on macOS or Linux. ' + + 'If building for Windows, download/create a .ico and pass it with --icon favicon.ico . ' + + 'If building for macOS/Linux, do it from macOS/Linux', ); - shell.exec(shellCommand, { silent: true }, (exitCode, stdOut, stdError) => { - if (exitCode) { - reject({ - stdOut, - stdError, - }); - return; - } + } - log.debug(`Conversion succeeded and produced icon at ${icoDestination}`); - resolve(icoDestination); - }); - }); + const shellCommand = `"${shellScriptPath}" "${icoSource}" "${icoDestination}"`; + log.debug( + `Converting icon ${icoSource} to ${icoDestination}.`, + `Calling shell command: ${shellCommand}`, + ); + const { stdout, stderr, status } = spawnSync( + shellScriptPath, + [icoSource, icoDestination], + { timeout: 10000 }, + ); + if (status) { + throw new Error( + `Icon conversion failed with status code ${status}.\nstdout: ${stdout.toString()}\nstderr: ${stderr.toString()}`, + ); + } + log.debug(`Conversion succeeded and produced icon at ${icoDestination}`); + return icoDestination; } -export function singleIco(icoSrc: string): Promise { +export function singleIco(icoSrc: string): string { return iconShellHelper( SCRIPT_PATHS.singleIco, icoSrc, @@ -60,7 +54,7 @@ export function singleIco(icoSrc: string): Promise { ); } -export async function convertToPng(icoSrc: string): Promise { +export function convertToPng(icoSrc: string): string { return iconShellHelper( SCRIPT_PATHS.convertToPng, icoSrc, @@ -68,7 +62,7 @@ export async function convertToPng(icoSrc: string): Promise { ); } -export async function convertToIco(icoSrc: string): Promise { +export function convertToIco(icoSrc: string): string { return iconShellHelper( SCRIPT_PATHS.convertToIco, icoSrc, @@ -76,7 +70,7 @@ export async function convertToIco(icoSrc: string): Promise { ); } -export async function convertToIcns(icoSrc: string): Promise { +export function convertToIcns(icoSrc: string): string { if (!isOSX()) { throw new Error('macOS is required to convert to a .icns icon'); }