Skip to content

Commit

Permalink
Merge pull request #21 from DhiWise/feature/build-overwrite
Browse files Browse the repository at this point in the history
Overwrite the application in storage if the application has more than one build
  • Loading branch information
saloni-dhiwise authored Jan 11, 2022
2 parents 14026f2 + 9179f57 commit 2f63219
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 21 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.1.0](https://github.com/DhiWise/nodejs-code-generator/compare/v1.0.0...v1.1.0) (2022-01-11)


### Bug Fixes

* **eslint formatting:** resolved eslint errors ([2c71bf2](https://github.com/DhiWise/nodejs-code-generator/commit/2c71bf2661c5e1599de3cb242b5431dce8720137))


### Features

* add confirmation popup when second time build application ([a7fe3c0](https://github.com/DhiWise/nodejs-code-generator/commit/a7fe3c001fab1972a8f2f151feb6deb18220c3e5)), closes [#20](https://github.com/DhiWise/nodejs-code-generator/issues/20)
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packages/*"
],
"npmClient": "npm",
"version": "1.0.0",
"version": "1.1.0",
"lerna": "4.0.0",
"command": {
"publish": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"release_patch": "lerna publish patch --conventional-commits && conventional-github-releaser --preset angular",
"release_premajor": "lerna publish premajor --conventional-commits && conventional-github-releaser --preset angular",
"release": "lerna publish --conventional-commits && conventional-github-releaser --preset angular",
"lint": "eslint ."
"lint": "eslint . --ext .js --fix"
},
"jest": {
"modulePathIgnorePatterns": [
Expand Down
11 changes: 11 additions & 0 deletions packages/client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.1.0](https://github.com/DhiWise/nodejs-code-generator/compare/v1.0.0...v1.1.0) (2022-01-11)


### Features

* add confirmation popup when second time build application ([a7fe3c0](https://github.com/DhiWise/nodejs-code-generator/commit/a7fe3c001fab1972a8f2f151feb6deb18220c3e5)), closes [#20](https://github.com/DhiWise/nodejs-code-generator/issues/20)
14 changes: 7 additions & 7 deletions packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nodejs-code-generator/client",
"version": "1.0.0",
"version": "1.1.0",
"private": true,
"dependencies": {
"@babel/parser": "^7.15.8",
Expand Down Expand Up @@ -34,6 +34,8 @@
"prop-types": "^15.7.2",
"rc-drawer": "^4.4.2",
"react": "^17.0.2",
"react-app-rewire-aliases": "^0.2.0",
"react-app-rewired": "^2.1.8",
"react-awesome-query-builder": "^4.7.0",
"react-content-loader": "^6.0.3",
"react-datepicker": "^4.2.1",
Expand Down Expand Up @@ -70,9 +72,7 @@
"tailwindcss": "^2.2.17",
"three": "^0.133.1",
"use-onclickoutside": "^0.4.0",
"web-vitals": "^1.1.2",
"react-app-rewire-aliases": "^0.2.0",
"react-app-rewired": "^2.1.8"
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "npm run watch:css && react-app-rewired start",
Expand Down Expand Up @@ -101,6 +101,7 @@
]
},
"devDependencies": {
"@babel/eslint-parser": "^7.15.8",
"@babel/plugin-proposal-class-properties": "^7.14.5",
"@babel/plugin-transform-react-jsx": "^7.14.9",
"eslint": "^7.32.0",
Expand All @@ -109,7 +110,6 @@
"eslint-plugin-import": "^2.22.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"@babel/eslint-parser": "^7.15.8"
"eslint-plugin-react-hooks": "^4.2.0"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const BuildAppDropdown = () => {
dispatch(setBuildCodeState({ buildArchitecture: val }));
};
const applicationId = useSelector((state) => state.projects.currentApplicationId);
const [isBuild, setBuild, hideBuild] = useBoolean(false);

return (
<>
Expand Down Expand Up @@ -166,6 +167,10 @@ export const BuildAppDropdown = () => {
<Button
loading={isBuildLoading}
onClick={() => {
if (generatedId) {
setBuild();
return;
}
dispatch(codeGenerator({ applicationId, projectType: architectureValue }));
}}
className="w-full"
Expand All @@ -177,6 +182,18 @@ export const BuildAppDropdown = () => {

</DropdownMenu>
<BuildVSCodePopup />
<ConfirmationAlert
isOpen={isBuild}
description="Building a new application will permanently delete your old build application."
okText="Build app"
variant="primary"
handleSubmit={() => {
hideBuild();
dispatch(codeGenerator({ applicationId, projectType: architectureValue }));
}}
handleClose={hideBuild}
/>

</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const TechnologySetStep = () => {
} = useForm({ mode: 'onChange' });
React.useEffect(() => {
dispatch(resetBuildState());
if (!location.state?.isNewApp) {
if (!location.state?.isNewApp && applicationId) {
redirectApplication(applicationId).then((response) => {
dispatch(setBuildCodeState({
buildArchitecture: response.data.projectType,
Expand Down
11 changes: 11 additions & 0 deletions packages/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.1.0](https://github.com/DhiWise/nodejs-code-generator/compare/v1.0.0...v1.1.0) (2022-01-11)


### Bug Fixes

* **eslint formatting:** resolved eslint errors ([2c71bf2](https://github.com/DhiWise/nodejs-code-generator/commit/2c71bf2661c5e1599de3cb242b5431dce8720137))
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nodejs-code-generator/server",
"private": true,
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "handler.js",
"scripts": {
Expand Down
85 changes: 78 additions & 7 deletions packages/server/usecase/application/generate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global _,appRootPath */
const fs = require('fs');
const path = require('path');
const fsExtra = require('fs-extra');
const mongoose = require('mongoose');
const os = require('os');
const { getAllDirFilesCount } = require('../../util-service/common');
Expand Down Expand Up @@ -41,13 +43,12 @@ const ProjectRoleAccessPermissions = new ProjectRoleAccessPermissionsRepo();
const NestedQueryBuilder = new NestedQueryBuilderRepo();

const generateNodeCode = async (
input, applicationRepo, definition, generatedId, dirPath, projectName, applicationId, generatorRepo,
input, applicationRepo, definition, generatedId, dirPath, projectName, applicationId, generatorRepo, isReBuild,
) => {
try {
const updatedApp = await applicationRepo.update(applicationId, { 'inProcessStatus.build_app': IN_PROCESS });
await generatorRepo.update(generatedId, { 'inProcessStatus.build_app': IN_PROCESS });

await generator(input);
await generator(input, isReBuild);
// eslint-disable-next-line max-len
const lastGeneratorRecord = await generatorRepo.getDetails({
find: {
Expand Down Expand Up @@ -419,6 +420,46 @@ const nodeExpressRequest = async (applicationId, project, schemaRepo) => {
};
};

const getDirectoryAndFiles = (source) => {
const directory = [];
const files = [];
fs.readdirSync(source).forEach((file) => {
if (fs.lstatSync(path.resolve(source, file)).isDirectory()) {
directory.push(file);
} else {
files.push(file);
}
});
return {
directory,
files,
};
};

const overwriteProject = async (generatedId, projectName) => {
await fsExtra.copy(`./output/${generatedId}/${projectName}/.env`, `./output/${generatedId}/${projectName}_dhiwise_temp_app/.env`);

const outputProjectDir = `./output/${generatedId}/${projectName}`;
// fsExtra.emptyDirSync(directory);

const {
directories, files,
} = await getDirectoryAndFiles(outputProjectDir);

_.forEach(directories, (dirName) => {
if (dirName !== 'node_modules') {
fsExtra.emptyDirSync(`${outputProjectDir}/${dirName}`);
}
});

_.forEach(files, (fileName) => {
fsExtra.remove(`${outputProjectDir}/${fileName}`);
});

await fsExtra.copy(`./output/${generatedId}/${projectName}_dhiwise_temp_app`, `./output/${generatedId}/${projectName}`); // copies file
await fsExtra.remove(`./output/${generatedId}/${projectName}_dhiwise_temp_app`);
};

const generate = (applicationRepo, schemaRepo, generatorRepo) => async (params) => {
try {
if (!params.applicationId) {
Expand Down Expand Up @@ -447,6 +488,9 @@ const generate = (applicationRepo, schemaRepo, generatorRepo) => async (params)
};
}

let generated;
let isReBuild = false;

const definition = {
generatorPath: `${appRootPath}/output`,
inputPath: `${appRootPath}/input`,
Expand All @@ -457,11 +501,35 @@ const generate = (applicationRepo, schemaRepo, generatorRepo) => async (params)
config: definition,
applicationId: params.applicationId,
type: 1,
status: 1,
status: 2,
versionNumber: '1',
semanticVersionNumber: '1.0',
};
let generated = await generatorRepo.create(obj);

if (params.applicationId) {
const query = {
find: {
applicationId: params.applicationId,
isDeleted: false,
config: definition,
type: 1,
status: 2,
},
sortBy: { _id: -1 },
};

const generatedApps = await generatorRepo.getDetails(query);

if (generatedApps.length > 0) {
// eslint-disable-next-line prefer-destructuring
generated = generatedApps[0]; // latest App
generated.id = generated._id;
isReBuild = true;
} else {
obj.status = 1;
generated = await generatorRepo.create(obj);
}
}
if (!generated) {
return GENERATOR_FAILED_CREATE;
}
Expand Down Expand Up @@ -566,9 +634,12 @@ const generate = (applicationRepo, schemaRepo, generatorRepo) => async (params)
await generatorRepo.update(generated.id, { 'inProcessStatus.build_app': IN_QUEUE });

const status = await generateNodeCode(
inputFile, applicationRepo, definition, generated.id, dirPath, project.name, params.applicationId, generatorRepo,
inputFile, applicationRepo, definition, generated.id, dirPath, project.name, params.applicationId, generatorRepo, isReBuild,
);
if (status) {
if (isReBuild) {
await overwriteProject(generated.id, jsonData.config.projectName);
}
return {
...PROJECT_GENERATED,
data: {
Expand All @@ -580,7 +651,7 @@ const generate = (applicationRepo, schemaRepo, generatorRepo) => async (params)
}
return SERVER_ERROR;
} catch (err) {
// console.log('error', err);
console.log('error', err);
return SERVER_ERROR;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CodeGenerator {
steps, templateFolderName, templateRegistry, userDirectoryStructure,
} = this.setup;
const jsonModels = _.cloneDeep(params.jsonData.models);
const { isReBuild } = params;
/*
* console.log('userDirectoryStructure ====== >', JSON.stringify(userDirectoryStructure));
* ? JSON PARSERS
Expand Down Expand Up @@ -101,7 +102,12 @@ class CodeGenerator {

// ? Create Project Dir
if (_.includes(steps, PROJECT_CREATION_STEP.CREATE_ROOT_DIRECTORY)) {
rootDirectory = await this.createProjectRootDirectory(params.directory, params.projectName);
let tempDirName = params.projectName;
if (isReBuild) {
tempDirName = `${tempDirName}_dhiwise_temp_app`;
}

rootDirectory = await this.createProjectRootDirectory(params.directory, tempDirName);
}

// ? Mongoose Type Validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function createPackageJson ({
name: createAppName(name),
version: '0.0.1',
private: true,
scripts: { start: `node ${mainJsFile}` },
scripts: { start: `nodemon ${mainJsFile}` },
dependencies: {
'cookie-parser': '~1.4.4',
debug: '~2.6.9',
Expand All @@ -67,6 +67,7 @@ async function createPackageJson ({
eslint: '~7.12.1',
'eslint-config-airbnb': '~18.2.1',
'eslint-plugin-import': '~2.22.1',
nodemon: '^2.0.15',
},
};
forEach(packages?.dependencies, (depValue, depName) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function trimModelNameFromInput (models) {
throw new Error('modelName should not contain spaces');
}

async function main (inputFilepath) {
async function main (inputFilepath, isReBuild) {
let projectPath;
try {
const inputData = fs.readFileSync(inputFilepath, { encoding: 'utf8' });
Expand All @@ -39,6 +39,7 @@ async function main (inputFilepath) {
directory: projectPath,
projectName: config.projectName,
jsonData,
isReBuild,
});
} catch (err) {
console.log(err);
Expand Down

0 comments on commit 2f63219

Please sign in to comment.