Files
freeCodeCamp/tools/challenge-editor/api/routes/tools-route.ts
T
Oliver Eyton-Williams 4ff00922da refactor: fix hidden eslint errors (#49365)
* refactor: explicit types for validate

* refactor: explicit return types for ui-components

* refactor: use exec instead of match

* refactor: add lots more boundary types

* refactor: more eslint warnings

* refactor: more explicit exports

* refactor: more explicit types

* refactor: even more explicit types

* fix: relax type contrainsts for superblock-order

* refactor: final boundaries

* refactor: avoid using 'object' type

* fix: use named import for captureException

This uses TypeScript (which works) instead of import/namespace
(which doesn't) to check if captureException exists in sentry/gatsby
(it does)
2023-02-13 07:13:50 -08:00

57 lines
1.4 KiB
TypeScript

import { exec } from 'child_process';
import { join } from 'path';
import { promisify } from 'util';
import { Request, Response } from 'express';
import { ToolsSwitch } from '../interfaces/tools';
const asyncExec = promisify(exec);
const toolsSwitch: ToolsSwitch = {
'create-next-step': directory => {
return asyncExec(`cd ${directory} && npm run create-next-step`);
},
'create-empty-steps': (directory, num) => {
return asyncExec(`cd ${directory} && npm run create-empty-steps ${num}`);
},
'insert-step': (directory, num) => {
return asyncExec(`cd ${directory} && npm run insert-step ${num}`);
},
'delete-step': (directory, num) => {
return asyncExec(`cd ${directory} && npm run delete-step ${num}`);
},
'update-step-titles': directory => {
return asyncExec(`cd ${directory} && npm run update-step-titles`);
}
};
export const toolsRoute = async (
req: Request,
res: Response
): Promise<void> => {
const { superblock, block, command } = req.params;
const { num } = req.body as Record<string, number>;
const directory = join(
__dirname,
'..',
'..',
'..',
'..',
'curriculum',
'challenges',
'english',
superblock,
block
);
if (!(command in toolsSwitch)) {
res.json({ stdout: '', stderr: 'Command not found' });
return;
}
const parsed = command as keyof ToolsSwitch;
const { stdout, stderr } = await toolsSwitch[parsed](directory, num);
res.json({ stdout, stderr });
};