Skip to content

Commit

Permalink
Add a replacer for the demo bundle HTML files
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso committed Nov 14, 2023
1 parent 26eb579 commit b49b9e9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
4 changes: 2 additions & 2 deletions demos/client-bundle-example/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div
class="procaptcha"
data-theme="light"
data-sitekey="5HUBceb4Du6dvMA9BiwN5VzUrzUsX9Zp7z7nSR2cC1TCv5jg"
data-sitekey="5EcwrDAyzUqtPrKR1qZcFDW643eQFubPKeNLq2wy2jzMHppm"
></div>
<input type="submit" class="mui-btn mui-btn--raised"/>
</form>
Expand All @@ -37,7 +37,7 @@

// Render the CAPTCHA explicitly on a container with id "procaptcha-container"
window.procaptcha.render('procaptcha-container', {
siteKey: '5HUBceb4Du6dvMA9BiwN5VzUrzUsX9Zp7z7nSR2cC1TCv5jg',
siteKey: '5EcwrDAyzUqtPrKR1qZcFDW643eQFubPKeNLq2wy2jzMHppm',
theme: 'dark',
callback: 'onCaptchaVerified',
})
Expand Down
7 changes: 6 additions & 1 deletion dev/scripts/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
import { LogLevel, getLogger } from '@prosopo/common'
import { deployDapp, deployProtocol } from '../contract/deploy/index.js'
import { exec } from '../util/index.js'
import { exec, updateDemoHTMLFiles } from '../util/index.js'
import { getContractNames, getPaths } from '@prosopo/config'
import { getEnv, loadEnv } from '@prosopo/cli'
import { getLogLevel } from '@prosopo/common'
Expand Down Expand Up @@ -99,6 +99,11 @@ export async function processArgs(args: string[]) {
dappContractAddress.toString(),
log
)
await updateDemoHTMLFiles(
[/data-sitekey="(\w{48})"/, /siteKey:\s*'(\w{48})'/],
dappContractAddress.toString(),
log
)
}
},
[]
Expand Down
47 changes: 40 additions & 7 deletions dev/scripts/src/util/updateEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,59 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Logger } from '@prosopo/common'
import { at } from '@prosopo/util'
import { getEnv } from '@prosopo/cli'
import { glob } from 'glob'
import dotenv from 'dotenv'
import fs from 'fs'
import path from 'path'

const ignore = [
'node_modules/**',
'node_modules/**',
'../../**/node_modules/**',
'../node_modules/**',
'../../node_modules/**',
]
const __dirname = path.resolve()
export async function findEnvFiles(logger: Logger) {
const env = getEnv()
const fileName = `.env.${env}`
// options is optional
logger.info('Searching for files')
return await glob.glob(`../../**/${fileName}`, {
ignore: [
'node_modules/**',
'node_modules/**',
'../../**/node_modules/**',
'../node_modules/**',
'../../node_modules/**',
],
ignore: ignore,
})
}

export async function updateDemoHTMLFiles(varMatchers: RegExp[], varValue: string, logger: Logger) {
// replace the site key in the html files
const files = await glob.glob('../../demos/**/*.html', {
ignore: ignore,
})
logger.info('HTML files found', files)
files.forEach((file) => {
// the following code loads a .env file, searches for the variable and replaces it
// then saves the file
const filePath = path.resolve(process.cwd(), file)
const contents = fs.readFileSync(filePath).toString()
let newContents = contents
for (const varMatcher of varMatchers) {
const matches = contents.match(varMatcher)
logger.info('varMatcher', varMatcher, 'Matches', matches)
if (matches) {
// replace the site key
const matchedVar = at(matches, 1)
logger.info('matchedVar', matchedVar)
newContents = contents.replaceAll(matchedVar, varValue)
break
}
}

if (newContents !== contents) {
// write the file back
fs.writeFileSync(path.resolve(__dirname, filePath), newContents)
}
})
}

Expand Down

0 comments on commit b49b9e9

Please sign in to comment.