@@ -4,6 +4,7 @@ import { writeFileSync, readFileSync } from 'fs';
4
4
import { Types } from '@graphql-codegen/plugin-helpers' ;
5
5
import detectIndent from 'detect-indent' ;
6
6
import { Answers } from './types' ;
7
+ import getLatestVersion from 'latest-version' ;
7
8
8
9
// Parses config and writes it to a file
9
10
export async function writeConfig ( answers : Answers , config : Types . Config ) {
@@ -44,30 +45,17 @@ export async function writePackage(answers: Answers, configLocation: string) {
44
45
pkg . devDependencies = { } ;
45
46
}
46
47
47
- // read codegen's version
48
- let version : string ;
49
-
50
- const dynamicImport = ( m : string ) => import ( m ) . then ( m => ( 'default' in m ? m . default : m ) ) ;
51
- try {
52
- // Works in tests
53
- const packageJson = await dynamicImport ( '../../package.json' ) ;
54
- version = packageJson . version ;
55
- } catch ( e ) {
56
- // Works in production (package dist is flat, everything is in the same folder)
57
- const packageJson = await dynamicImport ( './package.json' ) ;
58
- version = packageJson . version ;
59
- }
60
-
61
- answers . plugins . forEach ( plugin => {
62
- pkg . devDependencies [ plugin . package ] = version ;
63
- } ) ;
48
+ await Promise . all (
49
+ answers . plugins . map ( async plugin => {
50
+ pkg . devDependencies [ plugin . package ] = await getLatestVersion ( plugin . package ) ;
51
+ } )
52
+ ) ;
64
53
65
54
if ( answers . introspection ) {
66
- pkg . devDependencies [ '@graphql-codegen/introspection' ] = version ;
55
+ pkg . devDependencies [ '@graphql-codegen/introspection' ] = await getLatestVersion ( '@graphql-codegen/introspection' ) ;
67
56
}
68
57
69
- // If cli haven't installed yet
70
- pkg . devDependencies [ '@graphql-codegen/cli' ] = version ;
58
+ pkg . devDependencies [ '@graphql-codegen/cli' ] = await getLatestVersion ( '@graphql-codegen/cli' ) ;
71
59
72
60
writeFileSync ( pkgPath , JSON . stringify ( pkg , null , indent ) ) ;
73
61
}
0 commit comments