import path from 'path'
import json from '../../util/json'

const saguiScripts = {
  'build': 'sagui build',
  'develop': 'sagui develop --port 3000',
  'dist': 'NODE_ENV=production sagui build --optimize',
  'start': 'npm run develop',
  'test': 'npm run test:lint && npm run test:unit',
  'test:coverage': 'npm run test:unit -- --coverage',
  'test:lint': 'sagui lint',
  'test:unit': 'NODE_ENV=test sagui test',
  'test:unit:watch': 'npm run test:unit -- --watch'
}

export default function (projectPath) {
  const packagePath = path.join(projectPath, 'package.json')
  const packageJSON = json.read(packagePath)

  json.write(packagePath, {
    ...packageJSON,
    scripts: {
      ...saguiScripts,
      ...withoutDefaults(packageJSON.scripts)
    }
  })
}

/**
 * Remove default configurations generated by NPM that can be overwriten
 * We don't want to overwrite any user configured scripts
 */
function withoutDefaults (scripts = {}) {
  const defaultScripts = {
    'test': 'echo "Error: no test specified" && exit 1'
  }

  return Object.keys(scripts)
    .filter((key) => !scripts[key] !== '' && scripts[key] !== defaultScripts[key])
    .reduce((filtered, key) => {
      return {
        ...filtered,
        [key]: scripts[key]
      }
    }, {})
}

import path from 'path'
import json from '../../util/json'

const saguiScripts = {
  'build': 'sagui build',
  'develop': 'sagui develop --port 3000',
  'dist': 'NODE_ENV=production sagui build --optimize',
  'start': 'npm run develop',
  'test': 'npm run test:lint && npm run test:typecheck && npm run test:unit',
  'test:coverage': 'npm run test:unit -- --coverage',
  'test:lint': 'sagui lint',
  'test:typecheck': 'sagui typecheck',
  'test:unit': 'NODE_ENV=test sagui test',
  'test:unit:watch': 'npm run test:unit -- --watch'
}

export default function (projectPath) {
  const packagePath = path.join(projectPath, 'package.json')
  const packageJSON = json.read(packagePath)

  json.write(packagePath, {
    ...packageJSON,
    scripts: {
      ...saguiScripts,
      ...withoutDefaults(packageJSON.scripts)
    }
  })
}

/**
 * Remove default configurations generated by NPM that can be overwriten
 * We don't want to overwrite any user configured scripts
 */
function withoutDefaults (scripts = {}) {
  const defaultScripts = {
    'test': 'echo "Error: no test specified" && exit 1'
  }

  return Object.keys(scripts)
    .filter((key) => !scripts[key] !== '' && scripts[key] !== defaultScripts[key])
    .reduce((filtered, key) => {
      return {
        ...filtered,
        [key]: scripts[key]
      }
    }, {})
}

Add package.json scripts for type checking 🚕
Add package.json scripts for type checking 🚕
