How to access consuming applications environment from addon namespace - ember-cli

I am creating an addon project.
I have a file in app/utils with the following import
import config from '../config/environment';
I want to move this to addon/utils but the above import does not work anymore and I get an error when doing ember serve. I need access to the consuming applications environment.
How do I do this from the addon folder?

You simply can't. The addon namespace is meant to be isolated from the app namespace and it is, that's why it won't let you import the environment config. If you want to access it it needs to be in the app namespace.
One way around this, is to keep it in the addon namespace, and require users to import it in their own app/utils and pass in the config themselves. It would look something like this:
// app/utils/their-util.js
import config from '../config/environment';
import yourUtil from 'the-addon/utils/your-util';
export default function() {
return yourUtil(config);
}
You could even do this in your addon's app namespace to have it included by default, and they could then override it if they wanted to do something custom.
Obviously this isn't an ideal solution but it's what's available today.

Related

Share Custom Commands in Cypress across different github repo projects

My organization is fairly big, we use node and are looking into potentially using Cypress. We have like 100 different projects and each have their own Github repository, but we like to share functions and methods and import them. So my question is, is there a way to do that with Cypress custom commands so that it can be a dependency and you can just import them?
Is it as easy as just adding a Github URL in the dependency?
You will have to create a library/package that has all those commands implemented. And then in every project you should install Cypress and then import those commands. For example:
In the library you would have something like this in commands.ts if you're using Typescript or commands.js if you're using JavaScript.
Cypress.Commands.add('take', (testSelector) => {
return cy.get(`[data-test='${testSelector}']`);
});
Then, in the repo you want to use it you'll have to import it from the library.
In your cypress/support/index file you should add:
import '#organization/library-name/path/to/commands';
Note that the name of the package can depend on how you choose to generate it.

How to figure out the name to specify in import statement in Swift

Every time I import a Cocoapods library I have trouble in finding out what name to specify in the import statement.
First of all, should we always explicitly import the Cocoapods in all the files using the library?
If that's the case how can we find the name to specify in the import statement?
EDIT
Thanks for the answer about the necessity of import statement when using a library. I'm now completely clear about that.
About the package name, it seems like most of the Pods have a file with the extension ".modulemap" in their Support Files directory, and that is the one which specifies the module name. Is my understanding correct? It looks right but at the same time I have a doubt as well because some Pods(for example Fabric, GoogleSignIn etc.) don't have that.
should we always explicitly import the Cocoapods in all the files using the library?
Yes of course! If you don't import a module, you won't be able to use the types defined in the module.
how can we find the name to specify in the import statement?
Usually, this is the same as the name f the pod you write in your Podfile. The authors of most pods do this to avoid confusion. The names can be different though.
A good way of finding the module name of a pod is to go to the pods project in the left pane:
Click that and you will see all the modules listed out. For example:
Use the names you see in the list.
1. Every time I import a Cocoapods library I have trouble in finding out what name to specify in the import statement.
Sometimes it takes time to load pod installed in project as you had successfully installed a pod but when it comes to import name doesn't show
For this I usually do is command + b (Build project) just build project when you install a pod
or
Just Re-Open Project file after Successfully installing pods
2. Should we always explicitly import the Cocoapods in all the files using the library?
Yes, you need to import the pod in controller without this you won't be able to use its feature
3. how can we find the name to specify in the import statement
Usually the name is same as of pod File like alamofire
Pod AlamoFire and Import AlamoFire
if Still you find issues with name Check pod List installed In your project

How to import a default file with JSPM, by requiring the folder like node require()

I've been trying JSPM for few days, but couldn't find an easy way to import module from folder.
As described in the node documentation require for the method, it is possible to require a folder. This would resolve to folder/index.js if the file exists.
Is there a way to do that with SystemJs/JSPM ?

Typescript - simplify imports

Is there way to import modules with alias?
E.g. I have angular service in folder common/services/logger/logger.ts.
How to make import looks like import {Logger} from "services" where "services" contains all my services?
I've looked at this and this but didn't understand how I can use it.
Create one file that contains all of your services and give it a name, e.g. services.ts. That file can be seen as a barrels, because it will contain and export all of your services. The file could look like this:
export * from './logger/logger';
export * from ...;
etc.
Now you can import this single file as follow:
import * as Services from 'common/services';
Then you can access your servies via Services.Logger, or directly import the service you want via:
import {Logger} from 'common/services';
Note, you have change the paths since this is just an example. For more information about this technique, have a look here.
To solve this problem I used webpack. If you want to import dependencies everywhere you want, you will need to add alias in webpack config. For example, you have folder shared in root with services folders in it. Folder services should have index.ts that exports all services you need (e.g Logger). So alias would be "services" => "shared/services".
To make autocomplete work in WebStorm you just need to mark shared folder as a "Resource root".

Accessing an Ember CLI app's config from within an addon

I'm developing an addon for Ember CLI which requires me to dynamically load files from the app. I should be able to do this with a command like require('my-app/models/load-me'). The only problem is that my-app could be anything, depending on what the developer named their application. If I had access to the my-app/config/environment file, I could just get the modulePrefix from there, but unfortunately, that's also namespaced under my-app.
So does anyone know of another way to access the modulePrefix? I'm assuming there must be a way, because Ember CLI itself needs to get that prefix before it can load any files.
Found the answer here. Basically, you can look it up through the container like so:
this.container.lookupFactory('config:environment').modulePrefix;