tslint to check each file if there is specific string present at the start of the file

Aniruddha Das

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.

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.

/*
* 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.

Is there any way I can achieve it.

Aniruddha Das

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

1.install the module

npm i --save-dev pre-commit

2.develop a script which will run as procommit hook to find specific sting in your code.

// 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

{
  "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"
  ]
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

read through each line and check if line start with a specific string using batch file

How to check if a specific number is present in the lines of a file?

check if string is present in a list and output a third file if string is present

Check if each ip of a file is present or not in another file, then acting in consequence

Check file is present and it is not empty

Erlang - search for a specific string within an external file and append file if not present

How to check if a specific block of text is present in a file using Ansible?

How to check if all characters in a file is present in a specific encoding?

Read in a file and skip lines that start with a specific string

To check whether particular string is present in text file using robotframework

How blacklist imports in a specific file with tslint

How to check for a specific string with linebreaks in a file with grep?

Python: Check if a specific string of text is in a file

Iteration over file - check if the string from other file is present in the line of source file

identify a file with specific chars present in it and rename the file

check if each of string is present in another however the order

Check only specific field present in the string

Check if a substring is present at a specific location in a string

How do check for a specific character at the end of each line in a text file

How to check if each <book> element has a specific subchild in xml file

for each line of a file, grep a specific string and make string substitution

I want to loop though each sub-directories & check if *.v or *.sv file is present or not , if it is present then do some operation using csh script?

Check if string is in file, if so, replace that specific line that contains the string

How to extract all variables that start with specific string from netCDF file?

How to check if a file is present in a particular directory in Unix?

Only check whether a line present in a file (ansible)

Check if a folder has a certain type of file present

Check if a particular file is present in the folder using python

Plot scatterplot colour specific values present in file