{"size": 2514, "ext": "js", "max_stars_repo_path": "src/commands/pages/upload.js", "max_stars_repo_name": "EcomGraduates/quickshot", "max_stars_repo_stars_event_min_datetime": "2015-03-09T02:11:59.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-15T21:34:09.000Z", "max_issues_repo_path": "src/commands/pages/upload.js", "max_issues_repo_name": "LisoCodes/quickshot", "max_issues_repo_issues_event_min_datetime": "2015-03-18T02:55:16.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-18T17:38:48.000Z", "max_forks_repo_path": "src/commands/pages/upload.js", "max_forks_repo_name": "LisoCodes/quickshot", "max_forks_repo_forks_event_min_datetime": "2015-03-17T23:18:10.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-15T21:32:29.000Z"}
{"max_stars_count": 108, "max_issues_count": 73, "max_forks_count": 26, "avg_line_length": 25.6530612245, "max_line_length": 85, "alphanum_fraction": 0.5894988067}

const _ = require(`lodash`)
const Promise = require(`bluebird`)
const { log, getTarget, loadConfig, to, parsePage } = require(`../../helpers`)
const ignoreParser = require(`gitignore-parser`)
const path = require(`path`)
const fs = require(`fs`)
Promise.promisifyAll(fs)
const requestify = require(`../../requestify`)
const glob = require(`glob`)

module.exports = async function (argv) {
  let ignore = null
  const config = await loadConfig()
  const target = await getTarget(config, argv)

  let total = 0
  var filter = argv.filter ? new RegExp(`^${argv.filter}`) : null

  try {
    const ignoreFile = await fs.readFileAsync(`.quickshot-ignore`, `utf8`)
    ignore = ignoreParser.compile(ignoreFile)
  } catch (err) {}

  let files = glob.sync(`pages/*`, { nodir: true })

  if (ignore) {
    files = _.reject(files, function (file) {
      return ignore.denies(file)
    })
  }

  if (filter) {
    files = _.filter(files, function (file) {
      const pathParts = file.split(path.sep)
      const trimmedParts = _.drop(pathParts, (_.lastIndexOf(pathParts, `pages`) + 1))
      const key = trimmedParts.join(`/`)
      return filter.test(key)
    })
  }

  files = files.map((file) => {
    const pathParts = file.split(path.sep)
    const trimmedParts = _.drop(pathParts, (_.lastIndexOf(pathParts, `pages`) + 1))
    const filepath = trimmedParts.join(path.sep)

    return {
      key: filepath,
      name: path.basename(filepath),
      path: file
    }
  })

  await Promise.map(files, async function (file) {
    const source = await fs.readFileAsync(file.path, `utf8`)
    const page = parsePage(source)

    let res = await requestify(target, {
      method: `get`,
      url: `/pages.json`,
      qs: {
        handle: page.handle
      }
    })

    const shopifyPage = _.get(res, `body.pages[0]`)

    if (shopifyPage != null) {
      page.id = shopifyPage.id

      res = await requestify(target, {
        method: `put`,
        url: `/pages/${page.id}.json`,
        body: {
          page: _.pick(page, `id`, `body_html`, `author`, `title`, `handle`)
        }
      })
    } else {
      res = await requestify(target, {
        method: `post`,
        url: `/pages.json`,
        body: {
          page: _.pick(page, `body_html`, `author`, `title`, `handle`)
        }
      })
    }

    if (res.isError) {
      log(res, `red`)
    } else {
      total += 1
      log(`uploaded ${file.key}`, `green`)
    }
  }, { concurrency: config.concurrency })

  return `Uploaded ${total} pages.`
}
