@@ -11,17 +11,43 @@ import { exec } from 'child_process';
1111
1212import Handlebars from 'handlebars' ;
1313import { promisify } from 'util' ;
14+ import { currentFileDir } from '../cli.js' ;
1415
1516const execAsync = promisify ( exec ) ;
1617
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+
1729function 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+ }
2347}
2448
49+ const getAdminforthVersion = detectAdminforthVersion ( ) ;
50+
2551
2652export function parseArgumentsIntoOptions ( rawArgs ) {
2753 const args = arg (
@@ -67,7 +93,6 @@ export async function promptForMissingOptions(options) {
6793 ...options ,
6894 appName : options . appName || answers . appName ,
6995 db : options . db || answers . db ,
70- adminforthVersion : detectAdminforthVersion ( ) ,
7196 } ;
7297}
7398
@@ -215,7 +240,7 @@ async function writeTemplateFiles(dirname, cwd, options) {
215240 dest : 'package.json' ,
216241 data : {
217242 appName,
218- adminforthVersion : options . adminforthVersion || 'latest'
243+ adminforthVersion : getAdminforthVersion ,
219244 } ,
220245 } ,
221246 {
0 commit comments