Skip to content

Commit 6a5d5f3

Browse files
authored
Merge pull request #194 from devforth/create-cli
Create cli
2 parents fe1bff2 + b579484 commit 6a5d5f3

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

adminforth/commands/createApp/utils.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,33 @@ function generateDbUrlForPrisma(connectionString) {
9696
return connectionString.toString();
9797
}
9898

99-
function initialChecks() {
99+
function initialChecks(options) {
100100
return [
101101
{
102102
title: '👀 Checking Node.js version...',
103103
task: () => checkNodeVersion(20)
104104
},
105105
{
106106
title: '👀 Validating current working directory...',
107-
task: () => checkForExistingPackageJson()
107+
task: () => checkForExistingPackageJson(options)
108108
}
109109
]
110110
}
111111

112-
function checkForExistingPackageJson() {
113-
if (fs.existsSync(path.join(process.cwd(), 'package.json'))) {
112+
function checkForExistingPackageJson(options) {
113+
const projectDir = path.join(process.cwd(), options.appName);
114+
if (fs.existsSync(projectDir)) {
114115
throw new Error(
115-
`A package.json already exists in this directory.\n` +
116-
`Please remove it or use an empty directory.`
116+
`Directory "${options.appName}" already exists.\n` +
117+
`Please remove it or use a different name.`
117118
);
118119
}
119120
}
120121

121122
async function scaffoldProject(ctx, options, cwd) {
123+
const projectDir = path.join(cwd, options.appName);
124+
await fse.ensureDir(projectDir);
125+
122126
const connectionString = parseConnectionString(options.db);
123127
const provider = detectDbProvider(connectionString.protocol);
124128
const prismaDbUrl = generateDbUrlForPrisma(connectionString);
@@ -130,9 +134,9 @@ async function scaffoldProject(ctx, options, cwd) {
130134
const dirname = path.dirname(filename);
131135

132136
// Prepare directories
133-
ctx.customDir = path.join(cwd, 'custom');
137+
ctx.customDir = path.join(projectDir, 'custom');
134138
await fse.ensureDir(ctx.customDir);
135-
await fse.ensureDir(path.join(cwd, 'resources'));
139+
await fse.ensureDir(path.join(projectDir, 'resources'));
136140

137141
// Copy static assets to `custom/assets`
138142
const sourceAssetsDir = path.join(dirname, 'assets');
@@ -141,13 +145,14 @@ async function scaffoldProject(ctx, options, cwd) {
141145
await fse.copy(sourceAssetsDir, targetAssetsDir);
142146

143147
// Write templated files
144-
writeTemplateFiles(dirname, cwd, {
148+
writeTemplateFiles(dirname, projectDir, {
145149
dbUrl: connectionString.toString(),
146150
prismaDbUrl,
147151
appName,
148152
provider,
149153
});
150154

155+
return projectDir; // Return the new directory path
151156
}
152157

153158
async function writeTemplateFiles(dirname, cwd, options) {
@@ -241,9 +246,13 @@ async function installDependencies(ctx, cwd) {
241246
]);
242247
}
243248

244-
function generateFinalInstructions(skipPrismaSetup) {
249+
function generateFinalInstructions(skipPrismaSetup, options) {
245250
let instruction = '⏭️ Run the following commands to get started:\n';
246251
if (!skipPrismaSetup)
252+
instruction += `
253+
${chalk.dim('// Go to the project directory')}
254+
${chalk.cyan(`$ cd ${options.appName}`)}\n`;
255+
247256
instruction += `
248257
${chalk.dim('// Generate and apply initial migration')}
249258
${chalk.cyan('$ npm run makemigration -- --name init')}\n`;
@@ -272,33 +281,35 @@ export function prepareWorkflow(options) {
272281
title: '🔍 Initial checks...',
273282
task: (_, task) =>
274283
task.newListr(
275-
initialChecks(),
284+
initialChecks(options),
276285
{ concurrent: true },
277286
)
278287
},
279288
{
280289
title: '🚀 Scaffolding your project...',
281-
task: async (ctx) => scaffoldProject(ctx, options, cwd)
290+
task: async (ctx) => {
291+
ctx.projectDir = await scaffoldProject(ctx, options, cwd);
292+
}
282293
},
283294
{
284295
title: '📦 Installing dependencies...',
285-
task: async (ctx) => installDependencies(ctx, cwd)
296+
task: async (ctx) => installDependencies(ctx, ctx.projectDir)
286297
},
287298
{
288299
title: '📝 Preparing final instructions...',
289300
task: (ctx) => {
290-
console.log(chalk.green(`✅ Successfully created your new Adminforth project!\n`));
291-
console.log(generateFinalInstructions(ctx.skipPrismaSetup));
301+
console.log(chalk.green(`✅ Successfully created your new Adminforth project in ${ctx.projectDir}!\n`));
302+
console.log(generateFinalInstructions(ctx.skipPrismaSetup, options));
292303
console.log('\n\n');
304+
}
293305
}
294-
}],
306+
],
295307
{
296308
rendererOptions: {collapseSubtasks: false},
297309
concurrent: false,
298310
exitOnError: true,
299311
collectErrors: true,
300-
}
301-
);
312+
});
302313

303314
return tasks;
304315
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { IAdminForth, IHttpServer, AdminForthResourcePages, AdminForthResou
33
import type { PluginOptions } from './types.js';
44

55

6-
export default class ChatGptPlugin extends AdminForthPlugin {
6+
export default class {{pluginName}} extends AdminForthPlugin {
77
options: PluginOptions;
88

99
constructor(options: PluginOptions) {

0 commit comments

Comments
 (0)