Skip to content

Commit

Permalink
db-use-core fix cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcardle committed Dec 1, 2024
1 parent 9432185 commit 0d74a06
Show file tree
Hide file tree
Showing 7 changed files with 3,699 additions and 4,209 deletions.
2 changes: 1 addition & 1 deletion bin/django-builder
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node

require = require('esm')(module /*, options*/);
require('../packages/djangobuilder.io/src/cli').cli(process.argv);
require('../lib/djangobuilder-core/src/cli').cli(process.argv);
1 change: 1 addition & 0 deletions example-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "DjangoProject",
"description": "A Django Project with great potential",
"htmx": false,
"postgres": false,
"apps": [
{
"name": "DjangoApp1",
Expand Down
3 changes: 2 additions & 1 deletion lib/djangobuilder-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lint": "eslint .",
"fix-lint": "eslint . --fix",
"smoketest": "npx tsc && npx ts-node src/smoketest.ts",
"test": "jest ."
"test": "jest .",
"cli": "tsc -t es5 src/cli.ts --outDir dist --esModuleInterop && node dist/cli.js render"
}
}
69 changes: 69 additions & 0 deletions lib/djangobuilder-core/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env node
import DjangoProject, { DjangoApp, DjangoModel, DjangoField, DjangoRelationship } from './api';
import Renderer from './rendering';
import { DjangoVersion } from './types';

const args = process.argv.slice(2);

console.log("ARGS", args);

if (args.length === 0) {
console.error('No arguments provided.');
process.exit(1);
}

const command = args[0];

switch (command) {
case 'render':
if (args.length < 2) {
console.error('No template provided for rendering.');
process.exit(1);
}
if (args.length < 3) {
console.error('No outputfile provided.');
process.exit(1);
}
const input_file = args[1];
const output_file = args[2];
const projectData = JSON.parse(require('fs').readFileSync(input_file, 'utf8'));

const projectParams = {
channels: projectData.channels || false,
htmx: projectData.htmx || false,
postgres: projectData.postgres || false,
pillow: projectData.pillow || false,
}
const project = new DjangoProject(
projectData.name,
projectData.description,
projectData.version || DjangoVersion.DJANGO4,
projectParams,
);

for (const appData of projectData["apps"]) {

const djangoApp = new DjangoApp(project, appData.name);

const models = appData.models.map((modelData: any) => {
const model = new DjangoModel(djangoApp, modelData.name, false, []);
model.fields.forEach((field: any) => {
model.addField(field.name, field.type, field.options);
})
return model;

});

project.addApp(appData.name, models);
}
console.log("Created Project", project);
const renderer = new Renderer();
const tarballContent = renderer.tarballContent(project);
// write to file
require('fs').writeFileSync(output_file, tarballContent);
console.log(`Rendered project to ${output_file}`);
break;
default:
console.error(`Unknown command: ${command}`);
process.exit(1);
}
7 changes: 6 additions & 1 deletion lib/djangobuilder-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {"outDir": "dist"},
"compilerOptions": {
"rootDir": "src",
"moduleResolution": "node",
"outDir": "dist",
"skipLibCheck": true,
},
"include": ["src"],
"exclude": ["node_modules"],
}
4 changes: 3 additions & 1 deletion script/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

set -ex

./bin/django-builder example-project.json output.tar
cd lib/djangobuilder-core
yarn run cli ../../example-project.json ../../output.tar
cd ../..

tar -xvf output.tar

Expand Down
Loading

0 comments on commit 0d74a06

Please sign in to comment.