@@ -34,21 +34,35 @@ var events;
3434var credentials ;
3535
3636var runtimePath ;
37- var package = readPkgUp . sync ( ) ;
38- if ( package . pkg . name === 'node-red' ) {
39- runtimePath = path . join ( process . cwd ( ) , package . pkg . main ) ;
40- initRuntime ( runtimePath ) ;
41- } else {
37+
38+ function findRuntimePath ( ) {
39+ const upPkg = readPkgUp . sync ( ) ;
40+ // case 1: we're in NR itself
41+ if ( upPkg . pkg . name === 'node-red' ) {
42+ return path . join ( path . dirname ( upPkg . path ) , upPkg . pkg . main ) ;
43+ }
44+ // case 2: NR is resolvable from here
4245 try {
43- runtimePath = require . resolve ( 'node-red' ) ;
44- initRuntime ( runtimePath ) ;
45- } catch ( err ) {
46- // no runtime path - init must be called from test
46+ return require . resolve ( 'node-red' ) ;
47+ } catch ( ignored ) { }
48+ // case 3: NR is installed alongside node-red-node-test-helper
49+ if ( ( upPkg . pkg . dependencies && upPkg . pkg . dependencies [ 'node-red' ] ) ||
50+ ( upPkg . pkg . devDependencies && upPkg . pkg . devDependencies [ 'node-red' ] ) ) {
51+ const dirpath = path . join ( path . dirname ( upPkg . path ) , 'node_modules' , 'node-red' ) ;
52+ try {
53+ const pkg = require ( path . join ( dirpath , 'package.json' ) ) ;
54+ return path . join ( dirpath , pkg . main ) ;
55+ } catch ( ignored ) { }
4756 }
4857}
4958
5059function initRuntime ( requirePath ) {
5160
61+ requirePath = requirePath || findRuntimePath ( ) ;
62+ if ( ! requirePath ) {
63+ return ;
64+ }
65+
5266 try {
5367 RED = require ( requirePath ) ;
5468
@@ -57,18 +71,18 @@ function initRuntime(requirePath) {
5771 events = RED . events ;
5872 log = RED . log ;
5973
60- // access internal Node-RED runtime methods
61- var prefix = requirePath . substring ( 0 , requirePath . indexOf ( 'red.js' ) ) ;
62- context = require ( prefix + " runtime/ nodes/ context" ) ;
63- comms = require ( prefix + " api/ editor/ comms" ) ;
64- credentials = require ( prefix + " runtime/ nodes/ credentials" ) ;
74+ // access some private/ internal Node-RED runtime
75+ const prefix = path . dirname ( requirePath ) ;
76+ context = require ( path . join ( prefix , ' runtime' , ' nodes' , ' context' ) ) ;
77+ comms = require ( path . join ( prefix , ' api' , ' editor' , ' comms' ) ) ;
78+ credentials = require ( path . join ( prefix , ' runtime' , ' nodes' , ' credentials' ) ) ;
6579
6680 } catch ( err ) {
6781 // ignore, assume init will be called again by a test script supplying the runtime path
6882 }
6983}
7084
71- initRuntime ( runtimePath ) ;
85+ initRuntime ( ) ;
7286
7387var app = express ( ) ;
7488
0 commit comments