Skip to content

Commit

Permalink
Merge pull request #67 from JC-Coder/fix/refactor-code-struct
Browse files Browse the repository at this point in the history
seperated creation of backend projects into different files
  • Loading branch information
JC-Coder authored Jun 16, 2024
2 parents 23c8bca + 8619b99 commit eecb76c
Show file tree
Hide file tree
Showing 162 changed files with 600 additions and 472 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
.env
.idea
.DS_Store
.vscode
12 changes: 6 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import figlet from "figlet";
import { program } from "commander";
import chalk from "chalk";
import useGradient from "./src/utils/useGradient.js";
import { createBackendProject } from "./src/utils/create-backend-project.js";
import useGradient from "./src/core/utils/useGradient.js";
import { createBackendProject } from "./src/core/create-backend-project.js";
import {
promptBackendFramework,
promptDatabase,
Expand All @@ -15,10 +15,10 @@ import {
promptProjectName,
promptProjectStack,
promptDependenciesInstall
} from "./src/utils/prompts.js";
import { createFrontendProject } from "./src/utils/create-frontend-project.js";
import { validateProjectName } from "./src/utils/helper.js";
import { sendQueuedStats } from "./src/utils/stat.js";
} from "./src/core/prompts.js";
import { createFrontendProject } from "./src/core/create-frontend-project.js";
import { validateProjectName } from "./src/core/utils/helper.js";
import { sendQueuedStats } from "./src/core/stat.js";

const toolName = "StartEase";
const jsBackendStacks = ["expressjs", "nestjs"];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"axios": "^1.6.3",
"chalk": "^5.3.0",
"commander": "^11.0.0",
"dotenv": "^16.4.5",
"figlet": "^1.6.0",
"fs-extra": "^11.1.1",
"gradient-string": "^2.0.2",
Expand Down
82 changes: 82 additions & 0 deletions src/core/create-backend-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { addGitignore } from "./utils/file-manager.js";
import path from "path";
import ora from "ora";
import { processDependenciesInstall } from "./utils/helper.js";
import { sendStat } from "./stat.js";
import { errorHandler } from "./utils/errorHandler.js";
import { createExpressJsJavascriptProject } from "./create-projects-handlers/expressjs/createExpressJsJavascript.js";
import { createNestjsProject } from "./create-projects-handlers/nestjs/createNestjsProject.js";
import { createDjangoProject } from "./create-projects-handlers/django/createDjangoProject.js";

/**
* function to create backend projects
*/

export async function createBackendProject(
projectName,
framework,
database,
orm,
installDependencies,
) {
try {
const spinner = ora("Creating Project ...").start();

const destinationPath = path.join(
process.cwd(),
projectName ?? `project-starter-${framework}-template`,
);

switch (framework) {
case "expressjs":
await createExpressJsJavascriptProject(
projectName,
framework,
database,
orm,
destinationPath,
spinner,
);
break;
case "nestjs":
await createNestjsProject({
destinationPath,
database,
orm,
spinner,
});
break;
case "django":
await createDjangoProject({
projectName,
destinationPath,
database,
spinner,
});
break;
default:
spinner.fail("Invalid framework");
break;
}

addGitignore(framework, destinationPath);

// process dependencies install
if (installDependencies) {
spinner.succeed();
spinner.start("Installing dependencies ...");
await processDependenciesInstall(framework, destinationPath);
}

// success message
spinner.succeed();
spinner.succeed(
`Backend project created successfully! : ${destinationPath}`,
);

// send stat
await sendStat("startease", framework);
} catch (e) {
errorHandler.handleError(e);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addGitignore, copyFile, getTemplateDir } from "./file-manager.js";
import { addGitignore, copyFile, getTemplateDir } from "./utils/file-manager.js";
import path from "path";
import ora from "ora";
import { sendStat } from "./stat.js";
Expand Down
115 changes: 115 additions & 0 deletions src/core/create-projects-handlers/django/createDjangoProject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import {
copyFile,
getTemplateDir,
updateFileContent,
writeToFile,
} from "../../utils/file-manager.js";
import shell from "shelljs";
import { DJANGO_MANAGER } from "../../../templates/backend/django/base/manage.js";
import { DJANGO_WSGI } from "../../../templates/backend/django/base/wsgi.js";
import { DJANGO_ASGI } from "../../../templates/backend/django/base/asgi.js";
import { DJANGO_SETTINGS } from "../../../templates/backend/django/base/settings.js";
import { DJANGO_ENV_VARIABLES } from "../../../templates/backend/django/base/env.js";
import {
DJANGO_POSTGRES_SETUP,
DJANGO_SQLITE_SETUP,
} from "../../../templates/backend/django/base/database.js";

export async function createDjangoProject({
projectName,
destinationPath,
database,
spinner,
}) {
// django does not support some file namings so the name has to be parsed into a valid python identifier.
projectName = projectName.replaceAll(/[-. ]/g, "");

// copy django template to directory

copyFile(getTemplateDir("backend/django/django-temp"), destinationPath);

// rename project name in final source

shell.mv(
`${destinationPath}/django_boilerplate`,
`${destinationPath}/${projectName}`,
);

writeToFile(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES);

writeToFile(`${destinationPath}/manage.py`, DJANGO_MANAGER);

writeToFile(`${destinationPath}/${projectName}/settings.py`, DJANGO_SETTINGS);

writeToFile(`${destinationPath}/${projectName}/wsgi.py`, DJANGO_WSGI);

writeToFile(`${destinationPath}/${projectName}/asgi.py`, DJANGO_ASGI);

if (database && database !== "sqlite3") {
switch (database) {
case "postgresql":
updateFileContent(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
{
projectName,
DATABASE_IMPORT: "import dj_database_url",
DATABASE_SETUP: DJANGO_POSTGRES_SETUP,
},
);

updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
DATABASE_ENV:
"DATABASE_URL=postgres://username:password@localhost:5432",
});
break;
}
} else {
updateFileContent(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
{
projectName,
DATABASE_IMPORT: "",
DATABASE_SETUP: DJANGO_SQLITE_SETUP,
},
);

updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
DATABASE_ENV: "",
});
}

// add updates to django starter files

updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
});

updateFileContent(`${destinationPath}/manage.py`, DJANGO_MANAGER, {
projectName,
});

updateFileContent(`${destinationPath}/${projectName}/wsgi.py`, DJANGO_WSGI, {
projectName,
});

updateFileContent(`${destinationPath}/${projectName}/asgi.py`, DJANGO_ASGI, {
projectName,
});

if (shell.which("git")) {
// initialize git for the final source

spinner.succeed();
spinner.start("Initializing git ...");

shell.cd(`${destinationPath}`);
shell.exec(`git init`);
shell.exec(`git add .`);
shell.exec(`git commit -m "Initial commit"`);
shell.cd("-");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { ExpressJsPackageJsonTemplate } from "../../../templates/backend/expressjs/base/package-json.js";
import {
copyFile,
createAndUpdateFile,
createFolder,
getTemplateDir,
updateFileContent,
writeToFile,
} from "../../utils/file-manager.js";
import { EXPRESSJS_SERVER_TEMPLATE } from "../../../templates/backend/expressjs/base/server.js";
import {
ExpressJsMongodbMongooseConnectionTemplate,
ExpressJsMongoDbMongooseSampleSchema,
} from "../../../templates/backend/expressjs/base/database.js";
import { ExpressJsEnvironmentTemplate } from "../../../templates/backend/expressjs/base/config.js";

export async function createExpressJsJavascriptProject(
framework,
database,
orm,
destinationPath,
spinner,
) {
try {
let database_config = "";
let database_config_import = "";
let additional_environment_variables = "";
let packageJson = ExpressJsPackageJsonTemplate;

console.log({ framework, database, orm, destinationPath });

// copy expressjs template to directory
copyFile(
getTemplateDir("backend/expressjs/expressjs-temp"),
destinationPath,
);

console.log("check 1");

// add server.js file
writeToFile(`${destinationPath}/src/server.js`, EXPRESSJS_SERVER_TEMPLATE);

if (database) {
spinner.succeed();
spinner.start("Adding Database Module ...");

// create schema folder
createFolder(`${destinationPath}/src/modules/schemas`);

switch (database) {
case "mongodb":
switch (orm) {
case "mongoose":
default:
// create db config file
createAndUpdateFile(
`${destinationPath}/src/common/config/database.js`,
ExpressJsMongodbMongooseConnectionTemplate,
);

// create sample schema file
createAndUpdateFile(
`${destinationPath}/src/modules/schemas/sample.schema.js`,
ExpressJsMongoDbMongooseSampleSchema,
);

// update database config for server js file
database_config_import = `import { connectDb } from "./common/config/database.js";`;
database_config = ` connectDb()`;

// update packageJson
packageJson.dependencies = {
...packageJson.dependencies,
mongoose: "^7.5.2",
};

// update db config
additional_environment_variables += `DB: {
URL: process.env.DB_URL
}`;
}
}
}

// update server template
updateFileContent(
`${destinationPath}/src/server.js`,
EXPRESSJS_SERVER_TEMPLATE,
{
database_config,
database_config_import,
},
);

// add and update config file
updateFileContent(
`${destinationPath}/src/common/config/environment.js`,
ExpressJsEnvironmentTemplate,
{ additional_environment_variables },
);

// add package json file
createAndUpdateFile(
`${destinationPath}/package.json`,
JSON.stringify(ExpressJsPackageJsonTemplate, null, " "),
);
} catch (error) {
console.log(
`Error: There was an error creating the project, please try again`,
);
}
}
Loading

0 comments on commit eecb76c

Please sign in to comment.