From d13f22d2aa00aae73ef74717a725340f97a73056 Mon Sep 17 00:00:00 2001 From: Hoon Kim Date: Fri, 18 Dec 2020 03:03:35 -0800 Subject: [PATCH] added extra checks for config parsing --- README.md | 2 +- src/cli/deploy.ts | 7 +------ src/models/glitchGit.ts | 32 +++++++++++++++++--------------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 01235c1..6534af8 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ Installing this as part of your project's dependency will allow you to directly The base command for the CLI is `glitch-deploy-tool` with two sub-commands: `from-local` and `github`. -You can also pass some of the parameters from an environmental variable. +This tool can read parameters as an environmental variable. ``` usage: glitch-deploy-tool from-local [OPTIONS] diff --git a/src/cli/deploy.ts b/src/cli/deploy.ts index 18c829b..0a23847 100644 --- a/src/cli/deploy.ts +++ b/src/cli/deploy.ts @@ -3,12 +3,7 @@ import { GlitchGit, GlitchProject } from '../models'; export const importFromFolder = async (repoUrl: string, targetPath?: string, debugMessage?: boolean) => { const glitchRepo = new GlitchGit(repoUrl, debugMessage); - try { - await glitchRepo.publishFilesToGlitch(targetPath); - } catch (e) { - glitchRepo.cleanGitInstance(); - throw e; - } + await glitchRepo.publishFilesToGlitch(targetPath); console.log('successfully imported projects from ' + (targetPath || process.cwd())); }; diff --git a/src/models/glitchGit.ts b/src/models/glitchGit.ts index 58fe2d3..31bcb27 100644 --- a/src/models/glitchGit.ts +++ b/src/models/glitchGit.ts @@ -12,8 +12,6 @@ export interface CommitAuthor { const REPO_FOLDER = '.__source-repo'; -const ROOT_DIR = process.cwd(); - const DEFAULT_AUTHOR: CommitAuthor = { name: 'Glitch Deploy Tool', email: 'glitch@users.noreply.deploy.com', @@ -30,8 +28,6 @@ export default class GlitchRepo { private _authorInfo: CommitAuthor; constructor(remoteOrigin: string, logMessage = false, commitAs?: CommitAuthor) { - if (!remoteOrigin || typeof remoteOrigin !== 'string') throw new Error('Please provide a remote origin'); - if (!remoteOrigin.startsWith('https://')) { // add the https prefix if the user did not provide one this._remoteOrigin = 'https://' + remoteOrigin; @@ -39,7 +35,7 @@ export default class GlitchRepo { this._remoteOrigin = remoteOrigin; } const gitOptions: SimpleGitOptions = { - baseDir: ROOT_DIR, + baseDir: this.rootDir, binary: 'git', maxConcurrentProcesses: 2, }; @@ -51,6 +47,10 @@ export default class GlitchRepo { this._authorInfo = commitAs || DEFAULT_AUTHOR; } + public get rootDir() { + return process.cwd(); + } + public get git() { return this._gitInst; } @@ -61,10 +61,10 @@ export default class GlitchRepo { // if no folder name or path is given, import everything in the current directory if (!targetFolder || targetFolder === '*' || targetFolder === '.') { - folderToCopy = ROOT_DIR; + folderToCopy = this.rootDir; } 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); + folderToCopy = targetFolder.startsWith('/') ? targetFolder : path.join(this.rootDir, targetFolder); if (!fs.existsSync(folderToCopy)) { throw new Error(`target folder ${folderToCopy} does not exists`); } @@ -81,9 +81,8 @@ export default class GlitchRepo { await this._pushChangesToRemote(); this._writeLog('successfully deployed to Glitch!'); - } catch (e) { - console.error(e); } finally { + // clean the left over files before exiting this.cleanGitInstance(); } } @@ -101,7 +100,7 @@ export default class GlitchRepo { private async _cloneRepo() { // get the absolute directory of the repo folder - const repoDir = path.join(ROOT_DIR, REPO_FOLDER); + const repoDir = path.join(this.rootDir, REPO_FOLDER); if (fs.existsSync(repoDir)) { this._writeLog('found an existing repository, removing it before cloning a new one'); @@ -131,14 +130,14 @@ export default class GlitchRepo { 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); + const folderToCopy = sourceFolder.startsWith('/') ? sourceFolder : path.join(this.rootDir, sourceFolder); 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() { + private async _parseSystemAuthor() { const gitConfigList = await this._gitInst.listConfig(); const config = { name: gitConfigList.all['user.name'], @@ -150,17 +149,20 @@ export default class GlitchRepo { 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; + + // if the author information does not exists, return null + if (!user.email || !user.name) return null; + else return user.email.startsWith('none') || user.name.startsWith('none') ? null : user; } private async _pushChangesToRemote() { // add everything in the working directory await this._gitInst.add('./*'); - const localAuth = await this._getCurrentAuthor(); + const localAuth = await this._parseSystemAuthor(); // 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); + const useLocalAuthor = !!(localAuth && _.isEqual(this._authorInfo, DEFAULT_AUTHOR)); // 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