-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added commit author * bump version
- Loading branch information
Showing
4 changed files
with
120 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,49 +2,45 @@ | |
//note: this cli script is for testing and being executed directly from source | ||
|
||
import path from 'path'; | ||
import { GlitchGit, GlitchProject } from '../src/models'; | ||
import { GlitchGit } from '../src/models'; | ||
|
||
const gitPushTest = async () => { | ||
const gitConfigTest = async () => { | ||
const source = process.env.REPO_SOURCE; | ||
|
||
if (typeof source === 'undefined') { | ||
throw new Error('target repository has not been provided'); | ||
} | ||
|
||
// the folder to where it will copy things | ||
const targetFolder = 'target'; | ||
// the folder that contains the deploy files | ||
const targetFolder = 'dist'; | ||
|
||
const glitchRepo = new GlitchGit(source, true); | ||
const glitchRepo = new GlitchGit(source, true, { name: 'Hello World', email: '[email protected]' }); | ||
|
||
await glitchRepo.publishFilesToGlitch(path.join(__dirname, targetFolder)); | ||
}; | ||
|
||
const gitImportTest = async () => { | ||
const projId = process.env.GLITCH_PROJ_ID; | ||
const token = process.env.GLITCH_USER_TOKEN; | ||
const gitConfigList = await glitchRepo.git.listConfig(); | ||
const config = { | ||
name: gitConfigList.all['user.name'], | ||
email: gitConfigList.all['user.email'], | ||
}; | ||
|
||
if (typeof projId === 'undefined' || typeof token === 'undefined') { | ||
throw new Error('the required environment variables were not provided'); | ||
} | ||
// obtain the first entry of the user info if there are multiple values | ||
const user = { | ||
name: Array.isArray(config.name) ? config.name[0] : config.name, | ||
email: Array.isArray(config.email) ? config.email[0] : config.email, | ||
}; | ||
|
||
const sourceRepo = 'staketechnologies/lockdrop-ui'; | ||
const glitchProj = new GlitchProject(token, projId); | ||
const author = user.email.startsWith('none') || user.name.startsWith('none') ? null : user; | ||
|
||
const res = await glitchProj.importFromGithub(sourceRepo, 'public'); | ||
console.log(author); | ||
|
||
if (res.status !== 200) { | ||
throw new Error('Failed to import project'); | ||
} | ||
console.log('successfully imported project ' + sourceRepo); | ||
await glitchRepo.publishFilesToGlitch(targetFolder); | ||
}; | ||
|
||
// script entry point | ||
(async () => { | ||
throw new Error('Script not implemented!'); | ||
//await gitPushTest(); | ||
//await gitImportTest(); | ||
//await gitConfigTest(); | ||
|
||
//process.exit(0); | ||
process.exit(0); | ||
})().catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,25 +3,38 @@ import path from 'path'; | |
import fs from 'fs-extra'; | ||
import rimraf from 'rimraf'; | ||
import * as Helpers from '../helpers'; | ||
import _ from 'lodash'; | ||
|
||
export interface CommitAuthor { | ||
name: string; | ||
email: string; | ||
} | ||
|
||
const REPO_FOLDER = '.__source-repo'; | ||
|
||
const ROOT_DIR = process.cwd(); | ||
|
||
const DEFAULT_AUTHOR: CommitAuthor = { | ||
name: 'Glitch Deploy Tool', | ||
email: '[email protected]', | ||
}; | ||
export default class GlitchRepo { | ||
private _gitUrl: string; | ||
private _remoteOrigin: string; | ||
|
||
private _gitInst: SimpleGit; | ||
|
||
private _glitchRepoDir: string | undefined; | ||
|
||
private _logMsg: boolean; | ||
|
||
constructor(gitUrl: string, logMessage = false) { | ||
if (!gitUrl.startsWith('https://')) { | ||
private _authorInfo: CommitAuthor; | ||
|
||
constructor(remoteOrigin: string, logMessage = false, commitAs?: CommitAuthor) { | ||
if (!remoteOrigin.startsWith('https://')) { | ||
// add the https prefix if the user did not provide one | ||
this._gitUrl = 'https://' + gitUrl; | ||
this._remoteOrigin = 'https://' + remoteOrigin; | ||
} else { | ||
this._gitUrl = gitUrl; | ||
this._remoteOrigin = remoteOrigin; | ||
} | ||
const gitOptions: SimpleGitOptions = { | ||
baseDir: ROOT_DIR, | ||
|
@@ -32,42 +45,52 @@ export default class GlitchRepo { | |
this._logMsg = logMessage; | ||
// setup a git client | ||
this._gitInst = simpleGit(gitOptions); | ||
|
||
this._authorInfo = commitAs || DEFAULT_AUTHOR; | ||
} | ||
|
||
public async publishFilesToGlitch(targetFolder?: string) { | ||
let folderToCopy = ''; | ||
public get git() { | ||
return this._gitInst; | ||
} | ||
|
||
// if no folder name or path is given, import everything in the current directory | ||
if (!targetFolder || targetFolder === '*' || targetFolder === '.') { | ||
folderToCopy = ROOT_DIR; | ||
} else { | ||
// if the folder name was given, check if it's in absolute path or not | ||
folderToCopy = targetFolder.startsWith('/') ? targetFolder : path.join(ROOT_DIR, targetFolder); | ||
if (!fs.existsSync(folderToCopy)) { | ||
throw new Error(`target folder ${folderToCopy} does not exists`); | ||
public async publishFilesToGlitch(targetFolder?: string) { | ||
try { | ||
let folderToCopy = ''; | ||
|
||
// if no folder name or path is given, import everything in the current directory | ||
if (!targetFolder || targetFolder === '*' || targetFolder === '.') { | ||
folderToCopy = ROOT_DIR; | ||
} else { | ||
// if the folder name was given, check if it's in absolute path or not | ||
folderToCopy = targetFolder.startsWith('/') ? targetFolder : path.join(ROOT_DIR, targetFolder); | ||
if (!fs.existsSync(folderToCopy)) { | ||
throw new Error(`target folder ${folderToCopy} does not exists`); | ||
} | ||
} | ||
} | ||
|
||
await this._cloneRepo(); | ||
|
||
if (!this._glitchRepoDir) { | ||
throw new Error('Glitch project is not cloned to the local machine yet'); | ||
} | ||
await this._cloneRepo(); | ||
|
||
this._replaceRepoContentWith(folderToCopy); | ||
if (!this._glitchRepoDir) { | ||
throw new Error('Glitch project is not cloned to the local machine yet'); | ||
} | ||
|
||
await this._pushChangesToRemote(); | ||
this._replaceRepoContentWith(folderToCopy); | ||
|
||
this._writeLog('cleaning up...'); | ||
this.cleanGitInstance(); | ||
await this._pushChangesToRemote(); | ||
|
||
this._writeLog('done'); | ||
this._writeLog('successfully deployed to Glitch!'); | ||
} catch (e) { | ||
console.error(e); | ||
} finally { | ||
this.cleanGitInstance(); | ||
} | ||
} | ||
|
||
public cleanGitInstance() { | ||
if (!this._glitchRepoDir) { | ||
throw new Error('Glitch project is not cloned to the local machine yet'); | ||
} | ||
this._writeLog('cleaning up the local repo...'); | ||
|
||
this._gitInst = this._gitInst.clearQueue(); | ||
// remove the local repo | ||
|
@@ -86,7 +109,7 @@ export default class GlitchRepo { | |
|
||
this._writeLog('cloning repository...'); | ||
// clone the glitch project repo to a folder | ||
await this._gitInst.clone(this._gitUrl, REPO_FOLDER); | ||
await this._gitInst.clone(this._remoteOrigin, REPO_FOLDER); | ||
|
||
// set the location | ||
this._glitchRepoDir = repoDir; | ||
|
@@ -100,28 +123,54 @@ export default class GlitchRepo { | |
throw new Error('Glitch project is not cloned to the local machine yet'); | ||
} | ||
|
||
this._writeLog('removing everything inside the Glitch repository'); | ||
this._writeLog('removing everything inside the cloned repository'); | ||
|
||
// remove everything excluding the git metadata | ||
Helpers.emptyFolderContent(this._glitchRepoDir, ['.git']); | ||
|
||
// if the source folder is an absolute directory, don't append the path | ||
const folderToCopy = sourceFolder.startsWith('/') ? sourceFolder : path.join(ROOT_DIR, sourceFolder); | ||
|
||
this._writeLog(`cloning everything inside ${folderToCopy} to Glitch`); | ||
this._writeLog(`copying everything inside ${folderToCopy} to the local repo`); | ||
// move the new contents to Glitch | ||
Helpers.copyFolderContent(folderToCopy, this._glitchRepoDir, ['.git', REPO_FOLDER]); | ||
} | ||
|
||
private async _getCurrentAuthor() { | ||
const gitConfigList = await this._gitInst.listConfig(); | ||
const config = { | ||
name: gitConfigList.all['user.name'], | ||
email: gitConfigList.all['user.email'], | ||
}; | ||
|
||
// obtain the first entry of the user info if there are multiple values | ||
const user: CommitAuthor = { | ||
name: Array.isArray(config.name) ? config.name[0] : config.name, | ||
email: Array.isArray(config.email) ? config.email[0] : config.email, | ||
}; | ||
return user.email.startsWith('none') || user.name.startsWith('none') ? null : user; | ||
} | ||
|
||
private async _pushChangesToRemote() { | ||
this._writeLog('committing all changes to Glitch...'); | ||
// add everything in the working directory | ||
await this._gitInst.add('*'); | ||
await this._gitInst.add('./*'); | ||
|
||
const localAuth = await this._getCurrentAuthor(); | ||
|
||
// use the local author information if there is a local author and the user did not provide any author info during instantiation | ||
const useLocalAuthor = localAuth && _.isEqual(this._authorInfo, DEFAULT_AUTHOR); | ||
|
||
// commit all changes to git | ||
await this._gitInst.commit('[Auto commit] ' + Date.now()); | ||
// we can do a non-null assertion because the above state must be true for it to pass | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const { name, email } = useLocalAuthor ? localAuth! : this._authorInfo; | ||
|
||
this._writeLog('pushing folder to Glitch...'); | ||
// commit all changes added above to git and author it with the provided information | ||
const commitRes = await this._gitInst.commit(`[Auto commit] ${Date.now()}`, undefined, { | ||
'--author': `"${name} <${email}>"`, | ||
}); | ||
|
||
this._writeLog('committing the following changes to Glitch...'); | ||
this._writeLog(commitRes.summary); | ||
|
||
// pull before pushing | ||
await this._gitInst.pull('origin', 'master'); | ||
|
@@ -134,6 +183,13 @@ export default class GlitchRepo { | |
* @param message message to write | ||
*/ | ||
private _writeLog<T>(message?: T) { | ||
if (this._logMsg) console.log(message); | ||
if (this._logMsg) { | ||
// set the simple-git debug output variable. Refer to this for the details: <https://github.com/steveukx/git-js#enable-logging> | ||
if (process.env.DEBUG !== 'simple-git:task:*') { | ||
process.env.DEBUG = 'simple-git:task:*'; | ||
} | ||
// output custom logs | ||
console.debug(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters