Created
May 18, 2018 04:21
-
-
Save vhanla/4f960195a56c8a4de46c4abe440fde9c to your computer and use it in GitHub Desktop.
PowerShell script to help maintain git on Vuepress's built directory
This file contains hidden or 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 script allows to maintain a github hosted page | |
# First: make sure to clone or init your git in the destination directory | |
# this script will make a backup of the .git directory | |
# once vuepress has built again (replacing all things in destination directory) | |
# this script will restore the original .git directory | |
# Finally you can git add . & commit as you wish | |
# so .git directory will always be the latest one | |
cls | |
Write-Host "Vuepress git tool v1.0" | |
Write-Host "Author: vhanla" | |
function Show-Menu { | |
param( | |
[string]$Title = 'Options' | |
) | |
Write-Host "================== $Title ==================" | |
Write-Host "1: Develop." | |
Write-Host "2: Build." | |
Write-Host "3: Deploy." | |
# Write-Host "Q: Quit." | |
} | |
function Is-Root { | |
if(Test-Path -Path ($PWD.Path + '\.vuepress')){ | |
return $true | |
} | |
else { | |
Write-Host 'Wrong directory!' | |
return $false | |
} | |
} | |
function Config-Exists { | |
if(Test-Path ($PWD.Path + '.\.vuepress\config.js')){ | |
return $true | |
} | |
else { | |
return $false | |
} | |
} | |
function Copy-ToEmptyDir($source, $dest){ | |
Delete-Dir $dest | |
if (Test-Path $source){ | |
Create-Dir $dest | |
Copy-Item $source\* $dest -Recurse -Force | |
return $true | |
}else{ | |
Write-Host 'Warning! no GIT repository in ' $source | |
return $false | |
} | |
} | |
function Delete-Dir ($dir){ | |
if(Test-Path $dir){ | |
Get-ChildItem -Path $dir -Force -Recurse | Remove-Item -Force -Recurse | |
Remove-Item $dir -Force | |
} | |
} | |
function Create-Dir ($dir){ | |
New-Item -ItemType Directory -Force -Path $dir | |
} | |
function Get-DistDir { | |
# assume it is on root path | |
if (Config-Exists){ | |
$config = Get-Content -Path ($PWD.Path + '.\.vuepress\config.js') | |
$json = $config -replace '^(\s)*module.exports(\s)*=(\s)*' | |
$dist = '' | |
$dist = (((($json -match '(,)*(\s)*dest(.)*(,)*') -replace '(,)*(\s)*dest(\s)*:') -replace ',').Trim() -replace "'") -replace '"' | |
if (!$dist.Trim()){ | |
# we should use the default .vuepress/dist folder | |
$dist = $PWD.Path + '\.vuepress\dist' | |
} | |
else{ | |
$dist = $PWD.Path + '\' + $dist | |
} | |
return $dist | |
} | |
else{ | |
return ($PWD.Path + '\.vuepress\dist') | |
} | |
} | |
function Copy-GitDir { | |
if (Is-Root){ | |
$dest = Get-DistDir | |
# we need to copy .git directory, the other files don't matter | |
Copy-ToEmptyDir ($dest + '\.git') ($PWD.Path + '\.vuepress\git') | |
Delete-Dir $dest | |
} | |
} | |
function Restore-GitDir { | |
if (Is-Root){ | |
$dest = Get-DistDir | |
Copy-ToEmptyDir ($PWD.Path + '\.vuepress\git') ($dest + '\.git') | |
} | |
} | |
# do{ | |
Show-Menu | |
$input = Read-Host "Please make a selection" | |
switch ($input) { | |
'1' { | |
cls | |
vuepress dev | |
} | |
'2' { | |
Copy-GitDir | |
vuepress build | |
Restore-GitDir | |
} | |
'3' { | |
$location = Get-DistDir | |
Set-Location -Path $location | |
} | |
'q' { | |
return | |
} | |
} | |
# } | |
# until ($input -eq 'q') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment