|
1 | 1 | import*asfsfrom'fs'
|
2 |
| -import{exec,exists}from'../node' |
| 2 | +import{promisify}from'util' |
| 3 | +import{exec}from'../node' |
| 4 | + |
| 5 | +constremoveFile=promisify(fs.unlink) |
3 | 6 |
|
4 | 7 | interfaceInput{
|
5 | 8 | hash:string
|
6 | 9 | branch:string
|
7 | 10 | }
|
8 | 11 |
|
| 12 | +constignoreError=()=>{} |
| 13 | + |
9 | 14 | // note: attempted to do this as a bash script
|
10 | 15 | // but requires the bash script has git permissions
|
11 | 16 | constreset=async({ branch, hash}:Input):Promise<void>=>{
|
12 |
| -// TODO: capture branch |
13 |
| -constlocalBranch='master' |
14 |
| - |
15 |
| -// switch to an empty branch |
16 |
| -awaitexec({ |
17 |
| -command:'git checkout --orphan reset-orphan-branch', |
18 |
| -}) |
19 |
| -// stash any current work |
20 |
| -awaitexec({ |
21 |
| -command:'git stash', |
22 |
| -}) |
23 |
| -// remove any other files |
24 |
| -awaitexec({ |
25 |
| -command:'git rm -rf .', |
26 |
| -}) |
27 |
| -// TODO: delete .gitignore |
28 |
| - |
29 |
| -awaitexec({ |
30 |
| -command:`git branch -D${localBranch}`, |
31 |
| -}) |
32 |
| -awaitexec({ |
33 |
| -command:`git checkout -b${localBranch}`, |
34 |
| -}) |
35 |
| - |
36 |
| -// load git timeline |
37 |
| -awaitexec({ |
38 |
| -command:`git fetch coderoad${branch}`, |
39 |
| -}) |
40 |
| -awaitexec({ |
41 |
| -command:`git merge coderoad/${localBranch}`, |
42 |
| -}) |
43 |
| -// reset to target commit hash |
44 |
| -awaitexec({ |
45 |
| -command:`git reset --hard${hash}`, |
46 |
| -}) |
| 17 | +constremote='coderoad' |
| 18 | + |
| 19 | +try{ |
| 20 | +// if no git init, will initialize |
| 21 | +// otherwise re-initializes git |
| 22 | +awaitexec({command:'git init'}).catch(console.log) |
| 23 | + |
| 24 | +// capture current branch |
| 25 | +consthasBranch=awaitexec({command:'git branch --show-current'}) |
| 26 | +constlocalBranch=hasBranch.stdout |
| 27 | +// check if coderoad remote exists |
| 28 | +consthasRemote=awaitexec({command:'git remote -v'}).catch(console.warn) |
| 29 | +if(!hasRemote||!hasRemote.stdout||!hasRemote.stdout.length){ |
| 30 | +thrownewError('No remote found') |
| 31 | +}elseif(!hasRemote.stdout.match(newRegExp(remote))){ |
| 32 | +thrownewError(`No "${remote}" remote found`) |
| 33 | +} |
| 34 | + |
| 35 | +// switch to an empty branch |
| 36 | +awaitexec({ |
| 37 | +command:'git checkout --orphan reset-orphan-branch', |
| 38 | +}) |
| 39 | +// stash any current work |
| 40 | +awaitexec({ |
| 41 | +command:'git stash', |
| 42 | +}).catch(ignoreError) |
| 43 | + |
| 44 | +// remove any other files |
| 45 | +awaitexec({ |
| 46 | +command:'git rm -rf .', |
| 47 | +}).catch(ignoreError) |
| 48 | +awaitremoveFile('.gitignore').catch(ignoreError) |
| 49 | + |
| 50 | +awaitexec({ |
| 51 | +command:`git branch -D${localBranch}`, |
| 52 | +}) |
| 53 | +awaitexec({ |
| 54 | +command:`git checkout -b${localBranch}`, |
| 55 | +}) |
| 56 | + |
| 57 | +// load git timeline |
| 58 | +awaitexec({ |
| 59 | +command:`git fetch coderoad${branch}`, |
| 60 | +}) |
| 61 | +awaitexec({ |
| 62 | +command:`git merge coderoad/${branch}`, |
| 63 | +}) |
| 64 | +// reset to target commit hash |
| 65 | +awaitexec({ |
| 66 | +command:`git reset --hard${hash}`, |
| 67 | +}) |
| 68 | +}catch(error){ |
| 69 | +console.error('Error resetting') |
| 70 | +console.error(error.message) |
| 71 | +} |
47 | 72 | }
|
48 | 73 |
|
49 | 74 | exportdefaultreset
|