2022-09-27 10:45:27 +03:00
import semver from "semver" ;
import { FetchRequest } from "../utils/index.js" ;
2022-09-30 05:57:27 +03:00
import { atomicWrite } from "./utils/fs.js" ;
2023-02-23 05:53:56 +03:00
import { getLogs } from "./utils/git.js" ;
2022-09-30 05:57:27 +03:00
import { loadJson , saveJson } from "./utils/json.js" ;
2022-09-27 10:45:27 +03:00
import { resolve } from "./utils/path.js" ;
const cache = { } ;
async function getNpmPackage ( name ) {
if ( ! cache [ name ] ) {
const resp = await ( new FetchRequest ( "https:/\/registry.npmjs.org/" + name ) ) . send ( ) ;
resp . assertOk ( ) ;
cache [ name ] = resp . bodyJson ;
}
return cache [ name ] || null ;
}
2022-09-30 05:57:27 +03:00
function writeVersion ( version ) {
2023-02-16 16:19:59 +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 */ \n export const version: string = " ${ version } "; \n ` ;
2022-09-30 05:57:27 +03:00
atomicWrite ( resolve ( "src.ts/_version.ts" ) , content ) ;
2022-09-27 10:45:27 +03:00
}
( 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 ] ;
2022-09-30 05:57:27 +03:00
const remoteGitHead = remotePkgInfo . gitHead ;
2022-09-30 06:19:40 +03:00
let gitHead = "" ;
2023-02-23 05:53:56 +03:00
for ( const log of await getLogs ( [ "." ] ) ) {
2022-09-30 06:19:40 +03:00
if ( log . body . startsWith ( "admin:" ) ) {
continue ;
}
2022-10-01 08:34:06 +03:00
if ( log . body . startsWith ( "tests:" ) ) {
continue ;
}
2022-09-30 06:19:40 +03:00
gitHead = log . commit ;
break ;
}
if ( gitHead === "" ) {
throw new Error ( "no meaningful commit found" ) ;
}
2022-09-30 05:57:27 +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
2022-09-27 10:45:27 +03:00
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" ) ;
}
2022-09-30 05:57:27 +03:00
pkgInfo . gitHead = gitHead ;
// Save the package.json
2023-04-06 11:37:10 +03:00
const check = { "default" : 1 , "require" : 1 , "import" : 1 , "types" : 1 } ;
2023-02-19 06:18:42 +03:00
saveJson ( pkgPath , pkgInfo , ( path , a , b ) => {
2023-04-06 11:37:10 +03:00
if ( ( path . startsWith ( "./" ) || path === "." ) && check [ a ] && check [ b ] ) {
const cmp = a . localeCompare ( b ) ;
if ( cmp === 0 ) {
return cmp ;
}
if ( a === "types" || b === "default" ) {
2023-02-19 06:18:42 +03:00
return - 1 ;
}
2023-04-06 11:37:10 +03:00
if ( b === "types" || a === "default" ) {
2023-02-19 06:18:42 +03:00
return 1 ;
}
2023-04-06 11:37:10 +03:00
return cmp ;
2023-02-19 06:18:42 +03:00
}
return a . localeCompare ( b ) ;
} ) ;
2022-09-30 05:57:27 +03:00
// Save the src.ts/_version.ts
writeVersion ( pkgInfo . version ) ;
2022-09-27 10:45:27 +03:00
}
} ) ( ) . catch ( ( error ) => {
2022-09-30 05:57:27 +03:00
console . log ( "ERROR" ) ;
2022-09-27 10:45:27 +03:00
console . log ( error ) ;
2022-09-30 05:57:27 +03:00
process . exit ( 1 ) ;
2022-09-27 10:45:27 +03:00
} ) ;
2022-09-30 05:57:27 +03:00
//# sourceMappingURL=update-version.js.map