Skip to content

Commit

Permalink
fix: Prebuild script
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroPetaccio committed Jan 25, 2024
1 parent acb2e0a commit 4787092
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
28 changes: 15 additions & 13 deletions scripts/prebuild.cjs
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
/* eslint-disable */
const fs = require('fs')
const dotenv = require('dotenv')

dotenv.config()

let ENV_CONTENT = {}

// read files
if (fs.existsSync('.env')) {
Object.assign(ENV_CONTENT, dotenv.parse(fs.readFileSync('.env')))
}

const packageJson = JSON.parse(fs.readFileSync('./package.json').toString())
const publicPackageJson = JSON.parse(fs.readFileSync('./public/package.json').toString())

ENV_CONTENT['REACT_APP_WEBSITE_VERSION'] = packageJson.version
// set version
ENV_CONTENT['VITE_REACT_APP_WEBSITE_VERSION'] = packageJson.version
publicPackageJson.version = packageJson.version

// set public url
Object.assign(ENV_CONTENT, getPublicUrls())

packageJson.homepage = ENV_CONTENT['PUBLIC_URL']

packageJson.homepage = ENV_CONTENT['VITE_BASE_URL']
publicPackageJson.homepage = packageJson.homepage
if (packageJson.homepage) {
// github action outputs. Do not touch.
console.log('::set-output name=public_url::' + packageJson.homepage)
console.log('::set-output name=public_path::' + new URL(packageJson.homepage).pathname)
}

// log stuff
console.log('VERSIONS: ', Object.entries(ENV_CONTENT), '\n')

// save files
fs.writeFileSync(
'.env',
Object.entries(ENV_CONTENT)
.map(e => e[0] + '=' + JSON.stringify(e[1]))
.join('\n') + '\n'
)

publicPackageJson.homepage = packageJson.homepage
publicPackageJson.version = packageJson.version

fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2))
fs.writeFileSync('./public/package.json', JSON.stringify(publicPackageJson, null, 2))

// public url logic
function getPublicUrls() {
const isStatic = !!process.env.GEN_STATIC_LOCAL
const isCI = !!process.env.CI
Expand All @@ -50,15 +52,15 @@ function getPublicUrls() {
console.log('is CDN', isCDN)
if (isCDN) {
// master/main branch, also releases
const cdnUrl = `https://cdn.decentraland.org/${packageJson.name}/${packageJson.version}`
const cdnUrl = `https://cdn.decentraland.org/${publicPackageJson.name}/${publicPackageJson.version}`
console.log(`Using CDN as public url: "${cdnUrl}"`)
return {
PUBLIC_URL: cdnUrl
VITE_BASE_URL: cdnUrl
}
}
// localhost
console.log(`Using empty pubic url`)
console.log('Using empty pubic url')
return {
PUBLIC_URL: ``
VITE_BASE_URL: ''
}
}
5 changes: 2 additions & 3 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"name": "builder",
"build": {
"env": {
"NODE_PATH": "src",
"NODE_ENV": "development",
"GEN_STATIC_LOCAL": "true",
"PUBLIC_URL": "/",
"REACT_APP_DCL_DEFAULT_ENV": "dev",
"VITE_BASE_URL": "/",
"VITE_REACT_APP_DCL_DEFAULT_ENV": "dev",
"NODE_OPTIONS": "--max-old-space-size=6144"
}
},
Expand Down

0 comments on commit 4787092

Please sign in to comment.