-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3297 from storybooks/cli-helpful-errors
CLI: add error handling for latest_version helper
- Loading branch information
Showing
2 changed files
with
36 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,40 @@ | ||
import { sync as spawnSync } from 'cross-spawn'; | ||
import { spawn } from 'cross-spawn'; | ||
import hasYarn from './has_yarn'; | ||
|
||
const packageManager = hasYarn() ? 'yarn' : 'npm'; | ||
|
||
export default async function latestVersion(packageName) { | ||
const result = spawnSync(packageManager, ['info', packageName, '--json'], { | ||
cwd: process.cwd(), | ||
env: process.env, | ||
stdio: 'pipe', | ||
encoding: 'utf-8', | ||
silent: true, | ||
}); | ||
export default function latestVersion(packageName) { | ||
return new Promise((resolve, reject) => { | ||
const command = spawn(packageManager, ['info', packageName, 'version', '--json', '--silent'], { | ||
cwd: process.cwd(), | ||
env: process.env, | ||
stdio: 'pipe', | ||
encoding: 'utf-8', | ||
silent: true, | ||
}); | ||
|
||
command.stdout.on('data', data => { | ||
try { | ||
const info = JSON.parse(data); | ||
if (info.error) { | ||
// npm error | ||
reject(new Error(info.error.summary)); | ||
} else if (info.type === 'inspect') { | ||
// yarn success | ||
resolve(info.data); | ||
} else { | ||
// npm success | ||
resolve(info); | ||
} | ||
} catch (e) { | ||
// yarn info output | ||
} | ||
}); | ||
|
||
const info = JSON.parse(result.output[1].toString()); | ||
return info.data.version; | ||
command.stderr.on('data', data => { | ||
// yarn error | ||
const info = JSON.parse(data); | ||
reject(new Error(info.data)); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters