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