Skip to content

Commit

Permalink
Switch to using tabs for indentation (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach McElrath authored Sep 17, 2020
1 parent 249dc6d commit fbe9024
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 60 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ SFDX plugin for managing Skuid metadata
<!-- install -->
# Installation

First, ensure you have [installed `sfdx`](https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_install_cli.htm).

Now, install the skuid-sfdx plugin:
First, ensure you have [installed `sfdx`](https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_install_cli.htm).

Now, install the skuid-sfdx plugin:

```sh-session
echo 'y' | sfdx plugins:install skuid-sfdx
Expand All @@ -38,18 +37,36 @@ To use `skuid-sfdx` in a CI environment, you will either need to auto-trust the

<!-- usage -->
# Usage

To pull Skuid Pages from a Salesforce org to the filesystem, use the `skuid:page:pull` command. You can use various arguments to specify which Pages in the org you want to pull, and you can output the pages to a directory of your choice.

For each Page, two files will be written:

- an XML file containing the Page's layout
- a JSON file containing metadata about the Page

### Example

```sh-session

$ sfdx skuid:page:pull
Wrote 85 pages to skuidpages

$ sfdx skuid:page:pull --module SamplePages --dir pages/sample
Wrote 4 pages to pages/sample
```

Page XML will be pretty-printed, with indentation automatically added, to make it easy to review and commit changes to Skuid Pages line-by-line to source control. (Note: tabs are used for indentation by default, but if you would like to use a different indentation, you can set the `SKUID_XML_INDENT` environment variable, e.g. `export SKUID_XML_INDENT=" "` to use 2 spaces instead of tabs.)

Going the other direction, to move Skuid Pages from the filesystem up to a Salesforce org, use the `skuid:page:push` command. You can use file glob patterns to specify which Pages in your filesystem that you want to push, for example:

```sh-session

$ sfdx skuid:page:push salesapp/*Foo*
3 Pages successfully pushed.

```

<!-- usagestop -->

<!-- commands -->
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "skuid-sfdx",
"description": "SFDX plugin for managing Skuid metadata",
"version": "0.3.0",
"version": "0.4.0",
"author": "Skuid",
"bugs": "https://github.com/skuid/skuid-sfdx/issues",
"dependencies": {
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/xml.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { xml, xmlmin } from 'vkbeautify';

const PADDING = ' '.repeat(2); // set desired indent size here

/**
* Pretty-prints a string of XML, adding indentation and newlines between tags
* @param condensedXml {String} Skuid Page XML
* @returns {String}
*/
function formatXml(condensedXml: string): string {
return xml(condensedXml, PADDING);
// For consistency across all of Skuid, use tabs for pretty printing of XML by default.
// However, allow for this to be configurable via an environment variable.
const indent = process.env.SKUID_XML_INDENT || '\t';
return xml(condensedXml, indent);
}

/**
Expand Down
110 changes: 57 additions & 53 deletions test/helpers/xml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,72 @@
import { condenseXml, formatXml } from "../../src/helpers/xml";
import { expect } from '@salesforce/command/lib/test';

const formatted = `<skuid__page>
<models>
<model id="foo" sobject="Account">
<fields>
<field id="Name"/>
<field id="CreatedDate"/>
</fields>
</model>
<model id="bar" sobject="Contact">
<fields>
<field id="Name"/>
<field id="CreatedDate"/>
<field id="Forecast" uionly="true" displaytype="FORMULA" returntype="TEXT">
<formula>CASE(
MONTH({{CreatedDate}}),
1, "Snow",
7, "Humidity",
"Who knows"
)</formula>
</field>
</fields>
</model>
</models>
<resources>
<javascript>
<jsitem location="inline" name="SomeInlineJS">//This should be preserved
const formula = `CASE(
MONTH({{CreatedDate}}),
1, "Snow",
7, "Humidity",
"Who knows"
)`;
const inlineJS = `//This should be preserved
const foo = "bar";
if (foo == "bar") {
// Indentation in here should be preserved
console.log("Hello: " + foo);
// Indentation in here should be preserved
console.log("Hello: " + foo);
} else {
console.log("Hola: " + foo);
console.log("Hola: " + foo);
}
console.log(foo);
</jsitem>
</javascript>
</resources>
`;
const getFormatted = (alternateIndent = null) => {
let part1 = `<skuid__page>
<models>
<model id="foo" sobject="Account">
<fields>
<field id="Name"/>
<field id="CreatedDate"/>
</fields>
</model>
<model id="bar" sobject="Contact">
<fields>
<field id="Name"/>
<field id="CreatedDate"/>
<field id="Forecast" uionly="true" displaytype="FORMULA" returntype="TEXT">
<formula>`;
let part2 = `</formula>
</field>
</fields>
</model>
</models>
<resources>
<javascript>
<jsitem location="inline" name="SomeInlineJS">`;
let part3 = `</jsitem>
</javascript>
</resources>
</skuid__page>`;
const condensed = `<skuid__page><models><model id="foo" sobject="Account"><fields><field id="Name"/><field id="CreatedDate"/></fields></model><model id="bar" sobject="Contact"><fields><field id="Name"/><field id="CreatedDate"/><field id="Forecast" uionly="true" displaytype="FORMULA" returntype="TEXT"><formula>CASE(
MONTH({{CreatedDate}}),
1, "Snow",
7, "Humidity",
"Who knows"
)</formula></field></fields></model></models><resources><javascript><jsitem location="inline" name="SomeInlineJS">//This should be preserved
const foo = "bar";
if (foo == "bar") {
// Indentation in here should be preserved
console.log("Hello: " + foo);
} else {
console.log("Hola: " + foo);
}
console.log(foo);
</jsitem></javascript></resources></skuid__page>`;
if (alternateIndent) {
part1 = part1.replace(/\t/g, alternateIndent);
part2 = part2.replace(/\t/g, alternateIndent);
part3 = part3.replace(/\t/g, alternateIndent);
}
return part1 + formula + part2 + inlineJS + part3;
};
const condensed = `<skuid__page><models><model id="foo" sobject="Account"><fields><field id="Name"/><field id="CreatedDate"/></fields></model><model id="bar" sobject="Contact"><fields><field id="Name"/><field id="CreatedDate"/><field id="Forecast" uionly="true" displaytype="FORMULA" returntype="TEXT"><formula>${formula}</formula></field></fields></model></models><resources><javascript><jsitem location="inline" name="SomeInlineJS">${inlineJS}</jsitem></javascript></resources></skuid__page>`;

describe('condenseXml', () => {
it("should remove recreateable whitespace from a string of XML", () => {
expect(condenseXml(formatted)).to.equal(condensed);
});
it("should remove recreateable whitespace from a string of XML", () => {
expect(condenseXml(getFormatted())).to.equal(condensed);
});
});

describe('formatXml', () => {
it("should pretty print a string of condensed XML using 2 spaces as indentation", () => {
expect(formatXml(condensed).replace("\r\n", "\n")).to.equal(formatted);
});
it("should pretty print a string of condensed XML using tabs as indentation by default", () => {
expect(formatXml(condensed).replace("\r\n", "\n")).to.equal(getFormatted());
});
it("should allow overrides of the indentation padding via environment variable", () => {
// override indent to 2 spaces
const alternateIndent = process.env.SKUID_XML_INDENT = " ";
expect(formatXml(condensed).replace("\r\n", "\n")).to.equal(getFormatted(alternateIndent));
delete process.env.SKUID_XML_INDENT;
});
});

0 comments on commit fbe9024

Please sign in to comment.