-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builder): gen support watch mode
- Loading branch information
Showing
3 changed files
with
37 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,44 @@ | ||
import 'dotenv/config'; | ||
|
||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { readFileSync, writeFileSync, watch as _watch } from 'fs'; | ||
|
||
import MagicString from 'magic-string'; | ||
|
||
const text = readFileSync('./wrangler.tpl.toml').toString(); | ||
const tplFilePath = './wrangler.tpl.toml'; | ||
const targetFilePath = './wrangler.toml'; | ||
|
||
const magic = new MagicString(text); | ||
export function run() { | ||
const text = readFileSync(tplFilePath).toString(); | ||
|
||
const regex = /{{(.+?)}}/gm; | ||
const magic = new MagicString(text); | ||
|
||
const matches = text.matchAll(regex); | ||
const regex = /{{(.+?)}}/gm; | ||
|
||
if (!matches) { | ||
process.exit(0); | ||
} | ||
const matches = text.matchAll(regex); | ||
|
||
if (!matches) { | ||
return; | ||
} | ||
|
||
for (const match of matches) { | ||
const key = match[1]; | ||
const v = process.env[key]; | ||
if (v && match.index) { | ||
magic.update(match.index, match.index + match[0].length, v); | ||
console.log(`"${key}" will be replaced to "[redacted]"`); | ||
} else { | ||
console.warn(`env variable "${key}" not found.`); | ||
for (const match of matches) { | ||
const key = match[1]; | ||
const v = process.env[key]; | ||
if (v && match.index) { | ||
magic.update(match.index, match.index + match[0].length, v); | ||
console.log(`"${key}" will be replaced to "[redacted]"`); | ||
} else { | ||
console.warn(`env variable "${key}" not found.`); | ||
} | ||
} | ||
|
||
writeFileSync(targetFilePath, magic.toString()); | ||
} | ||
|
||
writeFileSync('./wrangler.toml', magic.toString()); | ||
export function watch() { | ||
console.log('watching template file changes...'); | ||
|
||
_watch(tplFilePath, {}, () => { | ||
console.log(`${tplFilePath} changed.`); | ||
run(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters