简体   繁体   中英

Combining all of the node_modules packages' LICENSE files into one file

In my node_modules folder, let's say that I have 5 folders:

Folder1

Folder2

Folder3

Folder4

Folder5

Each folder contains a "LICENSE" file or "LICENSE.md" file. I want to run a script of some kind whether it be Python or JS that takes the contents of each LICENSE file along with name of package and add it to one single file "Destination.txt". Is there an easier way to do this? I know that NPM License Checker allows me to list licenses and then I can output the results into a text file, but I also want to see the contents of each LICENSE file into a single file.

So sample output for Destination.txt should be like:

Folder1

text from LICENSE file

Folder2

text from LICENSE file

Folder3

text from LICENSE file

Folder4

text from LICENSE file

Folder5

text from LICENSE file

What I'm thinking about is possibly a Python script that traverses each folder in node_modules and tries to find a LICENSE file. When it goes into a folder and finds a LICENSE, it copies / pastes the contents into Destination.txt and then moves on to the next LICENSE file. I'm not sure how to implement this and would appreciate any tips. Doesn't have to be Python. I'm on a Windows computer so if there's a creative way to use the CLI commands to generate what I'm looking for, that'd be great.

TLDR; I want to list the names of my packages as well as the contents of their associated LICENSE files all in one file. So if package1 is listed, the contents of the package1 LICENSE file will be listed below it. Same with package2 and so on.

most languages has a package that already does that. eg

nodejs:

python:

ruby:

each of which has ability to produce a json file, which you can then parse and combine as you wish.

if you specifically interested in npm packages, then just use npm view like so

$ npm view eslint --json license
"MIT"

and if you want it to all your npm packages, then you can combine it with npm ls like so

for PKG in $(npm ls --json | jq -r '.dependencies | keys | .[]')
do
  npm view $PKG name license --json
done

The closest thing I've been able to find that doesn't require a custom script is Yarn Licenses .

I used command yarn licenses generate-disclaimer > output.txt

It worked even though I have never installed anything via Yarn before. Only NPM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM