Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added flag to support variable types & respective prompt #1482

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentstack-launch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install -g @contentstack/cli-launch
$ csdx COMMAND
running command...
$ csdx (--version|-v)
@contentstack/cli-launch/1.0.19 darwin-arm64 node-v22.2.0
@contentstack/cli-launch/1.1.0 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-launch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/cli-launch",
"version": "1.0.19",
"version": "1.1.0",
"description": "Launch related operations",
"author": "Contentstack CLI",
"bin": {
Expand Down
146 changes: 82 additions & 64 deletions packages/contentstack-launch/src/adapters/base-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,19 @@ export default class BaseClass {
this.log(error, 'error');
this.exit(1);
})) || [];
this.config.selectedStack = await ux
.inquire({
name: 'stack',
type: 'search-list',
choices: listOfStacks,
message: 'Stack',
})
.then((name) => find(listOfStacks, { name }));

if (this.config.selectedStack) {
this.config.selectedStack = find(listOfStacks, { api_key: this.config.selectedStack });
} else {
this.config.selectedStack = await ux
.inquire({
name: 'stack',
type: 'search-list',
choices: listOfStacks,
message: 'Stack',
})
.then((name) => find(listOfStacks, { name }));
}
}

/**
Expand Down Expand Up @@ -299,16 +304,19 @@ export default class BaseClass {
this.exit(1);
})) || [];

this.config.deliveryToken = await ux
.inquire({
type: 'search-list',
name: 'deliveryToken',
choices: listOfDeliveryTokens,
message: 'Delivery token',
})
.then((name) => find(listOfDeliveryTokens, { name }) as Record<string, any>);

this.config.environment = this.config.deliveryToken.scope[0]?.environments[0]?.name;
if (this.config.deliveryToken) {
this.config.deliveryToken = find(listOfDeliveryTokens, { token: this.config.deliveryToken });
} else {
this.config.deliveryToken = await ux
.inquire({
type: 'search-list',
name: 'deliveryToken',
choices: listOfDeliveryTokens,
message: 'Delivery token',
})
.then((name) => find(listOfDeliveryTokens, { name }) as Record<string, any>);
}
this.config.environment = this.config.deliveryToken?.scope[0]?.environments[0]?.name;
}

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

do {
const variable = await ux
.inquire({
type: 'input',
name: 'variable',
message:
'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',
})
.then((variable) => {
return map(split(variable as string, ','), (variable) => {
let [key, value] = split(variable as string, ':');
value = (value || '').trim();
key = (key || '').trim();

return { key, value };
}).filter(({ key }) => key);
});
if (!this.config.envVariables) {
do {
const variable = await ux
.inquire({
type: 'input',
name: 'variable',
message:
'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',
})
.then((variable) => {
return map(split(variable as string, ','), (variable) => {
let [key, value] = split(variable as string, ':');
value = (value || '').trim();
key = (key || '').trim();

return { key, value };
}).filter(({ key }) => key);
});

envVariables.push(...variable);
envVariables.push(...variable);

if (
!(await ux.inquire({
type: 'confirm',
name: 'canImportFromStack',
message: 'Would you like to add more variables?',
}))
) {
addNew = false;
}
} while (addNew);
if (
!(await ux.inquire({
type: 'confirm',
name: 'canImportFromStack',
message: 'Would you like to add more variables?',
}))
) {
addNew = false;
}
} while (addNew);

this.envVariables.push(...envVariables);
} else {
if (typeof this.config.envVariables === 'string') {
const variable = map(split(this.config.envVariables as string, ','), (variable) => {
let [key, value] = split(variable as string, ':');
value = (value || '').trim();
key = (key || '').trim();

this.envVariables.push(...envVariables);
return { key, value };
});
this.envVariables.push(...variable);
}
}
}

/**
Expand Down Expand Up @@ -492,19 +513,16 @@ export default class BaseClass {
* @memberof BaseClass
*/
async handleEnvImportFlow(): Promise<void> {
const variablePreparationTypeOptions = [
'Import variables from a stack',
'Manually add custom variables to the list',
'Import variables from the local env file',
];
const variablePreparationType: Array<string> = await ux.inquire({
type: 'checkbox',
name: 'variablePreparationType',
default: this.config.framework,
choices: variablePreparationTypeOptions,
message: 'Import variables from a stack and/or manually add custom variables to the list',
// validate: this.inquireRequireValidation,
});
const variablePreparationType =
this.config.variableType ||
(await ux.inquire({
type: 'checkbox',
name: 'variablePreparationType',
default: this.config.framework,
choices: this.config.variablePreparationTypeOptions,
message: 'Import variables from a stack and/or manually add custom variables to the list',
// validate: this.inquireRequireValidation,
}));

if (includes(variablePreparationType, 'Import variables from a stack')) {
await this.importEnvFromStack();
Expand All @@ -519,8 +537,8 @@ export default class BaseClass {
if (this.envVariables.length) {
this.printAllVariables();
} else {
this.log('Import variables from a stack and/or manually add custom variables to the list', 'warn');
// this.exit(1);
this.log('Please provide env file!', 'error');
this.exit(1);
}
}

Expand All @@ -533,10 +551,10 @@ export default class BaseClass {
async importVariableFromLocalConfig(): Promise<void> {
const localEnv =
dotEnv.config({
path: this.config.projectBasePath,
path: `${this.config.projectBasePath}/.env.local`,
}).parsed ||
dotEnv.config({
path: `${this.config.projectBasePath}/.env.local`,
path: this.config.projectBasePath,
}).parsed;

if (!isEmpty(localEnv)) {
Expand Down
13 changes: 10 additions & 3 deletions packages/contentstack-launch/src/adapters/file-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import FormData from 'form-data';
import filter from 'lodash/filter';
import includes from 'lodash/includes';
import { basename, resolve } from 'path';
import { cliux, ux } from '@contentstack/cli-utilities';
import { cliux, configHandler, ux } from '@contentstack/cli-utilities';
import { createReadStream, existsSync, PathLike, statSync } from 'fs';

import { print } from '../util';
Expand Down Expand Up @@ -110,9 +110,14 @@ export default class FileUpload extends BaseClass {
framework,
environment,
'build-command': buildCommand,
'output-directory': outputDirectory,
'out-dir': outputDirectory,
'variable-type': variableType,
'env-variables': envVariables,
alias,
} = this.config.flags;

const { token, apiKey } = configHandler.get(`tokens.${alias}`) ?? {};
this.config.selectedStack = apiKey;
this.config.deliveryToken = token;
// this.fileValidation();
await this.selectOrg();
await this.createSignedUploadUrl();
Expand Down Expand Up @@ -166,6 +171,8 @@ export default class FileUpload extends BaseClass {
message: 'Output Directory',
default: (this.config.outputDirectories as Record<string, string>)[this.config?.framework || 'OTHER'],
}));
this.config.variableType = variableType as unknown as string;
this.config.envVariables = envVariables;
await this.handleEnvImportFlow();
}

Expand Down
Loading
Loading