Skip to content

Commit

Permalink
Handling no layout folder found in cleanorg cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienTaillon committed Mar 12, 2020
1 parent 2f26d54 commit df82040
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 56 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ EXAMPLE
Data exported!
```

_See code: [src/commands/texei/data/export.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/data/export.ts)_
_See code: [src/commands/texei/data/export.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/data/export.ts)_

## `sfdx texei:data:import -d <string> [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -97,7 +97,7 @@ EXAMPLE
Data imported!
```

_See code: [src/commands/texei/data/import.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/data/import.ts)_
_See code: [src/commands/texei/data/import.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/data/import.ts)_

## `sfdx texei:data:plan:generate -d <string> -o <string> [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand All @@ -124,7 +124,7 @@ EXAMPLE
$ sfdx texei:data:plan:generate --objects Account,Contact,MyCustomObject__c --outputdir ./data
```

_See code: [src/commands/texei/data/plan/generate.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/data/plan/generate.ts)_
_See code: [src/commands/texei/data/plan/generate.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/data/plan/generate.ts)_

## `sfdx texei:org:contractfieldhistory:fix [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -152,7 +152,7 @@ EXAMPLE
History tracking fixed.
```

_See code: [src/commands/texei/org/contractfieldhistory/fix.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/org/contractfieldhistory/fix.ts)_
_See code: [src/commands/texei/org/contractfieldhistory/fix.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/org/contractfieldhistory/fix.ts)_

## `sfdx texei:org:shape:extract [-d <string>] [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -187,7 +187,7 @@ EXAMPLE
$ sfdx texei:org:shape:extract -u [email protected]"
```

_See code: [src/commands/texei/org/shape/extract.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/org/shape/extract.ts)_
_See code: [src/commands/texei/org/shape/extract.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/org/shape/extract.ts)_

## `sfdx texei:package:dependencies:install [-k <string>] [-b <string>] [-p <string>] [-s <string>] [-n <string>] [-w <number>] [-r] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -245,7 +245,7 @@ EXAMPLE
$ texei:package:dependencies:install -u MyScratchOrg -v MyDevHub -k "1:MyPackage1Key 2: 3:MyPackage3Key" -b "DEV"
```

_See code: [src/commands/texei/package/dependencies/install.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/package/dependencies/install.ts)_
_See code: [src/commands/texei/package/dependencies/install.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/package/dependencies/install.ts)_

## `sfdx texei:profile:clean [-k <string>] [-p <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -279,7 +279,7 @@ EXAMPLES
custom-sfdx-source-folder/main/profiles,source-folder-2/main/profiles/myAdmin.profile-meta.xml
```

_See code: [src/commands/texei/profile/clean.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/profile/clean.ts)_
_See code: [src/commands/texei/profile/clean.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/profile/clean.ts)_

## `sfdx texei:skinnyprofile:retrieve [-t <string>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -312,7 +312,7 @@ EXAMPLE
$ texei:skinnyprofile:retrieve -u MyScratchOrg
```

_See code: [src/commands/texei/skinnyprofile/retrieve.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/skinnyprofile/retrieve.ts)_
_See code: [src/commands/texei/skinnyprofile/retrieve.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/skinnyprofile/retrieve.ts)_

## `sfdx texei:source:customlabel:replace -l <string> -v <string> [-p <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand All @@ -336,7 +336,7 @@ EXAMPLE
$ texei:source:customlabel:replace --label GreatSalesforceBlog --value https://blog.texei.com
```

_See code: [src/commands/texei/source/customlabel/replace.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/source/customlabel/replace.ts)_
_See code: [src/commands/texei/source/customlabel/replace.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/source/customlabel/replace.ts)_

## `sfdx texei:source:layouts:cleanorg [-p <string>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -369,7 +369,7 @@ EXAMPLES
$ texei:source:layouts:cleanorg --targetusername myScratchOrg --targetdevhubusername myDevHub
```

_See code: [src/commands/texei/source/layouts/cleanorg.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/source/layouts/cleanorg.ts)_
_See code: [src/commands/texei/source/layouts/cleanorg.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/source/layouts/cleanorg.ts)_

## `sfdx texei:user:update [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand Down Expand Up @@ -401,5 +401,5 @@ EXAMPLES
$ sfdx texei:user:update --values "UserPermissionsKnowledgeUser=true" --json
```

_See code: [src/commands/texei/user/update.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.0/src/commands/texei/user/update.ts)_
_See code: [src/commands/texei/user/update.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.4.1/src/commands/texei/user/update.ts)_
<!-- commandsstop -->
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "texei-sfdx-plugin",
"description": "Texeï's plugin for sfdx",
"version": "1.4.0",
"version": "1.4.1",
"author": "Texeï",
"bugs": "https://github.com/https://github.com/texei/texei-sfdx-plugin/issues",
"dependencies": {
Expand Down
102 changes: 58 additions & 44 deletions src/commands/texei/source/layouts/cleanorg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,56 +59,70 @@ export default class CleanOrg extends SfdxCommand {

// Read files
const readDir = util.promisify(fs.readdir);
let layoutsFiles = await readDir(filesPath, "utf8");
// Don't know why metadata API retrieved & as %26 whereas other characters are ok. Hardcoding for now (booo)
layoutsFiles = layoutsFiles.map(x => x.replace('.layout-meta.xml','').replace('%26','&').replace('%27','\''));

// Only look at standard objects
let standardObjects:Set<String> = new Set<String>(layoutsFiles.map(x => {
const obj = x.split('-')[0];
if (!obj.includes('__')) {
// Should be enough to know if it's a standard object
return obj;
let layoutsFiles = await readDir(filesPath, "utf8").catch(err => {
if (err.code === 'ENOENT') {
const noent = 'No layouts folder found';
this.ux.log(noent);
deletedLayouts.push(noent);
}
}));
standardObjects.delete(undefined);

// Query the org to get layouts for these standard objects
const conn = this.org.getConnection();
const objectList:string = `'${Array.from(standardObjects).join().replace(/,/gi,'\',\'')}'`;
const query = `Select TableEnumOrId, Name from Layout where TableEnumOrId IN (${objectList}) order by TableEnumOrId`;
const results = await conn.tooling.query(query) as any;

let layoutsOnOrg:Set<String> = new Set<String>();
for (const layout of results.records) {
layoutsOnOrg.add(`${layout.TableEnumOrId}-${layout.Name}`);
}

const layoutsToDelete:string[] = Array.from(layoutsOnOrg).filter(lay => layoutsFiles.includes(lay) ? undefined : lay) as string[];

if (layoutsToDelete.length > 0) {

// TODO: log after delete, once errors are handled correctly
this.ux.log(`Deleting layouts:`);
for (const lay of layoutsToDelete) {
this.ux.log(lay);
else {
this.ux.error(err);
}
});

// Use metadata API so that this won't be visible in force:source:status
// This call is limited to 10 records, splitting (maybe refactor later to use destructiveChanges.xml)
let promises: Array<Promise<SaveResult | SaveResult[]>> = new Array<Promise<SaveResult | SaveResult[]>>();
if (layoutsFiles) {

// Don't know why metadata API retrieved & as %26 whereas other characters are ok. Hardcoding for now (booo)
layoutsFiles = layoutsFiles.map(x => x.replace('.layout-meta.xml','').replace('%26','&').replace('%27','\''));

while(layoutsToDelete.length) {
promises.push(conn.metadata.delete('Layout', layoutsToDelete.splice(0,10)));
// Only look at standard objects
let standardObjects:Set<String> = new Set<String>(layoutsFiles.map(x => {
const obj = x.split('-')[0];
if (!obj.includes('__')) {
// Should be enough to know if it's a standard object
return obj;
}
}));
standardObjects.delete(undefined);

// Query the org to get layouts for these standard objects
const conn = this.org.getConnection();
const objectList:string = `'${Array.from(standardObjects).join().replace(/,/gi,'\',\'')}'`;
const query = `Select TableEnumOrId, Name from Layout where TableEnumOrId IN (${objectList}) order by TableEnumOrId`;
const results = await conn.tooling.query(query) as any;

let layoutsOnOrg:Set<String> = new Set<String>();
for (const layout of results.records) {
layoutsOnOrg.add(`${layout.TableEnumOrId}-${layout.Name}`);
}

const layoutsToDelete:string[] = Array.from(layoutsOnOrg).filter(lay => layoutsFiles.includes(lay) ? undefined : lay) as string[];

if (layoutsToDelete.length > 0) {

// TODO: log after delete, once errors are handled correctly
this.ux.log(`Deleting layouts:`);
for (const lay of layoutsToDelete) {
this.ux.log(lay);
deletedLayouts.push(lay);
}

// Use metadata API so that this won't be visible in force:source:status
// This call is limited to 10 records, splitting (maybe refactor later to use destructiveChanges.xml)
let promises: Array<Promise<SaveResult | SaveResult[]>> = new Array<Promise<SaveResult | SaveResult[]>>();

while(layoutsToDelete.length) {
promises.push(conn.metadata.delete('Layout', layoutsToDelete.splice(0,10)));
}

// TODO: handle errors correctly
await Promise.all(promises);
}
else {
this.ux.log(`Nothing to delete.`);
}

// TODO: handle errors correctly
await Promise.all(promises);
}
else {
this.ux.log(`Nothing to delete.`);
}

return { deleted: deletedLayouts };
}
}

0 comments on commit df82040

Please sign in to comment.