-
-
Notifications
You must be signed in to change notification settings - Fork 142
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
feat: moving context command under config namespace #79
Conversation
@magicmatatjahu can you please help me with this. The save function is not working as intended, I am trying everything but I can't figure out what is happening. save(context: Context) {
try {
fs.writeFileSync(this.contextFilePath, JSON.stringify({
current: context.current,
store: context.store
}), { encoding: 'utf8' });
fs.writeFileSync(this.contextFilePath, JSON.stringify(context)); // changed this because initial was also not working
return context;
} catch (error) {
console.warn(error);// eslint-disable-line no-undef, no-console
return undefined;
}
} I tried with context rather than breaking it because that was also not working. whatever the context is only this is getting saved |
@Souvikns As I tested, you save context three times and only last content is saved - I don't know why this function is executed so many times. Check the path from main function to I have similar logs:
And only last content is saved, so please return to solution with: fs.writeFileSync(this.contextFilePath, JSON.stringify({
current: context.current,
store: context.store
}), { encoding: 'utf8' }); and fix the "bug" with multiple savings. |
Kudos, SonarCloud Quality Gate passed!
|
import { ContextMessageWriter, Messages } from './messages'; | ||
|
||
@injectable() | ||
export class ContextComponent { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see think that this is a good approach. For me, this class has so many responsabilities and does not look like an intuitive react code.
It's managing so many things, validating input, throwing errors and managing the view. For me, all of this, should be individual components that call the hooks that manage those actions. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
look like an intuitive react code.
😄 Yeah, I agree, Just trying to find a way to test, because calling the container.resolve
function in the component itself makes the code coupled. With this, I am able to pass in mock context to test the implementation.
It's managing so many things, validating input, throwing errors and managing the view
Thinking on this issue. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the key here is to be able to put context command inside the config one, why don't create just another config command router and route to the context one instead of all these changes?? Am I forgetting something that should be take in care? 🤔
export class ContextMessageWriter { | ||
list(ctx: Context): ReactElement { | ||
return <Box flexDirection="column"> | ||
{Object.keys(ctx.store).map((contextName: string) => <Text key={contextName}>{contextName}</Text>)} | ||
</Box>; | ||
} | ||
|
||
current(ctx: Context): ReactElement { | ||
return <Text>{ctx.current}</Text>; | ||
} | ||
|
||
add(message: string): ReactElement { | ||
return <Text color="green">{message}</Text>; | ||
} | ||
|
||
use(ctx: Context): ReactElement { | ||
return <Text>{ctx.current}: {ctx.getContext(ctx.current as string)}</Text>; | ||
} | ||
|
||
remove(message: string): ReactElement { | ||
return <Text>{message}</Text>; | ||
} | ||
|
||
throwError(message: string) { | ||
return <Text color="red">{message}</Text>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this ContextMessageWritter?? I think that if we want to abstract or name any of the views, we can create a separate component but for me, this isn't so intuitive 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContextMessageWritter
might be overcomplicating things. Currently, I am just using it for running tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the key of having the text strings that will be displayed, not this case as far as I know (even could be intestesting to take care of future internacionalization) but extracting all of these set of texts in order to make easier the asserts on the tests, is not a good practice. You could extract individual views as components in order to make unit testing and make sure that those individual views behave as you expect. This could be useful if you only want to assert (on an integration test or bigger unit test that contains any of these single compontents) that it contains certain string, not to do an assert about all the view tree. 😛
Firstly my aim in this PR is to bring the
context
command under theconfig
namespace so that the final command would look something like thisasyncapi config context current
also incorporating the updatedContextService
class from #72.Doing so I was experimenting with some things, I want an opinion from @derberg and @magicmatatjahu and @jotamusik if I am overcomplicating things.
File structure
I was trying something like this -
because the way our router is written, allows us to break the complete command string into commands, subcommands and arguments and we repeatedly use this to create hierarchical relations accordingly. Consider
asyncapi config context list
which would be something like this.Was trying to model this and create the file structure.
Injecting the context service and separating the logic for messages
Currently, we have a
messages.ts
file storing all the messages constants. I think that in the future all the different commands will have specific messages that would not be shared with any other commands. All commands have different input needs as well for that I was separating the logic and injecting it. I am trying something like this.All separate commands have separate
messageWriters
and then we can also reuse them for testing, Now I was not creatingsuccess
anderror
messages because maybe after #76 we don't need to account for this. For now, I will write it.And for the
ContextService
instead of creating hooks I was thinking of making something like this, though I don't know it is bad.and then resolving container in the
router.tsx
doing something like thisRelated issue(s)
see also #37