88const fs = require ( 'fs-extra' )
99const glob = require ( 'it-glob' )
1010
11- const findHttpClientPkg = ( targetDir ) => {
12- const location = require . resolve ( 'ipfs-http-client' , {
13- paths : [
14- targetDir
15- ]
16- } )
17-
18- return require ( location . replace ( 'src/index.js' , 'package.json' ) )
19- }
20-
2111const isDep = ( name , pkg ) => {
2212 return Object . keys ( pkg . dependencies || { } ) . filter ( dep => dep === name ) . pop ( )
2313}
@@ -47,6 +37,16 @@ const hasYarnLock = (targetDir) => {
4737 return fs . existsSync ( path . join ( targetDir , 'yarn.lock' ) )
4838}
4939
40+ const printFailureMessage = ( message ) => {
41+ console . info ( '-------------------------------------------------------------------' ) // eslint-disable-line no-console
42+ console . info ( '' ) // eslint-disable-line no-console
43+ console . info ( message ) // eslint-disable-line no-console
44+ console . info ( '' ) // eslint-disable-line no-console
45+ console . info ( 'Dependant project has not been tested with updated dependencies' ) // eslint-disable-line no-console
46+ console . info ( '' ) // eslint-disable-line no-console
47+ console . info ( '-------------------------------------------------------------------' ) // eslint-disable-line no-console
48+ }
49+
5050const installDependencies = async ( targetDir ) => {
5151 console . info ( 'Installing dependencies' ) // eslint-disable-line no-console
5252 if ( hasYarnLock ( targetDir ) ) {
@@ -64,46 +64,56 @@ const installDependencies = async (targetDir) => {
6464 }
6565}
6666
67- const linkIPFSInDir = async ( targetDir , ipfsDir , ipfsPkg , httpClientPkg ) => {
67+ const upgradeDependenciesInDir = async ( targetDir , deps ) => {
6868 const modulePkgPath = path . join ( targetDir , 'package.json' )
6969 const modulePkg = require ( modulePkgPath )
7070
71- // if the project also depends on the http client, upgrade it to the same version we are using
72- if ( dependsOn ( 'ipfs-http-client' , modulePkg ) ) {
73- console . info ( 'Upgrading ipfs-http-client dependency to' , httpClientPkg . version ) // eslint-disable-line no-console
74- await exec ( 'npm' , [ 'install' , `ipfs-http-client@${ httpClientPkg . version } ` ] , {
75- cwd : targetDir
76- } )
71+ modulePkg . dependencies = modulePkg . dependencies || { }
72+ modulePkg . peerDependencies = modulePkg . peerDependencies || { }
73+ modulePkg . optionalDependencies = modulePkg . optionalDependencies || { }
74+ modulePkg . devDependencies = modulePkg . devDependencies || { }
75+
76+ console . info ( 'Upgrading deps' ) // eslint-disable-line no-console
77+
78+ for ( const dep of Object . keys ( deps ) ) {
79+ const existingVersion = modulePkg . dependencies [ dep ] || modulePkg . peerDependencies [ dep ] || modulePkg . optionalDependencies [ dep ] || modulePkg . devDependencies [ dep ]
80+ console . info ( 'Upgrading' , dep , 'from version' , existingVersion , 'to' , deps [ dep ] ) // eslint-disable-line no-console
81+
82+ if ( modulePkg . dependencies [ dep ] || modulePkg . peerDependencies [ dep ] || modulePkg . optionalDependencies [ dep ] ) {
83+ modulePkg . dependencies [ dep ] = deps [ dep ]
84+ } else if ( modulePkg . devDependencies [ dep ] ) {
85+ modulePkg . devDependencies [ dep ] = deps [ dep ]
86+ }
7787 }
7888
79- console . info ( `Linking ipfs@${ ipfsPkg . version } in dir ${ targetDir } ` ) // eslint-disable-line no-console
80- await exec ( 'npx' , [ 'connect-deps' , 'link' , path . relative ( await fs . realpath ( targetDir ) , await fs . realpath ( ipfsDir ) ) ] , {
81- cwd : targetDir
82- } )
83- await exec ( 'npx' , [ 'connect-deps' , 'connect' ] , {
89+ await fs . writeFile ( modulePkgPath , JSON . stringify ( modulePkg , null , 2 ) )
90+
91+ await exec ( 'npm' , [ 'install' ] , {
8492 cwd : targetDir
8593 } )
8694}
8795
88- const testModule = async ( targetDir , ipfsDir , ipfsPkg , httpClientPkg ) => {
96+ const testModule = async ( targetDir , deps ) => {
8997 const pkgPath = path . join ( targetDir , 'package.json' )
9098
9199 if ( ! fs . existsSync ( pkgPath ) ) {
92- console . info ( `No package.json found at ${ pkgPath } ` ) // eslint-disable-line no-console
100+ printFailureMessage ( `No package.json found at ${ pkgPath } ` )
93101
94102 return
95103 }
96104
97105 const modulePkg = require ( pkgPath )
98106
99- if ( ! dependsOn ( 'ipfs' , modulePkg ) && ! dependsOn ( 'ipfs-http-client' , modulePkg ) ) {
100- console . info ( `Module ${ modulePkg . name } does not depend on IPFS or the IPFS HTTP Client` ) // eslint-disable-line no-console
107+ for ( const dep of Object . keys ( deps ) ) {
108+ if ( ! dependsOn ( dep , modulePkg ) ) {
109+ printFailureMessage ( `Module ${ modulePkg . name } does not depend on ${ dep } ` )
101110
102- return
111+ return
112+ }
103113 }
104114
105115 if ( ! modulePkg . scripts || ! modulePkg . scripts . test ) {
106- console . info ( `Module ${ modulePkg . name } has no tests` ) // eslint-disable-line no-console
116+ printFailureMessage ( `Module ${ modulePkg . name } has no tests` )
107117
108118 return
109119 }
@@ -114,26 +124,26 @@ const testModule = async (targetDir, ipfsDir, ipfsPkg, httpClientPkg) => {
114124 cwd : targetDir
115125 } )
116126 } catch ( err ) {
117- console . info ( `Failed to run the tests of ${ modulePkg . name } , aborting` , err . message ) // eslint-disable-line no-console
127+ printFailureMessage ( `Failed to run the tests of ${ modulePkg . name } , aborting` , err . message )
118128
119129 return
120130 }
121131
122- // upgrade IPFS to the rc
123- await linkIPFSInDir ( targetDir , ipfsDir , ipfsPkg , httpClientPkg )
132+ // upgrade passed dependencies
133+ await upgradeDependenciesInDir ( targetDir , deps )
124134
125- // run the tests with the new IPFS/IPFSHTTPClient
135+ // run the tests with the new deps
126136 await exec ( 'npm' , [ 'test' ] , {
127137 cwd : targetDir
128138 } )
129139}
130140
131- const testRepo = async ( targetDir , ipfsDir , ipfsPkg , httpClientPkg ) => {
141+ const testRepo = async ( targetDir , deps ) => {
132142 await installDependencies ( targetDir )
133- await testModule ( targetDir , ipfsDir , ipfsPkg , httpClientPkg )
143+ await testModule ( targetDir , deps )
134144}
135145
136- const testMonoRepo = async ( targetDir , ipfsDir , ipfsPkg , httpClientPkg ) => {
146+ const testMonoRepo = async ( targetDir , deps ) => {
137147 await installDependencies ( targetDir )
138148
139149 let lerna = path . join ( 'node_modules' , '.bin' , 'lerna' )
@@ -163,17 +173,13 @@ const testMonoRepo = async (targetDir, ipfsDir, ipfsPkg, httpClientPkg) => {
163173 // test each package that depends on ipfs/http client
164174 for ( const pattern of packages ) {
165175 for await ( const match of glob ( targetDir , pattern ) ) {
166- await testModule ( path . join ( targetDir , match ) , ipfsDir , ipfsPkg , httpClientPkg )
176+ await testModule ( path . join ( targetDir , match ) , deps )
167177 }
168178 }
169179}
170180
171- async function testExternal ( opts ) {
172- // work out our versions
173- const ipfsDir = process . cwd ( )
174- const ipfsPkg = require ( path . join ( ipfsDir , 'package.json' ) )
175- const httpClientPkg = findHttpClientPkg ( ipfsDir )
176- const targetDir = path . join ( os . tmpdir ( ) , `${ opts . name } -${ Date . now ( ) } ` )
181+ async function testDependant ( opts ) {
182+ const targetDir = path . join ( os . tmpdir ( ) , `test-dependant-${ Date . now ( ) } ` )
177183
178184 console . info ( `Cloning ${ opts . repo } into ${ targetDir } ` ) // eslint-disable-line no-console
179185 await exec ( 'git' , [ 'clone' , opts . repo , targetDir ] , {
@@ -187,13 +193,13 @@ async function testExternal (opts) {
187193 }
188194
189195 if ( isMonoRepo ( targetDir ) ) {
190- await testMonoRepo ( targetDir , ipfsDir , ipfsPkg , httpClientPkg )
196+ await testMonoRepo ( targetDir , opts . deps )
191197 } else {
192- await testRepo ( targetDir , ipfsDir , ipfsPkg , httpClientPkg )
198+ await testRepo ( targetDir , opts . deps )
193199 }
194200
195201 console . info ( `Removing ${ targetDir } ` ) // eslint-disable-line no-console
196202 await fs . remove ( targetDir )
197203}
198204
199- module . exports = testExternal
205+ module . exports = testDependant
0 commit comments