2019-01-14 21:34:47 +00:00
import * as path from 'path'
2019-01-16 14:46:42 +00:00
import { promisify } from 'util'
2019-01-14 21:34:47 +00:00
2019-01-16 13:01:06 +00:00
import { licenseOverrides } from './license-overrides'
2019-01-14 21:34:47 +00:00
2020-07-08 08:41:20 +00:00
import _legalEagle from 'legal-eagle'
2019-01-16 16:16:15 +00:00
const legalEagle = promisify ( _legalEagle )
2019-01-14 21:34:47 +00:00
import { getVersion } from '../../app/package-info'
2022-03-04 15:06:21 +00:00
import { readFile , writeFile } from 'fs/promises'
2019-01-14 21:34:47 +00:00
2019-01-16 14:46:42 +00:00
export async function updateLicenseDump (
2019-01-14 21:34:47 +00:00
projectRoot : string ,
outRoot : string
) : Promise < void > {
const appRoot = path . join ( projectRoot , 'app' )
const outPath = path . join ( outRoot , 'static' , 'licenses.json' )
2019-01-16 16:16:15 +00:00
let summary = await legalEagle ( {
2019-01-16 14:46:42 +00:00
path : appRoot ,
overrides : licenseOverrides ,
omitPermissive : true ,
} )
if ( Object . keys ( summary ) . length > 0 ) {
let licensesMessage = ''
for ( const key in summary ) {
const license = summary [ key ]
licensesMessage += ` ${ key } ( ${ license . repository } ): ${ license . license } \ n `
}
2019-01-14 21:34:47 +00:00
2019-01-16 14:46:42 +00:00
const overridesPath = path . join ( __dirname , 'license-overrides.ts' )
2019-01-14 21:34:47 +00:00
2019-01-16 14:46:42 +00:00
const message = ` The following dependencies have unknown or non-permissive licenses. Check it out and update ${ overridesPath } if appropriate: \ n ${ licensesMessage } `
throw new Error ( message )
}
2019-01-14 21:34:47 +00:00
2019-01-16 16:16:15 +00:00
summary = await legalEagle ( {
2019-01-16 14:46:42 +00:00
path : appRoot ,
overrides : licenseOverrides ,
} )
2019-01-14 21:34:47 +00:00
2019-01-16 14:46:42 +00:00
// legal-eagle still chooses to ignore the LICENSE at the root
// this injects the current license and pins the source URL before we
// dump the JSON file to disk
const licenseSource = path . join ( projectRoot , 'LICENSE' )
2022-03-04 14:58:53 +00:00
const licenseText = await readFile ( licenseSource , { encoding : 'utf-8' } )
2019-01-16 14:46:42 +00:00
const appVersion = getVersion ( )
2019-01-14 21:34:47 +00:00
2019-01-16 14:46:42 +00:00
summary [ ` desktop@ ${ appVersion } ` ] = {
repository : 'https://github.com/desktop/desktop' ,
license : 'MIT' ,
source : ` https://github.com/desktop/desktop/blob/release- ${ appVersion } /LICENSE ` ,
sourceText : licenseText ,
}
2019-01-14 21:34:47 +00:00
2022-03-04 15:06:21 +00:00
await writeFile ( outPath , JSON . stringify ( summary ) , { encoding : 'utf8' } )
2019-01-14 21:34:47 +00:00
}