Skip to content

Commit

Permalink
chore(release): 0.3.28-alpha.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mguleryuz committed Mar 5, 2025
1 parent 90b2c4f commit aaa6a47
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"compile": "bun tools/compile/index.ts && prettier --write ./src/data.ts",
"build": "bun clean && bun build:types && bun build:esm && bun build:cjs",
"build:types": "tsc --project ./tsconfig.build.json --module ESNext --declarationDir ./dist/types --emitDeclarationOnly --declaration && tsc-alias --outDir ./dist/types",
"build:esm": "tsc --project ./tsconfig.build.json --module ESNext --outDir ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json && tsc-alias --outDir ./dist/esm && ts-add-js-extension --dir=dist/esm --showchanges=false && echo 'export {};' > ./dist/esm/data.js",
"build:esm": "tsc --project ./tsconfig.build.json --module ESNext --outDir ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json && tsc-alias --outDir ./dist/esm && ts-add-js-extension --dir=dist/esm --showchanges=false && echo 'export {};' > ./dist/esm/data.js && node tools/build-esm.js",
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json && tsc-alias --outDir ./dist/cjs && ts-add-js-extension --dir=dist/cjs --showchanges=false && cp tools/constants/data-cjs-template.js dist/cjs/data.js",
"clean": "rm -rf dist tsconfig.tsbuildinfo tsconfig.build.tsbuildinfo",
"postinstall": "husky || true && node tools/build-esm.js",
Expand Down
72 changes: 40 additions & 32 deletions tools/build-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@ import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

// Get dirname in ESM
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// Read and parse the .d.ts
const dtsPath = path.resolve(__dirname, '../dist/types/data.d.ts')
const dtsContent = fs.readFileSync(dtsPath, 'utf-8')

function parseDTStoJson(dtsContent) {
// First, extract the content between the outermost brackets
let match = dtsContent.match(
/export\s+declare\s+const\s+data:\s*readonly\s*\[([\s\S]*)\];/
)
const isCI = process.env.GITHUB_ACTIONS === 'true'

if (!match) {
throw new Error('Could not parse the TypeScript declaration')
}
if (!isCI) {
// Get dirname in ESM
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

let content = match[1]
// Read and parse the .d.ts
const dtsPath = path.resolve(__dirname, '../dist/types/data.d.ts')
const dtsContent = fs.readFileSync(dtsPath, 'utf-8')

// Remove all 'readonly' keywords
content = content.replace(/readonly\s+/g, '')
function parseDTStoJson(dtsContent) {
// First, extract the content between the outermost brackets
let match = dtsContent.match(
/export\s+declare\s+const\s+data:\s*readonly\s*\[([\s\S]*)\];/
)

// Replace semicolons with commas
content = content.replace(/;\s*/g, ',')
if (!match) {
throw new Error('Could not parse the TypeScript declaration')
}

// If there are these ,} or ,] replace them with } or ]
content = content.replace(/,\s*\}\s*/g, '}')
content = content.replace(/,\s*\]\s*/g, ']')
let content = match[1]

return eval(`[${content}]`)
}
// Remove all 'readonly' keywords
content = content.replace(/readonly\s+/g, '')

// Replace semicolons with commas
content = content.replace(/;\s*/g, ',')

// If there are these ,} or ,] replace them with } or ]
content = content.replace(/,\s*\}\s*/g, '}')
content = content.replace(/,\s*\]\s*/g, ']')

const data = parseDTStoJson(dtsContent)
return eval(`[${content}]`)
}

const data = parseDTStoJson(dtsContent)

// write the data to dist/esm/data.js
fs.writeFileSync(
path.resolve(__dirname, '../dist/esm/data.js'),
`export const data = ${JSON.stringify(data, null, 2)}`
)
// write the data to dist/esm/data.js
fs.writeFileSync(
path.resolve(__dirname, '../dist/esm/data.js'),
`export const data = ${JSON.stringify(data, null, 2)}`
)

console.log('Generated data.js file during package installation')
} else {
console.log('Skipping data.js file generation during package installation')
}

0 comments on commit aaa6a47

Please sign in to comment.