Skip to content

Instantly share code, notes, and snippets.

@SMoni
Last active June 30, 2022 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SMoni/7b3a3564a5cb69b1d1c2726877b77e96 to your computer and use it in GitHub Desktop.
Save SMoni/7b3a3564a5cb69b1d1c2726877b77e96 to your computer and use it in GitHub Desktop.
Copy all npm-packages for production into the dist-folder
Clear-Host
Set-Location $PSScriptRoot
$main = {
npm list --prod |
Select-Object -Skip 1 | # First line is the project itself
Get-Dependency |
Sort-Object |
Get-Unique |
Copy-Dependency
}
function Get-Dependency() {
process {
if([string]::IsNullOrWhiteSpace($_)) {
return
}
$result = [regex]::Matches($_, '--\s(?<file>.+)@')[0].groups['file'].Value
if([string]::IsNullOrWhiteSpace($result)) {
return
}
$result. `
Replace(' deduped', ''). `
Replace('/', '\'). `
Trim()
}
}
function Copy-Dependency() {
begin {
$sourceFolder = '.\node_modules'
$destinationFolder = '.\dist\node_modules'
if(Test-Path $destinationFolder) {
Remove-Item $destinationFolder -Force -Recurse
}
}
process {
Write-Host $_
$source = "$sourceFolder\$_"
$destination = "$destinationFolder\$_"
Copy-Item -Path $source -Destination $destination -Recurse
}
}
&$main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment