import ghRelease from "gh-release" ; import ghauth , { AuthOptions , TokenData } from "ghauth" ; import { promises as fs , existsSync , mkdirSync } from "fs" ; import { join } from "path" ; import { spawnSync } from "child_process" ; import zipFolder from "zip-folder" ; main ( ) . catch ( handleError ) ; async function main ( ) { const args = process . argv . slice ( 2 ) ; const target = args [ 0 ] ; const description = args . length > 1 ? args [ 1 ] : "No notes" ; const packages = [ "packages-common" , "packages-native" , "packages-web" , "packages-hybrid" ] ; console . log ( "Target" , target ) ; const projectPackage = await findPackage ( packages , target ) ; if ( projectPackage . path . indexOf ( "packages-web" ) !== - 1 ) { console . log ( "CHECKOUT: Checking out SVN project to upload together with the widget" ) ; spawnSync ( "npm" , [ "run" , "svncheckout" ] , { cwd : projectPackage . path } ) ; console . log ( "CHECKOUT: Checkout done" ) ; } spawnSync ( "npm" , [ "run" , "release" ] , { cwd : projectPackage . path } ) ; await createChangeLog ( description , projectPackage . version , projectPackage . path ) ; await zipTestProjects ( projectPackage . path ) ; const url = await releaseWithAuth ( await getAuth ( ) , projectPackage ) ; console . log ( `Version ${ projectPackage . version } of ${ projectPackage . name } successfully published @ ${ url }` ) ; process . exit ( 0 ) ; } async function findPackage ( packages , target ) { for ( const pack of packages ) { const folders = await fs . readdir ( pack ) ; for ( const folder of folders ) { const pathToPackage = join ( pack , folder ) ; try { const stat = await fs . stat ( join ( pathToPackage , "package.json" ) ) ; if ( stat ) { const contentBuffer = await fs . readFile ( join ( pathToPackage , "package.json" ) ) ; const content = JSON . parse ( contentBuffer . toString ( ) ) ; if ( content . name === target ) { return { ... content , path : pathToPackage } ; } } } catch ( e ) { } } } throw Error ( `Package for ${ target } not found` ) ; } function getAuth ( ) { return new Promise < TokenData > ( ( resolve , reject ) => { const options = { configName : "gh-release" , scopes : [ "repo" ] , note : "gh-release" , userAgent : "gh-release" } ; ghauth ( options , ( error , auth ) => ( error ? reject ( error ) : resolve ( auth ) ) ) ; } ) ; } async function releaseWithAuth ( auth , projectPackage ) { const assets = [ `dist/${ projectPackage . version }/${ projectPackage . widgetName }.mpk` , `dist/${ projectPackage . version }/module/${ projectPackage . config . moduleName }.mpk` , `dist/${ projectPackage . version }/${ projectPackage . packagePath }.${ projectPackage . widgetName }.mpk` , "dist/tmp/TestProjects.zip" ] ; console . log ( "Assets" , assets . filter ( asset => existsSync ( projectPackage . path + "/" + asset ) ) ) ; const tagName = `AppStore release ${ projectPackage . name . replace ( /^\w/ , ( c ) => c . toUpperCase ( ) ) } v${ projectPackage . version }` ; const options = { tag_name : `${ projectPackage . name }-v${ projectPackage . version }` , name : tagName , assets : assets . filter ( asset => existsSync ( projectPackage . path + "/" + asset ) ) , auth , workpath : projectPackage . path , target_commitish : "master" , draft : true } ; return new Promise ( resolve => { ghRelease ( options , ( error , result ) => { if ( error ) { return handleError ( error ) ; } resolve ( result . html_url ) ; } ) ; } ) ; } function handleError ( error ) { console . error ( error ) ; process . exit ( 1 ) ; } function createChangeLog ( description , version , path ) { const date = new Date ( ) ; const body = `## [${ version }] - ${ date . getFullYear ( ) }-${ date . getMonth ( ) }-${ date . getDay ( ) }
    ${ description }
` ; return fs . appendFile ( `${ path }/CHANGELOG.md` , body ) ; } function zipTestProjects ( path ) { const source = join ( path , "tests" , "TestProjects" ) ; return new Promise ( resolve => { if ( existsSync ( source ) ) { const destination = join ( path , "dist" , "tmp" ) ; if ( ! existsSync ( destination ) ) { mkdirSync ( destination , { recursive : true } ) ; } zipFolder ( source , join ( destination , "TestProjects.zip" ) , ( error ) => { if ( error ) { console . log ( "Error trying to zip testProjects" , error ) ; return resolve ( false ) ; } console . log ( "Successfully zipped the TestProjects" ) ; resolve ( true ) ; } ) ; } else { return resolve ( false ) ; } } ) ; }