[英]tslint to check each file if there is specific string present at the start of the file
I am working on a project which need to be open source now and we need to add Apache license string at top of the each file.我正在开发一个现在需要开源的项目,我们需要在每个文件的顶部添加 Apache 许可证字符串。
Having said that, I want my tslint
to check if a particular string is present at top of each typescript file and show error if that string is not present.话虽如此,我希望我的
tslint
检查每个打字稿文件顶部是否存在特定字符串,如果该字符串不存在则显示错误。
/*
* Copyright 2017 proje*** contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
I did not see any TS Lint configuration to check if a string is present or not.我没有看到任何用于检查字符串是否存在的 TS Lint 配置。
Is there any way I can achieve it.有什么办法可以实现它。
You should write custom rule , include it into your tslint.json
and define it's directory in the rulesDirectory
property.您应该编写自定义规则,将其包含到您的
tslint.json
并在rulesDirectory
属性中定义它的目录。
I think that there are no suitable built-in visit function for leading comment in source file, so you can use the applyWithFunction
abstract method from the Lint.Rules.AbstractRule
class.我认为没有合适的内置访问函数用于在源文件中引导注释,因此您可以使用
Lint.Rules.AbstractRule
类中的applyWithFunction
抽象方法。 Examples can be found in tslint built-in rules sources .示例可以在tslint 内置规则源中找到。
After reviewing many options, I placed a pre-commit hook in our code and configure the node script to execute before a commit is tried on the repository.在查看了许多选项后,我在我们的代码中放置了一个预提交钩子,并配置节点脚本以在对存储库尝试提交之前执行。
There is a module available in npmjs which will make it more easy and you can do it easily npmjs 中有一个可用的模块,这将使它更容易,您可以轻松完成
1.install the module 1.安装模块
npm i --save-dev pre-commit
2.develop a script which will run as procommit hook to find specific sting in your code. 2. 开发一个脚本,该脚本将作为 procommit 钩子运行以查找代码中的特定字符串。
// code to find specific string
(function () {
var fs = require('fs');
var glob = require('glob-fs')();
var path = require('path');
var result = 0;
var exclude = ['LICENSE',
path.join('e2e', 'util', 'db-ca', 'rds-combined-ca-bundle.pem'),
path.join('src', 'favicon.ico')];
var files = [];
files = glob.readdirSync('**');
files.map((file) => {
try {
if (!fs.lstatSync(file).isDirectory() && file.indexOf('.json') === -1
&& exclude.indexOf(file) === -1) {
var data = fs.readFileSync(file, 'utf8');
if (data.indexOf('Copyright 2017 candifood contributors') === -1) {
console.log('Please add License text in coment in the file ' + file);
result = 1;
}
}
} catch (e) {
console.log('Error:', e.stack);
}
});
process.exit(result);
})();
3.place the hook to be executed in the package.json 3.将要执行的hook放在package.json中
{
"name": "project-name",
"version": 1.0.0",
"license": "Apache 2.0",
"scripts": {
"license-check": "node license-check",
},
"private": true,
"dependencies": {
},
"devDependencies": {
"pre-commit": "1.2.2",
},
"pre-commit": [
"license-check"
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.