2022-11-04 16:11:33 +00:00
import fs from "fs/promises" ;
import { createRequire } from "module" ;
import tempDir from "temp-dir" ;
import { signAddon } from "sign-addon" ;
2021-03-09 19:11:46 +00:00
async function sign (
apiKey ,
apiSecret ,
extensionId ,
unsignedPath ,
version ,
destination
) {
const result = await signAddon ( {
xpiPath : unsignedPath ,
version ,
apiKey ,
apiSecret ,
id : extensionId ,
downloadDir : tempDir ,
} ) ;
if ( ! result . success ) {
throw result ;
}
if ( result . downloadedFiles . length === 1 ) {
2021-04-16 03:25:10 +00:00
// Copy the downloaded file to the destination.
// (Avoid `rename` because it fails if the destination is on a different drive.)
2022-11-04 16:11:33 +00:00
await fs . copyFile ( result . downloadedFiles [ 0 ] , destination ) ;
await fs . unlink ( result . downloadedFiles [ 0 ] ) ;
2021-03-09 19:11:46 +00:00
} else {
console . warn (
"Unexpected downloads for signed Firefox extension, expected 1."
) ;
console . warn ( result ) ;
}
}
2022-11-04 16:11:33 +00:00
try {
2021-03-09 19:11:46 +00:00
if (
process . env . MOZILLA _API _KEY &&
process . env . MOZILLA _API _SECRET &&
process . env . FIREFOX _EXTENSION _ID
) {
2022-11-04 16:11:33 +00:00
// TODO: Import as a JSON module once it becomes stable.
const require = createRequire ( import . meta . url ) ;
2021-04-15 19:29:17 +00:00
const { version } = require ( "../assets/manifest.json" ) ;
2021-03-09 19:11:46 +00:00
await sign (
process . env . MOZILLA _API _KEY ,
process . env . MOZILLA _API _SECRET ,
process . env . FIREFOX _EXTENSION _ID ,
process . argv [ 2 ] ,
version ,
process . argv [ 3 ]
) ;
} else {
console . log (
"Skipping signing of Firefox extension. To enable this, please provide MOZILLA_API_KEY, MOZILLA_API_SECRET and FIREFOX_EXTENSION_ID environment variables"
) ;
}
2022-11-04 16:11:33 +00:00
} catch ( error ) {
2021-04-16 03:26:49 +00:00
console . error ( "Error while signing Firefox extension:" ) ;
console . error ( error ) ;
process . exit ( - 1 ) ;
2022-11-04 16:11:33 +00:00
}