Pass globally installed package from step to step - amazon-web-services

I have a CDK codepipeline, which, simplified, looks something like this:
const pipeline = new CodePipeline(this, 'Pipeline', {
synth: new CodeBuildStep('InstallStep', {
commands: ['npm install -g some-package'],
}),
});
const initStep = new CodeBuildStep(`InitStep`, {
commands: ['some-package']
})
An NPM package is installed globally during synth step. Is there a way to use it in other build steps without reinstalling it again? I know I can easily pass build artifacts betwen the steps, but not sure about globally installed things.

Considering each code build step is a new compute instance, I don't think you can cache global node_modules. You may be able to instead do something like
const initStep = new CodeBuildStep(`InitStep`, {
commands: ['npx some-package']
})
Using npx to call the package avoids having to have the extra install step, assuming you call it like a cli command.

Related

Using "m1-medium" not recognized as valid by EAS

When I follow these instructions on an M2 MBA using Expo SDK 47.0.13 and EAS CLI 3.5.2 (darwin-arm64) I get
InvalidEasJsonError: eas.json is not valid.
- "build.dev-hardware.resourceClass" must be one of [default, medium]
which seems like a direct contradiction of those instructions. Why isn't the specified value (m1-medium) recognized as valid?
I had the same issue and I solved it by updating my eas-cli on global level. In my situation I tried updating it with
npm install -g eas-cli
If you've used a different package manager like I did to install eas-cli earlier, you may need to run the command accordingly. In my case it was
yarn global add eas-cli
Also, it's maybe worth checking if in your eas.json file you have any setting related to the cli version like this:
{
"cli": {
"version": ">= 3.3.0"
},
"build": {
"development": {
"developmentClient": true,
"distribution": "internal",
"ios": {
"resourceClass": "m1-medium"
}
},
"production": {
"ios": {
"resourceClass": "m1-medium"
}
}
}
}
EDIT
I remembered that when this was happening, whenever I ran eas build the console printed a message like this:
I ran the suggested npm install command, but message was still prompted, which led me to believe that yarn was in control of the version of the eas-cli that executes eas build.
This is why I ran yarn global add, which fixed the issue.

Is there a way to reference environment variables for an npm package from a consumer app?

I am creating a package on npm and for simplicity reasons I am trying to use environment variables in my npm package ( since user secret is needed ) that would be defined by a consumer app. But I seem to run into build issues when using the created package.
The structure looks something like this:
My demo app
|
|-----My npm package
|
|-----Other packages for my demo app
...
I have my npm package ( on npm ) and a demo app to showcase how the package works. I have environment variables used in the package, but I want to define them in my demo app.
In my package I am referencing the environment variables as such:
// My package
// auth-config.js
const config = {
variable1: import.meta.env['PUBLIC_VITE_VARIABLE_1'],
variable2: import.meta.env['PUBLIC_VITE_VARIABLE_2'],
variable3: import.meta.env['PUBLIC_VITE_VARIABLE_3']
};
export default config;
And later using them in my package code:
// My package
// auth-service.ts
const user = await getUser({
variable1: config.variable1,
variable2: config.variable2,
variable3: config.variable3
});
Then I have them defined in my demo app:
// Demo app
// .env
PUBLIC_VITE_VARIABLE_1=some-value
PUBLIC_VITE_VARIABLE_2=some-value-2
PUBLIC_VITE_VARIABLE_3=some-value-3
Then, when building ( npm run build ) my demo application, I am getting the following errors:
TypeError: Cannot read properties of undefined (reading 'PUBLIC_VITE_VARIABLE_1')
at file:///C:/Users/user1/Namu/login-with-auth/node_modules/my-app/auth0/auth-config.js:2:28
However, the dev build works completely fine. And it uses the variables successfully as well.
My configuration for both the package and the demo app is:
Svelte + Svelte Kit
Vite
Typescript
I have tried using different approaches to import environment variables ( process.env instead of import.meta.env ) inside the package. I have also tried updating the svelte.config.js and vite.config.js with different paths to environment variables to try and reach the scope of the application that uses this package.
I am not sure where to go from here, since I can't find any definitive answer online and nothing seems to work so far.
I was able to solve the issue in the end. Keep in mind I am using svelte + svelte-kit + vite in both the package and the demo app using the package.
The fix goes like this:
Name the environment variables properly. Naming convention should follow what the bundler says, not what the framework says. In my case for vite it has to start with VITE_, so the environment variable would be VITE_VARIABLE_1. More info can be found Here.
Side note: this is also configurable, but I could not make the configurations work in my favour.
Using proper references in the package. For vite, the correct way to reference an env variable is by using import.meta.env['VITE_VARIABLE_1'].
Adding checks for undefined when using the variables in the package like so: import.meta.env ? import.meta.env['VITE_VARIABLE_1'] : ''.
This let's you avoid the build errors that I was running into, since at build time, import.meta.env is not defined.
And this is how the final files look like:
Where the environment variables are referenced:
// My package
// auth-config.js
const variable1 = import.meta.env ? import.meta.env['PUBLIC_VITE_VARIABLE_1'] : '';
const variable2 = import.meta.env ? import.meta.env['PUBLIC_VITE_VARIABLE_2'] : '';
const variable3 = import.meta.env ? import.meta.env['PUBLIC_VITE_VARIABLE_3'] : '';
const config = {
variable1,
variable2,
variable3
};
Where the environment variables are used:
// My package
// auth-service.ts
const user = await getUser({
variable1: config.variable1,
variable2: config.variable2,
variable3: config.variable3
});
Where the environment variables are defined:
// Demo app
// .env
PUBLIC_VITE_VARIABLE_1=some-value
PUBLIC_VITE_VARIABLE_2=some-value-2
PUBLIC_VITE_VARIABLE_3=some-value-3
You can also look at the insides of the created Package.
And the coresponding files: auth-config.js, auth-service.ts.

adonis issue: error: `migration:run` is not a registered command

I Have installed #adonisjs/lucid.
create a migration to use command
adonis make:migration task
** task code**
'use strict'
const Schema = use('Schema')
class TaskSchema extends Schema {
up() {
this.create('tasks', table => {
table.increments()
table.timestamps()
table.string('name')
table.text('description')
table.integer('project_id').unsigned()
table
.foreign('project_id')
.references('projects.id')
.onDelete('cascade')
})
}
down() {
this.drop('tasks')
}
}
module.exports = TaskSchema
i have run my migration to show this error
error: `migration:run` is not a registered command
**
i have not understand this bug. I know that if not install the
dependencies #adonisjs/lucid than show this error but After I
installed dependencies why the error occurred
**
What you need here is adonis/cli.
npm i -g #adonisjs/cli
But I think the you don't really need this extra dependencies. Using ace is enough
node ace make:migration task
Here is the solution to this question
In fact, I don’t need to install the CLI in production.
Run your server with node server.js and execute internal commands with node ace XXX.
node ace migration:run --force

ember-cli-eslint, ember-cli-stylelint to run automatically only if desired

I understand that the purpose of ember-cli-eslint, ember-cli-stylelint to run automatically.
I am wondering if there is a way to control this behavior.
Like, run ember-cli-eslint, ember-cli-stylelint automatically only if there is certain ENVIRONMENT_VARIABLE or maybe write a custom script.
I am wondering if that is possible. Google search did not provide me any pointer.
Yes.
For ESLint:
Remove the addon ember-cli-eslint
Install the npm package eslint in your project
ESLint will then run only when you actually run ./node_modules/.bin/eslint .
You should update your package.json's lint:js script as well.
For Stylelint:
Remove the addon ember-cli-stylelint
Install the npm package stylelint in your project
Stylelint will then run only when you actually run ./node_modules/.bin/stylelint
You should update your package.json's lint:css script as well.
As suggested by #Turbo87 at https://github.com/ember-cli/ember-cli-eslint/issues/333 I have updated ember-cli-build.js like so:
const blacklist = [];
if (process.env.DISABLE_AUTO_LINT) {
blacklist.push('ember-cli-eslint', 'ember-cli-eslint');
}
let app = new EmberApp(defaults, {
addons: { blacklist },
});
And it works as desired.
A simplified package.json/script looks something like so:
"scripts": {
"eslint": "eslint .",
"stylelint": "stylelint app/styles",
"lint": "npm run eslint && npm run stylelint",
"start": "DISABLE_AUTO_LINT=true ember serve",
"test": "npm run lint --silent && DISABLE_AUTO_LINT=true ember exam --split=10 --parallel",
}
ember serve functions as business as usual.

How do I deploy monorepo code to AWS Lambda using lerna?

I am attempting to make two AWS Lambda functions (written in typescript). Both of these functions share the same code for interacting with an API. In order to not have to copy the same code to two different Lambdas, I would like to move my shared code to a local module, and have both my Lambdas depend on said module.
My initial attempt at staring code between the two lambdas was to use a monorepo and lerna. My current project structure looks like this:
- lerna.json
- package.json
- packages
- api
- package.json
- lambdas
- funcA
- package.json
- func B
- package.json
lerna.json:
{
"packages": [
"packages/api",
"packages/lambdas/*"
],
"version": "1.0.0"
}
In each of my package.json for my Lambda functions, I am able to include my local api module as such:
"dependencies": {
"#local/api": "*"
}
With this, I've been able to move the common code to its own module. However, I'm now not sure how to bundle my functions to deploy to AWS Lambda. Is there a way for lerna to be able to create a bundle that can be deployed?
As cp -rL doesn't work on the mac I had to come up with something similar.
Here is a workflow that works if all of your packages belong to one scope (#org):
In the package.json of your lerna repo:
"scripts": {
"deploy": "lerna exec \"rm -rf node_modules\" && lerna bootstrap -- --production && lerna run deploy && lerna bootstrap"
}
In the package that contains your lambda function:
"scripts":{
"deploy": "npm pack && tar zxvf packagename-version.tgz && rm -rf node_modules/#org && cp -r node_modules/* package/node_modules && cd package && npm dedupe"
}
Now replace "packagename-version" and "#org" with the respective values of your project. Also add all of the dependent packages to "bundledDependencies".
After running npm run deploy in the root of your lerna mono repo you end up with a folder "package" in the package that contains your lambda function. It has all the dependencies needed to run your function. Take it from there.
I had hoped that using npm pack would allow me to utilize .npmignore files but it seems that that doesn't work. If anyone has an idea how to make it work let me know.
I have struggled with this same problem for a while now, and I was finally forced to do something about it.
I was using a little package named slice-node-modules, as found here in this similar question, which was good enough for my purposes for a while. As I have consolidated more of my projects into monorepos and begun using shared dependencies which reside as siblings rather than being externally published, I ran into shortcomings with that approach.
I've created a new tool called lerna-to-lambda which was specifically tailored to my use case. I published it publicly with minimal documentation, hopefully enough to help others in similar situations. The gist of it is that you run l2l in your bundling step, after you've installed all of your dependencies, and it copies what is needed into an output directory which is then ready to deploy to Lambda using SAM or whatever.
For example, from the README, something like this might be in your Lambda function's package.json:
"scripts": {
...
"clean": "rimraf build lambda",
"compile": "tsc -p tsconfig.build.json",
"package": "l2l -i build -o lambda",
"build": "yarn run clean && yarn run compile && yarn run package"
},
In this case, the compile step is compiling TypeScript files from a source directory into JavaScript files in the build directory. Then the package step bundles up all the code from build along with all of the Lambda's dependencies (except aws-sdk) into the directory lambda, which is what you'd deploy to AWS. If someone were using plain JavaScript rather than TypeScript, they could just copy the necessary .js files into the build directory before packaging.
I realize this question is over 2 years old, and you've probably figured out your own solutions and/or workarounds since then. But since it is still relevant to me, I assume it's still relevant to someone out there, so I am sharing.
Running lerna bootstrap will create a node_modules folder in each "package". This will include all of your lerna managed dependencies as well as external dependencies for that particular package.
From then on, your deployment of each lambda will be agnostic of the fact that you're using lerna. The deployment package will need to include the code for that specific lambda and the node_modules folder for that lambda - you can zip these and upload them manually, or use something like SAM or CloudFormation.
Edit: as you rightly point out you'll end up with symlinks in your node_modules folder which make things awkward to package up. To get around this, you could run something like this prior to packaging for deployment:
cp -rL lambdas/funcA/node_modules lambdas/funcA/packaged/node_modules
The -L will force the symlinked directories to be copied into the folder, which you can then zip.
I have used a custom script to copy the dependencies during the install process.. This will allow me to develop and deploy the application with the same code.
Project structure
In the package.json file of the lambda_a, I have the following line:
"scripts": {
"install": "node ./install_libs.js #libs/library_a"
},
#libs/library_a can be used by the lambda code using the following statement:
const library_a = require('#libs/library_a')
for SAM builds, I use the following command from the lmbdas frolder:
export SAM_BUILD=true && sam build
install_libs.js
console.log("Starting module installation")
var fs = require('fs');
var path = require('path');
var {exec} = require("child_process");
if (!fs.existsSync("node_modules")) {
fs.mkdirSync("node_modules");
}
if (!fs.existsSync("node_modules/#libs")) {
fs.mkdirSync("node_modules/#libs");
}
const sam_build = process.env.SAM_BUILD || false
libs_path = "../../"
if (sam_build) {
libs_path = "../../" + libs_path
}
process.argv.forEach(async function (val, index, array) {
if (index > 1) {
var currentLib = libs_path + val
console.log(`Building lib ${currentLib}`)
await exec(`cd ${currentLib} && npm install` , function (error, stdout, stderr){
if (error) {
console.log(`error: ${error.message}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log('Importing module : ' + currentLib);
copyFolderRecursiveSync(currentLib, "node_modules/#libs")
});
}
});
function copyFolderRecursiveSync(source, target) {
var files = [];
// Check if folder needs to be created or integrated
var targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
// Copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
function copyFileSync(source, target) {
var targetFile = target;
// If target is a directory, a new file with the same name will be created
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}