2024-08-13 04:53:07 +03:00
import * as cp from 'node:child_process'
import * as fs from 'node:fs'
import { createRequire } from 'node:module'
import * as path from 'node:path'
2024-08-12 09:19:45 +03:00
import * as glob from 'glob'
import ts from 'typescript'
import * as stc from '@teidesu/slow-types-compiler'
2024-08-13 04:53:07 +03:00
2024-08-12 09:19:45 +03:00
const _ _dirname = path . dirname ( new URL ( import . meta . url ) . pathname )
const rootPackageJson = JSON . parse ( fs . readFileSync ( path . join ( _ _dirname , '../package.json' ) , 'utf-8' ) )
2023-10-16 19:23:53 +03:00
if ( process . argv . length < 3 ) {
console . log ( 'Usage: build-package.js <package name>' )
process . exit ( 0 )
}
2024-04-20 01:07:53 +03:00
const IS _JSR = process . env . JSR === '1'
2024-04-03 18:08:07 +03:00
const packagesDir = path . join ( _ _dirname , '../packages' )
const packageDir = path . join ( packagesDir , process . argv [ 2 ] )
2024-04-20 01:07:53 +03:00
let outDir = path . join ( packageDir , 'dist' )
if ( IS _JSR ) outDir = path . join ( outDir , 'jsr' )
2023-10-16 19:23:53 +03:00
2023-11-02 21:23:17 +03:00
function exec ( cmd , params ) {
cp . execSync ( cmd , { cwd : packageDir , stdio : 'inherit' , ... params } )
}
function transformFile ( file , transform ) {
const content = fs . readFileSync ( file , 'utf8' )
2024-03-04 20:59:27 +03:00
const res = transform ( content , file )
if ( res != null ) fs . writeFileSync ( file , res )
2023-10-16 19:23:53 +03:00
}
2024-08-12 09:19:45 +03:00
// todo make them esm
const require = createRequire ( import . meta . url )
2023-10-16 19:23:53 +03:00
const buildConfig = {
buildTs : true ,
buildCjs : true ,
removeReferenceComments : true ,
esmOnlyDirectives : false ,
2024-01-31 19:29:49 +03:00
esmImportDirectives : false ,
2023-11-02 21:23:17 +03:00
before : ( ) => { } ,
final : ( ) => { } ,
... ( ( ) => {
let config
try {
config = require ( path . join ( packageDir , 'build.config.cjs' ) )
} catch ( e ) {
2024-04-13 07:44:59 +03:00
if ( e . code !== 'MODULE_NOT_FOUND' ) throw e
2023-11-02 21:23:17 +03:00
return { }
}
console . log ( '[i] Using custom build config' )
if ( typeof config === 'function' ) {
config = config ( {
fs ,
path ,
2024-03-04 20:59:27 +03:00
glob ,
2023-11-02 21:23:17 +03:00
exec ,
transformFile ,
packageDir ,
outDir ,
2024-04-20 01:07:53 +03:00
jsr : IS _JSR ,
2023-11-02 21:23:17 +03:00
} )
}
return config
} ) ( ) ,
2023-10-16 19:23:53 +03:00
}
2024-04-20 01:07:53 +03:00
function getPackageVersion ( name ) {
return require ( path . join ( packagesDir , name , 'package.json' ) ) . version
}
2023-10-16 19:23:53 +03:00
function buildPackageJson ( ) {
const pkgJson = JSON . parse ( fs . readFileSync ( path . join ( packageDir , 'package.json' ) , 'utf-8' ) )
if ( buildConfig . buildCjs ) {
pkgJson . main = 'cjs/index.js'
pkgJson . module = 'esm/index.js'
}
2024-06-28 00:38:42 +03:00
// copy common fields from root
for ( const field of [ 'license' , 'author' , 'contributors' , 'homepage' , 'repository' , 'bugs' ] ) {
if ( rootPackageJson [ field ] ) {
pkgJson [ field ] = rootPackageJson [ field ]
}
}
2023-10-16 19:23:53 +03:00
const newScripts = { }
if ( pkgJson . keepScripts ) {
for ( const script of pkgJson . keepScripts ) {
newScripts [ script ] = pkgJson . scripts [ script ]
}
delete pkgJson . keepScripts
}
pkgJson . scripts = newScripts
delete pkgJson . devDependencies
delete pkgJson . private
if ( pkgJson . distOnlyFields ) {
Object . assign ( pkgJson , pkgJson . distOnlyFields )
delete pkgJson . distOnlyFields
}
2024-04-20 01:07:53 +03:00
if ( pkgJson . jsrOnlyFields ) {
if ( IS _JSR ) {
Object . assign ( pkgJson , pkgJson . jsrOnlyFields )
}
delete pkgJson . jsrOnlyFields
}
2023-10-16 19:23:53 +03:00
function replaceWorkspaceDependencies ( field ) {
if ( ! pkgJson [ field ] ) return
const dependencies = pkgJson [ field ]
for ( const name of Object . keys ( dependencies ) ) {
const value = dependencies [ name ]
if ( value . startsWith ( 'workspace:' ) ) {
2023-11-08 17:28:45 +03:00
if ( value !== 'workspace:^' && value !== 'workspace:*' ) {
2023-10-31 21:17:39 +03:00
throw new Error (
2023-11-08 17:28:45 +03:00
` Cannot replace workspace dependency ${ name } with ${ value } - only workspace:^ and * are supported ` ,
2023-10-31 21:17:39 +03:00
)
}
if ( ! name . startsWith ( '@mtcute/' ) ) {
throw new Error ( ` Cannot replace workspace dependency ${ name } - only @mtcute/* is supported ` )
}
2023-11-08 17:28:45 +03:00
// note: pnpm replaces workspace:* with the current version, unlike this script
2024-04-20 01:07:53 +03:00
const depVersion = value === 'workspace:*' ? '*' : ` ^ ${ getPackageVersion ( name . slice ( 8 ) ) } `
dependencies [ name ] = depVersion
2023-10-16 19:23:53 +03:00
}
}
}
replaceWorkspaceDependencies ( 'dependencies' )
replaceWorkspaceDependencies ( 'devDependencies' )
replaceWorkspaceDependencies ( 'peerDependencies' )
replaceWorkspaceDependencies ( 'optionalDependencies' )
delete pkgJson . typedoc
2024-04-20 01:07:53 +03:00
if ( pkgJson . browser ) {
function maybeFixPath ( p , repl ) {
if ( ! p ) return p
2023-11-29 20:31:18 +03:00
2024-04-20 01:07:53 +03:00
if ( p . startsWith ( './src/' ) ) {
return repl + p . slice ( 6 )
}
2023-11-29 20:31:18 +03:00
2024-04-20 01:07:53 +03:00
if ( p . startsWith ( './' ) ) {
return repl + p . slice ( 2 )
}
return p
}
2023-11-29 20:31:18 +03:00
for ( const key of Object . keys ( pkgJson . browser ) ) {
if ( ! key . startsWith ( './src/' ) ) continue
const path = key . slice ( 6 )
pkgJson . browser [ ` ./esm/ ${ path } ` ] = maybeFixPath ( pkgJson . browser [ key ] , './esm/' )
if ( buildConfig . buildCjs ) {
pkgJson . browser [ ` ./cjs/ ${ path } ` ] = maybeFixPath ( pkgJson . browser [ key ] , './cjs/' )
}
delete pkgJson . browser [ key ]
}
}
2024-04-20 01:07:53 +03:00
// fix exports
if ( pkgJson . exports ) {
function maybeFixPath ( path , repl ) {
if ( ! path ) return path
if ( pkgJson . exportsKeepPath ? . includes ( path ) ) return path
if ( path . startsWith ( './src/' ) ) {
path = repl + path . slice ( 6 )
} else if ( path . startsWith ( './' ) ) {
path = repl + path . slice ( 2 )
}
return path . replace ( /\.ts$/ , '.js' )
}
function fixValue ( value ) {
if ( IS _JSR ) {
return maybeFixPath ( value , './' ) . replace ( /\.js$/ , '.ts' )
}
if ( buildConfig . buildCjs ) {
return {
import : maybeFixPath ( value , './esm/' ) ,
require : maybeFixPath ( value , './cjs/' ) ,
}
}
return maybeFixPath ( value , './' )
}
if ( typeof pkgJson . exports === 'string' ) {
pkgJson . exports = {
'.' : fixValue ( pkgJson . exports ) ,
}
} else {
for ( const key of Object . keys ( pkgJson . exports ) ) {
const value = pkgJson . exports [ key ]
if ( typeof value !== 'string' ) {
2024-08-13 04:53:07 +03:00
throw new TypeError ( 'Conditional exports are not supported' )
2024-04-20 01:07:53 +03:00
}
pkgJson . exports [ key ] = fixValue ( value )
}
}
delete pkgJson . exportsKeepPath
}
if ( ! IS _JSR ) {
fs . writeFileSync ( path . join ( outDir , 'package.json' ) , JSON . stringify ( pkgJson , null , 2 ) )
}
return pkgJson
2023-10-16 19:23:53 +03:00
}
// clean
fs . rmSync ( path . join ( outDir ) , { recursive : true , force : true } )
2024-08-07 18:42:55 +03:00
fs . rmSync ( path . join ( packageDir , 'tsconfig.tsbuildinfo' ) , { recursive : true , force : true } )
2023-10-16 19:23:53 +03:00
fs . mkdirSync ( path . join ( outDir ) , { recursive : true } )
2024-04-20 01:07:53 +03:00
// for jsr - copy typescript sources
if ( IS _JSR ) {
buildConfig . buildCjs = false
}
2023-11-02 21:23:17 +03:00
buildConfig . before ( )
2024-04-20 01:07:53 +03:00
if ( buildConfig . buildTs && ! IS _JSR ) {
2023-10-16 19:23:53 +03:00
console . log ( '[i] Building typescript...' )
2023-11-09 00:20:43 +03:00
2024-04-20 01:07:53 +03:00
const tsconfigPath = path . join ( packageDir , 'tsconfig.json' )
fs . cpSync ( tsconfigPath , path . join ( packageDir , 'tsconfig.backup.json' ) )
2023-11-09 00:20:43 +03:00
2024-04-20 01:07:53 +03:00
const tsconfig = ts . parseConfigFileTextToJson ( tsconfigPath , fs . readFileSync ( tsconfigPath , 'utf-8' ) ) . config
if ( tsconfig . extends === '../../tsconfig.json' ) {
tsconfig . extends = '../../.config/tsconfig.build.json'
} else {
throw new Error ( 'expected tsconfig to extend base config' )
}
fs . writeFileSync ( path . join ( packageDir , 'tsconfig.json' ) , JSON . stringify ( tsconfig , null , 2 ) )
2023-11-09 00:20:43 +03:00
2023-11-12 01:17:20 +03:00
const restoreTsconfig = ( ) => {
fs . renameSync ( path . join ( packageDir , 'tsconfig.backup.json' ) , path . join ( packageDir , 'tsconfig.json' ) )
}
2023-11-09 00:20:43 +03:00
try {
exec ( 'pnpm exec tsc --build' , { cwd : packageDir , stdio : 'inherit' } )
} catch ( e ) {
2023-11-12 01:17:20 +03:00
restoreTsconfig ( )
2023-11-09 00:20:43 +03:00
throw e
}
2023-10-16 19:23:53 +03:00
if ( buildConfig . buildCjs ) {
console . log ( '[i] Building typescript (CJS)...' )
const originalFiles = { }
2024-04-03 18:08:07 +03:00
for ( const f of glob . sync ( path . join ( packagesDir , '**/*.ts' ) ) ) {
const content = fs . readFileSync ( f , 'utf8' )
if ( ! content . includes ( '@only-if-esm' ) ) continue
originalFiles [ f ] = content
2023-10-16 19:23:53 +03:00
2024-04-03 18:08:07 +03:00
fs . writeFileSync ( f , content . replace ( / @ o n l y - i f - e s m . * ? @ \ / o n l y - i f - e s m / g s , ' ' ) )
}
for ( const f of glob . sync ( path . join ( packagesDir , '**/*.ts' ) ) ) {
const content = fs . readFileSync ( f , 'utf8' )
if ( ! content . includes ( '@esm-replace-import' ) ) continue
originalFiles [ f ] = content
2024-04-25 13:46:11 +03:00
fs . writeFileSync ( f , content . replace ( / ( ? < = @ e s m - r e p l a c e - i m p o r t . * ? ) a w a i t i m p o r t / g s , ' r e q u i r e ' ) )
2023-10-16 19:23:53 +03:00
}
2024-01-31 19:29:49 +03:00
2024-04-03 18:08:07 +03:00
// set type=commonjs in all package.json-s
for ( const pkg of fs . readdirSync ( packagesDir ) ) {
const pkgJson = path . join ( packagesDir , pkg , 'package.json' )
if ( ! fs . existsSync ( pkgJson ) ) continue
const orig = fs . readFileSync ( pkgJson , 'utf8' )
originalFiles [ pkgJson ] = orig
2024-04-20 01:07:53 +03:00
fs . writeFileSync (
pkgJson ,
JSON . stringify (
{
... JSON . parse ( orig ) ,
type : 'commonjs' ,
} ,
null ,
2 ,
) ,
)
2024-04-03 18:08:07 +03:00
// maybe also dist/package.json
const distPkgJson = path . join ( packagesDir , pkg , 'dist/package.json' )
if ( fs . existsSync ( distPkgJson ) ) {
const orig = fs . readFileSync ( distPkgJson , 'utf8' )
originalFiles [ distPkgJson ] = orig
2024-04-20 01:07:53 +03:00
fs . writeFileSync (
distPkgJson ,
JSON . stringify (
{
... JSON . parse ( orig ) ,
type : 'commonjs' ,
} ,
null ,
2 ,
) ,
)
2024-01-31 19:29:49 +03:00
}
}
2023-10-16 19:23:53 +03:00
let error = false
try {
2024-04-03 18:08:07 +03:00
exec ( 'pnpm exec tsc --outDir dist/cjs' , {
cwd : packageDir ,
stdio : 'inherit' ,
} )
2023-10-16 19:23:53 +03:00
} catch ( e ) {
error = e
}
for ( const f of Object . keys ( originalFiles ) ) {
fs . writeFileSync ( f , originalFiles [ f ] )
}
2023-11-12 01:17:20 +03:00
if ( error ) {
restoreTsconfig ( )
throw error
}
2023-10-16 19:23:53 +03:00
}
2023-11-12 01:17:20 +03:00
restoreTsconfig ( )
2023-11-09 00:20:43 +03:00
2024-04-20 01:07:53 +03:00
// todo: can we remove these?
2023-10-16 19:23:53 +03:00
console . log ( '[i] Post-processing...' )
if ( buildConfig . removeReferenceComments ) {
for ( const f of glob . sync ( path . join ( outDir , '**/*.d.ts' ) ) ) {
let content = fs . readFileSync ( f , 'utf8' )
let changed = false
2024-08-13 04:53:07 +03:00
if ( content . includes ( '/// <reference types="' ) ) {
2023-10-16 19:23:53 +03:00
changed = true
2024-04-30 16:27:58 +03:00
content = content . replace ( /\/\/\/ <reference types="(node|deno\/ns)".+?\/>\n?/g , '' )
2023-10-16 19:23:53 +03:00
}
if ( changed ) fs . writeFileSync ( f , content )
}
}
2024-04-20 01:07:53 +03:00
} else if ( buildConfig . buildTs && IS _JSR ) {
console . log ( '[i] Copying sources...' )
fs . cpSync ( path . join ( packageDir , 'src' ) , outDir , { recursive : true } )
const printer = ts . createPrinter ( )
for ( const f of glob . sync ( path . join ( outDir , '**/*.ts' ) ) ) {
let fileContent = fs . readFileSync ( f , 'utf8' )
let changed = false
// replace .js imports with .ts
const file = ts . createSourceFile ( f , fileContent , ts . ScriptTarget . ESNext , true )
let changedTs = false
for ( const imp of file . statements ) {
if ( imp . kind !== ts . SyntaxKind . ImportDeclaration && imp . kind !== ts . SyntaxKind . ExportDeclaration ) {
continue
}
if ( imp . kind === ts . SyntaxKind . ExportDeclaration && ! imp . moduleSpecifier ) {
continue
}
const mod = imp . moduleSpecifier . text
if ( mod [ 0 ] === '.' && mod . endsWith ( '.js' ) ) {
changedTs = true
imp . moduleSpecifier = {
kind : ts . SyntaxKind . StringLiteral ,
2024-08-13 04:53:07 +03:00
text : ` ${ mod . slice ( 0 , - 3 ) } .ts ` ,
2024-04-20 01:07:53 +03:00
}
}
}
if ( changedTs ) {
fileContent = printer . printFile ( file )
changed = true
}
// add shims for node-specific APIs and replace NodeJS.* types
// pretty fragile, but it works for now
const typesToReplace = {
'NodeJS\\.Timeout' : 'number' ,
'NodeJS\\.Immediate' : 'number' ,
}
const nodeSpecificApis = {
setImmediate : '(cb: (...args: any[]) => void, ...args: any[]) => number' ,
clearImmediate : '(id: number) => void' ,
Buffer :
2024-08-13 04:53:07 +03:00
'{ '
+ 'concat: (...args: any[]) => Uint8Array, '
+ 'from: (data: any, encoding?: string) => { toString(encoding?: string): string }, '
+ ' }' ,
2024-04-20 01:07:53 +03:00
SharedWorker : [ 'type' , 'never' ] ,
2024-04-29 05:50:18 +03:00
WorkerGlobalScope :
2024-08-13 04:53:07 +03:00
'{ '
+ ' new (): typeof WorkerGlobalScope, '
+ ' postMessage: (message: any, transfer?: Transferable[]) => void, '
+ ' addEventListener: (type: "message", listener: (ev: MessageEvent) => void) => void, '
+ ' }' ,
2024-04-20 01:07:53 +03:00
process : '{ ' + 'hrtime: { bigint: () => bigint }, ' + '}' ,
}
for ( const [ name , decl _ ] of Object . entries ( nodeSpecificApis ) ) {
if ( fileContent . includes ( name ) ) {
2024-04-29 01:36:42 +03:00
if ( name === 'Buffer' && fileContent . includes ( 'node:buffer' ) ) continue
2024-04-20 01:07:53 +03:00
changed = true
const isType = Array . isArray ( decl _ ) && decl _ [ 0 ] === 'type'
const decl = isType ? decl _ [ 1 ] : decl _
if ( isType ) {
2024-08-13 04:53:07 +03:00
fileContent = ` declare type ${ name } = ${ decl } ; \n ${ fileContent } `
2024-04-20 01:07:53 +03:00
} else {
2024-08-13 04:53:07 +03:00
fileContent = ` declare const ${ name } : ${ decl } ; \n ${ fileContent } `
2024-04-20 01:07:53 +03:00
}
}
}
for ( const [ oldType , newType ] of Object . entries ( typesToReplace ) ) {
if ( fileContent . match ( oldType ) ) {
changed = true
fileContent = fileContent . replace ( new RegExp ( oldType , 'g' ) , newType )
}
}
if ( changed ) {
fs . writeFileSync ( f , fileContent )
}
}
2023-10-16 19:23:53 +03:00
}
2024-04-20 01:07:53 +03:00
console . log ( '[i] Copying misc files...' )
const builtPkgJson = buildPackageJson ( )
2023-10-16 19:23:53 +03:00
if ( buildConfig . buildCjs ) {
fs . writeFileSync ( path . join ( outDir , 'cjs/package.json' ) , JSON . stringify ( { type : 'commonjs' } , null , 2 ) )
2024-04-25 13:46:11 +03:00
const CJS _DEPRECATION _WARNING = `
"use strict" ;
if ( typeof globalThis !== 'undefined' && ! globalThis . _MTCUTE _CJS _DEPRECATION _WARNED ) {
globalThis . _MTCUTE _CJS _DEPRECATION _WARNED = true
console . warn ( "[${builtPkgJson.name}] CommonJS support is deprecated and will be removed soon. Please consider switching to ESM, it's " + ( new Date ( ) ) . getFullYear ( ) + " already." )
console . warn ( "[${builtPkgJson.name}] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c" )
}
` .trim()
const entrypoints = [ ]
if ( typeof builtPkgJson . exports === 'string' ) {
entrypoints . push ( builtPkgJson . exports )
} else if ( builtPkgJson . exports && typeof builtPkgJson . exports === 'object' ) {
for ( const entrypoint of Object . values ( builtPkgJson . exports ) ) {
entrypoints . push ( entrypoint . require )
}
}
for ( const entry of entrypoints ) {
if ( ! entry . endsWith ( '.js' ) ) continue
2024-08-13 04:53:07 +03:00
transformFile ( path . join ( outDir , entry ) , content => ` ${ CJS _DEPRECATION _WARNING } \n ${ content } ` )
2024-04-25 13:46:11 +03:00
}
2023-10-16 19:23:53 +03:00
}
2024-04-30 16:27:58 +03:00
// validate exports
if ( typeof builtPkgJson . exports === 'object' ) {
for ( const [ name , target ] of Object . entries ( builtPkgJson . exports ) ) {
if ( name . includes ( '*' ) ) {
throw new Error ( ` Wildcards are not supported: ${ name } -> ${ target } ` )
}
}
}
2024-04-20 01:07:53 +03:00
if ( IS _JSR ) {
// generate deno.json from package.json
// https://jsr.io/docs/package-configuration
const importMap = { }
if ( builtPkgJson . dependencies ) {
for ( const [ name , version ] of Object . entries ( builtPkgJson . dependencies ) ) {
if ( name . startsWith ( '@mtcute/' ) ) {
importMap [ name ] = ` jsr: ${ name } @ ${ version } `
2024-04-28 22:41:28 +03:00
} else if ( version . startsWith ( 'npm:@jsr/' ) ) {
const jsrName = version . slice ( 9 ) . split ( '@' ) [ 0 ] . replace ( '__' , '/' )
const jsrVersion = version . slice ( 9 ) . split ( '@' ) [ 1 ]
importMap [ name ] = ` jsr:@ ${ jsrName } @ ${ jsrVersion } `
2024-04-20 01:07:53 +03:00
} else {
importMap [ name ] = ` npm: ${ name } @ ${ version } `
}
}
}
const denoJson = path . join ( outDir , 'deno.json' )
fs . writeFileSync (
denoJson ,
JSON . stringify (
{
name : builtPkgJson . name ,
version : builtPkgJson . version ,
exports : builtPkgJson . exports ,
exclude : [ '**/*.test.ts' , '**/*.test-utils.ts' , '**/__fixtures__/**' ] ,
imports : importMap ,
... builtPkgJson . denoJson ,
} ,
null ,
2 ,
) ,
)
2024-04-29 01:36:42 +03:00
if ( process . env . E2E ) {
// populate dependencies, if any
const depsToPopulate = [ ]
for ( const dep of Object . values ( importMap ) ) {
if ( ! dep . startsWith ( 'jsr:' ) ) continue
if ( dep . startsWith ( 'jsr:@mtcute/' ) ) continue
depsToPopulate . push ( dep . slice ( 4 ) )
}
if ( depsToPopulate . length ) {
console . log ( '[i] Populating %d dependencies...' , depsToPopulate . length )
2024-04-29 05:50:18 +03:00
cp . spawnSync (
'pnpm' ,
[
'exec' ,
'slow-types-compiler' ,
'populate' ,
'--downstream' ,
process . env . JSR _URL ,
'--token' ,
process . env . JSR _TOKEN ,
'--unstable-create-via-api' ,
... depsToPopulate ,
] ,
{
stdio : 'inherit' ,
} ,
)
2024-04-29 01:36:42 +03:00
}
}
2024-04-20 01:07:53 +03:00
console . log ( '[i] Processing with slow-types-compiler...' )
const project = stc . createProject ( )
stc . processPackage ( project , denoJson )
2024-08-13 04:53:07 +03:00
const unsavedSourceFiles = project . getSourceFiles ( ) . filter ( s => ! s . isSaved ( ) )
2024-04-20 01:07:53 +03:00
if ( unsavedSourceFiles . length > 0 ) {
console . log ( '[v] Changed %d files' , unsavedSourceFiles . length )
project . saveSync ( )
}
2024-04-30 16:27:58 +03:00
} else {
// make shims for esnext resolution (that doesn't respect package.json `exports` field)
function makeShim ( name , target ) {
if ( name === '.' ) name = './index.js'
2024-08-07 18:42:55 +03:00
if ( ! name . endsWith ( '.js' ) ) return
if ( fs . existsSync ( path . join ( outDir , name ) ) ) return
if ( name === target ) throw new Error ( ` cannot make shim to itself: ${ name } ` )
2024-04-30 16:27:58 +03:00
2024-08-07 18:42:55 +03:00
fs . writeFileSync ( path . join ( outDir , name ) , ` export * from ' ${ target } ' \n ` )
fs . writeFileSync ( path . join ( outDir , name . replace ( /\.js$/ , '.d.ts' ) ) , ` export * from ' ${ target } ' \n ` )
2024-04-30 16:27:58 +03:00
}
if ( typeof builtPkgJson . exports === 'string' ) {
makeShim ( '.' , builtPkgJson . exports )
} else if ( typeof builtPkgJson . exports === 'object' ) {
for ( const [ name , target ] of Object . entries ( builtPkgJson . exports ) ) {
let esmTarget
if ( typeof target === 'object' ) {
if ( ! target . import ) throw new Error ( ` Invalid export target: ${ name } -> ${ JSON . stringify ( target ) } ` )
esmTarget = target . import
} else if ( typeof target === 'string' ) {
if ( buildConfig . buildCjs ) throw new Error ( ` Invalid export target (with cjs): ${ name } -> ${ target } ` )
esmTarget = target
}
makeShim ( name , esmTarget )
}
}
2024-04-20 01:07:53 +03:00
}
2023-10-16 19:23:53 +03:00
try {
fs . cpSync ( path . join ( packageDir , 'README.md' ) , path . join ( outDir , 'README.md' ) )
} catch ( e ) {
2024-08-13 04:53:07 +03:00
console . log ( ` [!] Failed to copy README.md: ${ e . message } ` )
2023-10-16 19:23:53 +03:00
}
2023-10-23 12:18:04 +03:00
fs . cpSync ( path . join ( _ _dirname , '../LICENSE' ) , path . join ( outDir , 'LICENSE' ) )
2024-04-20 01:07:53 +03:00
if ( ! IS _JSR ) {
fs . writeFileSync ( path . join ( outDir , '.npmignore' ) , '*.tsbuildinfo\n' )
}
2023-10-16 19:23:53 +03:00
2024-08-12 09:19:45 +03:00
await buildConfig . final ( )
if ( IS _JSR && ! process . env . CI ) {
console . log ( '[i] Trying to publish with --dry-run' )
exec ( 'deno publish --dry-run --allow-dirty --quiet' , { cwd : outDir } )
console . log ( '[v] All good!' )
} else {
console . log ( '[v] Done!' )
}