Skip to content

Commit 880e00b

Browse files
author
yaroslav8765
committed
fix: Updated getting of version
1 parent 51964cc commit 880e00b

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

adminforth/commands/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function showHelp() {
2525
);
2626
}
2727

28-
function currentFileDir(importMetaUrl) {
28+
export function currentFileDir(importMetaUrl) {
2929
const filePath = importMetaUrl.replace("file://", "");
3030
const fileDir = path.dirname(filePath);
3131
return fileDir;

adminforth/commands/createApp/utils.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,43 @@ import { exec } from 'child_process';
1111

1212
import Handlebars from 'handlebars';
1313
import { promisify } from 'util';
14+
import { currentFileDir } from '../cli.js';
1415

1516
const 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+
1729
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+
}
2347
}
2448

49+
const getAdminforthVersion = detectAdminforthVersion();
50+
2551

2652
export 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

Comments
 (0)