Skip to content

Commit 4240ff0

Browse files
authored
fix: resolve command incompatibility issue on Windows (#86)
1 parent 8026ab3 commit 4240ff0

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path"
10+
"runtime"
1011
"strings"
1112
"time"
1213

@@ -164,17 +165,25 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) {
164165
return nil, fmt.Errorf("hugo not found, install it from https://gohugo.io/: %s", err)
165166
}
166167

168+
// Create output directory
169+
err = os.MkdirAll(outputDirPath, 0700)
170+
if err != nil {
171+
log.Fatal().
172+
Err(err).
173+
Str("outputDirPath", outputDirPath).
174+
Msg("error creating output directory")
175+
return nil, fmt.Errorf("error creating output directory '%s': %s", outputDirPath, err)
176+
}
177+
167178
commands := []string{
168179
"git version",
169180
"hugo version",
170-
fmt.Sprintf("mkdir -p %s", outputDirPath),
171181
// Use YAML file as it is easier to edit it afterward than TOML
172182
fmt.Sprintf("cd %s && hugo new site %s --format yaml", outputDirPath, siteName),
173183
fmt.Sprintf("cd %s && git clone https://github.com/adityatelange/hugo-PaperMod themes/PaperMod --depth=1",
174184
path.Join(outputDirPath, siteName)),
175-
fmt.Sprintf("cd %s && rm -rf themes/PaperMod/.git themes/PaperMod/.github", path.Join(outputDirPath, siteName)),
176185
// Set theme to PaperMod
177-
fmt.Sprintf(`echo "theme: 'PaperMod'">> %s/hugo.yaml`, path.Join(outputDirPath, siteName)),
186+
fmt.Sprintf(`echo theme: 'PaperMod'>> %s/hugo.yaml`, path.Join(outputDirPath, siteName)),
178187
// Verify that the site is set up correctly
179188
fmt.Sprintf("cd %s && hugo", path.Join(outputDirPath, siteName)),
180189
}
@@ -184,7 +193,16 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) {
184193
Int("totalSteps", len(commands)).
185194
Str("cmd", command).
186195
Msg("Running Hugo setup command")
187-
output, err := exec.Command("bash", "-c", command).Output()
196+
var (
197+
output []byte
198+
err error
199+
)
200+
if runtime.GOOS == "windows" {
201+
output, err = exec.Command("cmd", "/C", command).Output()
202+
} else {
203+
// mac & Linux
204+
output, err = exec.Command("bash", "-c", command).Output()
205+
}
188206
if err != nil {
189207
log.Error().
190208
Err(err).
@@ -196,6 +214,22 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) {
196214
log.Debug().Msgf("Hugo setup output: %s", output)
197215
}
198216

217+
// Delete .git directory
218+
deleteDirs := []string{
219+
path.Join(outputDirPath, siteName, ".git"),
220+
path.Join(outputDirPath, siteName, "themes/PaperMod/.git"),
221+
}
222+
for _, dir := range deleteDirs {
223+
err := os.RemoveAll(dir)
224+
if err != nil {
225+
log.Error().
226+
Err(err).
227+
Str("dir", dir).
228+
Msg("error removing directory")
229+
return nil, fmt.Errorf("error removing directory '%s': %s", dir, err)
230+
}
231+
}
232+
199233
siteDir := path.Join(outputDirPath, siteName)
200234
log.Info().
201235
Str("location", siteDir).

0 commit comments

Comments
 (0)