UNPKG

8.76 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13/* eslint-disable no-console */
14const crypto = require('crypto');
15const yargs = require('yargs');
16const chalk = require('chalk');
17
18const defaultConfig = require('./config/adobeioruntime-node10.js');
19
20class CLI {
21 constructor() {
22 this._yargs = yargs()
23 .pkgConf('wsk')
24 .option('verbose', {
25 alias: 'v',
26 type: 'boolean',
27 default: false,
28 })
29 .option('build', {
30 description: 'Build the deployment package',
31 type: 'boolean',
32 default: true,
33 })
34 .option('directory', {
35 description: 'Project directory',
36 type: 'string',
37 default: '.',
38 })
39 .option('deploy', {
40 description: 'Automatically deploy to OpenWhisk',
41 type: 'boolean',
42 default: false,
43 })
44 .option('test', {
45 description: 'Invoke action after deployment. Can be relative url.',
46 type: 'string',
47 })
48 .option('test-params', {
49 description: 'Invoke openwhisk action after deployment with the given params.',
50 type: 'array',
51 default: [],
52 })
53 .option('hints', {
54 alias: 'no-hints',
55 description: 'Show additional hints for deployment',
56 type: 'boolean',
57 default: true,
58 })
59 .option('version-link', {
60 alias: 'l',
61 description: 'Create symlinks (sequences) after deployment. "major" and "minor" will create respective version links',
62 type: 'string',
63 array: true,
64 })
65 .option('delete', {
66 description: 'Delete the action from OpenWhisk. Implies no-build',
67 type: 'boolean',
68 default: false,
69 })
70 .option('linkPackage', {
71 description: 'Package name for version links',
72 type: 'string',
73 })
74 .group(['build', 'deploy', 'test', 'test-params', 'hints', 'update-package', 'version-link', 'linkPackage', 'delete'], 'Operation Options')
75
76 .option('name', {
77 description: 'OpenWhisk action name. Can be prefixed with package.',
78 })
79 .option('namespace', {
80 description: 'OpenWhisk namespace. Needs to match the namespace provided with the openwhisk credentials.',
81 })
82 .option('pkgVersion', {
83 description: 'Version use in the embedded package.json.',
84 })
85 .option('kind', {
86 description: 'Specifies the action kind.',
87 default: 'nodejs:10',
88 })
89 .option('web-export', {
90 description: 'Annotates the action as web-action',
91 type: 'boolean',
92 default: true,
93 })
94 .option('raw-http', {
95 description: 'Annotates the action as raw web-action (enforces web-export=true)',
96 type: 'boolean',
97 default: false,
98 })
99 .option('web-secure', {
100 description: 'Annotates the action with require-whisk-auth. leave empty to generate random token.',
101 type: 'string',
102 coerce: (value) => {
103 if (typeof value === 'string') {
104 if (value === 'true') {
105 return true;
106 }
107 if (value === 'false') {
108 return false;
109 }
110 return (value.trim() ? value.trim() : crypto.randomBytes(32).toString('base64'));
111 }
112 return value;
113 },
114 })
115 .option('docker', {
116 description: 'Specifies a docker image.',
117 })
118 .option('params', {
119 alias: 'p',
120 description: 'Include the given action param. can be json or env.',
121 type: 'array',
122 default: [],
123 })
124 .option('modules', {
125 alias: 'm',
126 description: 'Include a node_module as is.',
127 type: 'array',
128 default: [],
129 })
130 .option('params-file', {
131 alias: 'f',
132 description: 'Include the given action param from a file; can be json or env.',
133 type: 'array',
134 default: [],
135 })
136 .option('timeout', {
137 alias: 't',
138 description: 'the timeout limit in milliseconds after which the action is terminated',
139 type: 'integer',
140 default: 60000,
141 })
142 .option('memory', {
143 description: 'the maximum memory LIMIT in MB for the action',
144 type: 'integer',
145 })
146 .option('concurrency', {
147 description: 'the maximum intra-container concurrent activation LIMIT for the action',
148 type: 'integer',
149 })
150 .option('updated-by', {
151 description: 'user that updated the action or sequence.',
152 type: 'string',
153 })
154 .option('updated-at', {
155 description: 'unix timestamp when the action or sequence was updated (defaults to the current time).',
156 type: 'number',
157 default: (new Date().getTime()),
158 })
159 .group([
160 'name', 'kind', 'docker', 'params', 'params-file', 'web-export', 'raw-http', 'web-secure',
161 'namespace', 'timeout', 'updated-by', 'updated-at'], 'OpenWhisk Action Options')
162
163 .option('update-package', {
164 description: 'Create or update wsk package.',
165 type: 'boolean',
166 default: false,
167 })
168 .option('package.name', {
169 description: 'OpenWhisk package name.',
170 type: 'string',
171 })
172 .option('package.shared', {
173 description: 'OpenWhisk package scope.',
174 type: 'boolean',
175 default: false,
176 })
177 .option('package.params', {
178 description: 'OpenWhisk package params.',
179 type: 'array',
180 default: [],
181 })
182 .option('package.params-file', {
183 description: 'OpenWhisk package params file.',
184 type: 'array',
185 default: [],
186 })
187 .group(['package.name', 'package.params', 'package.params-file', 'package.shared'], 'OpenWhisk Package Options')
188
189 .option('static', {
190 alias: 's',
191 description: 'Includes a static file into the archive',
192 type: 'array',
193 default: [],
194 })
195 .option('entryFile', {
196 description: 'Specifies the entry file.',
197 default: 'src/index.js',
198 })
199 .option('externals', {
200 description: 'Defines the externals for webpack.',
201 type: 'array',
202 default: [],
203 })
204 .group(['static', 'entryFile', 'externals', 'modules'], 'Bundling Options')
205 .help();
206 }
207
208 // eslint-disable-next-line class-methods-use-this
209 createBuilder() {
210 // eslint-disable-next-line global-require
211 const ActionBuilder = require('./action_builder.js');
212 return new ActionBuilder();
213 }
214
215 prepare(args) {
216 const argv = this._yargs.parse(args);
217
218 if (argv.externals.length === 0) {
219 argv.externals = defaultConfig.externals;
220 }
221 return this.createBuilder()
222 .verbose(argv.verbose)
223 .withDirectory(argv.directory)
224 .withBuild(argv.build)
225 .withDelete(argv.delete)
226 .withDeploy(argv.deploy)
227 .withTest(argv.test)
228 .withTestParams(argv.testParams)
229 .withHints(argv.hints)
230 .withStatic(argv.static)
231 .withName(argv.name)
232 .withNamespace(argv.namespace)
233 .withUpdatedAt(argv.updatedAt)
234 .withUpdatedBy(argv.updatedBy)
235 .withParams(argv.params)
236 .withParamsFile(argv.paramsFile)
237 .withVersion(argv.pkgVersion)
238 .withKind(argv.kind)
239 .withEntryFile(argv.entryFile)
240 .withExternals(argv.externals)
241 .withDocker(argv.docker)
242 .withModules(argv.modules)
243 .withWebExport(argv.webExport)
244 .withWebSecure(argv.webSecure)
245 .withRawHttp(argv.rawHttp)
246 .withUpdatePackage(argv.updatePackage)
247 .withPackageName(argv.package.name)
248 .withPackageParams(argv.package.params)
249 .withPackageParamsFile(argv.package['params-file'])
250 .withTimeout(argv.timeout)
251 .withMemory(argv.memory)
252 .withConcurrency(argv.concurrency)
253 .withLinks(argv.versionLink)
254 .withLinksPackage(argv.linksPackage)
255 .withPackageShared(argv.package.shared);
256 }
257
258 async run(args) {
259 try {
260 const res = await this.prepare(args).run();
261 console.log(JSON.stringify(res, null, 2));
262 } catch (err) {
263 console.log(`${chalk.red('error:')} ${err.message}`);
264 process.exitCode = 1;
265 }
266 }
267}
268
269module.exports = CLI;