Skip to content

Commit

Permalink
Merge pull request #16 from fahimahammed/stage
Browse files Browse the repository at this point in the history
updated
  • Loading branch information
fahimahammed authored Jun 24, 2024
2 parents 5100e2f + f32e754 commit 774fe6c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
51 changes: 24 additions & 27 deletions dist/documentationGenerator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
// src/documentationGenerator.ts
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand All @@ -17,13 +9,24 @@ const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const PRISMA_SCHEMA_PATH = path_1.default.join(process.cwd(), 'prisma', 'schema.prisma');
const OUTPUT_DOC_PATH = path_1.default.join(process.cwd(), 'prisma-docs.md');
const parseSchema = () => __awaiter(void 0, void 0, void 0, function* () {
const schema = yield fs_1.promises.readFile(PRISMA_SCHEMA_PATH, 'utf-8');
const generateDocumentation = async () => {
try {
const schema = await fs_1.promises.readFile(PRISMA_SCHEMA_PATH, 'utf-8');
const models = parseModels(schema);
const enums = parseEnums(schema);
const documentation = generateMarkdown(models, enums);
await fs_1.promises.writeFile(OUTPUT_DOC_PATH, documentation);
console.log(`Documentation generated at ${OUTPUT_DOC_PATH}`);
}
catch (error) {
console.error('Error generating documentation:', error);
}
};
exports.generateDocumentation = generateDocumentation;
const parseModels = (schema) => {
const modelRegex = /model (\w+) \{([^}]+)\}/g;
const fieldRegex = /(\w+)\s+(\w+)(\[\])?(!)?(\s*@unique)?/g;
const enumRegex = /enum (\w+) \{([^}]+)\}/g;
const models = [];
const enums = [];
let match;
while ((match = modelRegex.exec(schema)) !== null) {
const modelName = match[1];
Expand All @@ -42,15 +45,20 @@ const parseSchema = () => __awaiter(void 0, void 0, void 0, function* () {
}
models.push({ name: modelName, fields });
}
// Parse enums
return models;
};
const parseEnums = (schema) => {
const enumRegex = /enum (\w+) \{([^}]+)\}/g;
const enums = [];
let match;
while ((match = enumRegex.exec(schema)) !== null) {
const enumName = match[1];
const valuesBlock = match[2].trim();
const enumValues = valuesBlock.split('\n').map(value => value.trim().replace(',', ''));
enums.push({ name: enumName, values: enumValues });
}
return generateMarkdown(models, enums);
});
return enums;
};
const generateMarkdown = (models, enums) => {
let markdown = '# Prisma Schema Documentation\n\n';
// Generate documentation for models
Expand Down Expand Up @@ -79,14 +87,3 @@ const generateMarkdown = (models, enums) => {
}
return markdown;
};
const generateDocumentation = () => __awaiter(void 0, void 0, void 0, function* () {
try {
const documentation = yield parseSchema();
yield fs_1.promises.writeFile(OUTPUT_DOC_PATH, documentation);
console.log(`Documentation generated at ${OUTPUT_DOC_PATH}`);
}
catch (error) {
console.error('Error generating documentation:', error);
}
});
exports.generateDocumentation = generateDocumentation;
12 changes: 10 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
"use strict";
// src/index.ts
Object.defineProperty(exports, "__esModule", { value: true });
const documentationGenerator_1 = require("./documentationGenerator");
function main() {
(0, documentationGenerator_1.generateDocumentation)();
async function main() {
try {
await (0, documentationGenerator_1.generateDocumentation)();
console.log('Documentation generation completed.');
}
catch (error) {
console.error('Error generating documentation:', error);
process.exit(1); // Exit with a non-zero code to indicate failure
}
}
main();
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prismadocify",
"version": "1.0.9",
"version": "1.0.0",
"description": "Generate documentation for Prisma schemas.",
"main": "dist/index.js",
"author": "Fahim Ahammed Firoz",
Expand All @@ -9,10 +9,8 @@
"dev": "ts-node src/index.ts",
"start": "node dist/index.js",
"build": "tsc",
"test": "echo \"No tests specified\""
},
"bin": {
"prismadocify": "./dist/index.js"
"test": "echo \"No tests specified\"",
"prismadocify": "node dist/index.js"
},
"keywords": [
"Prisma",
Expand All @@ -22,6 +20,9 @@
"files": [
"dist"
],
"bin": {
"prismadocify": "./dist/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/fahimahammed/prismadocify"
Expand All @@ -39,4 +40,4 @@
"@prisma/client": "^5.15.1",
"prisma": "^5.15.1"
}
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";
// src/index.ts

import { generateDocumentation } from './documentationGenerator';

Expand Down

0 comments on commit 774fe6c

Please sign in to comment.