https://pulumi.com logo
#typescript
Title
# typescript
a

acceptable-stone-35112

04/28/2020, 3:28 PM
I got into situation when I need to use outputs from StackReference in components that also generate outputs and need to assign them to index.ts module exports, so they appear in stack output. Once I wrap them in .apply() callback, exports they were producing can't be assigned anymore. Is there a clean way to work around this without writing promises that would register them in module exports once resolved?
b

boundless-airport-99052

04/28/2020, 7:46 PM
This should works
Could you give an example?
a

acceptable-stone-35112

04/29/2020, 8:37 AM
Here's basic reproduction
@boundless-airport-99052 its simple flow. all stacks are in same project, while dev/qa/prod/whatever stacks need outputs of "infra" stack. In console log you can see that module exports actually contains new property, but in outer log and stack outputs its missing
b

boundless-airport-99052

04/29/2020, 8:47 AM
to export a value, you don’t have to use
apply
by, the way, in typescript, I can’t do
export
in a condition. I should be to the top level. But, here is how I do:
Copy code
import { StackReference } from "@pulumi/pulumi";

const stackRef = new StackReference(`${<http://config.org|config.org>}/${pulumi.getProject()}/infra`);
exports.infraStackName = stackRef.requireOutput("stack")
pulumi manages the Output in exports for you
a

acceptable-stone-35112

04/29/2020, 12:34 PM
I see
I put it like that for sake of simplicity
in actual project I pass outputs from stack ref to components that in turn generate outputs of their own and I need to propagate all of them to stack output
so I can only use apply() for that purpose
And btw, exports do work inside conditions e.g. following works fine in both if and switch case scope
Copy code
if (pulumi.getStack() != "infra") {
    exports.test = "123";
the only case I see when exports don't have effect at module scope is inside apply() callback
@boundless-airport-99052 I guess it has something to do with applyHelperAsync, didn't deep dive into that code yet. e.g. following doesn't set exports:
Copy code
setImmediate(() => exports.test = "ABC");
while this one does:
Copy code
(async () => {
    exports.test = "ABC";
})();
b

boundless-airport-99052

04/29/2020, 12:53 PM
And btw, exports do work inside conditions
e.g. following works fine in both if and switch case scope
AFAIK, it didn’t with typescript
in actual project I pass outputs from stack ref to components that in turn generate outputs of their own and I need to propagate all of them to stack output
I don’t understand what you do exactly but you don’t need to use
apply
when you use
pulumi.Input
and `pulumi.Output`everywhere (including your components)
a

acceptable-stone-35112

04/29/2020, 1:07 PM
Thanks a lot @boundless-airport-99052 I got your point and indeed passing Input<string> there instead of string solves the problem I used apply initially because the code was using discovery to get resource, which returned strings for arns/ids and components expected strings.
👍 1
I'd prefer pulumi to provide global registry for stack outputs instead of relying on module exports, so I can just register my outputs from any component
it would also be a language agnostic api, common for all runtimes