Comparing version
57
git.js
#!/usr/bin/env node | ||
import question from './src/question.js'; | ||
import GitLoader from './src/GitLoader.js'; | ||
import ora from 'ora'; | ||
import readLineSync from 'readline-sync'; | ||
import ini from 'ini'; | ||
import fs, { read } from 'fs'; | ||
const spinner = ora(`running git ${process.argv[2] || ''} ... `).start(); | ||
async function main() { | ||
const spinner = ora(`running git ${process.argv[2] || ''} ... `).start(); | ||
const oLoader = new GitLoader(); | ||
const oLoader = new GitLoader(); | ||
const aIgnoreCommands = ["clone", "init", "status"]; | ||
const aIgnoreCommands = ['clone', 'init', 'status']; | ||
if (!aIgnoreCommands.includes(process.argv[2]) && !oLoader.config.user) { | ||
oLoader.config.user = {}; | ||
oLoader.config.user.name = readLineSync.question("Enter your user name: "); | ||
oLoader.config.user.email = readLineSync.question("Enter your email: "); | ||
fs.writeFileSync(`${oLoader.base.dir}/${oLoader.base.gitdir}/config`, ini.stringify(oLoader.config)); | ||
} | ||
if (!aIgnoreCommands.includes(process.argv[2]) && !oLoader.config.user.token) { | ||
oLoader.config.user.token = readLineSync.question("Enter your token: "); | ||
fs.writeFileSync(`${oLoader.base.dir}/${oLoader.base.gitdir}/config`, ini.stringify(oLoader.config)); | ||
} | ||
if (!aIgnoreCommands.includes(process.argv[2]) && !oLoader.config.user) { | ||
oLoader.config.user = {}; | ||
oLoader.config.user.name = await question('Enter your user name: '); | ||
oLoader.config.user.email = await question('Enter your email: '); | ||
fs.writeFileSync( | ||
`${oLoader.base.dir}/${oLoader.base.gitdir}/config`, | ||
ini.stringify(oLoader.config) | ||
); | ||
} | ||
if ( | ||
!aIgnoreCommands.includes(process.argv[2]) && | ||
!oLoader.config.user.token | ||
) { | ||
oLoader.config.user.token = await question('Enter your token: '); | ||
fs.writeFileSync( | ||
`${oLoader.base.dir}/${oLoader.base.gitdir}/config`, | ||
ini.stringify(oLoader.config) | ||
); | ||
} | ||
oLoader.runCommand().then((rc) => { | ||
if (rc) { | ||
console.log(rc); | ||
try{ | ||
const rc = await oLoader.runCommand(); | ||
if (rc) { | ||
console.log(rc); | ||
} | ||
}catch(e){ | ||
console.log(e.toString()); | ||
} | ||
}).catch((e) => { | ||
spinner.stop(); | ||
} | ||
console.log(e.toString()); | ||
}).finally(() => { | ||
spinner.stop(); | ||
main().then(()=>{ | ||
process.exit(); | ||
}); |
{ | ||
"name": "git-pwa", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "git to use in a progressive web app", | ||
@@ -9,7 +9,3 @@ "bin": { | ||
}, | ||
"files": [ | ||
"dist", | ||
"src/GitLoader.js", | ||
"README.md" | ||
], | ||
"files": ["dist", "src/GitLoader.js", "README.md"], | ||
"main": "./dist/git-pwa.umd.js", | ||
@@ -31,6 +27,3 @@ "module": "./dist/git-pwa.es.js", | ||
}, | ||
"keywords": [ | ||
"pwa", | ||
"git" | ||
], | ||
"keywords": ["pwa", "git"], | ||
"author": "Rich Hildred", | ||
@@ -44,6 +37,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"git-pwa": "^1.0.2", | ||
"ini": "^4.1.0", | ||
"isomorphic-git": "^1.23.0", | ||
"ora": "^6.3.0", | ||
"readline-sync": "^1.4.10" | ||
"ora": "^6.3.0" | ||
}, | ||
@@ -50,0 +43,0 @@ "devDependencies": { |
import fs from 'fs'; | ||
import fsp from 'fs/promises' | ||
import fsp from 'fs/promises'; | ||
import path from 'path'; | ||
@@ -10,175 +10,185 @@ import ini from 'ini'; | ||
export default class { | ||
constructor(init) { | ||
if (typeof init != 'undefined') { | ||
Object.assign(this, init); | ||
} else { | ||
this.argv = parseArgs(process.argv); | ||
} | ||
this.base = { | ||
gitdir: '.git', | ||
dir: '.', | ||
fs: fs, | ||
http | ||
} | ||
try { | ||
this.config = ini.parse(fs.readFileSync(`${this.base.dir}/${this.base.gitdir}/config`, 'utf-8')); | ||
this.base.onAuth = () => ({ username:this.config.user.token}); | ||
} catch { | ||
this.config = {}; | ||
} | ||
this.base.corsProxy = 'https://corsproxy-dqo.pages.dev/gitcorsproxy'; | ||
if (this.config && this.config.http && this.config.http.corsProxy) { | ||
this.base.corsProxy = this.config.http.corsProxy; | ||
} | ||
constructor(init) { | ||
if (typeof init != 'undefined') { | ||
Object.assign(this, init); | ||
} else { | ||
this.argv = parseArgs(process.argv); | ||
} | ||
match(first, second){ | ||
// If we reach at the end of both strings, | ||
// we are done | ||
if (first.length == 0 && second.length == 0) | ||
return true; | ||
this.base = { | ||
gitdir: '.git', | ||
dir: '.', | ||
fs: fs, | ||
http, | ||
}; | ||
try { | ||
this.config = ini.parse( | ||
fs.readFileSync(`${this.base.dir}/${this.base.gitdir}/config`, 'utf-8') | ||
); | ||
this.base.onAuth = () => ({ username: this.config.user.token }); | ||
} catch { | ||
this.config = {}; | ||
} | ||
this.base.corsProxy = 'https://corsproxy-dqo.pages.dev/gitcorsproxy'; | ||
if (this.config && this.config.http && this.config.http.corsProxy) { | ||
this.base.corsProxy = this.config.http.corsProxy; | ||
} | ||
} | ||
match(first, second) { | ||
// If we reach at the end of both strings, | ||
// we are done | ||
if (first.length == 0 && second.length == 0) return true; | ||
// Make sure that the characters after '*' | ||
// are present in second string. | ||
// This function assumes that the first | ||
// string will not contain two consecutive '*' | ||
if (first.length > 1 && first[0] == '*' && | ||
second.length == 0) | ||
return false; | ||
// Make sure that the characters after '*' | ||
// are present in second string. | ||
// This function assumes that the first | ||
// string will not contain two consecutive '*' | ||
if (first.length > 1 && first[0] == '*' && second.length == 0) return false; | ||
// If the first string contains '?', | ||
// or current characters of both strings match | ||
if ((first.length > 1 && first[0] == '?') || | ||
(first.length != 0 && second.length != 0 && | ||
first[0] == second[0])) | ||
return this.match(first.substring(1), | ||
second.substring(1)); | ||
// If the first string contains '?', | ||
// or current characters of both strings match | ||
if ( | ||
(first.length > 1 && first[0] == '?') || | ||
(first.length != 0 && second.length != 0 && first[0] == second[0]) | ||
) | ||
return this.match(first.substring(1), second.substring(1)); | ||
// If there is *, then there are two possibilities | ||
// a) We consider current character of second string | ||
// b) We ignore current character of second string. | ||
if (first.length > 0 && first[0] == '*') | ||
return this.match(first.substring(1), second) || | ||
this.match(first, second.substring(1)); | ||
// If there is *, then there are two possibilities | ||
// a) We consider current character of second string | ||
// b) We ignore current character of second string. | ||
if (first.length > 0 && first[0] == '*') | ||
return ( | ||
this.match(first.substring(1), second) || | ||
this.match(first, second.substring(1)) | ||
); | ||
return false; | ||
return false; | ||
} | ||
isInGitignore(second) { | ||
for (let first of this.gitignore) { | ||
first = first.replace(/\/$/, ''); | ||
if (this.match(first, second)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
isInGitignore(second) { | ||
for (let first of this.gitignore) { | ||
first = first.replace(/\/$/, ""); | ||
if(this.match(first, second)){ | ||
return true; | ||
} | ||
async walk(sDir, oConfig, filelist = []) { | ||
const files = await fsp.readdir(sDir); | ||
for (const file of files) { | ||
if (this.isInGitignore(file)) continue; | ||
const filepath = path.join(sDir, file); | ||
const stat = await fsp.stat(filepath); | ||
if (stat && stat.isDirectory()) { | ||
filelist = await this.walk(filepath, oConfig, filelist); | ||
} else { | ||
let filepath = file; | ||
if (sDir != '.') { | ||
filepath = `${sDir}/${file}`; | ||
} | ||
return false; | ||
oConfig.filepath = filepath; | ||
const sStatus = await git.status(oConfig); | ||
if (sStatus != 'unmodified') { | ||
filelist.push(`${filepath} ${sStatus}`); | ||
} | ||
} | ||
} | ||
async walk(sDir, oConfig, filelist = []) { | ||
const files = await fsp.readdir(sDir); | ||
for (const file of files) { | ||
if(this.isInGitignore(file)) continue; | ||
const filepath = path.join(sDir, file); | ||
const stat = await fsp.stat(filepath); | ||
if (stat && stat.isDirectory()) { | ||
filelist = await this.walk(filepath, oConfig, filelist); | ||
} else { | ||
let filepath = file; | ||
if(sDir != "."){ | ||
filepath = `${sDir}/${file}`; | ||
} | ||
oConfig.filepath = filepath; | ||
const sStatus = await git.status(oConfig); | ||
if (sStatus != "unmodified") { | ||
filelist.push(`${filepath} ${sStatus}`); | ||
} | ||
} | ||
} | ||
return filelist; | ||
return filelist; | ||
} | ||
async runCommand() { | ||
try { | ||
this.base.ref = await git.currentBranch(this.base); | ||
} catch { | ||
// don't need this | ||
0; | ||
} | ||
async runCommand() { | ||
try{ | ||
this.base.ref = await git.currentBranch(this.base); | ||
}catch{ | ||
// don't need this | ||
0; | ||
this.commandData = { | ||
clone: { | ||
url: this.argv._[3], | ||
ref: this.argv.b || this.argv.branch || 'main', | ||
singleBranch: true, | ||
depth: 10, | ||
dir: this.argv._[4] || path.basename(this.argv._[3] || '', '.git'), | ||
gitdir: undefined, | ||
}, | ||
add: { | ||
filepath: this.argv._[3], | ||
}, | ||
rm: { | ||
filepath: this.argv._[3], | ||
}, | ||
status: { | ||
filepath: this.argv._[3], | ||
}, | ||
commit: { | ||
message: this.argv.m, | ||
}, | ||
addRemote: { | ||
remote: this.argv._[3], | ||
url: this.argv._[4], | ||
}, | ||
push: { | ||
remote: this.argv._[3] || 'origin', | ||
ref: this.argv._[4] || this.base.ref | ||
}, | ||
init: { | ||
dir: this.argv._[3] || this.base.dir, | ||
defaultBranch: this.argv.b || this.base.ref | ||
}, | ||
branch: { | ||
ref: this.argv._[3] || this.base.ref | ||
}, | ||
}; | ||
this.command = { | ||
deploy: (oConfig) => { | ||
// see https://isomorphic-git.org/docs/en/snippets | ||
return 'deployed'; | ||
}, | ||
status: async (oConfig) => { | ||
if (oConfig.filepath) { | ||
return git.status(oConfig); | ||
} else { | ||
let filelist = ['', `on branch ${this.base.ref}`]; | ||
try { | ||
this.gitignore = fs | ||
.readFileSync('.gitignore') | ||
.toString() | ||
.split('\n'); | ||
} catch { | ||
this.gitignore = []; | ||
} | ||
this.gitignore.unshift('.git'); | ||
await this.walk('.', oConfig, filelist); | ||
if (filelist.length > 2) { | ||
return filelist.join('\n'); | ||
} else { | ||
return 'working folder up to date'; | ||
} | ||
} | ||
this.commandData = { | ||
clone: { | ||
url: this.argv._[3], | ||
ref: this.argv.b || this.argv.branch || 'main', | ||
singleBranch: true, | ||
depth: 10, | ||
dir: this.argv._[4] || path.basename(this.argv._[3] || "", ".git"), | ||
gitdir: undefined | ||
}, | ||
add: { | ||
filepath: this.argv._[3] | ||
}, | ||
rm: { | ||
filepath: this.argv._[3] | ||
}, | ||
status: { | ||
filepath: this.argv._[3] | ||
}, | ||
commit: { | ||
message: this.argv.m | ||
}, | ||
addRemote: { | ||
remote: this.argv._[3], | ||
url: this.argv._[4] | ||
}, | ||
push: { | ||
remote: this.argv._[3] || "origin", | ||
ref: this.argv._[4] || this.base.ref | ||
}, | ||
init: { | ||
dir: this.argv._[4] || this.base.dir | ||
} | ||
} | ||
this.command = { | ||
deploy: (oConfig) => { | ||
// see https://isomorphic-git.org/docs/en/snippets | ||
return 'deployed'; | ||
}, | ||
status: async (oConfig) => { | ||
if(oConfig.filepath){ | ||
return git.status(oConfig); | ||
}else{ | ||
let filelist = [""]; | ||
this.gitignore = fs.readFileSync('.gitignore').toString().split("\n"); | ||
this.gitignore.unshift(".git"); | ||
await this.walk(".", oConfig, filelist); | ||
if(filelist.length > 1){ | ||
return filelist.join("\n"); | ||
}else{ | ||
return "working folder up to date"; | ||
} | ||
} | ||
}, | ||
push: async (oConfig) =>{ | ||
const rc = await git.push(oConfig); | ||
if(rc.ok){ | ||
return "pushed"; | ||
}else{ | ||
throw new Error(rc); | ||
} | ||
} | ||
}; | ||
let oConfig = {}; | ||
Object.assign(oConfig, this.base); | ||
if (typeof this.commandData[this.argv._[2]] != 'undefined') { | ||
Object.assign(oConfig, this.commandData[this.argv._[2]]); | ||
} | ||
if (typeof this.command[this.argv._[2]] != 'undefined') { | ||
// add new command | ||
return await this.command[this.argv._[2]](oConfig); | ||
} else if (typeof git[this.argv._[2]] != 'undefined') { | ||
return await git[this.argv._[2]](oConfig); | ||
}, | ||
push: async (oConfig) => { | ||
const rc = await git.push(oConfig); | ||
if (rc.ok) { | ||
return 'pushed'; | ||
} else { | ||
throw new Error('unimplemented'); | ||
throw new Error(rc); | ||
} | ||
}, | ||
}; | ||
let oConfig = {}; | ||
Object.assign(oConfig, this.base); | ||
if (typeof this.commandData[this.argv._[2]] != 'undefined') { | ||
Object.assign(oConfig, this.commandData[this.argv._[2]]); | ||
} | ||
if (typeof this.command[this.argv._[2]] != 'undefined') { | ||
// add new command | ||
return await this.command[this.argv._[2]](oConfig); | ||
} else if (typeof git[this.argv._[2]] != 'undefined') { | ||
return await git[this.argv._[2]](oConfig); | ||
} else { | ||
throw new Error('unimplemented'); | ||
} | ||
} | ||
} |
227
12.94%9313
-5.67%+ Added
- Removed
- Removed