Skip to content

Instantly share code, notes, and snippets.

@tomByrer
Last active March 29, 2021 12:52
Show Gist options
  • Save tomByrer/f6510da9f971a815b9b91d2964d00647 to your computer and use it in GitHub Desktop.
Save tomByrer/f6510da9f971a815b9b91d2964d00647 to your computer and use it in GitHub Desktop.
/* squoosh-cli2node v0.5.0
use the @squoosh/cli tool in Node via CLI API
@2021 Tom Byrer, #licence MIT
known bugs: only npm (no yarn pnpm) & bad error reporting
*/
const { execSync } = require("child_process")
// find squoosh in npm global packages
function isFoundInCliResults(command, str){
if (execSync(command).toString().search(str) === -1){
return false
} else {
return true
}
}
let globalRoot // buffer at first then str
if (isFoundInCliResults(`npm list -g --depth 0`, `squoosh/cli`)){
globalRoot = execSync(`npm root -g`)
} else {
console.error(`Could not find global install.
Install via: npm i -g @squoosh/cli`)
process.exit(1)
}
const squooshIndex = `${ globalRoot.toString().trim() }/@squoosh/cli/build/index.js`
function squoosh(options){
return execSync(`node ${squooshIndex} ${options}`).toString().trim()
}
// usage: just pass all options & file list as a single string, eg:
console.info(`squoosh-cli version: ${squoosh('-V')}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment