问题
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