Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteModule API updated #1232

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 66 additions & 9 deletions tools/cli/commands/deleteModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,76 @@ const {
* @param moduleName - The name of a new module
* @param old - The flag that describes if the command invoked for a new structure or not
*/
function deleteModule({ logger, packageName, moduleName, old }) {
const modulePackageName = getModulePackageName(packageName, old);
const modulePath = computeModulePath(modulePackageName, old, moduleName);
const params = { logger, moduleName, modulePath, packageName, modulePackageName, old };
function deleteModule(logger, templatePath, module, location) {
logger.info(`Deleting ${location} files…`);

// pascalize
const Module = pascalize(module);
const startPath = `${__dirname}/../../..`;
const modulePath = `${startPath}/packages/${location}/src/modules/${module}`;
const commonGraphqlFile = 'commonGraphql.js';
const commonGraphqlPath = `${startPath}/packages/${location}/src/modules/common/components/web/${commonGraphqlFile}`;

if (fs.existsSync(modulePath)) {
deleteTemplates(params);
removeFromModules(params);
if (!old) removeDependency(params);
// remove module directory
shell.rm('-rf', modulePath);

// change to destination directory
shell.cd(`${startPath}/packages/${location}/src/modules/`);

// get module input data
const path = `${startPath}/packages/${location}/src/modules/index.js`;
let data = fs.readFileSync(path);

// extract Feature modules
const re = /Feature\(([^()]+)\)/g;
const match = re.exec(data);
const modules = match[1].split(',').filter(featureModule => featureModule.trim() !== module);

// remove import module line
const lines = data
.toString()
.split('\n')
.filter(line => line.match(`import ${module} from './${module}';`) === null);
fs.writeFileSync(path, lines.join('\n'));

// remove module from Feature function
//shell.sed('-i', re, `Feature(${modules.toString().trim()})`, 'index.js');
shell
.ShellString(shell.cat('index.js').replace(RegExp(re, 'g'), `Feature(${modules.toString().trim()})`))
.to('index.js');

if (location === 'server') {
// change to database migrations directory
shell.cd(`${startPath}/packages/${location}/src/database/migrations`);
// check if any migrations files for this module exist
if (shell.find('.').filter(file => file.search(`_${Module}.js`) > -1).length > 0) {
let okMigrations = shell.rm(`*_${Module}.js`);
if (okMigrations) {
logger.info(chalk.green(`✔ Database migrations files successfully deleted!`));
}
}

// change to database seeds directory
shell.cd(`${startPath}/packages/${location}/src/database/seeds`);
// check if any seed files for this module exist
if (shell.find('.').filter(file => file.search(`_${Module}.js`) > -1).length > 0) {
let okSeeds = shell.rm(`*_${Module}.js`);
if (okSeeds) {
logger.info(chalk.green(`✔ Database seed files successfully deleted!`));
}
}
}

logger.info(chalk.green(`✔ Module ${moduleName} for package ${packageName} successfully deleted!`));
// continue only if directory does not jet exist
logger.info(chalk.green(`✔ Module for ${location} successfully deleted!`));
} else {
logger.info(chalk.red(`✘ Module ${moduleName} for package ${packageName} not found!`));
logger.info(chalk.red(`✘ Module ${location} location for ${modulePath} not found!`));
}

if (fs.existsSync(commonGraphqlPath)) {
const graphqlQuery = `${Module}Query`;
deleteModuleFromCommonGraphqlFile(module, commonGraphqlPath, graphqlQuery);
}
}

Expand Down
55 changes: 55 additions & 0 deletions tools/cli/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ function computeRootModulesPath(moduleName) {
* @param old - The flag that describes if the command invoked for a new structure or not
* @returns {string} - Return the computed path
*/


function generateCommonGraphqlFile(module, commonGraphqlPath, moduleGraphqlContainer) {
const importGraphqlContainer = `import ${moduleGraphqlContainer} from '../../../${module}/containers/${moduleGraphqlContainer}';\n`;
const exportGraphqlContainer = `\nexport default {\n ${moduleGraphqlContainer}\n};\n`;

if (fs.existsSync(commonGraphqlPath)) {
const commonGraphqlData = fs.readFileSync(commonGraphqlPath);
const commonGraphql = commonGraphqlData.toString().trim();
if (commonGraphql.length > 1) {
const index = commonGraphql.lastIndexOf("';");
const computedIndex = index >= 0 ? index + 3 : false;
if (computedIndex) {
let computedCommonGraphql =
commonGraphql.slice(0, computedIndex) +
importGraphqlContainer +
commonGraphql.slice(computedIndex, commonGraphql.length);
computedCommonGraphql = computedCommonGraphql.replace(/(,|)\s};/g, `,\n ${moduleGraphqlContainer}\n};`);
return fs.writeFileSync(commonGraphqlPath, computedCommonGraphql);
}
}
}
return fs.writeFileSync(commonGraphqlPath, importGraphqlContainer + exportGraphqlContainer);
}

function computeModulePackageName(moduleName, packageName, old) {
return old ? `./${moduleName}` : `@gqlapp/${decamelize(moduleName, { separator: '-' })}-${packageName}`;
}
Expand Down Expand Up @@ -232,6 +257,34 @@ function deleteStackDir(stackDirList) {
});
}

function generateField(value, update = false) {
let result = '';
const hasTypeOf = targetType => value.type === targetType || value.type.prototype instanceof targetType;
if (hasTypeOf(Boolean)) {
result += 'Boolean';
} else if (hasTypeOf(DomainSchema.ID)) {
result += 'ID';
} else if (hasTypeOf(DomainSchema.Int)) {
result += 'Int';
} else if (hasTypeOf(DomainSchema.Float)) {
result += 'Float';
} else if (hasTypeOf(String)) {
result += 'String';
} else if (hasTypeOf(Date)) {
result += 'Date';
} else if (hasTypeOf(DomainSchema.DateTime)) {
result += 'DateTime';
} else if (hasTypeOf(DomainSchema.Time)) {
result += 'Time';
}

if (!update && !value.optional) {
result += '!';
}

return result;
}

module.exports = {
getModulePackageName,
getTemplatesPath,
Expand All @@ -250,4 +303,6 @@ module.exports = {
deleteDir,
getPathsSubdir,
deleteStackDir,
generateCommonGraphqlFile,
generateField,
};