Skip to content

Commit bc3d8ad

Browse files
committed
Add option to remap paths at the start
1 parent 3668976 commit bc3d8ad

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/cli.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const ERROR_CODE = {
1717

1818
function logUsage() {
1919
stdout.write(`🙅🏻‍♀️ staticbuild - a static site generator that isn't for you!\n\n`)
20-
stdout.write(`Usage: staticbuild <inputDirectory> <outputDirectory> [--watch, --dry-run, --check]\n`)
20+
stdout.write(`Usage: staticbuild <inputDirectory> <outputDirectory> [arguments]\n`)
2121

2222
stdout.write(`\nArguments:\n`)
2323
stdout.write(`<inputDirectory> Path to directory containing content\n`)
@@ -26,6 +26,8 @@ function logUsage() {
2626
stdout.write(`--dry-run Prevent writing files to disk, instead logging the file list out\n`)
2727
stdout.write(`--check, -c Perform a dead link check on the output files\n`)
2828
stdout.write(`--verbose, -v Output all info and warning logs\n`)
29+
stdout.write(`--ignore List of start of paths to ignore\n`)
30+
stdout.write(`--remap Mapping of start of paths that will get renamed, e.g 'projects':'blog' \n`)
2931
}
3032

3133
async function main() {
@@ -99,6 +101,21 @@ async function main() {
99101
continue
100102
}
101103

104+
case "remap": {
105+
const pathRemaps: Record<string, string> = {}
106+
107+
for (const mapping of arg.value.split(",")) {
108+
const [from, to] = mapping.split(":")
109+
if (from === undefined || to === undefined) continue
110+
if (pathRemaps[from]) continue
111+
112+
pathRemaps[path.join(options.outputDirectory, from)] = path.join(options.outputDirectory, to)
113+
}
114+
115+
options.pathRemaps = pathRemaps
116+
continue
117+
}
118+
102119
default: {
103120
stdout.write(`Error: Unknown argument ${arg.key}\n`)
104121
return ERROR_CODE.CALLED_WITH_ILLEGAL_PARAMETERS

src/staticbuild.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface StaticBuildOptions {
5656
dryRun?: boolean
5757
check?: boolean
5858
ignoredPaths?: string[]
59+
pathRemaps?: Record<string, string>
5960
baseURL?: string
6061

6162
logger?: {
@@ -190,11 +191,20 @@ function getLayouts(inputDirectory: string): Templates {
190191
return templates
191192
}
192193

194+
function replaceStart(text: string, searchValue: string, replaceValue: string): string {
195+
if (text.startsWith(searchValue)) {
196+
return replaceValue + text.slice(searchValue.length)
197+
}
198+
199+
return text
200+
}
201+
193202
function shouldSkipFilePath(
194203
relativeFilePath: string,
195204
ignoredPaths: string[] = [],
196205
): [shouldSkip: boolean, reason: string] {
197206
const filename = path.basename(relativeFilePath)
207+
if (filename === "_config.js") return [false, ""]
198208
if (filename.startsWith("_")) return [true, "underscore_prefix"]
199209

200210
for (const ignoredPath of ignoredPaths || []) {
@@ -912,13 +922,20 @@ export default async function staticbuild(options: StaticBuildOptions) {
912922

913923
options.logger?.time("Write")
914924

925+
const pathRemaps: Exclude<StaticBuildOptions["pathRemaps"], undefined> = options.pathRemaps || {}
926+
915927
for (const [_, file] of outputFiles) {
916928
const buffer: Buffer<ArrayBuffer> = isExternalFile(file)
917929
? Buffer.from(fs.readFileSync(file.inputPath))
918930
: file.buffer
919931

932+
for (const [from, to] of Object.entries(pathRemaps)) {
933+
file.outputPath = replaceStart(file.outputPath, from, to)
934+
}
935+
920936
if (options.dryRun) {
921937
options.logger?.info("[fake_file_write]")
938+
922939
if (isExternalFile(file)) {
923940
options.logger?.info(" in: " + file.inputPath)
924941
} else {

0 commit comments

Comments
 (0)