-
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.
feat(core): separate services ( writer, command runner, interactive r…
…unner)
- Loading branch information
Showing
14 changed files
with
1,239 additions
and
1,138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,4 +108,6 @@ dist | |
.vscode | ||
|
||
# MAC specific | ||
.DS_STORE | ||
.DS_STORE | ||
|
||
output |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Implements CRUD options from a file. Replaces the usage of a database, acts like environment variables. | ||
*/ | ||
import fs from "fs"; | ||
import jsonfile from "jsonfile"; | ||
import path from "path"; | ||
|
||
const FILE_NAME = "../data.json"; | ||
|
||
/** | ||
* This variable exists only to specify which environment variables are being used. | ||
*/ | ||
export enum EnvironmentVariables { | ||
AC_ACCESS_TOKEN = "AC_ACCESS_TOKEN", | ||
API_HOSTNAME = "API_HOSTNAME", | ||
AUTH_HOSTNAME = "AUTH_HOSTNAME", | ||
OUTPUT = "plain", | ||
} | ||
|
||
const DefaultEnvironmentVariables = { | ||
API_HOSTNAME: "https://api.appcircle.io", | ||
AUTH_HOSTNAME: "https://auth.appcircle.io", | ||
AC_ACCESS_TOKEN: "", | ||
}; | ||
|
||
export type ConsoleOutputType = "json" | "plain" | ||
|
||
let output_type = (process.env.CONSOLE_OUTPUT_TYPE || "plain") as ConsoleOutputType; | ||
|
||
export const setConsoleOutputType = (type:ConsoleOutputType) => { | ||
output_type = type; | ||
}; | ||
export const getConsoleOutputType = () => { | ||
return output_type; | ||
}; | ||
|
||
export function writeVariable(variable: EnvironmentVariables, value: string): void { | ||
const currentPath = path.join(__dirname, "", FILE_NAME); | ||
try { | ||
const currentData = jsonfile.readFileSync(currentPath); | ||
currentData[variable] = value; | ||
jsonfile.writeFile(currentPath, currentData, { flag: "w+" }); | ||
} catch { | ||
console.error("Could not write variable to the file."); | ||
} | ||
} | ||
|
||
export function readVariable(variable: EnvironmentVariables): string { | ||
const currentPath = path.join(__dirname, "", FILE_NAME); | ||
try { | ||
const currentData = jsonfile.readFileSync(currentPath); | ||
return currentData[variable] || ""; | ||
} catch { | ||
console.error("Could not read data, returning empty."); | ||
return ""; | ||
} | ||
} | ||
|
||
/** | ||
* Prioritizes environment variables over the file contents. | ||
*/ | ||
function initializeFile() { | ||
const currentPath = path.join(__dirname, "", FILE_NAME); | ||
let currentData = { ...DefaultEnvironmentVariables }; | ||
if (fs.existsSync(currentPath)) { | ||
try { | ||
currentData = jsonfile.readFileSync(currentPath); | ||
} catch (e) { | ||
console.info("Corrupted data.json file, rewriting it..."); | ||
} | ||
} | ||
for (const key in EnvironmentVariables) { | ||
(currentData as any)[key] = process.env[key] || (currentData as any)[key] || (DefaultEnvironmentVariables as any)[key]; | ||
} | ||
jsonfile.writeFileSync(currentPath, currentData, { flag: "w+" }); | ||
} | ||
|
||
(async () => { | ||
initializeFile(); | ||
})(); |
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 |
---|---|---|
@@ -1,3 +1,45 @@ | ||
export const APPCIRCLE_COLOR = "#ff8F34"; | ||
export const API_HOSTNAME = process.env.API_HOSTNAME || "https://api.appcircle.io"; | ||
export const AUTH_HOSTNAME = process.env.AUTH_HOSTNAME || "https://auth.appcircle.io"; | ||
|
||
export const BuildStatus = { | ||
"0": "Success", | ||
"1": "Failed", | ||
"2": "Canceled", | ||
"3": "Timeout", | ||
"90": "Waiting", | ||
"91": "Running", | ||
"92": "Completing", | ||
"99": "Unknown", | ||
}; | ||
|
||
export const AuthenticationTypes = { | ||
1: "None", | ||
2: "Individual Enrollment", | ||
3: "Static Username and Password", | ||
}; | ||
|
||
export const OperatingSystems = { | ||
0: "None", | ||
1: "iOS", | ||
2: "Android", | ||
}; | ||
|
||
export const PlatformTypes = { | ||
0: "None", | ||
1: "Swift/Objective-C", | ||
2: "Java/Kotlin", | ||
3: "Smartface", | ||
4: "React Native", | ||
5: "Xamarin", | ||
6: "Flutter", | ||
}; | ||
|
||
export const PublishTypes = { | ||
0: "None", | ||
1: "Beta", | ||
2: "Live", | ||
}; | ||
|
||
export const EnvironmentVariableTypes = { | ||
TEXT: "text", | ||
FILE: "file", | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import path from "path"; | ||
import os from "os"; | ||
import { CommandTypes } from "./commands"; | ||
import { writeVariable, EnvironmentVariables } from "../config"; | ||
import { createOra } from "../utils/orahelper"; | ||
import { ProgramCommand } from "../program"; | ||
import { | ||
getToken, | ||
getBuildProfiles, | ||
getBranches, | ||
getWorkflows, | ||
getCommits, | ||
getBuildsOfCommit, | ||
getDistributionProfiles, | ||
startBuild, | ||
downloadArtifact, | ||
uploadArtifact, | ||
createDistributionProfile, | ||
getEnvironmentVariableGroups, | ||
createEnvironmentVariableGroup, | ||
getEnvironmentVariables, | ||
createEnvironmentVariable, | ||
getEnterpriseProfiles, | ||
getEnterpriseAppVersions, | ||
publishEnterpriseAppVersion, | ||
unpublishEnterpriseAppVersion, | ||
removeEnterpriseAppVersion, | ||
notifyEnterpriseAppVersion, | ||
uploadEnterpriseApp, | ||
uploadEnterpriseAppVersion, | ||
getEnterpriseDownloadLink, | ||
} from "../services"; | ||
import { commandWriter } from "./writer"; | ||
|
||
export const runCommand = async (command: ProgramCommand) => { | ||
const params = command.opts() as any; | ||
const commandName = command.name(); | ||
let responseData; | ||
|
||
switch (commandName) { | ||
case CommandTypes.LOGIN: { | ||
responseData = await getToken(params); | ||
writeVariable(EnvironmentVariables.AC_ACCESS_TOKEN, responseData.access_token); | ||
commandWriter(CommandTypes.LOGIN, responseData); | ||
break; | ||
} | ||
case CommandTypes.LIST_BUILD_PROFILES: { | ||
responseData = await getBuildProfiles(params); | ||
commandWriter(CommandTypes.LIST_BUILD_PROFILES, responseData); | ||
break; | ||
} | ||
case CommandTypes.LIST_BUILD_PROFILE_BRANCHES: { | ||
responseData = await getBranches(params); | ||
commandWriter(CommandTypes.LIST_BUILD_PROFILE_BRANCHES, responseData); | ||
break; | ||
} | ||
case CommandTypes.LIST_BUILD_PROFILE_WORKFLOWS: { | ||
responseData = await getWorkflows(params); | ||
commandWriter(CommandTypes.LIST_BUILD_PROFILE_WORKFLOWS, responseData); | ||
break; | ||
} | ||
case CommandTypes.LIST_BUILD_PROFILE_COMMITS: { | ||
responseData = await getCommits(params); | ||
commandWriter(CommandTypes.LIST_BUILD_PROFILE_COMMITS, responseData); | ||
break; | ||
} | ||
case CommandTypes.LIST_BUILD_PROFILE_BUILDS_OF_COMMIT: { | ||
responseData = await getBuildsOfCommit(params); | ||
commandWriter(CommandTypes.LIST_BUILD_PROFILE_BUILDS_OF_COMMIT, responseData); | ||
break; | ||
} | ||
case CommandTypes.LIST_DISTRIBUTION_PROFILES: { | ||
responseData = await getDistributionProfiles(params); | ||
commandWriter(CommandTypes.LIST_DISTRIBUTION_PROFILES, responseData); | ||
break; | ||
} | ||
case CommandTypes.BUILD: { | ||
const spinner = createOra(`Try to start a new build with ${params.workflow}`).start(); | ||
try { | ||
responseData = await startBuild(params); | ||
commandWriter(CommandTypes.BUILD, responseData); | ||
spinner.text = `Build added to queue successfully.\n\nTaskId: ${responseData.taskId}\nQueueItemId: ${responseData.queueItemId}`; | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.fail("Build failed"); | ||
} | ||
break; | ||
} | ||
case CommandTypes.DOWNLOAD: { | ||
const downloadPath = path.resolve((params.path || "").replace("~", `${os.homedir}`)); | ||
const spinner = createOra(`Downloading file artifact.zip`).start(); | ||
try { | ||
responseData = await downloadArtifact(params, downloadPath); | ||
commandWriter(CommandTypes.DOWNLOAD, responseData); | ||
spinner.text = `The file artifact.zip is downloaded successfully under path:\n${downloadPath}`; | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.text = "The file could not be downloaded."; | ||
spinner.fail(); | ||
} | ||
break; | ||
} | ||
case CommandTypes.UPLOAD: { | ||
const spinner = createOra("Try to upload the app").start(); | ||
try { | ||
responseData = await uploadArtifact(params); | ||
commandWriter(CommandTypes.UPLOAD, responseData); | ||
spinner.text = `App uploaded successfully.\n\nTaskId: ${responseData.taskId}`; | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.fail("Upload failed"); | ||
} | ||
break; | ||
} | ||
case CommandTypes.CREATE_DISTRIBUTION_PROFILE: { | ||
responseData = await createDistributionProfile(params); | ||
commandWriter(CommandTypes.CREATE_DISTRIBUTION_PROFILE, { ...responseData, name: params.name }); | ||
break; | ||
} | ||
case CommandTypes.LIST_ENVIRONMENT_VARIABLE_GROUPS: { | ||
responseData = await getEnvironmentVariableGroups(params); | ||
commandWriter(CommandTypes.LIST_ENVIRONMENT_VARIABLE_GROUPS, responseData); | ||
break; | ||
} | ||
case CommandTypes.CREATE_ENVIRONMENT_VARIABLE_GROUP: { | ||
responseData = await createEnvironmentVariableGroup(params); | ||
commandWriter(CommandTypes.CREATE_ENVIRONMENT_VARIABLE_GROUP, { ...responseData, name: params.name }); | ||
break; | ||
} | ||
case CommandTypes.LIST_ENVIRONMENT_VARIABLES: { | ||
responseData = await getEnvironmentVariables(params); | ||
commandWriter(CommandTypes.LIST_ENVIRONMENT_VARIABLES, responseData); | ||
break; | ||
} | ||
case CommandTypes.CREATE_ENVIRONMENT_VARIABLE: { | ||
responseData = await createEnvironmentVariable(params as any); | ||
commandWriter(CommandTypes.CREATE_ENVIRONMENT_VARIABLE, { ...responseData, key: params.key }); | ||
break; | ||
} | ||
case CommandTypes.LIST_ENTERPRISE_PROFILES: { | ||
responseData = await getEnterpriseProfiles(); | ||
commandWriter(CommandTypes.LIST_ENTERPRISE_PROFILES, responseData); | ||
break; | ||
} | ||
case CommandTypes.LIST_ENTERPRISE_APP_VERSIONS: { | ||
responseData = await getEnterpriseAppVersions(params); | ||
commandWriter(CommandTypes.LIST_ENTERPRISE_APP_VERSIONS, responseData); | ||
break; | ||
} | ||
case CommandTypes.PUBLISH_ENTERPRISE_APP_VERSION: { | ||
responseData = await publishEnterpriseAppVersion(params); | ||
commandWriter(CommandTypes.PUBLISH_ENTERPRISE_APP_VERSION, responseData); | ||
break; | ||
} | ||
case CommandTypes.UNPUBLISH_ENTERPRISE_APP_VERSION: { | ||
responseData = await unpublishEnterpriseAppVersion(params); | ||
commandWriter(CommandTypes.UNPUBLISH_ENTERPRISE_APP_VERSION, responseData); | ||
break; | ||
} | ||
case CommandTypes.REMOVE_ENTERPRISE_APP_VERSION: { | ||
const spinner = createOra("Try to delete the app version").start(); | ||
try { | ||
responseData = await removeEnterpriseAppVersion(params); | ||
commandWriter(CommandTypes.REMOVE_ENTERPRISE_APP_VERSION, responseData); | ||
spinner.text = `App version deleted successfully.\n\nTaskId: ${responseData.taskId}`; | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.fail("App version delete failed"); | ||
} | ||
break; | ||
} | ||
case CommandTypes.NOTIFY_ENTERPRISE_APP_VERSION: { | ||
const spinner = createOra(`Notifying users with new version for ${params.entVersionId}`).start(); | ||
try { | ||
responseData = await notifyEnterpriseAppVersion(params); | ||
commandWriter(CommandTypes.NOTIFY_ENTERPRISE_APP_VERSION, responseData); | ||
spinner.text = `Version notification sent successfully.\n\nTaskId: ${responseData.taskId}`; | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.fail("Notification failed"); | ||
} | ||
break; | ||
} | ||
case CommandTypes.UPLOAD_ENTERPRISE_APP: { | ||
const spinner = createOra("Try to upload the app").start(); | ||
try { | ||
responseData = await uploadEnterpriseApp(params); | ||
commandWriter(CommandTypes.UPLOAD_ENTERPRISE_APP, responseData); | ||
spinner.text = `New profile created and app uploaded successfully.\n\nTaskId: ${responseData.taskId}`; | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.fail("Upload failed"); | ||
} | ||
break; | ||
} | ||
case CommandTypes.UPLOAD_ENTERPRISE_APP_VERSION: { | ||
const spinner = createOra("Try to upload the app").start(); | ||
try { | ||
responseData = await uploadEnterpriseAppVersion(params); | ||
commandWriter(CommandTypes.UPLOAD_ENTERPRISE_APP_VERSION, responseData); | ||
spinner.text = `App version uploaded successfully.\n\nTaskId: ${responseData.taskId}`; | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.fail("Upload failed"); | ||
} | ||
break; | ||
} | ||
case CommandTypes.GET_ENTERPRISE_DOWNLOAD_LINK: { | ||
responseData = await getEnterpriseDownloadLink(params); | ||
commandWriter(CommandTypes.GET_ENTERPRISE_DOWNLOAD_LINK, responseData); | ||
break; | ||
} | ||
default: { | ||
console.error("Command not found"); | ||
break; | ||
} | ||
} | ||
}; |
Oops, something went wrong.