More PowerShell Function Hacks

powershellicon

 

Following up on my last post (PowerShell Profile Shortcuts), I have another really cool hack if you, like me, are creating many github repositories for hackathons or demos.

 

Wouldn’t it be cool if you could use the following from the command line

createrepo mynewrepo

And it would

  1. Create a repo on git hub
  2. Clone it locally
  3. CD into that directory
  4. Create a readme.md file
  5. Create a package.json
  6. Create a basic gitignore file
  7. Add all files using git add .
  8. Create a commit
  9. Push back up to your repo

Well you can, just create a function in my powershell profile to do all of this. (See below)

There are three prerequisites:

  1. Install gh —   (npm install -g gh)
  2. Install gig       (npm install -g gig)
  3. Set up npm init defaults (http://iamsim.me/set-your-npm-init-defaults/))
  4. change references to your own github account (says YourGitHubAccount in code now)

To modify your powershell profile

If using VS Code  just type code $profile from your powerhell command line. Otherwise, there are plenty of tutorials on how to modify your profile like this one (http://www.techrepublic.com/blog/data-center/how-to-use-a-powershell-profile-to-simplify-tasks/)

Either way, once you get it open, just paste this in (after prerequisites above) and change the github account to yours.

Function createrepo($reponame){
  #need to install gh --- npm install -g gh
  gh re --new $reponame -B false
  git clone https://github.com/YourGitHubAccount/$reponame.git
  cd $reponame
  #need to install gig npm install -g gig
  gig gen node
  #need to have defaults set up for this (http://iamsim.me/set-your-npm-init-defaults/)
  npm init -y
  #now add a readme file
  echo "# $reponame" >> README.md
  
  echo "## Synopsis" >> README.md

  echo "At the top of the file there should be a short introduction and/ or overview that explains **what** the project is. This description should match descriptions added for package managers (Gemspec, package.json, etc.)" >> README.md

  echo "## Code Example" >> README.md

  echo "Show what the library does as concisely as possible, developers should be able to figure out **how** your project solves their problem by looking at the code example. Make sure the API you are showing off is obvious, and that your code is short and concise." >> README.md

  echo "## Motivation" >> README.md

  echo "A short description of the motivation behind the creation and maintenance of the project. This should explain **why** the project exists." >> README.md

  echo "## Installation" >> README.md

  echo "Provide code examples and explanations of how to get the project." >> README.md

  echo "## API Reference" >> README.md

  echo "Depending on the size of the project, if it is small and simple enough the reference docs can be added to the README. For medium size to larger projects it is important to at least provide a link to where the API reference docs live." >> README.md

  echo "## Tests" >> README.md

  echo "Describe and show how to run the tests with code examples." >> README.md

  echo "## Contributors" >> README.md

  echo "Let people know how they can dive into the project, include important links to things like issue trackers, irc, twitter accounts if applicable." >> README.md

  echo "## License" >> README.md

  echo "A short snippet describing the license (MIT, Apache, etc.)" >> README.md
  git add .
  git commit -m "first commit"
  #if you dont want it to prompt you for credentials every time run this : git config --global credential.helper wincred 
  git push -u origin master
}