Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Jul 25, 2017
0 parents commit 94c42d5
Show file tree
Hide file tree
Showing 14 changed files with 1,811 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/lib/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
!/lib/*.js
!/bin/*.js
*.test.js
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2017 Smooth Code

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# smooth-code-cli

Smooth code utility for training exercises.

## Usage

```
Usage: smooth-code [options] [command]
Options:
-V, --version output the version number
-h, --help output usage information
Commands:
init <training-name> Go to the beginning of exercise
start <exercise> Go to the beginning of an exercise
end <exercise> Go to the end of an exercise
solution <exercise> Get the solution of an exercise
tag Tag all exercises [only for trainer]
```

## License

MIT
3 changes: 3 additions & 0 deletions bin/smooth-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib/index')
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "smooth-code-cli",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"bin": {
"smooth-code": "./bin/smooth-code"
},
"scripts": {
"prepublish": "npm run build",
"build": "babel -d lib src"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-env": "^1.6.0"
},
"dependencies": {
"commander": "^2.11.0",
"mz": "^2.6.0",
"opn": "^5.1.0"
}
}
15 changes: 15 additions & 0 deletions src/commands/end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { exec } from 'mz/child_process'

export default program =>
program
.command('end <exercise>')
.description('Go to the end of an exercise')
.action(async (exercise, options) => {
console.log('Stashing changes...')
await exec(`git stash save "end exercise ${exercise}"`)
console.log(`Going to the end of exercise ${exercise}`)
await exec(`git checkout end-exercise-${exercise}`)
console.log(`Installing dependencies...`)
await exec(`npm install`)
console.log('Project ready')
})
15 changes: 15 additions & 0 deletions src/commands/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { exec } from 'mz/child_process'

export default program =>
program
.command('init <training-name>')
.description('Go to the beginning of exercise')
.action(async (trainingName, options) => {
console.log('Cloning git repository')
await exec(`git clone https://github.com/smooth-code/${trainingName}.git`)
console.log('Go to exercise 1')
await exec(`cd ${trainingName} && git checkout start-exercise-1`)
console.log('Installing dependencies...')
await exec(`cd ${trainingName} && npm install`)
console.log(`Project initialized "cd ${trainingName}" to start`)
})
20 changes: 20 additions & 0 deletions src/commands/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { exec } from 'mz/child_process'
import opn from 'opn'

async function getProjectUrl() {
const remoteStr = String(await exec('git remote -v'))
const matches = remoteStr.match(/(https.*)\.git/)
return matches[1]
}

export default program =>
program
.command('solution <exercise>')
.description('Get the solution of an exercise')
.action(async (exercise, options) => {
const projectUrl = await getProjectUrl()
opn(
`${projectUrl}/compare/start-exercise-${exercise}...end-exercise-${exercise}`,
{ wait: false },
)
})
15 changes: 15 additions & 0 deletions src/commands/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { exec } from 'mz/child_process'

export default program =>
program
.command('start <exercise>')
.description('Go to the beginning of an exercise')
.action(async (exercise, options) => {
console.log('Stashing changes...')
await exec(`git stash save "start exercise ${exercise}"`)
console.log(`Going to the start of exercise ${exercise}`)
await exec(`git checkout start-exercise-${exercise}`)
console.log(`Installing dependencies...`)
await exec(`npm install`)
console.log('Project ready')
})
28 changes: 28 additions & 0 deletions src/commands/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { exec } from 'mz/child_process'

const parseLogLine = logLine => {
const matches = logLine.match(/^(.+) \[ex\-(\d+)]/)
return matches
? {
hash: matches[1],
exercise: Number(matches[2]),
}
: null
}

export default program =>
program
.command('tag')
.description('Tag all exercises [only for trainer]')
.action(async () => {
const logs = String(await exec('git log --pretty=oneline'))
const commits = logs.split('\n').map(parseLogLine).filter(x => x)
console.log(`${commits.length} valid commits detected`)
for (const commit of commits) {
await exec(`git tag -f end-exercise-${commit.exercise} ${commit.hash} `)
await exec(
`git tag -f start-exercise-${commit.exercise + 1} ${commit.hash}`,
)
console.log(`Tagging exercise #${commit.exercise}`)
}
})
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import program from 'commander'
import { exec } from 'mz/child_process'
import pkg from '../package.json'
import tag from './commands/tag'
import start from './commands/start'
import end from './commands/end'
import init from './commands/init'
import solution from './commands/solution'

program.version(pkg.version)

init(program)
start(program)
end(program)
solution(program)
tag(program)

program.parse(process.argv)

if (program.args.length === 0) program.help()
Loading

0 comments on commit 94c42d5

Please sign in to comment.