2022-09-30 05:54:58 +03:00
import semver from "semver" ;
import { FetchRequest } from "../utils/index.js" ;
import { atomicWrite } from "./utils/fs.js" ;
2023-02-23 05:52:33 +03:00
import { getLogs } from "./utils/git.js" ;
2022-09-30 05:54:58 +03:00
import { loadJson , saveJson } from "./utils/json.js" ;
import { resolve } from "./utils/path.js" ;
const cache : Record < string , any > = { } ;
async function getNpmPackage ( name : string ) : Promise < any > {
if ( ! cache [ name ] ) {
const resp = await ( new FetchRequest ( "https:/\/registry.npmjs.org/" + name ) ) . send ( ) ;
resp . assertOk ( ) ;
cache [ name ] = resp . bodyJson ;
}
return cache [ name ] || null ;
}
2023-02-23 05:52:33 +03:00
2022-09-30 05:54:58 +03:00
function writeVersion ( version : string ) : void {
2023-02-14 20:24:53 +03:00
const content = ` /* Do NOT modify this file; see /src.ts/_admin/update-version.ts */ \ n \ n/** \ n * The current version of Ethers. \ n */ \ nexport const version: string = " ${ version } "; \ n ` ;
2022-09-30 05:54:58 +03:00
atomicWrite ( resolve ( "src.ts/_version.ts" ) , content ) ;
}
( async function ( ) {
// Local pkg
const pkgPath = resolve ( "package.json" ) ;
const pkgInfo = loadJson ( pkgPath ) ;
const tag = pkgInfo . publishConfig . tag ;
// Get the remote version that matches our dist-tag
const remoteInfo = await getNpmPackage ( pkgInfo . name ) ;
const remoteVersion = remoteInfo [ "dist-tags" ] [ tag ] ;
// Remote pkg
const remotePkgInfo = remoteInfo . versions [ remoteVersion ] ;
const remoteGitHead = remotePkgInfo . gitHead ;
2022-09-30 06:17:36 +03:00
let gitHead = "" ;
2023-02-23 05:52:33 +03:00
for ( const log of await getLogs ( [ "." ] ) ) {
2022-09-30 06:17:36 +03:00
if ( log . body . startsWith ( "admin:" ) ) { continue ; }
2023-04-25 14:02:46 +03:00
if ( log . body . startsWith ( "docs:" ) ) { continue ; }
2022-10-01 08:33:18 +03:00
if ( log . body . startsWith ( "tests:" ) ) { continue ; }
2022-09-30 06:17:36 +03:00
gitHead = log . commit ;
break ;
}
if ( gitHead === "" ) { throw new Error ( "no meaningful commit found" ) ; }
2022-09-30 05:54:58 +03:00
// There are new commits, not reflected in the package
// published on npm; update the gitHead and version
if ( gitHead !== remoteGitHead ) {
// Bump the version from the remote version
if ( tag . indexOf ( "beta" ) >= 0 ) {
// Still a beta branch; advance the beta version
const prerelease = semver . prerelease ( remoteVersion ) ;
if ( prerelease == null || prerelease . length !== 2 ) {
throw new Error ( "no prerelease found" ) ;
}
pkgInfo . version = semver . inc ( remoteVersion , "prerelease" , String ( prerelease [ 0 ] ) ) ;
} else if ( semver . minor ( remoteVersion ) == semver . minor ( pkgInfo . version ) ) {
// If we want to bump the minor version, it was done explicitly in the pkg
pkgInfo . version = semver . inc ( remoteVersion , "patch" ) ;
}
pkgInfo . gitHead = gitHead ;
// Save the package.json
2023-04-06 10:42:22 +03:00
const check : Record < string , number > = { "default" : 1 , "require" : 1 , "import" : 1 , "types" : 1 } ;
2023-02-16 17:44:13 +03:00
saveJson ( pkgPath , pkgInfo , ( path : string , a : string , b : string ) = > {
2023-04-06 10:42:22 +03:00
if ( ( path . startsWith ( "./" ) || path === "." ) && check [ a ] && check [ b ] ) {
const cmp = a . localeCompare ( b ) ;
if ( cmp === 0 ) { return cmp ; }
2023-04-25 14:02:46 +03:00
// Make sure require comes first; it has the types built-in
// so its ok
if ( a === "require" ) { return - 1 ; }
if ( b === "require" ) { return 1 ; }
// Favour types the next-first and default for last
2023-04-06 10:42:22 +03:00
if ( a === "types" || b === "default" ) { return - 1 ; }
if ( b === "types" || a === "default" ) { return 1 ; }
return cmp ;
2023-02-16 17:44:13 +03:00
}
return a . localeCompare ( b ) ;
} ) ;
2022-09-30 05:54:58 +03:00
// Save the src.ts/_version.ts
writeVersion ( pkgInfo . version ) ;
}
} ) ( ) . catch ( ( error ) = > {
console . log ( "ERROR" ) ;
console . log ( error ) ;
process . exit ( 1 )
} ) ;