simple-gitパッケージをやめてコマンド直打ちで実装し直した。それでもgit push
を2回すればアップできた。なぜ2回必要なのかは謎。
ブツ
インストール&実行
NAME='Electron.MyLog.git.push.20220903094945'
git clone https://github.com/ytyaru/$NAME
cd $NAME
npm install
npm start
準備
- GitHubアカウントを作成する
repo
スコープ権限をもったアクセストークンを作成する- インストール&実行してアプリ終了する
db/setting.json
ファイルが自動作成される
db/setting.json
に以下をセットしファイル保存するusername
:任意のGitHubユーザ名email
:任意のGitHubメールアドレスtoken
:repo
スコープ権限を持ったトークンrepo
:任意リポジトリ名(mytestrepo
等)address
:任意モナコイン用アドレス(MEHCqJbgiNERCH3bRAtNSSD9uxPViEX1nu
等)
dst/mytestrepo/.git
が存在しないことを確認する(あればdst
ごと削除する)- GitHub上に同名リモートリポジトリが存在しないことを確認する(あれば削除する)
実行
npm start
で起動またはアプリでCtrl+Shift+Rキーを押す(リロードする)git init
コマンドが実行されるrepo/リポジトリ名
ディレクトリが作成され、その配下に.git
ディレクトリが作成される
- [createRepo][]実行後、リモートリポジトリが作成される
GitHub Pages デプロイ
アップロードされたファイルからサイトを作成する。
- アップロードしたユーザのリポジトリページにアクセスする(
https://github.com/ユーザ名/リポジトリ名
) - 設定ページにアクセスする(
https://github.com/ユーザ名/リポジトリ名/settings
) Pages
ページにアクセスする(https://github.com/ユーザ名/リポジトリ名/settings/pages
)Source
のコンボボックスがDeploy from a branch
になっていることを確認するBranch
をmaster
にし、ディレクトリを/(root)
にし、Saveボタンを押す- F5キーでリロードし、そのページに
https://ytyaru.github.io/リポジトリ名/
のリンクが表示されるまでくりかえす(数分かかる) https://ytyaru.github.io/リポジトリ名/
のリンクを参照する(デプロイ完了してないと404エラー)
すべて完了したリポジトリとそのサイトが以下。
まだ画像パスのバグを修正していないので投げモナボタンは表示されない。
経緯
リポジトリ | 結果 |
---|---|
Electron.MyLog.20220831094901 | なぜかasset/ ディレクトリがアップされない(ローカルにはあるのに) |
Electron.simple.git.20220902105438 | なぜか2回目のpushでアップされた |
simple-gitパッケージを使わず、gitコマンドを叩くだけでも、同じように2回目のpushでアップされるかもしれない。そう思って今回、試してみたところ、成功した。
コード抜粋
renderer.js
const exists = await git.init(document.getElementById('github-repo').value)
if (!exists) { // .gitがないなら
const res = await hub.createRepo({
'name': `${setting.github.repo}`,
'description': 'リポジトリの説明',
}, setting)
await maker.make()
await git.push('新規作成')
await git.push('なぜか初回pushではasset/ディレクトリなどがアップロードされないので2回やってみる')
}
アプリが起動したとき、かつローカルリポジトリが存在しない場合、以下のようにして必要なファイルをアップロードする。
git init
して作成- リモートリポジトリ作成
- サイト作成に必要なファイル一式を作成
git push
する- なぜか初回だけだと全ファイルがアップされないので2回目の
git push
も直後に行う
これで全ファイルがアップされた。2回git push
が必要な理由が謎。
とにかく、なぜか2回git push
しないとファイルのアップロードが完了しないのはわかった。
それはsimple-gitパッケージを使おうが、裸のコマンドで叩こうが同じ。ならパッケージがないほうがムダな依存関係を減らせるので助かる。というわけで、simple-gitパッケージは使わずに実装することにした。
今後もまだまだ未知で謎で原因不明なバグが山ほど出てくる予感しかしない……。
git.js
コマンド直打ちしているコードは以下。最初にわざわざ毎回cd
でカレントディレクトリをセットしている以外は、ふつうにコマンドを叩いている。
class Git {
async init(repo) {
this.repo = repo
const exists = await window.myApi.exists(`${this.dir}/${this.repo}/.git`)
if(!exists) {
await window.myApi.mkdir(`${this.dir}/${this.repo}`)
let res = await window.myApi.shell(`cd "${this.dir}/${this.repo}/"; git init;`)
res = await this.#remoteAddOrigin()
} else {
console.log(`${this.dir}/${this.repo}/.git は既存のためgit initしません。`)
}
return exists
}
async push(message='追記') {
let res = await this.#setUser()
res = await this.#add()
res = await this.#commit(message)
res = await this.#push()
}
async #setUser() {
console.log('setUser():', this.username, this.email)
const res1 = await window.myApi.shell(`git config --global user.name '${this.username}'`)
const res2 = await window.myApi.shell(`git config --global user.email '${this.email}'`)
return res1.stdout + '\n' + res2.stdout
}
async #add() {
return await window.myApi.shell(`cd "${this.dir}/${this.repo}"; git add .;`)
}
async #addList() {
return await window.myApi.shell(`cd "${this.dir}/${this.repo}"; git add -n .;`)
}
async #commit(message) {
return await window.myApi.shell(`cd "${this.dir}/${this.repo}"; git commit -m '${message}';`)
}
async #remoteAddOrigin() {
return await window.myApi.shell(`cd "${this.dir}/${this.repo}"; git remote add origin "https://${this.username}:${this.token}@github.com/${this.username}/${this.repo}.git";`)
}
async #remoteSetUrlOrigin() {
return await window.myApi.shell(`cd "${this.dir}/${this.repo}"; git remote set-url origin "https://${this.username}:${this.token}@github.com/${this.username}/${this.repo}.git";`)
}
async #push() {
return await window.myApi.shell(`cd "${this.dir}/${this.repo}"; git push origin ${this.branch}`)
}
}