-
Notifications
You must be signed in to change notification settings - Fork 1
/
needless.coffee
75 lines (63 loc) · 2.05 KB
/
needless.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
###
Copyright (c) 2013, Peter-Paul van Gemerden <[email protected]>
License: Simplified BSD (2-clause)
http://opensource.org/licenses/BSD-2-Clause
###
path = require 'path'
### Calls needless on itself. See the end of this file for clarification. ###
inception = -> needless module.filename
module.exports = needless = (moduleName) ->
###
Removes a module and all its children from the require cache.
###
throwErrorIfCircularReference()
parent = getModuleFromCache moduleName
if parent?
deleteModule mod for mod in getDescendants parent
return true
return false
throwErrorIfCircularReference = ->
if circularReference()
error = new Error 'Found a circular reference. Did you run `cake` on a directory?'
error.code = 'CIRCULAR_REFERENCE'
throw error
circularReference = ->
###
Returns true if any module in the require cache has itself as parent.
###
for key, mod of require.cache when mod.filename is mod.parent?.filename
if mod.filename is mod.parent?.filename
return true
return false
getModuleFromCache = (name) ->
if isRelativePath name then name = toFullPath name
modulePath = require.resolve name
require.cache[modulePath]
isRelativePath = (name) -> (name.indexOf './') is 0
toFullPath = (relativePath) ->
parentDirname = path.dirname module.parent.filename
path.join parentDirname, relativePath
getDescendants = (parent) ->
###
Returns array with parent and all direct and indirect descendents.
###
descendants = []
do recurse = (parent) ->
descendants.push parent
parent.children.forEach recurse
descendants
deleteModule = (mod) ->
delete require.cache[mod.id]
deleteModuleFromParent mod
deleteModuleFromParent = (mod) ->
###
Removes the given module from its parent's `.children`.
###
for child, i in mod.parent.children by -1 when child.id is mod.id
# Iterate backwards because of splice ^
mod.parent.children.splice i, 1
###
Calling `needless` on itself allows us to re-require needless in nested
modules, without having to worry about relative paths.
###
inception()