@znemz/cfn-include 1.6.4 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.js CHANGED
@@ -1,138 +1,191 @@
1
1
  #!/usr/bin/env node
2
+ /* eslint-disable global-require, no-console */
3
+ const exec = require('child_process').execSync;
4
+ const path = require('path');
5
+ const _ = require('lodash');
6
+ const pathParse = require('path-parse');
7
+ const yargs = require('yargs');
2
8
 
3
- var _ = require('lodash'),
4
- yaml = require('../lib/yaml'),
5
- exec = require('child_process').execSync,
6
- Client = require('../lib/cfnclient'),
7
- package = require('../package.json'),
8
- replaceEnv = require('../lib/replaceEnv');
9
- env = process.env,
10
- opts = require('yargs').command(
11
- '$0 [path] [options]',
12
- package.description,
13
- (y) => y.positional('path', {
9
+ const include = require('../index');
10
+ const yaml = require('../lib/yaml');
11
+ const Client = require('../lib/cfnclient');
12
+ const pkg = require('../package.json');
13
+ const replaceEnv = require('../lib/replaceEnv');
14
+
15
+ yargs.version(false);
16
+
17
+ const { env } = process;
18
+ // eslint-disable-next-line global-require
19
+ const opts = yargs
20
+ .command('$0 [path] [options]', pkg.description, (y) =>
21
+ y.positional('path', {
14
22
  positional: true,
15
23
  desc: 'location of template. Either path to a local file, URL or file on an S3 bucket (e.g. s3://bucket-name/example.template)',
16
24
  required: false,
17
- }))
18
- .options({
19
- minimize: {
20
- desc: 'minimize JSON output',
21
- default: false,
22
- boolean: true,
23
- alias: 'm',
24
- },
25
- metadata: {
26
- desc: 'add build metadata to output',
27
- default: false,
28
- boolean: true,
29
- },
30
- validate: {
31
- desc: 'validate compiled template',
32
- default: false,
33
- boolean: true,
34
- alias: 't',
35
- },
36
- yaml: {
37
- desc: 'output yaml instead of json',
38
- default: false,
39
- boolean: true,
40
- alias: 'y',
41
- },
42
- bucket: {
43
- desc: 'bucket name required for templates larger than 50k',
44
- },
45
- context: {
46
- desc: 'template full path. only utilized for stdin when the template is piped to this script',
47
- required: false,
48
- string: true,
49
- },
50
- prefix: {
51
- desc: 'prefix for templates uploaded to the bucket',
52
- default: 'cfn-include',
53
- },
54
- enable: {
55
- string: true,
56
- desc: `enable different options: ['env']`,
57
- choices: ['env']
58
- },
59
- version: {
60
- boolean: true,
61
- desc: 'print version and exit',
62
- callback: function () {
63
- console.log(package.version);
64
- process.exit(0);
65
- }
25
+ })
26
+ )
27
+ .options({
28
+ minimize: {
29
+ desc: 'minimize JSON output',
30
+ default: false,
31
+ boolean: true,
32
+ alias: 'm',
33
+ },
34
+ metadata: {
35
+ desc: 'add build metadata to output',
36
+ default: false,
37
+ boolean: true,
38
+ },
39
+ validate: {
40
+ desc: 'validate compiled template',
41
+ default: false,
42
+ boolean: true,
43
+ alias: 't',
44
+ },
45
+ yaml: {
46
+ desc: 'output yaml instead of json',
47
+ default: false,
48
+ boolean: true,
49
+ alias: 'y',
50
+ },
51
+ lineWidth: {
52
+ desc: 'output yaml line width',
53
+ default: 200,
54
+ number: true,
55
+ alias: 'l',
56
+ },
57
+ bucket: {
58
+ desc: 'bucket name required for templates larger than 50k',
59
+ },
60
+ context: {
61
+ desc:
62
+ // eslint-disable-next-line max-len
63
+ 'template full path. only utilized for stdin when the template is piped to this script',
64
+ required: false,
65
+ string: true,
66
+ },
67
+ prefix: {
68
+ desc: 'prefix for templates uploaded to the bucket',
69
+ default: 'cfn-include',
70
+ },
71
+ enable: {
72
+ string: true,
73
+ desc: `enable different options: ['env','eval'] or a combination of both via comma.`,
74
+ choices: ['', 'env', 'env,eval', 'eval,env', 'eval'], // '' hack
75
+ default: '',
76
+ },
77
+ inject: {
78
+ alias: 'i',
79
+ string: true,
80
+ // eslint-disable-next-line max-len
81
+ desc: `JSON string payload to use for template injection.`,
82
+ coerce: (valStr) => JSON.parse(valStr),
83
+ },
84
+ doLog: {
85
+ boolean: true,
86
+ // eslint-disable-next-line max-len
87
+ desc: `console log out include options in recurse step`,
88
+ },
89
+ version: {
90
+ boolean: true,
91
+ desc: 'print version and exit',
92
+ callback() {
93
+ console.log(pkg.version);
94
+ process.exit(0);
66
95
  },
67
- }).parse(),
68
- path = require('path'),
69
- include = require('../index'),
70
- pathParse = require('path-parse');
96
+ },
97
+ })
98
+ .parse();
99
+
100
+ // make enable an array
101
+ opts.enable = opts.enable.split(',');
71
102
 
72
- var promise;
103
+ let promise;
73
104
  if (opts.path) {
74
- var location, protocol = opts.path.match(/^\w+:\/\//);
105
+ let location;
106
+ const protocol = opts.path.match(/^\w+:\/\//);
75
107
  if (protocol) location = opts.path;
76
- else if (pathParse(opts.path).root) location = 'file://' + opts.path;
77
- else location = 'file://' + path.join(process.cwd(), opts.path);
108
+ else if (pathParse(opts.path).root) location = `file://${opts.path}`;
109
+ else location = `file://${path.join(process.cwd(), opts.path)}`;
78
110
  promise = include({
79
111
  url: location,
80
- doEnv: opts.enable === 'env',
112
+ doEnv: opts.enable.includes('env'),
113
+ doEval: opts.enable.includes('eval'),
114
+ inject: opts.inject,
115
+ doLog: opts.doLog,
81
116
  });
82
117
  } else {
83
118
  promise = new Promise((resolve, reject) => {
84
119
  process.stdin.setEncoding('utf8');
85
- var rawData = [];
86
- process.stdin.on('data', chunk => rawData.push(chunk));
87
- process.stdin.on('error', err => reject(err));
120
+ const rawData = [];
121
+ process.stdin.on('data', (chunk) => rawData.push(chunk));
122
+ process.stdin.on('error', (err) => reject(err));
88
123
  process.stdin.on('end', () => resolve(rawData.join('')));
89
- }).then(template => {
124
+ }).then((template) => {
90
125
  if (template.length === 0) {
91
126
  console.error('empty template received from stdin');
92
127
  process.exit(1);
93
128
  }
94
129
 
95
- const location = opts.context ? path.resolve(opts.context) :
96
- path.join(process.cwd(), 'template.yml');
130
+ const location = opts.context
131
+ ? path.resolve(opts.context)
132
+ : path.join(process.cwd(), 'template.yml');
133
+
134
+ template = opts.enable.includes('env') ? replaceEnv(template) : template;
97
135
 
98
- template = opts.enable === 'env' ? replaceEnv(template) : template;
99
-
100
136
  return include({
101
137
  template: yaml.load(template),
102
- url: 'file://' + location,
103
- doEnv: opts.enable === 'env',
104
- }).catch(err => console.error(err));
138
+ url: `file://${location}`,
139
+ doEnv: opts.enable.includes('env'),
140
+ doEval: opts.enable.includes('eval'),
141
+ inject: opts.inject,
142
+ doLog: opts.doLog,
143
+ }).catch((err) => console.error(err));
105
144
  });
106
145
  }
107
146
 
108
- promise.then(function (template) {
109
- if(opts.metadata) {
110
- try {
111
- var stdout = exec('git log -n 1 --pretty=%H', {
112
- stdio: [0, 'pipe', 'ignore']
113
- }).toString().trim();
114
- } catch (e) { }
115
- _.defaultsDeep(template, {
116
- Metadata: {
117
- CfnInclude: {
118
- GitCommit: stdout,
119
- BuildDate: new Date().toISOString(),
120
- }
147
+ promise
148
+ .then(function (template) {
149
+ if (opts.metadata) {
150
+ let stdout;
151
+ try {
152
+ stdout = exec('git log -n 1 --pretty=%H', {
153
+ stdio: [0, 'pipe', 'ignore'],
154
+ })
155
+ .toString()
156
+ .trim();
157
+ } catch (e) {
158
+ // eslint-disable-next-line no-empty
121
159
  }
122
- });
123
- }
124
- if (opts.validate) {
125
- const cfn = new Client({
126
- region: env.AWS_REGION || env.AWS_DEFAULT_REGION || 'us-east-1',
127
- bucket: opts.bucket,
128
- prefix: opts.prefix,
129
- });
130
- return cfn.validateTemplate(JSON.stringify(template)).then(() => template);
131
- } else return template;
132
- }).then(template => {
133
- console.log(opts.yaml ? yaml.dump(template) : JSON.stringify(template, null, opts.minimize ? null : 2));
134
- }).catch(function (err) {
135
- if (typeof err.toString === 'function') console.error(err.toString());
136
- else console.error(err);
137
- process.exit(1);
138
- });
160
+ _.defaultsDeep(template, {
161
+ Metadata: {
162
+ CfnInclude: {
163
+ GitCommit: stdout,
164
+ BuildDate: new Date().toISOString(),
165
+ },
166
+ },
167
+ });
168
+ }
169
+ if (opts.validate) {
170
+ const cfn = new Client({
171
+ region: env.AWS_REGION || env.AWS_DEFAULT_REGION || 'us-east-1',
172
+ bucket: opts.bucket,
173
+ prefix: opts.prefix,
174
+ });
175
+ return cfn.validateTemplate(JSON.stringify(template)).then(() => template);
176
+ }
177
+ return template;
178
+ })
179
+ .then((template) => {
180
+ console.log(
181
+ opts.yaml
182
+ ? yaml.dump(template, opts)
183
+ : JSON.stringify(template, null, opts.minimize ? null : 2)
184
+ );
185
+ })
186
+ .catch(function (err) {
187
+ if (typeof err.toString === 'function') console.error(err.toString());
188
+ else console.error(err);
189
+ console.log(err.stack);
190
+ process.exit(1);
191
+ });