Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Refactor Deploy Source task to utilize getMdapiPackage function #115

Merged
merged 3 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"compile": "tsc -b tsconfig.json"
},
"dependencies": {
"cli-table": "^0.3.1",
"fs-extra": "^8.1.0",
"ignore": "^5.1.6",
"rimraf": "^3.0.2",
Expand Down
64 changes: 26 additions & 38 deletions packages/core/src/sfdxwrappers/DeploySourceToOrgImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { isNullOrUndefined } from "util";
import { onExit } from "../OnExit";
let path = require("path");
import ignore from "ignore";
import getMDAPIPackageFromSourceDirectory from "../getMdapiPackage";
const Table = require("cli-table");

export interface DeploySourceResult {
deploy_id: string;
Expand All @@ -22,25 +24,20 @@ export interface DeploySourceResult {
}

export default class DeploySourceToOrgImpl {
temp_folder: string;
private mdapiDir: string;

public constructor(
private target_org: string,
private project_directory: string,
private source_directory: string,
private deployment_options: any,
private isToBreakBuildIfEmpty: boolean
) {
this.temp_folder = `${this.makefolderid(5)}_mdapi`;
}
) {}

public async exec(): Promise<DeploySourceResult> {
let commandExecStatus: boolean = false;
let deploySourceResult = {} as DeploySourceResult;

//Clean mdapi directory
rimraf.sync(this.temp_folder);

//Check empty conditions
let status = this.isToBreakBuildForEmptyDirectory();
if (status.result == "break") {
Expand All @@ -54,7 +51,10 @@ export default class DeploySourceToOrgImpl {
}

console.log("Converting source to mdapi");
await this.convertSourceToMDAPI();
let mdapiPackage = await getMDAPIPackageFromSourceDirectory(this.project_directory, this.source_directory);
this.mdapiDir = mdapiPackage["mdapiDir"];

this.printMetadataToDeploy(mdapiPackage["manifestAsJSON"]);

try {
if (this.deployment_options["checkonly"])
Expand Down Expand Up @@ -227,7 +227,7 @@ export default class DeploySourceToOrgImpl {
if (this.deployment_options["checkonly"]) command += ` -c`;

//directory
command += ` -d ${this.temp_folder}`;
command += ` -d ${this.mdapiDir}`;

//add json
command += ` --json`;
Expand Down Expand Up @@ -279,27 +279,6 @@ export default class DeploySourceToOrgImpl {
}
}

private async convertSourceToMDAPI(): Promise<void> {
try {
if (!isNullOrUndefined(this.project_directory))
console.log(
`Converting to Source Format ${this.source_directory} in project directory ${this.project_directory}`
);
else
console.log(
`Converting to Source Format ${this.source_directory} in project directory`
);
child_process.execSync(
`npx sfdx force:source:convert -r ${this.source_directory} -d ${this.temp_folder}`,
{ cwd: this.project_directory, encoding: "utf8" }
);
console.log("Converting to Source Format Completed");
} catch (error) {
console.log("Unable to convert source, exiting" + error.code);
throw error;
}
}

private isEmptyFolder(source_directory): boolean {
let files: string[] = readdirSync(source_directory);

Expand All @@ -325,14 +304,23 @@ export default class DeploySourceToOrgImpl {
else return false;
}

private makefolderid(length): string {
var result = "";
var characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
private printMetadataToDeploy(mdapiPackageManifest) {
let table = new Table({
head: ["Metadata Type", "API Name"],
});

for (let type of mdapiPackageManifest["Package"]["types"]) {
if (type["members"] instanceof Array) {
for (let member of type["members"]) {
let item = [type.name, member];
table.push(item);
}
} else {
let item = [type.name, type.members];
table.push(item);
}
}
return result;
console.log("The following metadata will be deployed:");
console.log(table.toString());
}
}