tunnckoCore / blankr

:heart: tasks, todos, ideas, streaks, WIP, etc
https://i.am.charlike.online
4 stars 1 forks source link

base-extract-comments #50

Closed tunnckoCore closed 8 years ago

tunnckoCore commented 8 years ago

index.js

/*!
 * base-extract-comments <https://github.com/tunnckoCore/base-extract-comments>
 *
 * Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
 * Released under the MIT license.
 */

'use strict'

var utils = require('./utils')

module.exports = function baseExtractComments (opts) {
  return function extractComments (app) {
    // i tried both is-valid-app and is-registered here as `isValid`
    if (!utils.isValid(app, 'base-extract-comments')) return

    this.use(utils.plugins())
    this.define('extractComments', function parse (input, options) {
      if (typeof input === 'object') {
        this.options = utils.extend({}, this.options, input)
        input = null
      }

      this.cache = utils.extend({}, this.cache)
      this.cache.input = input || this.cache.input
      this.options = utils.extend({
        preserve: false,
        block: true,
        line: false
      }, opts, this.options, options, {
        locations: true,
        unwrap: true
      })

      this.cache.comments = utils.extract(this.cache.input, this.options).comments
      var len = this.cache.comments.length
      var idx = -1

      while (++idx < len) {
        // not so cool workaround,
        // but we can't pass different than object currently
        // waiting `use` PR
        this.run({
          current: this.cache.comments[idx],
          index: idx,
          next: this.cache.comments[idx + 1]
        })
      }

      return this.cache.comments
    })
  }
}

test.js

/*!
 * base-extract-comments <https://github.com/tunnckoCore/base-extract-comments>
 *
 * Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
 * Released under the MIT license.
 */

'use strict'

var fs = require('fs')
var test = require('mukla')
var Base = require('base')
var plugin = require('./index')

test('base-extract-comments:', function (done) {
  var app = new Base({isApp: true}, {})
  app.use(plugin())
  app.use(function (app) {
    return function (comment) {
      comment = comment.current // workaround
      console.log('actual')
    }
  })
  app.extractComments(fs.readFileSync('./fixture.js', 'utf8'))

  done()
})

utils.js

'use strict'

var utils = require('lazy-cache')(require)
var fn = require
require = utils // eslint-disable-line no-undef, no-native-reassign

/**
 * Lazily required module dependencies
 */

require('acorn-extract-comments', 'extract')
require('base-plugins', 'plugins')
require('extend-shallow', 'extend')
// require('is-registered', 'isRegistered')
require('is-valid-app', 'isValid')
require = fn // eslint-disable-line no-undef, no-native-reassign

/**
 * Expose `utils` modules
 */

module.exports = utils

part of the package.json

  "dependencies": {
    "acorn-extract-comments": "^0.2.0",
    "base-plugins": "^0.4.13",
    "extend-shallow": "^2.0.1",
    "is-registered": "^0.1.5",
    "is-valid-app": "^0.2.0",
    "lazy-cache": "^2.0.1"
  },
  "devDependencies": {
    "base": "^0.11.1",
    "mukla": "^0.4.1",
    "pre-commit": "*"
  },
tunnckoCore commented 8 years ago

done