AceMetrix / bower-license

Generates a list of bower dependencies
Apache License 2.0
15 stars 29 forks source link

license check fails if package.json does not contain repository URL #18

Closed sebthom closed 8 years ago

sebthom commented 8 years ago

We are getting the following exception in some of our projects.

/opt/node-6.3.0/lib/node_modules/bower-license/node_modules/npm-license/lib/index.js:32
                moduleInfo.repository = json.repository.url.replace('git://github.com', 'https://github.com').replace('.git', '');
                                                           ^

TypeError: Cannot read property 'replace' of undefined
    at flatten (/opt/node-6.3.0/lib/node_modules/bower-license/node_modules/npm-license/lib/index.js:32:60)
    at /opt/node-6.3.0/lib/node_modules/bower-license/node_modules/npm-license/lib/index.js:131:9
    at /opt/node-6.3.0/lib/node_modules/bower-license/node_modules/read-installed/read-installed.js:142:5
    at /opt/node-6.3.0/lib/node_modules/bower-license/node_modules/read-installed/read-installed.js:263:14
    at asyncMap (/opt/node-6.3.0/lib/node_modules/bower-license/node_modules/slide/lib/async-map.js:27:18)
    at next (/opt/node-6.3.0/lib/node_modules/bower-license/node_modules/read-installed/read-installed.js:234:5)
    at /opt/node-6.3.0/lib/node_modules/bower-license/node_modules/read-installed/read-installed.js:167:12
    at /opt/node-6.3.0/lib/node_modules/bower-license/node_modules/read-package-json/read-json.js:356:5
    at checkBinReferences_ (/opt/node-6.3.0/lib/node_modules/bower-license/node_modules/read-package-json/read-json.js:320:45)
    at final (/opt/node-6.3.0/lib/node_modules/bower-license/node_modules/read-package-json/read-json.js:354:3)

The reason is that some bower modules are missing a repository URL in their package.json. We could work around it by changing in /opt/node-6.3.0/lib/node_modules/bower-license/node_modules/npm-license/lib/index.js

    if (!unversioned[json.name]){
        if (json.repository) {
            if (typeof json.repository === 'object') {
                   moduleInfo.repository = json.repository.url.replace('git://github.com', 'https://github.com').replace('.git', '');
            }
    if (!unversioned[json.name]){
        if (json.repository) {
            if (typeof json.repository === 'object') {
                if(typeof json.repository.url === "string") // NEW: test if url actually is a string
                   moduleInfo.repository = json.repository.url.replace('git://github.com', 'https://github.com').replace('.git', '');
            }
sebthom commented 8 years ago

David Glass's license checker seems to already handle repository URLs in a more robust way https://github.com/davglass/license-checker/blob/master/lib/index.js#L53

anacronw commented 8 years ago

I'll be happy to accept a PR if you can get to that

Thanks