UNPKG

@weichwarenprojekt/license-reporter

Version:

A tool that analyzes node modules and extracts the license information into a json file that can be used for rendering the third party software.

9 lines (8 loc) 8.01 kB
#!/usr/bin/env node "use strict";var T=Object.create;var d=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var F=Object.getPrototypeOf,M=Object.prototype.hasOwnProperty;var N=(e,o)=>()=>(o||e((o={exports:{}}).exports,o),o.exports),O=(e,o)=>{for(var r in o)d(e,r,{get:o[r],enumerable:!0})},y=(e,o,r,n)=>{if(o&&typeof o=="object"||typeof o=="function")for(let t of $(o))!M.call(e,t)&&t!==r&&d(e,t,{get:()=>o[t],enumerable:!(n=R(o,t))||n.enumerable});return e};var u=(e,o,r)=>(r=e!=null?T(F(e)):{},y(o||!e||!e.__esModule?d(r,"default",{value:e,enumerable:!0}):r,e)),S=e=>y(d({},"__esModule",{value:!0}),e);var j=N((ne,z)=>{z.exports={name:"@weichwarenprojekt/license-reporter",version:"1.0.0",description:"A tool that analyzes node modules and extracts the license information into a json file that can be used for rendering the third party software.",scripts:{build:"node esbuild.js && tsc --emitDeclarationOnly",cli:"ts-node src/index.ts",bundledCli:"node dist/license-reporter.js",test:"npm run build && jest",coverage:"npm run build && jest --coverage",prepublishOnly:"npm run build"},bin:"./dist/license-reporter.js",main:"./dist/license-reporter.js",types:"./dist/index.d.ts",repository:{type:"git",url:"git+https://github.com/Weichwarenprojekt/license-reporter.git"},author:"Weichwarenprojekt <info@weichwarenprojekt.de> (https://weichwarenprojekt.de/)",contributors:["Tim Niederer <tim.niederer@weichwarenprojekt.de> (https://weichwarenprojekt.de/)"],license:"MIT",bugs:{url:"https://github.com/Weichwarenprojekt/license-reporter/issues"},homepage:"https://github.com/Weichwarenprojekt/license-reporter#readme",keywords:["license","release","MIT","CI","CD","CLI"],dependencies:{"@weichwarenprojekt/ts-importer":"^0.1.7",chalk:"^4.1.2",commander:"^11.1.0",glob:"^10.3.10"},devDependencies:{"@jest/globals":"^29.7.0","@types/glob":"^8.1.0","@types/node":"^20.10.4","@typescript-eslint/eslint-plugin":"^6.14.0","@weichwarenprojekt/configuration":"^0.2.0","@weichwarenprojekt/wersion":"^1.2.1",esbuild:"^0.19.9",eslint:"^8.56.0","eslint-config-prettier":"^9.1.0","eslint-plugin-prettier":"^5.0.1",jest:"^29.7.0",prettier:"^3.1.1","ts-jest":"^29.1.1","ts-node":"^10.9.2",typescript:"^5.3.3"}}});var G={};O(G,{SearchMode:()=>m,cli:()=>k,defaultConfiguration:()=>a,loadConfiguration:()=>h});module.exports=S(G);var P=require("commander");var s=u(require("path")),x=require("@weichwarenprojekt/ts-importer");function l(e){return e.replace(/\\/g,"/")}var b=u(require("chalk")),m=(r=>(r.recursive="recursive",r.flat="flat",r))(m||{}),a={addFolder:[],config:"./license-reporter.config.ts",defaultLicenseText:"No license text found.",force:!1,ignore:[],ignoreMissingUrl:!1,output:"./3rdpartylicenses.json",overrides:[],root:process.cwd(),search:"recursive"};async function h(e){let o=Object.assign(a,e);try{let r=o.config;s.default.isAbsolute(r)||(r=s.default.resolve(o.root,r));let n=(0,x.loadModule)(r);n.configuration||console.warn('The specified configuration does not export a "configuration"'),o=Object.assign(o,n.configuration)}catch{console.warn(b.default.yellow("Could not find a configuration file!"))}return L(o)}function L(e){return s.default.isAbsolute(e.root)||(e.root=s.default.resolve(process.cwd(),e.root)),e.root=l(e.root),s.default.isAbsolute(e.output)||(e.output=s.default.resolve(e.root,e.output)),e.output=l(e.output),e.ignore=e.ignore.map(o=>{let r=l(s.default.isAbsolute(o)?o:s.default.resolve(e.root,o));return r.endsWith("/")?r:`${r}/`}),e.addFolder=e.addFolder.map(o=>l(s.default.isAbsolute(o)?o:s.default.resolve(e.root,o))),e}var c=u(require("path")),f=u(require("fs"));var p=u(require("chalk")),v=require("os"),g=require("glob");async function C(e){let o=await h(e),r=A(o),n=U(o,r),t=J(o,n),i=B(o,t);q(o,t),!o.force&&!i&&process.exit(1)}function A(e){let o=V(e),r=[];for(let n of o)r.push(...(0,g.globSync)(`${n}/**/package.json`));return D(e,r)}function V(e){let o=e.search==="recursive"?c.default.resolve(e.root,"**/"):e.root;o=c.default.resolve(o,"node_modules"),o=l(o);let r=(0,g.globSync)(o,{ignore:"**/node_modules/**/node_modules/**"});r=r.filter(n=>{for(let t of e.ignore)if(n.includes(t))return!1;return!0}),r.push(...e.addFolder),console.log(`Found ${r.length} ${r.length===1?"folder":"folders"}...`);for(let n of r)console.log(`- ${n}`);return r}function D(e,o){let r=o.filter(t=>{for(let i of e.ignore)if(t.includes(i))return!1;return!0}),n=r.length>0?`${c.default.dirname(r[0])}/`:"";for(let t=1;t<r.length;)r[t].includes(n)?r.splice(t,1):(n=`${c.default.dirname(r[t])}/`,t++);return console.log(`Found ${r.length} ${r.length===1?"package":"packages"}. Start processing...`),r.sort(),r}function U(e,o){let r=new Map;for(let n of o)try{let t=W(e,n);r.set(t.name,t)}catch(t){console.error(`Could not extract license information from "${n}".`),console.error(t)}return r}function W(e,o){let r=JSON.parse(f.default.readFileSync(o,"utf-8")),n=l(c.default.dirname(o)),t={name:typeof r.name=="string"?r.name:"",url:typeof r.homepage=="string"?r.homepage:"",licenseName:typeof r.license=="string"?r.license:"",licenseText:""};t.name||(t.name=c.default.basename(n));let i=typeof r.repository=="string"?r.repository:r.repository?.url;typeof i=="string"&&!t.url&&(t.url=i),t.url=t.url.replace(/^git\+/,""),t.url=t.url.replace(/^git:\/\//,"https://");let I={nocase:!0,cwd:n},_=(0,g.globSync)("licens*",I);_.length===0&&(_=(0,g.globSync)("copyin*",I));let w=_[0];return w&&(t.licenseText=f.default.readFileSync(c.default.resolve(n,w),"utf-8")),t.licenseText||(t.licenseText=e.defaultLicenseText),t}function J(e,o){for(let n of e.overrides){let t=n.name;if(!t)continue;let i=o.get(t);i?o.set(t,Object.assign(i,n)):o.set(t,{name:t,url:n.url??"",licenseName:n.licenseName??"",licenseText:n.licenseText??""})}let r=Array.from(o.values());return r.sort((n,t)=>n.name.localeCompare(t.name)),r}function B(e,o){let r=!0,n=(t,i)=>{r=!1,console.warn(p.default.yellow(`No "${p.default.bold(t)}" was found for the package "${p.default.bold(i)}". You can add "overrides" to the reporter configuration to manually complete the information of a package.`))};for(let t of o)!t.url&&!e.ignoreMissingUrl&&n("url",t.name),t.licenseName||n("licenseName",t.name),t.licenseText||n("licenseText",t.name);return r}function q(e,o){f.default.mkdirSync(c.default.dirname(e.output),{recursive:!0}),f.default.writeFileSync(e.output,`${JSON.stringify(o,null,4)}${v.EOL}`),console.log(p.default.green(`Finished. Results were written to "${p.default.bold(e.output)}"`))}var E=` __ ______ _ _ ____ _ \\ \\ / / _ \\ | | (_) ___ ___ _ __ ___ ___ | _ \\ ___ _ __ ___ _ __| |_ ___ _ __ \\ \\ /\\ / /| |_) | | | | |/ __/ _ \\ '_ \\/ __|/ _ \\ | |_) / _ \\ '_ \\ / _ \\| '__| __/ _ \\ '__| \\ V V / | __/ | |___| | (_| __/ | | \\__ \\ __/ | _ < __/ |_) | (_) | | | || __/ | \\_/\\_/ |_| |_____|_|\\___\\___|_| |_|___/\\___| |_| \\_\\___| .__/ \\___/|_| \\__\\___|_| |_|`,Y=new P.Command;async function k(e){let o=j();await Y.version(o.version).description(`${E} ${o.description}`).action(C).option("--config <value>","The path to the configuration file.",a.config).option("--defaultLicenseText <value>","The default license text that is used if the tool can't find a license text for a package.",a.defaultLicenseText).option("--force","Forces a good exit.",a.force).option("--ignoreMissingUrl","If true, license-reporter will not fail and warn you because of missing urls.",a.ignoreMissingUrl).option("--output <value>","The path to the output file.",a.output).option("--root <value>","The path to the root directory.",a.root).option("--search <value>",'The search mode. Can be "flat" or "recursive".',a.search).parseAsync(e)}k(process.argv).catch(e=>{console.error("Something unexpected happened!"),console.error(e),process.exit(1)});0&&(module.exports={SearchMode,cli,defaultConfiguration,loadConfiguration});