Skip to content

Commit bb529a1

Browse files
authored
Merge pull request #268 from devforth/next
Next
2 parents ab2aa45 + 6e873e9 commit bb529a1

File tree

90 files changed

+1217
-311
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1217
-311
lines changed

adminforth/commands/cli.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import createResource from "./createResource/main.js";
1212
import chalk from "chalk";
1313
import path from "path";
1414
import fs from "fs";
15+
import { fileURLToPath } from 'url';
1516

1617
function showHelp() {
1718
console.log(
@@ -20,23 +21,30 @@ function showHelp() {
2021
chalk.green(' create-plugin') + chalk.white(' Create a plugin for your AdminForth app\n') +
2122
chalk.green(' generate-models') + chalk.white(' Generate TypeScript models from your databases\n') +
2223
chalk.green(' bundle') + chalk.white(' Bundles your AdminForth app SPA for production\n') +
23-
chalk.green(' component') + chalk.white(' Scaffold a custom Vue component\n')
24+
chalk.green(' component') + chalk.white(' Scaffold a custom Vue component\n') +
25+
chalk.green(' resource') + chalk.white(' Scaffold a custom resource\n')
2426
);
2527
}
2628

27-
function currentFileDir(importMetaUrl) {
28-
const filePath = importMetaUrl.replace("file://", "");
29+
export function currentFileDir(importMetaUrl) {
30+
const filePath = fileURLToPath(importMetaUrl);
2931
const fileDir = path.dirname(filePath);
3032
return fileDir;
3133
}
3234

33-
function showVersion() {
35+
export function getVersion() {
3436
const ADMIN_FORTH_ABSOLUTE_PATH = path.join(currentFileDir(import.meta.url), '..');
3537

3638
const package_json = JSON.parse(fs.readFileSync(path.join(ADMIN_FORTH_ABSOLUTE_PATH, 'package.json'), 'utf8'));
3739

3840
const ADMINFORTH_VERSION = package_json.version;
3941

42+
return ADMINFORTH_VERSION;
43+
}
44+
45+
function showVersion() {
46+
const ADMINFORTH_VERSION = getVersion();
47+
4048
console.log(
4149
chalk.white('AdminForth CLI version: ') +
4250
chalk.cyan.bold(ADMINFORTH_VERSION)

adminforth/commands/createApp/templates/custom/tsconfig.json.hbs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"compilerOptions": {
33
"baseUrl": ".",
44
"paths": {
5-
"@/": "../node_modules/adminforth/dist/spa/src/",
6-
"": "../node_modules/adminforth/dist/spa/node_modules/",
7-
"@@/*": "."
5+
"@/*": ["../node_modules/adminforth/dist/spa/src/*"],
6+
"@@/*": ["./*"]
87
}
98
}
109
}

adminforth/commands/createApp/templates/index.ts.hbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import AdminForth from 'adminforth';
33
import usersResource from "./resources/adminuser.js";
44
import { fileURLToPath } from 'url';
55
import path from 'path';
6-
6+
import dotenv from "dotenv";
7+
dotenv.config({ path: '.env.local', override: true });
8+
dotenv.config({ path: '.env', override: true });
9+
710
const ADMIN_BASE_URL = '';
811

912
export const admin = new AdminForth({

adminforth/commands/createApp/templates/package.json.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"dependencies": {
2424
"@dotenvx/dotenvx": "^1.34.0",
25-
"adminforth": "latest",
25+
"adminforth": "{{adminforthVersion}}",
2626
"express": "latest-4"
2727
},
2828
"devDependencies": {

adminforth/commands/createApp/utils.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,31 @@ import { exec } from 'child_process';
1111

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

1516
const execAsync = promisify(exec);
1617

18+
function detectAdminforthVersion() {
19+
try {
20+
const version = getVersion();
21+
22+
if (typeof version !== 'string') {
23+
throw new Error('Invalid version format');
24+
}
25+
26+
if (version.includes('next')) {
27+
return 'next';
28+
}
29+
return 'latest';
30+
} catch (err) {
31+
console.warn('⚠️ Could not detect AdminForth version, defaulting to "latest".');
32+
return 'latest';
33+
}
34+
}
35+
36+
const adminforthVersion = detectAdminforthVersion();
37+
38+
1739
export function parseArgumentsIntoOptions(rawArgs) {
1840
const args = arg(
1941
{
@@ -203,7 +225,10 @@ async function writeTemplateFiles(dirname, cwd, options) {
203225
{
204226
src: 'package.json.hbs',
205227
dest: 'package.json',
206-
data: { appName },
228+
data: {
229+
appName,
230+
adminforthVersion: adminforthVersion,
231+
},
207232
},
208233
{
209234
src: 'index.ts.hbs',
@@ -287,7 +312,7 @@ async function installDependencies(ctx, cwd) {
287312
const isWindows = process.platform === 'win32';
288313

289314
const nodeBinary = process.execPath;
290-
const npmPath = path.join(path.dirname(nodeBinary), 'npm');
315+
const npmPath = path.join(path.dirname(nodeBinary), isWindows ? 'npm.cmd' : 'npm');
291316
const customDir = ctx.customDir;
292317
if (isWindows) {
293318
const res = await Promise.all([

adminforth/dataConnectors/mongo.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,8 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
194194
if (!value) {
195195
return null;
196196
}
197-
if (field._underlineType == 'timestamp' || field._underlineType == 'int') {
198-
// value is iso string now, convert to unix timestamp
199-
return dayjs(value).unix();
200-
} else if (field._underlineType == 'varchar') {
201-
// value is iso string now, convert to unix timestamp
202-
return dayjs(value).toISOString();
203-
}
197+
return dayjs(value).toDate();
198+
204199
} else if (field.type == AdminForthDataTypes.BOOLEAN) {
205200
return value === null ? null : (value ? true : false);
206201
} else if (field.type == AdminForthDataTypes.DECIMAL) {

adminforth/documentation/blog/2024-08-05-chatgpt/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Today LLM is already a must tool to speed-up writing, brainstorming, or generati
1111

1212
Here is how it looks in action:
1313

14-
![alt text](../../docs/tutorial/05-Plugins/demoChatGpt.gif)
14+
![alt text](../../docs/tutorial/07-Plugins/demoChatGpt.gif)
1515

1616
<!-- truncate -->
1717

adminforth/documentation/docs/tutorial/001-gettingStarted.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ Open `index.ts` in your project root and import the new resource:
314314
```ts title="./index.ts"
315315
...
316316
//diff-add
317-
import apartmentsResource from "./resources/apartments";
317+
import apartmentsResource from "./resources/apartments.js";
318318

319319
...
320320
export const admin = new AdminForth({
@@ -409,7 +409,7 @@ async function seedDatabase() {
409409
//diff-add
410410
};
411411

412-
if (import.meta.url === `file://${process.argv[1]}`) {
412+
if (fileURLToPath(import.meta.url) === path.resolve(process.argv[1])) {
413413

414414
...
415415

adminforth/documentation/docs/tutorial/01-helloWorld.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ Create `index.ts` file in root directory with following content:
131131

132132
```ts title="./index.ts"
133133
import express from 'express';
134-
import AdminForth, { AdminForthDataTypes, AdminUser, Filters } from 'adminforth';
134+
import AdminForth, { AdminForthDataTypes, Filters } from 'adminforth';
135+
import type { AdminForthResourceInput, AdminForthResource, AdminUser } from 'adminforth';
135136

136137
export const admin = new AdminForth({
137138
baseUrl: '',
@@ -208,6 +209,26 @@ export const admin = new AdminForth({
208209
},
209210
{ name: 'passwordHash', backendOnly: true, showIn: { all: false } }
210211
],
212+
hooks: {
213+
create: {
214+
beforeSave: async ({ record, adminUser, resource }: { record: any, adminUser: AdminUser, resource: AdminForthResource }) => {
215+
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
216+
return { ok: true };
217+
}
218+
},
219+
edit: {
220+
beforeSave: async ({ oldRecord, updates, adminUser, resource }: { oldRecord: any, updates: any, adminUser: AdminUser, resource: AdminForthResource }) => {
221+
console.log('Updating user', updates);
222+
if (oldRecord.id === adminUser.dbUser.id && updates.role) {
223+
return { ok: false, error: 'You cannot change your own role' };
224+
}
225+
if (updates.password) {
226+
updates.password_hash = await AdminForth.Utils.generatePasswordHash(updates.password);
227+
}
228+
return { ok: true }
229+
},
230+
},
231+
}
211232
},
212233
{
213234
table: 'post',

adminforth/documentation/docs/tutorial/03-Customization/03-virtualColumns.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ columns: [
6767
resource: AdminForthResourceCommon;
6868
adminUser: AdminUser
6969
}>();
70-
71-
###
7270
7371
function getFlagEmojiFromIso(iso) {
7472
return iso?.toUpperCase()?.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397));
@@ -88,6 +86,7 @@ One way to do it is to actually add a real column to a table and then fill it ev
8886
For this purpose following changes will be required for apartments config:
8987
9088
```ts title='./resources/apartments.ts'
89+
import { Filters } from "adminforth";
9190
...
9291
resourceId: 'aparts',
9392
...

0 commit comments

Comments
 (0)