From dcdb2bb49fe2e027fdc2368c162e31ca0f8e8e2c Mon Sep 17 00:00:00 2001 From: ac360 Date: Mon, 22 Feb 2016 17:25:19 -0800 Subject: [PATCH] Utils: create new utils on serverless isntance --- lib/Serverless.js | 1 + lib/utils/new.js | 120 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 lib/utils/new.js diff --git a/lib/Serverless.js b/lib/Serverless.js index 8bfe88827..7fdbf8e3a 100644 --- a/lib/Serverless.js +++ b/lib/Serverless.js @@ -60,6 +60,7 @@ class Serverless { this.hooks = {}; this.commands = {}; this.cli = null; + this.utils = require('./utils/new'); this.initProviders(); } diff --git a/lib/utils/new.js b/lib/utils/new.js new file mode 100644 index 000000000..b09629d23 --- /dev/null +++ b/lib/utils/new.js @@ -0,0 +1,120 @@ +'use strict'; + +/** + * Serverless: New Utilities + * - Cleaner, stable utilities for plugin developers. + * - Be sure to use fs-extra, instead of writing utilities, whenever possible + */ + +require('shelljs/global'); +let BbPromise = require('bluebird'), + rawDebug = require('debug'), + path = require('path'), + async = require('async'), + traverse = require('traverse'), + readdirp = require('readdirp'), + replaceall = require('replaceall'), + SError = require('../Error'), + SCli = require('./cli'), + dotenv = require('dotenv'), + fs = require('fs'), + fse = require('fs-extra'), + _ = require('lodash'), + shortid = require('shortid'); + + +module.exports = { + + /** + * Serverless Debug + */ + + sDebug:function() { + + if (process.env.DEBUG) { + let caller = getCaller(); + let context = pathToContext(caller); + let args = Array.prototype.slice.call(arguments); + args.unshift(context); + this.sDebugWithContext.apply(this, args); + } + }, + + /** + * Write File Sync + */ + + writeFileSync: function(filePath, contents) { + + this.sDebug(`Writing file: ${filePath}...`); + + if (contents === undefined) { + contents = ''; + } + + let parentDir = filePath.split('/'); + parentDir.pop(); + parentDir = parentDir.join('/'); + + try { + fse.mkdirsSync(parentDir); + } catch(e) { + throw new SError(`Could not create parent folders for: ${filePath}`); + } + + return fs.writeFileSync(filePath, contents); + }, + + /** + * Read File Sync + * - Reads file from file system + * - Auto-parses JSON and throws error if invalid JSON + */ + + readFileSync: function(filePath) { + + let contents; + + this.sDebug(`Reading file: ${filePath}...`); + + // Read file + try { + contents = fs.readFileSync(filePath); + } catch(e) { + throw new SError(`Error reading file ${filePath}`); + } + + // Auto-parse JSON + try { + contents = JSON.parse(contents); + } catch(e) { + throw new SError(`Could not parse JSON in file: ${filePath}`); + } + + return contents; + }, + + + + /** + * Export Object + * - Exports an object from a class + */ + + exportObject: function(data) { + traverse(data).forEach(function (val) { + if (this.key && this.key.startsWith('_')) this.remove(true); + if (typeof val === 'function') this.remove(true); + }); + return data; + }, + + /** + * Generate Short ID + */ + + generateShortId: function(maxLen) { + return shortid.generate().replace(/\W+/g, '').substring(0, maxLen).replace(/[_-]/g, ''); + } + +}; \ No newline at end of file