nadav-dav / rekuire

'rekuire' is basically node's 'require' without the relative paths
93 stars 10 forks source link

Suggestion - more intelligence ;) #6

Closed kristianmandrup closed 9 years ago

kristianmandrup commented 10 years ago

I was on the "same path" and developed another type of "smart loader":

Now I can do the following in LiveScript

rek          = require 'rekuire'
requires = rek 'requires'
requires.test 'test_setup'

[Book, User] = requires.fixtures 'book', 'user'
[permit-for, PermitMatcher, Permit, PermitRegistry] = require.files 'permit-for', 'permit_matcher', 'permit', 'permit-registry'

Not bad :)

I can just tweak the following file to add more "intelligence". Note, that this way I mostly avoid file scanning (except to find my 'requires' file using rek).

Cheers!

# requires.ls (compiled to requires.js)

rek = require 'rekuire'
require 'sugar'
_ = require 'prelude-ls'

underscore = (items) ->
  strings = items.map (item) ->
    String(item)
  _.map (.underscore!), strings

test-level = 1
file-level = 0

test-base-path = ->
  "test"
  # @bp ||= (['..'] * test-level).join '/'

file-base-path = ->
  "."
  # @fbp ||= (['..'] * file-level).join '/'

test-path = (...paths) ->
  upaths = underscore(...paths)
  [test-base-path!, upaths].flatten!.join '/'

module.exports =
  file-lv: (lvs) ->
    file-level := lvs

  test-lv: (lvs) ->
    test-level := lvs

  test: (...paths) ->
    rek test-path(paths)

  fixture: (path) ->
    @test 'fixtures', path

  # alias
  fix: (path) ->
    @fixture path

  file: (path) ->
    rek [file-base-path!, path.underscore!].join '/'

  # m - alias for module
  m: (path) ->
    @file path

  files: (...paths) ->
    paths.map (path) ->
      @file(path)

  fixtures: (...paths) ->
    paths.map (path) ->
      @fixture(path)

  tests: (...paths) ->
    paths.map (path) ->
      @test(path)

Could be interesting to add a similar feature to rek, as I'm sure most projects follow a certain structure like this.

nadav-dav commented 10 years ago

im sorry dude, but i seem to totally miss your point.. :/ can you elaborate?

kristianmandrup commented 10 years ago

Hey,

I have the following issues: One project, decoratow-mw has a file index.js in the root. some files in this project uses rek to require index. Some other files require another package middleware. In the middleware project, some files also require index via rek. But the required package resolves index to the parent project. Is there a way to scope the local rek to only look within a certain scope? It think it is basically since rek is a Singleton? Would be better if it was an instance and you could somehow set the root of each rek instance to a specific location.

nadav-dav commented 10 years ago

oh i get it now :) i tried to make it so packaged will not be accessible through other packaged, so rekuire does not scan "node-modules" folder (children modules), and does not scans up (parent modules).

kristianmandrup commented 9 years ago

No worries.

nadav-dav commented 9 years ago

Im sorry i was waiting for your reply and since i haven't heard from you i closed thiz issue.

If you are still having problems with it, im more than happy to assist :)

In order to understand what is happening there i need to understand your file structure. If you could send me an example that demonstrate the problem i will fix it in no time :)