Referring to the repository url in package.json in a script - npm

As usual in my package.json file I have my repository url defined:
{
"repository": {
"type": "git",
"url": "http://my.git.repo"
}
}
I am trying to define a script that will push to my repo and push tags. Usually something like this would do the trick:
{
"scripts": {
"push": "git push origin && git push origin --tags"
}
}
but we work with pull requests and for us origin is our private fork. public is usually the public repo but this can't be relied upon. I want my push script to always push to the public repo:
{
"scripts": {
"push": "git push http://my.git.repo && git push http://my.git.repo --tags"
}
}
but this involves duplicating the repo url and when people copy this seed project they will forget to update the url in all 3 places. I want to refer to the url of the repo that is already specified in the package.json
I have tried something along the lines of this:
{
"scripts": {
"push": "git push $npm_package_repository_url && git push $npm_package_repository_url --tags"
}
}
based on this webpage: https://docs.npmjs.com/misc/scripts#packagejson-vars but it doesn't work.
Many thanks

The syntax $npm_package_repository_url will work fine on Mac OS X/Linux (bash), however %npm_package_repository_url% is required for Windows. You could try utilizing cross-var to enable one script syntax (i.e. $npm_package_repository_url) to work cross-platform.
Thanks to RobC for this

Related

What should be used for endpoint in renovate-bot config.json?

I am trying to set up config.json for Bitbucket Cloud to automatically update dependencies in npm repos of Bitbucket Cloud. I found one example, but cannot figure out two things:
endpoint - what should go there (ABC)? - our company's bitbucket namespace link looks like: https://bitbucket.org/uvxyz/
Can I use renovate-bot to issue PRs without bitbucket pipelines? If so, can I make renovate to update only particular repo or repos via config.json mods or I shall put renovate.json file in each repo where automatic dependency update is required?
appreciate any examples on the latter.
config.json:
module.exports = {
"platform": "bitbucket",
"username": "<my.username>",
"password": "<bitbucket token on my account>",
"endpoint": "ABC",
"hostRules": [
{
"hostType": "bitbucket",
"domainName": "ABC",
"timeout": 10000,
"username": "<my.username>",
"password": "<bitbucket token on my account>"
}
]
};
according to the code:
const BITBUCKET_PROD_ENDPOINT = 'https://api.bitbucket.org/';
const defaults = { endpoint: BITBUCKET_PROD_ENDPOINT };
there's a default, it works for me without setting it
what you see in the documentation is all you need
I was able to get renovate working with BB after putting the following configuration into its config.js file as
{
hostType: 'bitbucket',
matchHost: 'https://api.bitbucket.org/2.0/',
username: "bb-username",
password: "<special app password generated for bb-username>",
}
for BitBucket app passwords please look at
https://support.atlassian.com/bitbucket-cloud/docs/create-an-app-password/
and
https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/

How to comment in package-scripts.js to show up in package.json?

Goal
I want to display comments of my nps commands from package-scripts.js in my package.json like this:
Tried
I actually don't know where the content of this overlay ("Run by the 'npm test' command") is coming from (vscode, npm, nps?). I've looked up all of their GitHub repositories without success.
I've tried to add comments the following way with no results:
// package-scripts.js
module.exports = {
scripts: {
/** Build image */
build: "docker build"
}
}
// package.json
{
scripts: {
"build": "nps build"
}
}
Is it possible at all? How is the shown comment made?
This seems to work with:
json schemas in your vscode settings or
switching to jsonc mode in vscode
Both are documented here: https://code.visualstudio.com/docs/languages/json.
I suggest to extend the package.json schema https://json.schemastore.org/package to fit your needs, but i don't have any experience with displaying comments as tooltips in vscode.

Environment variable in Vercel redirects

I want to deploy my create-react-app to Vercel.
I define my redirects in my now.json as follows:
{
"redirects": [
{ "source": "/api/(.*)", "destination": "BACKEND_URL/$1", "statusCode": 200 }
]
}
The destination URL depends on the environment variable BACKEND_URL, which is defined in the Vercel dashboard.
I am trying to replace the environment variable in the redirects in the following build command:
sed -i "s|BACKEND_URL|${BACKEND_URL}|g" now.json && yarn build
But unfortunately now.json doesn't seem to be available at build time:
09:54:44.243 sed: can't read now.json: No such file or directory
How to enable dynamic redirects in Vercel?
This is not possible since now.json is read to determine how to build so you can't dynamically generate it during a build.
Instead, consider using a framework like Next.js which provides a next.config.js that can read environment variables as defined in RFC 9081.
npm install next#canary react react-dom
// next.config.js
module.exports = {
experimental: {
async redirects() {
return [{
source: "/api/:path*",
destination: `${process.env.BACKEND_URL}/:path*`,
permanent: false,
}]
}
}
};
https://github.com/zeit/now/discussions/4351
I think this actually is possible.
If you are using create-react-app then you just need to preface your env var name with REACT_APP_. See here: https://create-react-app.dev/docs/adding-custom-environment-variables/
I am currently doing this with a websocket URL env var. I have named it REACT_APP_WS_URL and here is how I use it.
I have 2 different vercel files in the project:
vercel.staging.json which has this section:
"build": {
"env": {
"REACT_APP_WS_URL": "wss:staging.my-backend.com/socket"
}
}
vercel.live.json which has this section:
"build": {
"env": {
"REACT_APP_WS_URL": "wss:my-backend.com/socket"
}
}
I deploy to them with either of these commands:
vercel deploy --prod -A vercel.staging.json
vercel deploy --prod -A vercel.live.json
and in my code I can access process.env.REACT_APP_WS_URL anywhere.
I have not tried doing this with the Vercel dashboard env vars but it might be worth trying your original approach except rename your env var to REACT_APP_BACKEND_URL.
Note: my deployment commands only work when I don't assign domains to the project. If I assign domains to a project, they are automatically used for ALL --prod deploys, no matter what is in my alias field in the json config file.

Using the FileAPI library with browserify

The FileAPI library (https://github.com/mailru/FileAPI/issues/202) does not officially support CommonJS modules. I've tried using browserify-shim but I'm not able to make it work. After requireing fileapi I just get an empty object back. I've created a repo for reproduction here https://github.com/Prinzhorn/browserify-fileapi
Relevant package.json part
{
"dependencies": {
"fileapi": "2.0.15"
},
"devDependencies": {
"browserify": "11.1.0",
"browserify-shim": "3.8.10"
},
"browser": {
"fileapi": "./node_modules/fileapi/dist/FileAPI.html5.js"
},
"browserify-shim": {
"fileapi": "FileAPI"
}
}
If you want to try it locally:
git clone git#github.com:Prinzhorn/browserify-fileapi.git
npm install
npm run build
chromium-browser index.html
Check out the console in Chromium, you'll see an empty array from running console.log(Object.keys(require('fileapi'))). Note that there is a global window.FileAPI with the correct API.
Does anyone know if browserify-shim is able to shim FileAPI? Because I believe it does some exotic things to manage it's dependencies (the concatenated files expect certain globals).
You'll need to tell browserify to use browserify-shim as a transform in the package.json as outlined in this example
Mainly you're missing:
"browserify": {
"transform": [ "browserify-shim" ]
}

Utilising composers asset-installer-paths not working

I'm trying to install a certain package in composer into a certain path, the package name is cyphix333/nbbc and normally it would be installed into vendor/cyphix333/nbbc however I wanted to install it into vendor/nbbc so I tried this in the main project composer.json
"require": {
//......
"cyphix333/nbbc": "dev-master"
},
"extra": {
"asset-installer-paths": {
//.....
"cyphix333/nbbc": "vendor/nbbc"
}
}
However it didn't work, it still installed into vendor/cyphix333/nbbc.
Edit: ...and here is the full data from the extra part, which comes from my php framework yii2:
"extra": {
"yii\\composer\\Installer::postCreateProject": {
"setPermission": [
{
"runtime": "0777",
"web/assets": "0777",
"yii": "0755"
}
],
"generateCookieValidationKey": [
"config/web.php"
]
},
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower",
"cyphix333/nbbc": "vendor/nbbc"
}
}
What am I doing wrong here?
1. asset-installer-paths = Composer plugin fxp/composer-asset-plugin
The asset-installer-paths directive belongs to the Composer plugin fxp/composer-asset-plugin.
The plugin is required, for this directive to work. But you are not requiring it in your project repo or globally.
Docu - Installation
composer require "fxp/composer-asset-plugin:~1.0"
or
composer global require "fxp/composer-asset-plugin:~1.0"
2. Is cyphix333/nbbc a Bower or NPM asset?
No.
3. What am I doing wrong here?
You think, that you can use asset-installer-paths directive to move your package to a specific folder. You can't. Because your repo is not an Bower or NPM asset.
4. How can i move my package from vendor/cyphix333/nbbc to vendor/nbbc?
Stop trying that. It's wasted time. Why? Composer has an Autoloader and he does the mapping from classname to filename. Please do not care about the path, just define an autloading strategy (files or classmap) and start using your class.
If you really need to copy stuff, you might use the scripts section of your composer.json.
https://getcomposer.org/doc/articles/scripts.md