Skip to content

Commit

Permalink
Resource generator CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
arifszn committed Jul 1, 2024
1 parent e0bd501 commit 4096d44
Show file tree
Hide file tree
Showing 7 changed files with 1,168 additions and 31 deletions.
14 changes: 14 additions & 0 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { build } from 'gluegun';

const cli = build()
.brand('resource-generator')
.src(__dirname)
.plugins('./node_modules', {
matching: 'resource-generator-*',
hidden: true,
})
.help()
.version()
.create();

cli.run();
100 changes: 100 additions & 0 deletions commands/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { GluegunCommand } from 'gluegun';
import * as fs from 'fs-extra';
import * as path from 'path';
import { execSync } from 'child_process';

const command: GluegunCommand = {
name: 'generate',
description: 'Generate resource files',
run: async (toolbox) => {
const {
print: { info, success, error },
prompt,
strings,
} = toolbox;

// Prompt for resource name
const { resourceName } = await prompt.ask({
type: 'input',
name: 'resourceName',
message: 'Enter the resource name:',
initial: 'Article',
required: true,
});

const name = strings.pascalCase(resourceName); // UserAccount
const singularNameUpperCase = strings.singular(name);
const pluralizeNameUpperCase = strings.pluralize(name);
const singularNameLowerCase = strings.camelCase(name);
const pluralizeNameLowerCase = strings.camelCase(pluralizeNameUpperCase);

// Prompt for file types to generate
const { filesToGenerate } = await prompt.ask({
type: 'multiselect',
name: 'filesToGenerate',
message: 'Select the files to generate (Use the spacebar to select):',
choices: [
'entity',
'repository',
'service',
'controller',
'routes',
'DTO',
],
required: true,
});

if (filesToGenerate.length === 0) {
error('Please select at least one item to generate');
return;
}

const stubDir = path.join(__dirname, '..', 'stubs');
const outputDir = path.join(__dirname, '..', 'src');

// Array to store the paths of generated files
const generatedFiles: string[] = [];

for (const fileType of filesToGenerate) {
const stubPath = path.join(stubDir, `${fileType}.ts.stub`);
const outputPath = path.join(
outputDir,
`${fileType}s`,
`${singularNameLowerCase}.${fileType}.ts`,
);

try {
let content = await fs.readFile(stubPath, 'utf-8');
content = content
.replace(/Article/g, singularNameUpperCase)
.replace(/article/g, singularNameLowerCase)
.replace(/Articles/g, pluralizeNameUpperCase)
.replace(/articles/g, pluralizeNameLowerCase);

await fs.outputFile(outputPath, content);
generatedFiles.push(outputPath);
success(`Generated ${outputPath}`);
} catch (err) {
error(`Error generating ${fileType}: ${err.message}`);
}
}

// Run npm run lint:fix on the generated files
try {
const filesToLint = generatedFiles.join(' ');
execSync(`npm run lint:fix:file -- -- ${filesToLint}`, {
stdio: 'inherit',
});
success('Linting and fixing completed successfully');
} catch (error) {
error('Error running lint:fix');
console.error(error);
}

info('\nReminder:');
info('- Create a migration file for the new entity.');
info(`- Add the new route file to the index route (src/routes/index.ts).`);
},
};

module.exports = command;
Loading

0 comments on commit 4096d44

Please sign in to comment.