UNPKG

storybook-chromatic

Version:
35 lines (26 loc) 1.03 kB
import path from 'path'; import { execSync } from 'child_process'; import { readFileSync, writeFileSync } from 'jsonfile'; const command = 'chromatic'; const script = 'chromatic'; export function checkPackageJson({ appDir = process.cwd() } = {}) { const packageJson = readFileSync(path.resolve(appDir, './package.json')); return Object.entries(packageJson.scripts || {}).find( ([key, value]) => value.match(command) || key === script ); } export function addScriptToPackageJson(scriptName, scriptCommand, { appDir = process.cwd() } = {}) { const filename = path.resolve(appDir, './package.json'); const packageJson = readFileSync(filename); if (packageJson[scriptName]) { throw new Error(`Script named '${scriptName}' already exists in package.json`); } if (!packageJson.scripts) { packageJson.scripts = {}; } packageJson.scripts[scriptName] = scriptCommand; if (!packageJson.dependencies) { packageJson.dependencies = {}; } writeFileSync(filename, packageJson, { spaces: 2 }); }