-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from JC-Coder/fix/refactor-code-struct
seperated creation of backend projects into different files
- Loading branch information
Showing
162 changed files
with
600 additions
and
472 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ dist | |
.env | ||
.idea | ||
.DS_Store | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/utils/create-frontend-project.js → src/core/create-frontend-project.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/core/create-projects-handlers/django/createDjangoProject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("-"); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/core/create-projects-handlers/expressjs/createExpressJsJavascript.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
); | ||
} | ||
} |
Oops, something went wrong.