-
Notifications
You must be signed in to change notification settings - Fork 13
/
node_modules_self_reference.js
152 lines (117 loc) · 5.24 KB
/
node_modules_self_reference.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
node_modules_self_reference.js
The MIT License (MIT)
Copyright (c) 2018-2018, Reactive Sets
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
"use strict";
/*
Adds or removes a symbolic link from node_modules/<current package name>
to the current working directory.
This helps testing by providing a reference to the current package
name that can be used using require( '<your package name>' ).
For this to work, the current directory should contain a valid
package.json file with its "name" attribute string set.
To run the script automatically, which is required to work with
continuous integration sites such as Travis.ci, install this
in package.json into the section scripts/postinstall, and to
prevent recursions with npm link also in scripts/preinstall,
the following way:
"scripts": {
"preinstall" : "node ./node_modules_self_reference.js remove",
"postinstall": "node ./node_modules_self_reference.js add"
}
This also works on Windows and Cygwin and allows to run npm link
on Windows and Cygwin.
For this to work on Windows and Cygwin you need to run
your shell "as administrator", otherwise you will get a permission
error (EPERM) that could look like this
{ [Error: EPERM: operation not permitted, symlink '..' -> 'C:\cygwin64\home\User\toubkal\node_modules\<you package name>']
errno: -4048,
code: 'EPERM',
syscall: 'symlink',
path: '..',
dest: 'C:\\cygwin64\\home\\User\\toubkal\\node_modules\\<you package name>' }
Or while running npm link:
npm ERR! Error: EPERM: operation not permitted, symlink 'C:\cygwin64\home\User\<your cwd>' -> 'C:\Program Files\nodejs\node_modules\<your module name>'
npm ERR! at Error (native)
npm ERR! { [Error: EPERM: operation not permitted, symlink 'C:\cygwin64\home\User\<your cwd>' -> 'C:\Program Files\nodejs\node_modules\<your module name>']
npm ERR! errno: -4048,
npm ERR! code: 'EPERM',
npm ERR! syscall: 'symlink',
npm ERR! path: 'C:\\cygwin64\\home\\User\\<your cwd>',
npm ERR! dest: 'C:\\Program Files\\nodejs\\node_modules\\<your module name>' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
*/
var fs = require( 'fs' )
, path = require( 'path' )
, name = require( './package.json' ).name
, node_modules = 'node_modules'
, self_link = path.join( node_modules, name )
, self_target = '..'
, argv = process.argv
, argv_1 = argv[ 1 ].split( path.sep )
, parent_dir = argv_1.slice( -3, -2 )
, action = argv[ 2 ] || 'show'
, log = console.log.bind( null, argv_1.slice( -1 )[ 0 ] + ', package:', name + ', action:', action + ',' )
, link_exists = fs.existsSync( self_link )
;
if ( [ 'show', 'add', 'remove' ].indexOf( action ) == -1 ) {
console.log(
"Usage:\n" +
"node node_modules_self_reference.js <action>\n" +
"\n" +
"Where <action> is one of \"show\" (default), \"add\" or \"remove\"\n"
);
process.exit( 0 );
}
if ( action == 'show' ) {
log( 'link ' + ( link_exists ? 'exists' : 'does not exist' ) + ':', self_link );
process.exit( 0 );
}
if ( parent_dir == 'node_modules' ) {
log( 'no action because parent is node_modules' );
process.exit( 0 );
}
// Create node_modules directory if it does not exist
fs.existsSync( node_modules ) || fs.mkdirSync( node_modules );
switch( action ) {
case 'add':
if ( link_exists ) {
log( 'self link already exists:', self_link );
} else {
// only install link if not preinstall, which should typically be on postinstall
fs.symlink( self_target, self_link, 'dir', function( error ) {
if ( error ) {
console.error( error );
} else {
log( 'created self link:', self_link );
}
} )
}
break;
case 'remove':
if ( link_exists ) {
// remove link to prevent failure on dependencies installation through an infinite recursion
fs.unlinkSync( self_link );
log( 'removed self link:', self_link )
} else {
log( 'self link not yet created:', self_link );
}
break;
}