Skip to content

Commit 94c42d5

Browse files
committed
First version
0 parents  commit 94c42d5

14 files changed

+1811
-0
lines changed

.babelrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
["env", {
4+
"targets": {
5+
"node": "current"
6+
}
7+
}]
8+
]
9+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/lib/

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
!/lib/*.js
3+
!/bin/*.js
4+
*.test.js

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2017 Smooth Code
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# smooth-code-cli
2+
3+
Smooth code utility for training exercises.
4+
5+
## Usage
6+
7+
```
8+
Usage: smooth-code [options] [command]
9+
10+
11+
Options:
12+
13+
-V, --version output the version number
14+
-h, --help output usage information
15+
16+
17+
Commands:
18+
19+
init <training-name> Go to the beginning of exercise
20+
start <exercise> Go to the beginning of an exercise
21+
end <exercise> Go to the end of an exercise
22+
solution <exercise> Get the solution of an exercise
23+
tag Tag all exercises [only for trainer]
24+
```
25+
26+
## License
27+
28+
MIT

bin/smooth-code

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/index')

package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "smooth-code-cli",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"bin": {
7+
"smooth-code": "./bin/smooth-code"
8+
},
9+
"scripts": {
10+
"prepublish": "npm run build",
11+
"build": "babel -d lib src"
12+
},
13+
"devDependencies": {
14+
"babel-cli": "^6.24.1",
15+
"babel-preset-env": "^1.6.0"
16+
},
17+
"dependencies": {
18+
"commander": "^2.11.0",
19+
"mz": "^2.6.0",
20+
"opn": "^5.1.0"
21+
}
22+
}

src/commands/end.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { exec } from 'mz/child_process'
2+
3+
export default program =>
4+
program
5+
.command('end <exercise>')
6+
.description('Go to the end of an exercise')
7+
.action(async (exercise, options) => {
8+
console.log('Stashing changes...')
9+
await exec(`git stash save "end exercise ${exercise}"`)
10+
console.log(`Going to the end of exercise ${exercise}`)
11+
await exec(`git checkout end-exercise-${exercise}`)
12+
console.log(`Installing dependencies...`)
13+
await exec(`npm install`)
14+
console.log('Project ready')
15+
})

src/commands/init.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { exec } from 'mz/child_process'
2+
3+
export default program =>
4+
program
5+
.command('init <training-name>')
6+
.description('Go to the beginning of exercise')
7+
.action(async (trainingName, options) => {
8+
console.log('Cloning git repository')
9+
await exec(`git clone https://github.com/smooth-code/${trainingName}.git`)
10+
console.log('Go to exercise 1')
11+
await exec(`cd ${trainingName} && git checkout start-exercise-1`)
12+
console.log('Installing dependencies...')
13+
await exec(`cd ${trainingName} && npm install`)
14+
console.log(`Project initialized "cd ${trainingName}" to start`)
15+
})

src/commands/solution.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { exec } from 'mz/child_process'
2+
import opn from 'opn'
3+
4+
async function getProjectUrl() {
5+
const remoteStr = String(await exec('git remote -v'))
6+
const matches = remoteStr.match(/(https.*)\.git/)
7+
return matches[1]
8+
}
9+
10+
export default program =>
11+
program
12+
.command('solution <exercise>')
13+
.description('Get the solution of an exercise')
14+
.action(async (exercise, options) => {
15+
const projectUrl = await getProjectUrl()
16+
opn(
17+
`${projectUrl}/compare/start-exercise-${exercise}...end-exercise-${exercise}`,
18+
{ wait: false },
19+
)
20+
})

src/commands/start.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { exec } from 'mz/child_process'
2+
3+
export default program =>
4+
program
5+
.command('start <exercise>')
6+
.description('Go to the beginning of an exercise')
7+
.action(async (exercise, options) => {
8+
console.log('Stashing changes...')
9+
await exec(`git stash save "start exercise ${exercise}"`)
10+
console.log(`Going to the start of exercise ${exercise}`)
11+
await exec(`git checkout start-exercise-${exercise}`)
12+
console.log(`Installing dependencies...`)
13+
await exec(`npm install`)
14+
console.log('Project ready')
15+
})

src/commands/tag.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { exec } from 'mz/child_process'
2+
3+
const parseLogLine = logLine => {
4+
const matches = logLine.match(/^(.+) \[ex\-(\d+)]/)
5+
return matches
6+
? {
7+
hash: matches[1],
8+
exercise: Number(matches[2]),
9+
}
10+
: null
11+
}
12+
13+
export default program =>
14+
program
15+
.command('tag')
16+
.description('Tag all exercises [only for trainer]')
17+
.action(async () => {
18+
const logs = String(await exec('git log --pretty=oneline'))
19+
const commits = logs.split('\n').map(parseLogLine).filter(x => x)
20+
console.log(`${commits.length} valid commits detected`)
21+
for (const commit of commits) {
22+
await exec(`git tag -f end-exercise-${commit.exercise} ${commit.hash} `)
23+
await exec(
24+
`git tag -f start-exercise-${commit.exercise + 1} ${commit.hash}`,
25+
)
26+
console.log(`Tagging exercise #${commit.exercise}`)
27+
}
28+
})

src/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import program from 'commander'
2+
import { exec } from 'mz/child_process'
3+
import pkg from '../package.json'
4+
import tag from './commands/tag'
5+
import start from './commands/start'
6+
import end from './commands/end'
7+
import init from './commands/init'
8+
import solution from './commands/solution'
9+
10+
program.version(pkg.version)
11+
12+
init(program)
13+
start(program)
14+
end(program)
15+
solution(program)
16+
tag(program)
17+
18+
program.parse(process.argv)
19+
20+
if (program.args.length === 0) program.help()

0 commit comments

Comments
 (0)