-
Notifications
You must be signed in to change notification settings - Fork 77
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
Add ability to dynamically import namespaces from types in the tsd client #235
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d29ab7a
Add ability to dynamically import namespaces from types in the tsd cl…
rogebrd 060935b
Update namespace def
rogebrd ef5a1ca
Fix route formatting
rogebrd 2009803
Update emit_raw
rogebrd 433afb6
Wrap file in quotes
rogebrd 8eee405
Fix linter
rogebrd 0cbf06d
Fix typo
rogebrd File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,29 @@ | |
help=('Wraps the response in a response class') | ||
) | ||
|
||
_cmdline_parser.add_argument( | ||
'--import-namespaces', | ||
default=False, | ||
action='store_true', | ||
help=('Adds an import statement at the top of the file to import each ' | ||
'namespace from the as a named import. Must be used in conjunction ' | ||
'with the --export-namespaces command when generating the ts_types.') | ||
) | ||
_cmdline_parser.add_argument( | ||
'--import-template-string', | ||
type=str, | ||
default='IMPORT', | ||
help=('The name of the template string to replace with import statement. ' | ||
'Defaults to IMPORT, which replaces the string /*IMPORT*/ with import.') | ||
) | ||
_cmdline_parser.add_argument( | ||
'--types-file', | ||
type=str, | ||
default='', | ||
help=('If using the --import-namespaces flag, this if the file that contains ' | ||
'the named exports to import here.') | ||
) | ||
|
||
_header = """\ | ||
// Auto-generated by Stone, do not modify. | ||
""" | ||
|
@@ -103,9 +126,31 @@ def generate(self, api): | |
t_end = len(template) | ||
t_ends_with_newline = template[t_end - 1] == '\n' | ||
|
||
self.emit_raw(template[0:r_start] + ('\n' if not r_ends_with_newline else '')) | ||
if self.args.import_namespaces: | ||
import_template_string = self.args.import_template_string | ||
import_from_file = self.args.types_file | ||
# /*IMPORT*/ | ||
i_match = re.search("/\\*%s\\*/" % (import_template_string), template) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LOL this seems super manual, but I see it used in other places. Have we considered using an actual template language library? |
||
if not i_match: | ||
raise AssertionError( | ||
'Missing /*%s*/ in TypeScript template file.' % import_template_string) | ||
i_start = i_match.start() | ||
i_end = i_match.end() | ||
i_ends_with_newline = template[i_end - 1] == '\n' | ||
t_end = len(template) | ||
t_ends_with_newline = template[t_end - 1] == '\n' | ||
|
||
self.emit_raw(template[0:i_start] + ('\n' if not i_ends_with_newline else '')) | ||
self._generate_import(api, import_from_file) | ||
self.emit_raw(template[i_end + 1:r_end] + ('\n' if not r_ends_with_newline else '')) | ||
else: | ||
self.emit_raw(template[0:r_start] + ('\n' if not r_ends_with_newline else '')) | ||
self._generate_routes(api, spaces_per_indent, indent_level) | ||
self.emit_raw(template[r_end + 1:t_end] + ('\n' if not t_ends_with_newline else '')) | ||
|
||
def _generate_import(self, api, type_file): | ||
namespaces = ", ".join(map(lambda namespace: namespace.name, api.namespaces.values())) | ||
self.emit("import { %s } from '%s';" % (namespaces, type_file)) | ||
|
||
def _generate_routes(self, api, spaces_per_indent, indent_level): | ||
with self.indent(dent=spaces_per_indent * (indent_level + 1)): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: think this comment has a typo