Skip to content

Commit

Permalink
feat: changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jooherrera committed Jan 24, 2024
1 parent 69e9f78 commit e936b54
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 114 deletions.
42 changes: 33 additions & 9 deletions bin/createProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import createPackageJson from '../lib/createProject/package.js';
import setEvnVar from '../lib/createProject/setEnvVar.js';
import simpleGit from 'simple-git';
import fs from 'fs-extra';
import { join } from 'path';
import { dirname, join, resolve } from 'path';
import { fileURLToPath } from 'url';
import copyTemplatesFiles from '../lib/createProject/copyTemplateFolder.js';

const createProject = async() =>
{
Expand All @@ -37,14 +39,22 @@ const createProject = async() =>
{
answers.projectName = answers.projectName.trim().replaceAll(' ', '_').replaceAll('-', '_');

const tmpDir = join(process.cwd(),'.tmp')
const tmpDirName = ".tmp"
const tmpDir = join(process.cwd(),tmpDirName)
const cli_templates_dir = resolve(dirname(fileURLToPath(import.meta.url)), '../templates');

console.log(answers);
const tasks = new Listr([
{
{
title: 'Clone Node Experience',
task: async() =>
{
const exists = await fs.pathExists(tmpDir);
if(!exists)
{
await fs.mkdirs(tmpDirName)
}

const success = await fs.pathExists(tmpDir + '/node-experience');

if (!success)
Expand All @@ -61,6 +71,13 @@ const createProject = async() =>
}
}
},
{
title: 'Copy templates',
task: () =>
{
copyTemplatesFiles(cli_templates_dir, tmpDir);
}
},
{
title: 'Initialization',
task: async() =>
Expand All @@ -72,28 +89,28 @@ const createProject = async() =>
title: 'Copy Domain Files',
task: async() =>
{
await copyDomainFiles(answers, './');
await copyDomainFiles(answers, tmpDir);
}
},
{
title: 'Clean Domain Files',
task: async() =>
{
await cleanDomains(answers, './');
await cleanDomains(answers, tmpDir);
}
},
{
title: 'Copy Index Files',
task: async() =>
{
await copyIndexFiles(answers, './');
await copyIndexFiles(answers, tmpDir);
}
},
{
title: 'Copy Root Files',
task: async() =>
{
await copyRootFiles(answers, './');
await copyRootFiles(answers, tmpDir);
}
},
{
Expand All @@ -107,9 +124,16 @@ const createProject = async() =>
title: 'Create Package JSON',
task: async() =>
{
await createPackageJson(answers, './');
await createPackageJson(answers, tmpDir);
}
}
},
{
title: 'Clean init setup',
task: async() =>
{
await fs.remove(tmpDir)
}
}
]);

tasks.run().catch(err =>
Expand Down
1 change: 1 addition & 0 deletions lib/createProject/copyIndexFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const copyIndexFiles = async(vars, rootPath) =>
await copyTemplate(vars, join(rootPath, '/node-experience/src/initTestServer.ts'), rootDestinyPath);
await copyTemplate(vars, join(rootPath, '/node-experience/src/seed.ts'), rootDestinyPath);
await copyTemplate(vars, join(rootPath, 'templates/indexFiles/Shared/DI/container.ts'), `${rootDestinyPath}/Shared/DI/`);
await copyTemplate(vars, join(rootPath, 'templates/indexFiles/Config/validateEnv.ts'), `${rootDestinyPath}/Config/`);
};

export default copyIndexFiles;
1 change: 1 addition & 0 deletions lib/createProject/copyRootFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const copyRootFiles = async(vars, rootPath) =>
await copyTemplate(vars, join(rootPath, '/node-experience/.dependency-cruiser.js'), rootDestinyPath);
await copyTemplate(vars, join(rootPath, '/node-experience/artillery.yml'), rootDestinyPath);
await copyTemplate(vars, join(rootPath, 'templates/rootFiles/docker-compose.yml'), rootDestinyPath);
await copyTemplate(vars, join(rootPath, 'templates/rootFiles/.env.dev'), rootDestinyPath);
};

export default copyRootFiles;
7 changes: 7 additions & 0 deletions lib/createProject/copyTemplateFolder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { execSync } from 'child_process';
const copyTemplatesFiles = (from, dest) =>
{
execSync(`cp -r ${from} ${dest}`);
};

export default copyTemplatesFiles;
7 changes: 4 additions & 3 deletions lib/createProject/setEnvVar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const setEvnVar = async(vars) =>
{
const rootDestinyPath = join(resolve(), vars.projectName);
const port = vars.orm === 'Mongoose' ? '27017' : '5432';
await myReplace('DB_URI=mongodb://experience:experience@db:27017/experience', `DB_URI=mongodb://${vars.projectName}:${vars.projectName}123@db:27017/db_${vars.projectName}`, rootDestinyPath);
await myReplace('DB_URI=mongodb://experience:experience@db:27017/experience', `DB_URI=mongodb://${vars.projectName}:${vars.projectName}123@db:${port}/db_${vars.projectName}`, rootDestinyPath);

await myReplace('DB_USER=experience', `DB_USER=${vars.projectName}`, rootDestinyPath);
await myReplace('DB_DATABASE=experience', `DB_DATABASE=db_${vars.projectName}`, rootDestinyPath);
await myReplace('DB_PASSWORD=experience', `DB_PASSWORD=${vars.projectName}123`, rootDestinyPath);
await myReplace('DB_PORT=27017', `DB_PORT=${port}`, rootDestinyPath);
await myReplace('DB_ORM_DEFAULT=Mongoose', `DB_ORM_DEFAULT=${vars.orm}`, rootDestinyPath);
await myReplace('DB_PORT=5432', `DB_PORT=${port}`, rootDestinyPath);

await myReplace('DB_ORM_DEFAULT=', `DB_ORM_DEFAULT=${vars.orm}`, rootDestinyPath);
await myReplace('PRODUCT_NAME=nodeexperience', `PRODUCT_NAME=${vars.projectName}`, rootDestinyPath);
};

Expand Down
100 changes: 0 additions & 100 deletions lib/createProject/templates/rootFiles/docker-compose.yml

This file was deleted.

73 changes: 73 additions & 0 deletions templates/indexFiles/Config/validateEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { cleanEnv, str, port, bool, num, url } from 'envalid';

export function validateEnv()
{
return cleanEnv(process.env, {
NODE_ENV: str({ default: 'development' }),

APP_DEFAULT: str(),
APP_PATH: str(),
APP_PORT: port(),
APP_SET_APP_PROXY: bool(),
APP_SET_COOKIE_SECURE: bool(),
APP_SET_COOKIE_SAME_SITE: str(),
APP_CORS: str(),

AUTH_API_KEY: str(),
AUTH_HOST: str(),
AUTH_SECRET: str(),
AUTH_AUTHORIZATION: bool(),

CACHE_HOST: str({ default: 'redis' }),
CACHE_PORT: port({ default: 6379 }),
CACHE_USER: str({ default: 'experience' }),
CACHE_PASSWORD: str({ default: '12345678' }),
CACHE_ENABLE: bool({ default: false }),

{{#ifEquals orm "Mongoose" }}
DB_URI: str(),
{{/ifEquals}}
{{#ifEquals orm "MikroORM" }}
DB_HOST: str(),
DB_USER: str(),
DB_DATABASE: str(),
DB_PASSWORD: str(),
DB_PORT: str(),
DB_TYPE: str(),
{{/ifEquals}}

DB_ORM_DEFAULT: str(),

MINIO_HOST: str(),
MINIO_ACCESS_KEY: str(),
MINIO_SECRET_KEY: str(),
MINIO_USE_SSL: bool(),
MINIO_PORT: port(),
MINIO_PUBLIC_BUCKET: str(),
MINIO_PRIVATE_BUCKET: str(),
MINIO_REGION: str(),
FILESYSTEM_DEFAULT: str(),

TZ: str(),

JWT_SECRET: str(),
JWT_EXPIRES: num(),
JWT_ISS: str(),
JWT_AUD: str(),

SMTP_HOST: str(),
SMTP_PORT: num(),
SMTP_SECURE_SSL: bool(),
SMTP_SENDER_NAME: str(),
SMTP_SENDER_EMAIL_DEFAULT: str(),

URL_API: url(),
URL_WEB: url(),

PRODUCT_NAME: str(),
ENCRYPTION_DEFAULT: str(),
PUSH_PRIVATE_KEY: str(),
PUSH_PUBLIC_KEY: str(),
EXECUTE_CRONS: bool()
});
}
5 changes: 3 additions & 2 deletions templates/indexFiles/Shared/DI/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ container.register<IItemRepository>(REPOSITORIES.IItemRepository, { useFactory:
container.register<INotificationRepository<INotificationDomain>>(REPOSITORIES.INotificationRepository, { useClass: NotificationMongooseRepository }, { lifecycle: Lifecycle.Singleton });
{{/ifEquals}}

{{#ifEquals orm "Mongoose" }}
{{#ifEquals orm "MikroORM" }}
container.register<IItemRepository>(REPOSITORIES.IItemRepository, { useClass: ItemMikroORMRepository }, { lifecycle: Lifecycle.Singleton });
container.register<IAuthRepository>(REPOSITORIES.IAuthRepository, { useClass: AuthSupabaseRepository }, { lifecycle: Lifecycle.Singleton });
{{/ifEquals}}
container.register<IAuthRepository>(REPOSITORIES.IAuthRepository, { useClass: AuthSupabaseRepository }, { lifecycle: Lifecycle.Singleton });

// Shared
container.register<IEncryption>(FACTORIES.Md5EncryptionStrategy, { useClass: Md5EncryptionStrategy }, { lifecycle: Lifecycle.Singleton });

Expand Down
Loading

0 comments on commit e936b54

Please sign in to comment.