Run gulp task after NPM package install without any command

孤街浪徒 提交于 2019-12-13 02:55:22

问题


I've developed a small angular package that is hosted on npmjs. when I try to install my package I want to change my "selector" name so that I have written one gulp task like below:

gulp.task('tag-change', function () {
 // var files = fs.readFileSync('./node_modules/@syncfusion/ej2-angular-buttons/@syncfusion/ej2-angular-buttons.es5.js', 'utf8');
 var files = glob.sync('./dist/@syncfusion/*');
 
 for (var i = 0; i < files.length; i++) {
 var sourceFile = fs.readFileSync(files[i],'utf8');
 sourceFile = sourceFile.replace(`selector: '[ejs-button]'`,`selector: '[KD-button]'`);
 fs.writeFileSync(files[i], sourceFile, 'utf8');
 }
});

I want to run this task after my package got installed. for this, I have analyzed and found that we can able to use npm postinstall.

Then i have tried like below:

 "dependencies": {
    "postinstall": "*"
  },
  "scripts": {
    "postinstall": "gulp tag-change",
    "packagr": "ng-packagr -p ng-package.json && gulp npmrc-changelog-schematics-injection && gulp path-change"
  }

But it throws below error:

I have referred this gulp task reference from this issue - Run gulp task after NPM package install

my package structure lokks like below:


回答1:


Finally, I found out a solution to achieve my requirements.

I have created a tagchange.js file and placed the below content.

var fs = require('fs');
var glob = require('glob');

var files = glob.sync('./@syncfusion/*');
 
 for (var i = 0; i < files.length; i++) {
 var sourceFile = fs.readFileSync(files[i],'utf8');
 sourceFile = sourceFile.replace(`selector: '[ejs-button]'`,`selector: '[Kumar-button]'`);
 fs.writeFileSync(files[i], sourceFile, 'utf8');
 }

And I have called this in my package package.json file like below:

  "dependencies": {
    "postinstall": "*"
  },
  "scripts": {
    "postinstall": "node ./tagchange.js",
    "packagr": "ng-packagr -p ng-package.json && gulp npmrc-changelog-schematics-injection && gulp path-change"
  }

It is working fine




回答2:


gulp.task('tag-change', function () {
 // var files = fs.readFileSync('./node_modules/@syncfusion/ej2-angular-buttons/@syncfusion/ej2-angular-buttons.es5.js', 'utf8');
 var files = glob.sync('./dist/@syncfusion/*');
 
 for (var i = 0; i < files.length; i++) {
 var sourceFile = fs.readFileSync(files[i],'utf8');
 sourceFile = sourceFile.replace(`selector: '[ejs-button]'`,`selector: '[KD-button]'`);
 fs.writeFileSync(files[i], sourceFile, 'utf8');
 }
});


来源:https://stackoverflow.com/questions/58236303/run-gulp-task-after-npm-package-install-without-any-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!