Use enviroment variables in yarn package.json - heroku

I want to pull from a private package hosted on bitbucket. Since SSH is not an option for my deploy setup, I want to access the repo using the Application Password.
So my entry in the package JSON looks like this:
"dependencies": {
"#companyName/repository": "git+https://${$BITBUCKET_USER}:${BITBUCKET_APP_PASSWORD}#bitbucket.org/company name/repository.git",
Coding username and password hard into the repo URL works fine but when I perform yarn install as above, the environment variables are not replaced by its values.
Is there any way to use environment variables like this?

You can write a preinstall hook that updates package.json with values from the environment. Luckily the order of lifecycle hooks work as prescribed using yarn.
{
"name": "njs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"preinstall": "node preinstall.js"
},
"dependencies": {
"#companyName/repository": "git+https://${$BITBUCKET_USER}:${BITBUCKET_APP_PASSWORD}#bitbucket.org/companyName/repository.git"
},
"author": "",
"license": "ISC"
}
preinstall.js example:
const package = require('./package.json');
const fs = require('fs');
const {BITBUCKET_USER = 'test', BITBUCKET_APP_PASSWORD='test'} = process.env;
package.dependencies["#companyName/repository"] = package.dependencies["#companyName/repository"]
.replace("${$BITBUCKET_USER}", BITBUCKET_USER)
.replace("${BITBUCKET_APP_PASSWORD}", BITBUCKET_APP_PASSWORD);
fs.writeFileSync('package.json', JSON.stringify(package, null, 4));
Bonus:
How you choose to replace environment variables in preinstall.js is left to your good judgment. Yes, you can totally use ES6 template tags.

Related

How to run a previously built Canvas API project in simulator/smart-display?

I am trying to clone an application, which is given in their document under canvas API section. They have given an option for sample walkthrough [https://developers.google.com/actions/interactivecanvas/sample-walkthrough]. I have followed every step but I am getting an error that my intents are not handled.
You can see the code at [github.com/actions-on-google/dialogflow-snowman-nodejs.git].
I have tried seeing all the intent handlers, Also, i have tried changing all the packages to the lastest one, as i have mentioned in the question.
I am using following packages:
{
"name": "snowman-canvas-sample-functions",
"description": "Snowman Actions on Google Interactive Canvas Sample Functions",
"license": "Apache-2.0",
"private": true,
"engines": {
"node": "8"
},
"dependencies": {
"actions-on-google": "preview",
"firebase-admin": "~5.13.1",
"firebase-functions": "^2.0.2"
},
"scripts": {
"lint": "./node_modules/.bin/eslint --fix \"**/*.js\"",
"start": "firebase serve --only functions",
"deploy": "firebase deploy --only functions",
"test": "npm run lint"
},
"devDependencies": {
"eslint": "^6.0.1",
"eslint-config-google": "^0.13.0"
}
}
i am getting "Dialogflow IntentHandler not found for intent: Welcome at Function" error in the welcome intent itself. Please assist me what should i try next.
You need to make sure your Dialogflow intent name and your Firebase Function intent handler name are the same.
From here, you'd need to implement a handler:
const { dialogflow } = require('actions-on-google')
const app = dialogflow()
app.intent('Welcome', conv => {
conv.ask('Welcome!')
})

How to deploy Next.js with GraphQL backend on Zeit Now?

I have an Next.js/Express/Apollo GraphQL app running fine on localhost.
I try to deploy it on Zeit Now, and the Next.js part works fine, but the GraphQL backend fails because /graphql route returns:
502: An error occurred with your deployment
Code: NO_STATUS_CODE_FROM_LAMBDA
My now.json looks like:
{
"version": 2,
"builds": [
{ "src": "next.config.js", "use": "#now/next" },
{ "src": "server/server.js", "use": "#now/node" }
],
"routes": [
{ "src": "/api/(.*)", "dest": "server/server.js" },
{ "src": "/graphql", "dest": "server/server.js" }
]
}
Suggestions?
Here’s a complete example of Next.js/Apollo GraphQL running both on Zeit Now (as serverless function/lambda) and Heroku (with an Express server):
https://github.com/tomsoderlund/nextjs-pwa-graphql-sql-boilerplate
I was getting that error until I found on a solution on the Wes Bos slack channel.
The following worked for me, but it's possible you could be getting that error for a different reason.
I'm not sure why it works.
You can see it working here
cd backend
Run npm install graphql-import
Update scripts in package.json:
"deploy": "prisma deploy --env-file variables.env&& npm run writeSchema",
"writeSchema": "node src/writeSchema.js"
Note: For non windows users make sure to place space before &&
Create src/writeSchema.js:
const fs = require('fs');
const { importSchema } = require('graphql-import');
const text = importSchema("src/generated/prisma.graphql");
fs.writeFileSync("src/schema_prep.graphql", text)
Update src/db.js:
const db = new Prisma({
typeDefs: __dirname + "/schema_prep.graphql",
...
});
Update src/createServer.js:
return new GraphQLServer({
typeDefs: __dirname + '/schema.graphql',
...
});
Update src/schema.graphql:
# import * from './schema_prep.graphql'
Create now.json
{
"version": 2,
"name": "Project Name",
"builds": [
{ "src": "src/index.js", "use": "#now/node-server" }
],
"routes": [
{ "src": "/.*", "dest": "src/index.js" }
],
"env": {
"SOME_VARIABLE": "xxx",
...
}
}
Run npm run deploy to initially create schema_prep.graphql.
Run now
Another reply said this:
You should not mix graphql imports and js/ts imports. The syntax on the graphql file will be interpreted by graphql-import and will be ignored by ncc (the compiler which reads the __dirname stuff and move the file to the correct directory etc)
In my example 'schema_prep.graphql' is already preprocessed with the imports from the generated graphql file.
Hopefully this helps.

How to use the result of an NPM script in another NPM script?

It might be simple. But I can't get it work.
Let's consider the simple (and troncated) package.json below.
{
"name": "appName",
"version": "1.0.0",
"TEST": "1-0-0",
"scripts": {
"TEST_IN_SCRIPTS": " echo ${npm_package_version} | sed 's/\\./-/g' ",
"deploy": "gcloud app deploy --version ${npm_package_scripts_TEST_IN_SCRIPTS}"
},
"dependencies": {
"express": "^4.16.2",
...
}
}
I want to deploy an app with --version equals version (aka 1.0.0).
However, Google App Engine does not allow . (dot).
The idea is then to deploy a 1-0-0 (instead of 1.0.0) which is allowed by GAE.
TEST_IN_SCRIPTS works and returns 1-0-0
However, when I pass ${npm_package_scripts_TEST_IN_SCRIPTS} to deploy script, it fails because it returns the string ${npm_package_scripts_TEST_IN_SCRIPTS} instead of its result (1-0-0).
Any clue to make it works?
Thanks.
Try piping the output of the TEST_IN_SCRIPTS:
"deploy": "npm run TEST_IN_SCRIPTS | xargs gcloud app deploy --version",
I've found a simple solution that works for me.
Thanks #Juan, you show me the road!
Below my troncated package.json
{
"name": "...",
"version": "1.0.1",
"scripts": {
"deploy": "GAE_VERSION=$(echo ${npm_package_version} | sed 's/\\./-/g') && gcloud app deploy --version $GAE_VERSION"
},
"dependencies": {...},
"devDependencies": {...}
}
This is - in fact - pretty simple.
get npm_package_version and change with sed . by - (GAE is ok with hyphen)
Assign the result to a variable (here : GAE_VERSION)
Use gcloud command to deploy using GAE_VERSION variable.
An identical question where I post this very same answer.

How to properly create a types npm package for a private feed

I have a private NPM package feed I want to use for scoped packages.
I've read the Publishing article of the TypeScript docs. Also I tried to create the package according to the #types/jquery package.
So this is what I came up with: Created (and published) a package with the name "#myscope/i18n". It's a pretty simple one and contains a .d.ts file with the declaration of a global interface I18N with the method translate(string). The tarball includes the following files:
index.d.ts
/**
* Static members of I18N
*/
interface I18NStatic {
/**
* Returns the translation value based on the specified key
*
* #param key The translation key.
*/
translate(key: string): string;
}
declare module "i18n" {
export = I18N;
}
declare var I18N: I18NStatic;
package.json
{
"name": "#myscope/i18n",
"version": "1.0.11",
"description": "TypeScript definitions for MyScope Translator",
"license": "MIT",
"main": "",
"repository": {
"type": "git",
"url": "https://myscope.visualstudio.com/example/_git/i18n"
},
"scripts": {},
"dependencies": {},
"peerDependencies": {},
"typeScriptVersion": "2.0"
}
I consume it in a ASP.NET Core project with the following package.json
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
"#types/jquery": "2.0.48",
"#types/semantic-ui": "2.2.4",
"#myscope/i18n": "1.0.11"
}
}
But when I try to use I18N.translate(string) in my TS files I always get this Error
Error TS2304 (TS) Cannot find name 'I18N'. Example.UI.WebApp (tsconfig project) C:\Users\Sandro\Source\Repos\example\Example.UI.WebApp\TypeScript\Address.ts
When I use index.d.ts directly in my project (not via npm) it works perfectly. But as soon as I delete it again, it stops working. I have no errors or warnings in the "Dependencies/npm" section of the project in solution explorer view.
What am I missing?

grunt-contrib-jshint not show message

i have created a ne node project, i have installed a lot of modules and create the first index.js file
package.json
{
"name": "parser-test",
"version": "1.0.0",
"description": "parser test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "luca bottoni",
"license": "ISC",
"dependencies": {
"node-cmd": "^3.0.0"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt-contrib-jshint": "^1.1.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-exec": "^3.0.0"
}
}
index.js (it's failed, because i want see jshint message!)
console.log("test");
a={a=6};
if i send the cmd jshint index.js from project folder i see (work fine):
index.js: line 3, col 5, Expected ':' and instead saw '='.
1 error
now i want use the grunt task to check my file
Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch:{
scripts:{
files:["./*.js"],
tasks:['jshint']
}
},
jshint: {
files: ['Gruntfile.js',"index.js"]
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
// Default task(s).
grunt.registerTask('default', ['watch:scripts']);
grunt.registerTask('jshint', ['jshint']);
};
now i try use single task jshint with command grunt jshint, but the don't see any message. if i use the watch, watch task only the first time and after not check any change on file index.js.
i can not understand why the direct command jshint work, but the task grunt stay freezed
i have resolved after many test
i have turn-on verbose information on grunt command
grunt jshint --verbose and i have see infinite:
Running "jshint" task
Running "jshint" task
Running "jshint" task
Running "jshint" task
Running "jshint" task
Running "jshint" task
The register task and task in definition have the same name "jshint" and i have modified the name in grunt.registerTask('jshints', ['jshint']);
now work fine

Resources