Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gil Avignon committed Jul 12, 2017
0 parents commit 076e977
Show file tree
Hide file tree
Showing 12 changed files with 881 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules/
files/**/*.json
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Gil Avignon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# sfdc-generate-data-dictionary

generate data dictionary from a Salesforce Org

## Getting Started

Works in Unix like system. Windows is not tested.

### Installing

```
npm install -g sfdc-generate-data-dictionary
```

## Usage

### Command Line

```
$ sgd -h
Usage: sgd [options]
generate data dictionary from a Salesforce Org
Options:
-u, --username salesforce username
-p, --password salesforce user password
-l, --loginUrl salesforce login URL [https://login.salesforce.com]
-c, --customObjects retrieve all custom objects [true]
-d, --deleteFolders delete/clean temp folders [true]
-o, --output salesforce data dictionary directory path [.]
```

### Module

```
var sgd = require('sfdc-generate-data-dictionary');
sgd({
'username': '',
'password': options.password,
'loginUrl': options.loginUrl,
'projectName': '',
'allCustomObjects': true,
'cleanFolders': true,
'output':'.'
}, console.log);
```

## Built With

- [commander](https://github.com/tj/commander.js/) - The complete solution for node.js command-line interfaces, inspired by Ruby's commander.
- [bytes](https://github.com/visionmedia/bytes.js) - Utility to parse a string bytes to bytes and vice-versa.
- [excel4node](https://github.com/amekkawi/excel4node) - Node module to allow for easy Excel file creation.
- [jsforce](https://github.com/jsforce/jsforce) - Salesforce API Library for JavaScript applications (both on Node.js and web browser)

## Versioning

[SemVer](http://semver.org/) is used for versioning.

## Authors

- **Gil Avignon** - _Initial work_ - [gavignon](https://github.com/gavignon)

## License

This project is licensed under the MIT License - see the <LICENSE.md> file for details
22 changes: 22 additions & 0 deletions bin/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node
'use strict';

const program = require('commander');
const orchestrator = require('../index.js');
const pjson = require('../package.json');

program
.description(pjson.description)
.version(pjson.version)
.option('-u, --username [username]', 'salesforce username')
.option('-p, --password [password]', 'salesforce password')
.option('-l, --loginUrl [loginUrl]', 'salesforce login URL [https://login.salesforce.com]', 'https://login.salesforce.com')
.option('-c, --customObjects [customObjects]', 'retrieve all custom objects [true]', true)
.option('-d, --deleteFolders [deleteFolders]', 'delete/clean temp folders [true]', true)
.option('-o, --output [dir]', 'salesforce data dictionary directory path [.]', '.')
.parse(process.argv);

orchestrator(program, console.log)
.catch(function(err){
throw err;
});
Empty file added files/describe/.gitkeep
Empty file.
Empty file added files/metadata/.gitkeep
Empty file.
Empty file added files/tooling/.gitkeep
Empty file.
105 changes: 105 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict';
const jsforce = require('jsforce');
const Downloader = require('./lib/downloader.js');
const ExcelBuilder = require('./lib/excelbuilder.js');
const Utils = require('./lib/utils.js');

module.exports = (config, logger) => {
// Check all mandatory config options
if (typeof config.username === 'undefined' || config.username === null ||
typeof config.password === 'undefined' || config.password === null) {
throw new Error('Not enough config options');
}

// Set default values
if (typeof config.loginUrl === 'undefined' || config.loginUrl === null) {
config.loginUrl = 'https://login.salesforce.com';
}
if (typeof config.output === 'undefined' || config.output === null) {
config.output = '.';
}
if (typeof config.projectName === 'undefined' || config.projectName === null) {
config.projectName = 'PROJECT';
}
if (typeof config.allCustomObjects === 'undefined' || config.allCustomObjects === null) {
config.allCustomObjects = true;
}
if (typeof config.objects === 'undefined' || config.objects === null) {
config.objects = [
'Account',
'Contact'
];
}
if (typeof config.columns === 'undefined' || config.columns === null) {
config.columns = {
'ReadOnly': 5,
'Mandatory': 3,
'Name': 25,
'Description': 90,
'APIName': 25,
'Type': 27,
'Values': 45
};
}

// Clean folders that contain API files
if (config.cleanFolders) {
let utils = new Utils();
const statusRmDescribe = utils.rmDir(__dirname + '/files/describe', '.json', false);
const statusRmMetadata = utils.rmDir(__dirname + '/files/metadata', '.json', false);
logger('File folders cleaned');
}

// Main promise
const promise = new Promise((resolve, reject) => {

const conn = new jsforce.Connection({
loginUrl: config.loginUrl
});

// Salesforce connection
conn.login(config.username, config.password).then(result => {
logger('Connected as ' + config.username);
if (config.allCustomObjects) {
conn.describeGlobal().then(res => {
for (let i = 0; i < res.sobjects.length; i++) {
let object = res.sobjects[i];
if (config.objects === undefined)
config.objects = [];

// If the sObject is a real custom object
if (object.custom && (object.name.indexOf('__c') !== -1) && (object.name.split('__').length - 1 < 2))
config.objects.push(object.name);
}

const downloader = new Downloader(config, logger, conn);
const builder = new ExcelBuilder(config, logger);

// Download metadata files
downloader.execute().then(result => {
logger(result + ' downloaded');
// Generate the excel file
builder.generate().then(result => {
resolve();
});
})
});
} else {
if (config.objects.length > 0) {
const downloader = new Downloader(config, logger, conn);
const builder = new ExcelBuilder(config, logger);

// Download metadata files
downloader.execute().then(result => {
logger(result + ' downloaded');
// Generate the excel file
builder.generate().then(result => {
resolve();
});
})
}
}
}).catch(reject);
});
return promise;
};
99 changes: 99 additions & 0 deletions lib/downloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const fs = require('fs');
const path = require('path');
const bytes = require('bytes');
const Utils = require('./utils.js');

const FILE_DIR = '../files';

module.exports = class Downloader {
constructor(config, logger, conn) {
this.config = config;
this.logger = logger;
this.conn = conn;
this.utils = new Utils(logger);
}

downloadDescribe(sObject) {
const self = this;
return new Promise((resolve, reject) => {
self.conn.sobject(sObject).describe().then(meta => {
const filePath = path.join(__dirname, FILE_DIR, '/describe/', sObject + '.json');
fs.writeFileSync(filePath, JSON.stringify(meta.fields), 'utf-8');
const stats = fs.statSync(filePath);

resolve(stats.size);
});
});
}

downloadMetadata(sobjectList) {
const self = this;
return new Promise((resolve, reject) => {
self.conn.metadata.read('CustomObject', sobjectList).then(metadata => {
let filePath = '';

if (sobjectList.length === 1) {
let fields = metadata.fields;
fields.sort(self.utils.sortByProperty('fullName'));
filePath = path.join(__dirname, FILE_DIR, '/metadata/', metadata.fullName + '.json');
fs.writeFileSync(filePath, JSON.stringify(metadata), 'utf-8');
} else {
for (let i = 0; i < metadata.length; i++) {
let fields = metadata[i].fields;
if (Array.isArray(fields) && (fields !== undefined || fields.length > 0)) {
fields.sort(self.utils.sortByProperty('fullName'));
filePath = path.join(__dirname, FILE_DIR, '/metadata/', metadata[i].fullName + '.json');
fs.writeFileSync(filePath, JSON.stringify(metadata[i]), 'utf-8');
} else {
self.config.objects.splice(self.config.objects.indexOf(metadata[i]), 1);
}
}
}
const stats = fs.statSync(filePath);

resolve(stats.size);
}).catch(function(err) {
console.log(err.message);
console.log(err.stack);
});
});
}

execute() {
const promise = new Promise((resolve, reject) => {
const self = this;

this.logger('Downloading...');

let downloadArray = new Array();

for (let object of self.config.objects) {
downloadArray.push(self.downloadDescribe(object));
}

let loop = ~~(self.config.objects.length / 10);
if (self.config.objects.length % 10 > 0)
loop++;

let j = 0;
for (let i = 0; i < loop; i++) {
let objectList = self.config.objects.slice(j, j + 10);
j += 10;
downloadArray.push(self.downloadMetadata(objectList));
}

Promise.all(
downloadArray
).then(results => {
let total = 0;
for (let fileSize of results) {
total += fileSize;
}
resolve(bytes.format(total, {
decimalPlaces: 2
}));
}).catch(reject);
});
return promise;
}
}
Loading

0 comments on commit 076e977

Please sign in to comment.