Skip to content

Commit 5ec9089

Browse files
authored
Merge pull request #83 from argonlaser/master
Makes template to consume a template location
2 parents ff06bb0 + 5f00254 commit 5ec9089

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

microplan-init.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
var program = require('commander')
22
var fs = require('fs-extra')
33
var path = require('path')
4+
var utils = require('./utils/utils.js')
5+
46
var templateFileLocation = {
57
'yaml': 'parsers/yaml/template.yml',
68
'json': 'parsers/json/template.json'
79
}
810

11+
var suppFileExtns = ['.json', '.yml', '.yaml']
12+
913
var _copyTemplateToPath = function (templateFilePath, destFilePath) {
1014
let sourcePath = ''
1115
try {
@@ -24,25 +28,26 @@ var _copyTemplateToPath = function (templateFilePath, destFilePath) {
2428
})
2529
}
2630
program
27-
.option('-t, --template <type>', 'specify input template (json|yaml) [yaml]', 'yaml')
31+
.option('-t, --template <location>', 'specify input template location', templateFileLocation.yaml)
2832
.parse(process.argv)
2933

3034
var initArgs = program.args
3135

3236
if (initArgs.length > 1) {
33-
console.error('Filename required')
37+
console.error('Filename required. Please specify a valid', suppFileExtns.join('/'), 'template location as argument.')
3438
process.exit(1)
3539
}
3640

37-
if (program.template === 'yaml') {
38-
_copyTemplateToPath(templateFileLocation.yaml, initArgs[0])
39-
} else if (program.template === 'json') {
40-
_copyTemplateToPath(templateFileLocation.json, initArgs[0])
41-
} else {
42-
let type = path.extname(program.template)
43-
if (type !== '.yml' && type !== '.yaml' && type !== '.json') {
44-
console.error('Error : Please select proper template.')
45-
process.exit(1)
46-
}
47-
_copyTemplateToPath(program.template, initArgs[0])
41+
if (utils.fileExists(program.template) === false) {
42+
console.error('Please specify a valid', suppFileExtns.join('/'), 'template file location.')
43+
process.exit(1)
4844
}
45+
46+
let fileExt = path.extname(program.template)
47+
48+
if (suppFileExtns.indexOf(fileExt) === -1) {
49+
console.error('File extension not supported. Please use supported file extensions: ' + suppFileExtns.join(', '))
50+
process.exit(1)
51+
}
52+
53+
_copyTemplateToPath(program.template, initArgs[0])

utils/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var fs = require('fs')
2+
13
var parseSlug = function (slug) {
24
var result = {
35
namespace: null,
@@ -12,7 +14,16 @@ var parseSlug = function (slug) {
1214
return result
1315
}
1416

17+
function fileExists (filePath) {
18+
try {
19+
return fs.statSync(filePath).isFile()
20+
} catch (err) {
21+
return false
22+
}
23+
}
24+
1525
module.exports =
1626
{
27+
fileExists: fileExists,
1728
parseSlug: parseSlug
1829
}

0 commit comments

Comments
 (0)