Manzur Khan
Manzur Khan

Reputation: 2486

Skip npm postinstall script in Travis

I have an app that deploys on AWS as well as Cloud66(for some reason). In order to build the app for aws, we have to write the script in travis.yml and to deploy to Cloud66 we have to write the build commands in postInstall.

What happens is when travis is triggered, it does npm install for which it runs the postScript also which builds the application, also again in the yml script the app build is made. Thus there are two builds happening each time a change is triggered, which is unnecessary.

I tried to remove the postInstall script by adding an environment var in travis and running a preinstall script that will delete the postInstall script in the package. This deletes the postInstall script but it doesn't change anything, postInstall script somehow runs even then.

snippet of package.json

"preinstall": "node setEnvironment.js",
"postinstall": "npm run build-production"

snippet of travis.yml

script:
- if [ "$TRAVIS_BRANCH" = "prod" ]; then npm run build-production && gulp
  copy-deploy-configs --type=prod; else echo "not an prod branch"; fi

setEnvironment.js

const package = require('./package.json');
const fs = require('fs');

const environment = process.env.APP_ENVIRONMENT;

if (environment === 'travis') {
    delete package.scripts.postinstall;
} else {
    package.scripts["postinstall"] = `npm run build-${environment}`;
}

fs.writeFileSync('package.json', JSON.stringify(package, null, 4));

Upvotes: 2

Views: 1192

Answers (1)

alex kucksdorf
alex kucksdorf

Reputation: 2633

In your package.json you could do the following:

"postinstall": "if [ -z \"$TRAVIS_BRANCH\" ]; then npm run build-production; fi"

This should only be executed if $TRAVIS_BRANCH is not set, which should only happen on Cloud66.

Upvotes: 3

Related Questions