Skip to content

Commit 0af6392

Browse files
committed
fail handlebars parse silently
1 parent 391a08a commit 0af6392

2 files changed

Lines changed: 45 additions & 38 deletions

File tree

src/scaffold.ts

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function Scaffold({ ...options }: ScaffoldConfig) {
4141
log(options, LogLevel.Info, "Data:", options.data)
4242
for (let template of options.templates) {
4343
try {
44-
const _isGlob = template.includes("*")
44+
const _isGlob = glob.hasMagic(template)
4545
if (!_isGlob && !(await pathExists(template))) {
4646
const err: NodeJS.ErrnoException = new Error(`ENOENT, no such file or directory ${template}`)
4747
err.code = "ENOENT"
@@ -61,37 +61,34 @@ export async function Scaffold({ ...options }: ScaffoldConfig) {
6161
log(options, LogLevel.Debug, "before glob")
6262
const files = await promisify(glob)(template, {
6363
dot: true,
64-
debug: false,
65-
nodir: options.verbose === LogLevel.Debug,
66-
nobrace: true,
67-
noext: true,
68-
nocomment: true,
69-
nonegate: true,
64+
debug: options.verbose === LogLevel.Debug,
65+
nodir: true,
7066
})
7167
log(options, LogLevel.Debug, "after glob")
7268
for (const inputFilePath of files) {
73-
if (!(await isDir(inputFilePath))) {
74-
const relPath = makeRelativePath(path.dirname(removeGlob(inputFilePath).replace(_nonGlobTemplate, "")))
75-
const basePath = path
76-
.resolve(process.cwd(), relPath)
77-
.replace(process.cwd() + "/", "")
78-
.replace(process.cwd(), "")
79-
log(
80-
options,
81-
LogLevel.Debug,
82-
`\nprocess.cwd(): ${process.cwd()}`,
83-
`\norigTemplate: ${origTemplate}`,
84-
`\nrelPath: ${relPath}`,
85-
`\ntemplate: ${template}`,
86-
`\ninputFilePath: ${inputFilePath}`,
87-
`\nnonGlobTemplate: ${_nonGlobTemplate}`,
88-
`\nbasePath: ${basePath}`,
89-
`\nisDir: ${_isDir}`,
90-
`\nisGlob: ${_isGlob}`,
91-
`\n`
92-
)
93-
await handleTemplateFile(inputFilePath, basePath, options, options.data)
69+
if (await isDir(inputFilePath)) {
70+
continue
9471
}
72+
const relPath = makeRelativePath(path.dirname(removeGlob(inputFilePath).replace(_nonGlobTemplate, "")))
73+
const basePath = path
74+
.resolve(process.cwd(), relPath)
75+
.replace(process.cwd() + "/", "")
76+
.replace(process.cwd(), "")
77+
log(
78+
options,
79+
LogLevel.Debug,
80+
`\nprocess.cwd(): ${process.cwd()}`,
81+
`\norigTemplate: ${origTemplate}`,
82+
`\nrelPath: ${relPath}`,
83+
`\ntemplate: ${template}`,
84+
`\ninputFilePath: ${inputFilePath}`,
85+
`\nnonGlobTemplate: ${_nonGlobTemplate}`,
86+
`\nbasePath: ${basePath}`,
87+
`\nisDir: ${_isDir}`,
88+
`\nisGlob: ${_isGlob}`,
89+
`\n`
90+
)
91+
await handleTemplateFile(inputFilePath, basePath, options, options.data)
9592
}
9693
} catch (e: any) {
9794
handleErr(e)
@@ -112,12 +109,12 @@ async function handleTemplateFile(
112109
return new Promise(async (resolve, reject) => {
113110
try {
114111
const inputPath = path.resolve(process.cwd(), templatePath)
115-
const outputPathOpt = getOptionValueForFile(inputPath, data, options.output)
112+
const outputPathOpt = getOptionValueForFile(options, inputPath, data, options.output)
116113
const outputDir = path.resolve(
117114
process.cwd(),
118115
...([outputPathOpt, basePath, options.createSubFolder ? options.name : undefined].filter(Boolean) as string[])
119116
)
120-
const outputPath = handlebarsParse(path.join(outputDir, path.basename(inputPath)), data)
117+
const outputPath = handlebarsParse(options, path.join(outputDir, path.basename(inputPath)), data).toString()
121118
log(
122119
options,
123120
LogLevel.Debug,
@@ -129,7 +126,7 @@ async function handleTemplateFile(
129126
`\nFull output path: ${outputPath}`,
130127
`\n`
131128
)
132-
const overwrite = getOptionValueForFile(inputPath, data, options.overwrite ?? false)
129+
const overwrite = getOptionValueForFile(options, inputPath, data, options.overwrite ?? false)
133130
const exists = await pathExists(outputPath)
134131

135132
await createDirIfNotExists(path.dirname(outputPath), options)
@@ -140,7 +137,7 @@ async function handleTemplateFile(
140137
log(options, LogLevel.Info, `File ${outputPath} exists, overwriting`)
141138
}
142139
const templateBuffer = await readFile(inputPath)
143-
const outputContents = handlebarsParse(templateBuffer, data)
140+
const outputContents = handlebarsParse(options, templateBuffer, data)
144141

145142
if (!options.dryRun) {
146143
await writeFile(outputPath, outputContents)

src/utils.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export async function createDirIfNotExists(dir: string, options: ScaffoldConfig)
8080
}
8181

8282
export function getOptionValueForFile<T>(
83+
options: ScaffoldConfig,
8384
filePath: string,
8485
data: Record<string, string>,
8586
fn: FileResponse<T>,
@@ -90,15 +91,24 @@ export function getOptionValueForFile<T>(
9091
}
9192
return (fn as FileResponseFn<T>)(
9293
filePath,
93-
path.dirname(handlebarsParse(filePath, data)),
94-
path.basename(handlebarsParse(filePath, data))
94+
path.dirname(handlebarsParse(options, filePath, data).toString()),
95+
path.basename(handlebarsParse(options, filePath, data).toString())
9596
)
9697
}
9798

98-
export function handlebarsParse(templateBuffer: Buffer | string, data: Record<string, string>) {
99-
const parser = Handlebars.compile(templateBuffer.toString(), { noEscape: true })
100-
const outputContents = parser(data)
101-
return outputContents
99+
export function handlebarsParse(
100+
options: ScaffoldConfig,
101+
templateBuffer: Buffer | string,
102+
data: Record<string, string>
103+
) {
104+
try {
105+
const parser = Handlebars.compile(templateBuffer.toString(), { noEscape: true })
106+
const outputContents = parser(data)
107+
return outputContents
108+
} catch {
109+
log(options, LogLevel.Warning, "Couldn't parse file with handlebars, returning original content")
110+
return templateBuffer
111+
}
102112
}
103113

104114
export async function pathExists(filePath: string): Promise<boolean> {

0 commit comments

Comments
 (0)