Skip to content

Commit 72ddd4f

Browse files
committed
✨ Replace validation - only files in project dir
1 parent 97698ff commit 72ddd4f

8 files changed

Lines changed: 54 additions & 24 deletions

File tree

lib/Bootstrap.js

Lines changed: 7 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Bootstrap.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Files.js

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Files.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/StarterLoader.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/StarterLoader.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Files.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ export class Files {
66
path.join(import.meta.dirname, '..')
77
)
88

9-
public static async exists(path: string) {
10-
const stat = await fsp.stat(path).catch(() => undefined)
9+
public static async exists(filepath: string) {
10+
const stat = await fsp.stat(filepath).catch(() => undefined)
1111
return Boolean(stat)
1212
}
1313

14-
public static async existsAndIsDir(path: string) {
15-
const stat = await fsp.stat(path).catch(() => undefined)
14+
public static async existsAndIsDir(filepath: string) {
15+
const stat = await fsp.stat(filepath).catch(() => undefined)
1616
return Boolean(stat?.isDirectory())
1717
}
18-
public static async readUtf8File(path: string) {
19-
return fsp.readFile(path, 'utf8')
18+
public static async readUtf8File(filepath: string) {
19+
return fsp.readFile(filepath, 'utf8')
20+
}
21+
public static isInProjectDir(filepath: string) {
22+
const file = path.normalize(filepath)
23+
return file.startsWith(Files.PROJECT_DIR)
2024
}
2125
}

src/StarterLoader.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import glob from 'fast-glob'
22
import fs from 'node:fs'
33
import path from 'node:path'
4+
import { Files } from './Files.js'
45

56
export interface StarterConfig {
67
module: string
@@ -105,6 +106,16 @@ export class StarterLoader {
105106
`Invalid config at ${path}: "replace" must be array of files where strings should be replaced`
106107
)
107108
}
109+
if (config.replace) {
110+
const invalidReplace = config.replace.find(
111+
(replace: string) => !Files.isInProjectDir(replace)
112+
)
113+
if (invalidReplace) {
114+
throw new Error(
115+
`Invalid config at ${path}: "replace" must be array of files in the project directory, got: ${invalidReplace}`
116+
)
117+
}
118+
}
108119

109120
const invalidKeys = Object.keys(config).filter(
110121
key => !['module', 'name', 'prebuild', 'replace', 'id'].includes(key)

0 commit comments

Comments
 (0)