MCPcopy Create free account
hub / github.com/Snapchat/Valdi / valdiNewModule

Function valdiNewModule

npm_modules/cli/src/commands/newModule.ts:211–264  ·  view source on GitHub ↗
(argv: ArgumentsResolver<CommandParameters>)

Source from the content-addressed store, hash-verified

209}
210
211async function valdiNewModule(argv: ArgumentsResolver<CommandParameters>) {
212 console.log(referenceString);
213 const skipChecks = argv.getArgument('skipChecks');
214
215 const checks: Checks = skipChecks ? {} : await promptChecks();
216 const valdiModulePath = checks.valdiModulePath ?? path.join(await getBazelWorkspaceRoot(), 'modules');
217
218 let moduleName = await getModuleName(argv);
219
220 // Validate module name if provided via command line argument
221 if (argv.getArgument('moduleName')) {
222 const validationError = validateProjectName(moduleName);
223 if (validationError) {
224 throw new CliError(validationError);
225 }
226 moduleName = sanitizeProjectName(moduleName);
227 }
228
229 const destPath = path.join(valdiModulePath, moduleName);
230 const didConfirm = skipChecks || (await finalConfirmation(destPath, argv));
231 if (!didConfirm) {
232 throw new CliError('Cancelled new module creation.');
233 }
234
235 /**
236 * Do not overwrite existing files
237 */
238 if (fs.existsSync(destPath)) {
239 throw new CliError(`Path already exists ${destPath}`);
240 }
241
242 const choices: CliChoice<ModuleTemplate>[] = ALL_MODULE_TEMPLATES.map(target => ({
243 name: `${wrapInColor(target.name, ANSI_COLORS.GREEN_COLOR)} . ${target.description}`,
244 value: target,
245 }));
246
247 const templateOption = argv.getArgument('template');
248 const template = templateOption ? ALL_MODULE_TEMPLATES.find(template => template.path === templateOption) : undefined;
249 const moduleTemplate = template ?? await getUserChoice(choices, `Please choose the module type:`);
250
251 /**
252 * Copy bootstrap files
253 */
254 const sourcePath = path.join(BOOTSTRAP_DIR_PATH, 'modules', moduleTemplate.path);
255 const replacements: Replacements = {
256 MODULE_NAME: moduleName,
257 MODULE_NAME_PASCAL_CASED: toPascalCase(moduleName),
258 };
259
260 copyBootstrapFiles(sourcePath, destPath, replacements);
261
262 // Finalize message
263 console.log(`Success! New module directory: ${wrapInColor(destPath, ANSI_COLORS.GREEN_COLOR)}`);
264}
265
266export const command = `new_module [module-name]`;
267export const describe = referenceString;

Callers

nothing calls this directly

Calls 15

validateProjectNameFunction · 0.90
sanitizeProjectNameFunction · 0.90
wrapInColorFunction · 0.90
getUserChoiceFunction · 0.90
toPascalCaseFunction · 0.90
copyBootstrapFilesFunction · 0.90
promptChecksFunction · 0.85
getBazelWorkspaceRootFunction · 0.85
finalConfirmationFunction · 0.85
getModuleNameFunction · 0.70
logMethod · 0.65
getArgumentMethod · 0.45

Tested by

no test coverage detected