Skip to content

Commit

Permalink
#58 while cmdEvent docu generating: get list commandHandlers from ind…
Browse files Browse the repository at this point in the history
…ex file, not by "readDir"
  • Loading branch information
xeronimus@gmail.com authored and xeronimus@gmail.com committed Jun 21, 2020
1 parent 23e848a commit 39f6bdb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 42 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"prebuild": "node build/checkTranslationKeys.js && cd server/ && npm run docu",
"build": "node build/dockerImage.js",
"dependencyCheck": "ncu --packageFile ./package.json > npm_dependencies_report.poinz.md && ncu --packageFile ./client/package.json > npm_dependencies_report.poinz-client.md && ncu --packageFile ./server/package.json > npm_dependencies_report.poinz-server.md ",
"postinstall": "cd client/ && npm i && cd ../server && npm i"
"postinstall": "cd client/ && npm i && cd ../server && npm i",
"docu": "cd server/ && npm run docu"
},
"devDependencies": {
"bluebird": "3.7.2",
Expand Down
11 changes: 5 additions & 6 deletions server/docu/commandAndEventDocu.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Poinz Command and Event Docu
This is an autogenerated docu. 6/18/2020, 7:08:20 AM
This is an autogenerated docu. 6/21/2020, 2:24:22 PM

## Commands

Expand Down Expand Up @@ -678,9 +678,8 @@ Source: [/server/src/commandHandlers/toggleExclude.js](/server/src/commandHandle
Description

```text
A user can exclude himself from estimations.
Emits event "excludedFromEstimations" or "includedInEstimations" on toggle
If user is marked as excluded, he cannot estimate stories.
(If user is marked as excluded, he cannot estimate stories.)
```

Produces: **[includedInEstimations](#includedInEstimations)**, **[excludedFromEstimations](#excludedFromEstimations)**,
Expand Down Expand Up @@ -746,7 +745,7 @@ Produced by: **[addStory](#addStory)**
Description

```text
a new story was added to the room
```


Expand Down Expand Up @@ -879,7 +878,7 @@ Produced by: **[joinRoom](#joinRoom)** **[joinRoom](#joinRoom)** **[setUsername]
Description

```text
user set his name
```


Expand All @@ -892,7 +891,7 @@ Produced by: **[joinRoom](#joinRoom)** **[joinRoom](#joinRoom)** **[setEmail](#s
Description

```text
user set his email address
```


Expand Down
81 changes: 51 additions & 30 deletions server/docu/gatherData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ const babel = require('@babel/core');

module.exports = gatherData;

/**
* use "import" statements in file "commandHandlers.js" to find all commandHandlers
* we cannot "readDir" the cmdHandlersDirPath, since it contains also other files...
*
* @return {string[]} list of filenames
*/
async function getListOfCommandHandlerFiles(cmdHandlersDirPath) {
const parseResult = await parseFile(path.join(cmdHandlersDirPath, 'commandHandlers.js'));
const imports = [];
babel.traverse(parseResult, {
ImportDeclaration: (nodePath) => {
imports.push(nodePath.node.source.value + '.js');
}
});

imports.sort();
return imports;
}

/**
* gathers command and event metadata from our sources
*
Expand All @@ -14,7 +33,7 @@ module.exports = gatherData;
* @return {Promise<{commandHandlerFileData: any, eventList: *}>}
*/
async function gatherData(cmdHandlersDirPath, validationSchemasDirPath, evtHandlersDirPath) {
const commandHandlerFilenames = await fs.promises.readdir(cmdHandlersDirPath);
const commandHandlerFilenames = await getListOfCommandHandlerFiles(cmdHandlersDirPath);
let commandHandlerFileData = await Promise.all(
commandHandlerFilenames.map(
async (f) => await handleSingleCmdHandlerFile(path.join(cmdHandlersDirPath, f))
Expand Down Expand Up @@ -83,35 +102,11 @@ async function gatherData(cmdHandlersDirPath, validationSchemasDirPath, evtHandl
return '';
}

function getPoinzRelativePath(filePath) {
const absPath = path.resolve(filePath);

return absPath.substring(absPath.lastIndexOf('poinz') + 5);
}

async function parseFile(filePath) {
const source = await fs.promises.readFile(filePath, 'utf-8');
return await new Promise((resolve, reject) =>
babel.parse(source, (err, result) => {
if (err) {
reject(err);
}
resolve(result);
})
);
}

/**
* parse given commandHandler file with babel. pull information from AST
*/
async function handleSingleCmdHandlerFile(filePath) {
const commandName = path.basename(filePath, '.js');

if (commandName === 'commandHandlers') {
// special "indexing" file is not a command handler
return undefined;
}

const parseResult = await parseFile(filePath);
const schema = await getValidationSchemaForCommand(commandName);
const cmdHandlerInfo = {
Expand Down Expand Up @@ -147,10 +142,36 @@ async function gatherData(cmdHandlersDirPath, validationSchemasDirPath, evtHandl
* load matching validation schema json file for given command
*/
async function getValidationSchemaForCommand(commandName) {
const validationSchemaFileContent = await fs.promises.readFile(
path.join(validationSchemasDirPath, commandName + '.json'),
'utf-8'
);
return JSON.parse(validationSchemaFileContent);
const validationSchemaFileName = path.join(validationSchemasDirPath, commandName + '.json');
try {
const validationSchemaFileContent = await fs.promises.readFile(
validationSchemaFileName,
'utf-8'
);
return JSON.parse(validationSchemaFileContent);
} catch (readError) {
console.error(
`Could not read validationSchema for command "${commandName}"! Expected it to be here : ${validationSchemaFileName}`
);
return {};
}
}
}

function getPoinzRelativePath(filePath) {
const absPath = path.resolve(filePath);

return absPath.substring(absPath.lastIndexOf('poinz') + 5);
}

async function parseFile(filePath) {
const source = await fs.promises.readFile(filePath, 'utf-8');
return await new Promise((resolve, reject) =>
babel.parse(source, (err, result) => {
if (err) {
reject(err);
}
resolve(result);
})
);
}
3 changes: 3 additions & 0 deletions server/src/eventHandlers/emailSet.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* user set his email address
*/
const emailSetEventHandler = (room, eventPayload, userId) =>
room.updateIn(['users', userId], (user) => user.set('email', eventPayload.email));

Expand Down
12 changes: 7 additions & 5 deletions server/src/eventHandlers/storyAdded.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Immutable from 'immutable';

/**
* a new story was added to the room
*/
const storyAddedEventHandler = (room, eventPayload) => {
const newStory = Immutable.fromJS(
Object.assign(eventPayload, {
estimations: {}
})
);
const newStory = Immutable.fromJS({
...eventPayload,
estimations: {}
});

return room.update('stories', new Immutable.Map(), (stories) =>
stories.set(eventPayload.id, newStory)
Expand Down
3 changes: 3 additions & 0 deletions server/src/eventHandlers/usernameSet.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* user set his name
*/
const usernameSetEventHandler = (room, eventPayload, userId) =>
room.updateIn(['users', userId], (user) => user.set('username', eventPayload.username));

Expand Down

0 comments on commit 39f6bdb

Please sign in to comment.