const fs = require('fs')
const glob = require('globby')
const yamlFront = require('yaml-front-matter')
const args = require('minimist')(process.argv.splice(2))

const dir = args.dir || '.'
const storyDir = args.stories || ''

const _getFileNameFromPath = () => {
  var memo = {}

  return (path) => {
    if (!(path in memo))
      memo[path] = path.split('/').pop().split('.').shift()

    return memo[path]
  }
}()

const getComponents = glob([`${dir}/**/*.js*`]).then(paths => {
  fs.writeFile('./app/components.js', `
    ${paths.map(path => {
      return `import ${_getFileNameFromPath(path)} from '${path}'`
    }).join('\n\t\t')}

    const components = {
      ${paths.map(path => _getFileNameFromPath(path))}
    }

    export default components
  `)
})

const getStories = glob([`${dir}/${storyDir}/**/*.md`]).then(paths => {
  var stories = []

  paths.forEach(path => {
    fs.readFile(path, 'utf8', (err, md) => {
      if (err) throw err

      stories.push(yamlFront.loadFront(md))
    })
  })

  fs.writeFile('./app/fixtures.js', `
    const fixtures = {
      ${stories.forEach(story => {
        return story
      })}
    }
  `)
})

const fs = require('fs-extra')
const glob = require('globby')
const yamlFront = require('yaml-front-matter')
const args = require('minimist')(process.argv.splice(2))

const compDir = args.components || '.'
const storyDir = args.stories || ''

//    _getFileNameFromPath : string -> string
const _getFileNameFromPath = () => {
  var memo = {}

  return path => {
    if (!(path in memo))
      memo[path] = path.split('/').pop().split('.').shift()

    return memo[path]
  }
}()

//    _getComponent : string -> promise
const _getComponent = path => {
  return new Promise((res, rej) => {
    const componentsPath = `copied-components${path}`

    fs.copy(path, componentsPath, (err) => {
      if (err) rej(err)

      res(componentsPath)
    })
  })
}

//    _getStory : string -> promise
const _getStory = path => {
  return new Promise((res, rej) => {
    fs.readFile(path, 'utf8', (err, md) => {
      if (err) rej(err)

      var json = yamlFront.loadFront(md)

      delete json.__content
      res(JSON.stringify(json).slice(1, -1))
    })
  })
}

const getComponents = glob(`${compDir}/**/*.js*`).then(paths => {
  Promise.all(paths.map(path => _getComponent(path)))
    .then((paths) => {
      debugger
      fs.writeFile('./app/components.js', `
        ${paths.map(path => {
          return `import ${_getFileNameFromPath(path)} from '${path}'`
        }).join('\n\t\t')}
        const components = {
          ${paths.map(path => _getFileNameFromPath(path))}
        }
        export default components`)
    }).catch(err => console.log(err))
})

const getStories = glob([`${compDir}/${storyDir}/**/*.md`]).then(paths => {
  Promise.all(paths.map(path => _getStory(path)))
    .then(stories => {
      fs.writeFile('./app/fixtures.js', `
        const fixtures = {
          ${stories.map(story => story)}
        }
        export default fixtures`)
    }).catch(err => console.log(err))
})

build will now copy components so they can accessed on the server and generate manifests correctly