Skip to content

Commit bd29fd6

Browse files
authored
Merge pull request #1482 from contentstack/feat/DX-980
feat: added flag to support variable types & respective prompt
2 parents d423031 + 28bcc63 commit bd29fd6

File tree

10 files changed

+173
-132
lines changed

10 files changed

+173
-132
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-launch/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $ npm install -g @contentstack/cli-launch
1919
$ csdx COMMAND
2020
running command...
2121
$ csdx (--version|-v)
22-
@contentstack/cli-launch/1.0.19 darwin-arm64 node-v22.2.0
22+
@contentstack/cli-launch/1.1.0 darwin-arm64 node-v22.2.0
2323
$ csdx --help [COMMAND]
2424
USAGE
2525
$ csdx COMMAND

packages/contentstack-launch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/cli-launch",
3-
"version": "1.0.19",
3+
"version": "1.1.0",
44
"description": "Launch related operations",
55
"author": "Contentstack CLI",
66
"bin": {

packages/contentstack-launch/src/adapters/base-class.ts

Lines changed: 82 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,19 @@ export default class BaseClass {
262262
this.log(error, 'error');
263263
this.exit(1);
264264
})) || [];
265-
this.config.selectedStack = await ux
266-
.inquire({
267-
name: 'stack',
268-
type: 'search-list',
269-
choices: listOfStacks,
270-
message: 'Stack',
271-
})
272-
.then((name) => find(listOfStacks, { name }));
265+
266+
if (this.config.selectedStack) {
267+
this.config.selectedStack = find(listOfStacks, { api_key: this.config.selectedStack });
268+
} else {
269+
this.config.selectedStack = await ux
270+
.inquire({
271+
name: 'stack',
272+
type: 'search-list',
273+
choices: listOfStacks,
274+
message: 'Stack',
275+
})
276+
.then((name) => find(listOfStacks, { name }));
277+
}
273278
}
274279

275280
/**
@@ -299,16 +304,19 @@ export default class BaseClass {
299304
this.exit(1);
300305
})) || [];
301306

302-
this.config.deliveryToken = await ux
303-
.inquire({
304-
type: 'search-list',
305-
name: 'deliveryToken',
306-
choices: listOfDeliveryTokens,
307-
message: 'Delivery token',
308-
})
309-
.then((name) => find(listOfDeliveryTokens, { name }) as Record<string, any>);
310-
311-
this.config.environment = this.config.deliveryToken.scope[0]?.environments[0]?.name;
307+
if (this.config.deliveryToken) {
308+
this.config.deliveryToken = find(listOfDeliveryTokens, { token: this.config.deliveryToken });
309+
} else {
310+
this.config.deliveryToken = await ux
311+
.inquire({
312+
type: 'search-list',
313+
name: 'deliveryToken',
314+
choices: listOfDeliveryTokens,
315+
message: 'Delivery token',
316+
})
317+
.then((name) => find(listOfDeliveryTokens, { name }) as Record<string, any>);
318+
}
319+
this.config.environment = this.config.deliveryToken?.scope[0]?.environments[0]?.name;
312320
}
313321

314322
/**
@@ -321,38 +329,51 @@ export default class BaseClass {
321329
let addNew = true;
322330
const envVariables = [];
323331

324-
do {
325-
const variable = await ux
326-
.inquire({
327-
type: 'input',
328-
name: 'variable',
329-
message:
330-
'Enter key and value with a colon between them, and use a comma(,) for the key-value pair. Format: <key1>:<value1>, <key2>:<value2> Ex: APP_ENV:prod, TEST_ENV:testVal',
331-
})
332-
.then((variable) => {
333-
return map(split(variable as string, ','), (variable) => {
334-
let [key, value] = split(variable as string, ':');
335-
value = (value || '').trim();
336-
key = (key || '').trim();
337-
338-
return { key, value };
339-
}).filter(({ key }) => key);
340-
});
332+
if (!this.config.envVariables) {
333+
do {
334+
const variable = await ux
335+
.inquire({
336+
type: 'input',
337+
name: 'variable',
338+
message:
339+
'Enter key and value with a colon between them, and use a comma(,) for the key-value pair. Format: <key1>:<value1>, <key2>:<value2> Ex: APP_ENV:prod, TEST_ENV:testVal',
340+
})
341+
.then((variable) => {
342+
return map(split(variable as string, ','), (variable) => {
343+
let [key, value] = split(variable as string, ':');
344+
value = (value || '').trim();
345+
key = (key || '').trim();
346+
347+
return { key, value };
348+
}).filter(({ key }) => key);
349+
});
341350

342-
envVariables.push(...variable);
351+
envVariables.push(...variable);
343352

344-
if (
345-
!(await ux.inquire({
346-
type: 'confirm',
347-
name: 'canImportFromStack',
348-
message: 'Would you like to add more variables?',
349-
}))
350-
) {
351-
addNew = false;
352-
}
353-
} while (addNew);
353+
if (
354+
!(await ux.inquire({
355+
type: 'confirm',
356+
name: 'canImportFromStack',
357+
message: 'Would you like to add more variables?',
358+
}))
359+
) {
360+
addNew = false;
361+
}
362+
} while (addNew);
363+
364+
this.envVariables.push(...envVariables);
365+
} else {
366+
if (typeof this.config.envVariables === 'string') {
367+
const variable = map(split(this.config.envVariables as string, ','), (variable) => {
368+
let [key, value] = split(variable as string, ':');
369+
value = (value || '').trim();
370+
key = (key || '').trim();
354371

355-
this.envVariables.push(...envVariables);
372+
return { key, value };
373+
});
374+
this.envVariables.push(...variable);
375+
}
376+
}
356377
}
357378

358379
/**
@@ -492,19 +513,16 @@ export default class BaseClass {
492513
* @memberof BaseClass
493514
*/
494515
async handleEnvImportFlow(): Promise<void> {
495-
const variablePreparationTypeOptions = [
496-
'Import variables from a stack',
497-
'Manually add custom variables to the list',
498-
'Import variables from the local env file',
499-
];
500-
const variablePreparationType: Array<string> = await ux.inquire({
501-
type: 'checkbox',
502-
name: 'variablePreparationType',
503-
default: this.config.framework,
504-
choices: variablePreparationTypeOptions,
505-
message: 'Import variables from a stack and/or manually add custom variables to the list',
506-
// validate: this.inquireRequireValidation,
507-
});
516+
const variablePreparationType =
517+
this.config.variableType ||
518+
(await ux.inquire({
519+
type: 'checkbox',
520+
name: 'variablePreparationType',
521+
default: this.config.framework,
522+
choices: this.config.variablePreparationTypeOptions,
523+
message: 'Import variables from a stack and/or manually add custom variables to the list',
524+
// validate: this.inquireRequireValidation,
525+
}));
508526

509527
if (includes(variablePreparationType, 'Import variables from a stack')) {
510528
await this.importEnvFromStack();
@@ -519,8 +537,8 @@ export default class BaseClass {
519537
if (this.envVariables.length) {
520538
this.printAllVariables();
521539
} else {
522-
this.log('Import variables from a stack and/or manually add custom variables to the list', 'warn');
523-
// this.exit(1);
540+
this.log('Please provide env file!', 'error');
541+
this.exit(1);
524542
}
525543
}
526544

@@ -533,10 +551,10 @@ export default class BaseClass {
533551
async importVariableFromLocalConfig(): Promise<void> {
534552
const localEnv =
535553
dotEnv.config({
536-
path: this.config.projectBasePath,
554+
path: `${this.config.projectBasePath}/.env.local`,
537555
}).parsed ||
538556
dotEnv.config({
539-
path: `${this.config.projectBasePath}/.env.local`,
557+
path: this.config.projectBasePath,
540558
}).parsed;
541559

542560
if (!isEmpty(localEnv)) {

packages/contentstack-launch/src/adapters/file-upload.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import FormData from 'form-data';
66
import filter from 'lodash/filter';
77
import includes from 'lodash/includes';
88
import { basename, resolve } from 'path';
9-
import { cliux, ux } from '@contentstack/cli-utilities';
9+
import { cliux, configHandler, ux } from '@contentstack/cli-utilities';
1010
import { createReadStream, existsSync, PathLike, statSync } from 'fs';
1111

1212
import { print } from '../util';
@@ -110,9 +110,14 @@ export default class FileUpload extends BaseClass {
110110
framework,
111111
environment,
112112
'build-command': buildCommand,
113-
'output-directory': outputDirectory,
113+
'out-dir': outputDirectory,
114+
'variable-type': variableType,
115+
'env-variables': envVariables,
116+
alias,
114117
} = this.config.flags;
115-
118+
const { token, apiKey } = configHandler.get(`tokens.${alias}`) ?? {};
119+
this.config.selectedStack = apiKey;
120+
this.config.deliveryToken = token;
116121
// this.fileValidation();
117122
await this.selectOrg();
118123
await this.createSignedUploadUrl();
@@ -166,6 +171,8 @@ export default class FileUpload extends BaseClass {
166171
message: 'Output Directory',
167172
default: (this.config.outputDirectories as Record<string, string>)[this.config?.framework || 'OTHER'],
168173
}));
174+
this.config.variableType = variableType as unknown as string;
175+
this.config.envVariables = envVariables;
169176
await this.handleEnvImportFlow();
170177
}
171178

0 commit comments

Comments
 (0)