From 83268cc1ef4ae4914356e49af109425d4a89ce35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bachelier?= Date: Thu, 19 Feb 2015 23:12:52 +0100 Subject: [PATCH 1/4] fix(grunt.util._): switch to lodash --- tasks/env.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tasks/env.js b/tasks/env.js index 415b80a..e50de4e 100644 --- a/tasks/env.js +++ b/tasks/env.js @@ -16,7 +16,7 @@ module.exports = function (grunt) { grunt.registerMultiTask('env', 'Specify an ENV configuration for future tasks in the chain', function() { - var data = grunt.util._.clone(this.data); + var data = _.clone(this.data); delete data.src; processDirectives(data); @@ -110,4 +110,3 @@ function readIni(content) { return; } } - From a97ce015407b41918fef9d0cacef75afc2106856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bachelier?= Date: Thu, 19 Feb 2015 23:54:59 +0100 Subject: [PATCH 2/4] refactor(utils): move parsing function inside lib/utils --- lib/utils.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ tasks/env.js | 32 ++++++++------------------------ 2 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 lib/utils.js diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..f9b90eb --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,47 @@ +var ini = require('ini'); + +var types = { + json: readJson, + ini: readIni, + env: readIni, + default: readFile // default for unsupported format or no extension file +}; + +var extensionPattern = /\.([^\.]+)$/i; + +function readFile(grunt, file) { + return grunt.file.read(file); +}; + +function readJson(grunt, file) { + try { + return grunt.file.readJSON(file); + } catch(e) { + return; + } +} + +function readIni(grunt, file) { + try { + return ini.parse(grunt.file.read(file)); + } catch(e) { + return; + } +} + +module.exports = { + // Export a single parse function that call the proper parsing function + // Grunt is used here as it as already some parsing functions + parse: function (grunt, file) { + var match = file.match(extensionPattern); + var type = match ? match[1] : 'default'; // default to ini format + + try { + var parseFn = types[type] || types.default; + return parseFn(grunt, file) || {}; + } + catch (e) { + return; + } + } +}; diff --git a/tasks/env.js b/tasks/env.js index e50de4e..ec16ae9 100644 --- a/tasks/env.js +++ b/tasks/env.js @@ -9,10 +9,14 @@ "use strict"; var _ = require('lodash'), - ini = require('ini'), - path = require('path'); + path = require('path'), + utils = require(process.cwd() + '/lib/utils'); module.exports = function (grunt) { + var parse = function (file) { + // pass grunt reference to parse + return utils.parse(grunt, file); + }; grunt.registerMultiTask('env', 'Specify an ENV configuration for future tasks in the chain', function() { @@ -26,15 +30,13 @@ module.exports = function (grunt) { if(options.envdir) { var d = _.zipObject(this.files[0].src.map(function(file){ if(grunt.file.isFile(file)) { - return [path.basename(file), grunt.file.read(file)]; + return [path.basename(file), parse(file)]; } })); processDirectives(d); } else { this.files[0].src.forEach(function(file){ - var fileContent = grunt.file.read(file); - var data = readJson(fileContent) || readIni(fileContent) || {}; - processDirectives(data); + processDirectives(parse(file)); }); } } @@ -92,21 +94,3 @@ module.exports = function (grunt) { } } }; - - - -function readJson(content) { - try { - return JSON.parse(content); - } catch(e) { - return; - } -} - -function readIni(content) { - try { - return ini.parse(content); - } catch(e) { - return; - } -} From f114ea045f1a8615d9b9c2862d50c2b6ec17ff8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bachelier?= Date: Fri, 20 Feb 2015 00:01:54 +0100 Subject: [PATCH 3/4] feat(yaml): add support for YAML --- Gruntfile.js | 10 +++++++++- lib/utils.js | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index f994c5b..3623634 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -20,7 +20,7 @@ module.exports = function(grunt) { } }, testDotEnv : { - src : ['.env', '.env.json'] + src : ['.env', '.env.json', '.env.ini', '.env.yaml'] }, testEnvdir: { src : ['.envdir/*'], @@ -124,6 +124,8 @@ module.exports = function(grunt) { grunt.registerTask('writeDotEnv', function(){ grunt.file.write('.env', "dotEnvFileData=bar\ndotEnvFileOption=baz"); grunt.file.write('.env.json', '{"jsonValue" : "foo","push" : {"PATHLIKE":"jsonPath"}}'); + grunt.file.write('.env.ini', "dotEnvIniFileData=bar.ini\ndotEnvIniFileOption=baz.ini\n"); + grunt.file.write('.env.yaml', 'yamlValue: foo'); }); grunt.registerTask('testDotEnv', function(){ @@ -133,11 +135,17 @@ module.exports = function(grunt) { assert.equal(process.env.globalOption, 'foo', 'should still get global options'); assert.equal(process.env.dotEnvFileData, 'bar', 'dotEnvFileData should be set'); assert.equal(process.env.dotEnvFileOption, 'baz', 'dotEnvFileOption should be set'); + assert.equal(process.env.dotEnvIniFileData, 'bar.ini', 'dotEnvIniFileData should be set'); + assert.equal(process.env.dotEnvIniFileOption, 'baz.ini', 'ndotEnvIniFileOption should be set'); + assert.equal(process.env.yamlValue, 'foo', 'yamlValue should be set'); delete process.env.jsonValue; delete process.env.dotEnvFileData; delete process.env.dotEnvFileOption; delete process.env.PATHLIKE; delete process.env.globalOption; + delete process.env.dotEnvIniFileData; + delete process.env.dotEnvIniFileOption; + delete process.env.yamlValue; }); grunt.registerTask("writeEnvdir", function(){ diff --git a/lib/utils.js b/lib/utils.js index f9b90eb..993770a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,6 +2,7 @@ var ini = require('ini'); var types = { json: readJson, + yaml: readYaml, ini: readIni, env: readIni, default: readFile // default for unsupported format or no extension file @@ -21,6 +22,14 @@ function readJson(grunt, file) { } } +function readYaml(grunt, file) { + try { + return grunt.file.readYAML(file); + } catch(e) { + return; + } +} + function readIni(grunt, file) { try { return ini.parse(grunt.file.read(file)); From baab364af7e865803de2e47ae182c3d82932880a Mon Sep 17 00:00:00 2001 From: stephanebachelier Date: Wed, 23 Oct 2013 18:47:05 +0200 Subject: [PATCH 4/4] Update README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a915927..a7eed92 100644 --- a/README.md +++ b/README.md @@ -51,13 +51,16 @@ grunt.loadNpmTasks('grunt-env'); ``` ## Using external files -You can specify environment values in INI or JSON style and load them via the src option. +You can specify environment values in INI, JSON or YAML style and load them via the src option. ```js env : { dev : { src : "dev.json" }, + prod: { + src: "settings.yaml" + } heroku : { src : ".env" }