@@ -11,17 +11,43 @@ import { exec } from 'child_process';
11
11
12
12
import Handlebars from 'handlebars' ;
13
13
import { promisify } from 'util' ;
14
+ import { currentFileDir } from '../cli.js' ;
14
15
15
16
const execAsync = promisify ( exec ) ;
16
17
18
+
19
+ function getVersion ( ) {
20
+ const ADMIN_FORTH_ABSOLUTE_PATH = path . join ( currentFileDir ( import . meta. url ) , '..' , '..' ) ;
21
+
22
+ const package_json = JSON . parse ( fs . readFileSync ( path . join ( ADMIN_FORTH_ABSOLUTE_PATH , 'package.json' ) , 'utf8' ) ) ;
23
+
24
+ const ADMINFORTH_VERSION = package_json . version ;
25
+
26
+ return ADMINFORTH_VERSION ;
27
+ }
28
+
17
29
function detectAdminforthVersion ( ) {
18
- const userAgent = process . env . npm_config_user_agent || '' ;
19
- if ( userAgent . includes ( 'adminforth@next' ) ) {
20
- return 'next'
21
- } ;
22
- return 'main' ;
30
+ try {
31
+ const version = getVersion ( ) ;
32
+
33
+ if ( typeof version !== 'string' ) {
34
+ throw new Error ( 'Invalid version format' ) ;
35
+ }
36
+
37
+ if ( version . includes ( 'next' ) ) {
38
+ console . log ( "Installing next version of AdminForth" ) ;
39
+ return 'next' ;
40
+ }
41
+ console . log ( "Installing latest version of AdminForth" ) ;
42
+ return 'latest' ;
43
+ } catch ( err ) {
44
+ console . warn ( '⚠️ Could not detect AdminForth version, defaulting to "latest".' ) ;
45
+ return 'latest' ;
46
+ }
23
47
}
24
48
49
+ const getAdminforthVersion = detectAdminforthVersion ( ) ;
50
+
25
51
26
52
export function parseArgumentsIntoOptions ( rawArgs ) {
27
53
const args = arg (
@@ -67,7 +93,6 @@ export async function promptForMissingOptions(options) {
67
93
...options ,
68
94
appName : options . appName || answers . appName ,
69
95
db : options . db || answers . db ,
70
- adminforthVersion : detectAdminforthVersion ( ) ,
71
96
} ;
72
97
}
73
98
@@ -215,7 +240,7 @@ async function writeTemplateFiles(dirname, cwd, options) {
215
240
dest : 'package.json' ,
216
241
data : {
217
242
appName,
218
- adminforthVersion : options . adminforthVersion || 'latest'
243
+ adminforthVersion : getAdminforthVersion ,
219
244
} ,
220
245
} ,
221
246
{
0 commit comments