Skip to content

Commit

Permalink
feat: improve project scaffolding experience (#911)
Browse files Browse the repository at this point in the history
* feat: improve project scaffolding experience

* fix banner

* do not leak api key

* address review

* fix regex
  • Loading branch information
vigneshshanmugam committed Apr 2, 2024
1 parent 2435717 commit 7c75279
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
44 changes: 24 additions & 20 deletions src/generator/index.ts
Expand Up @@ -22,13 +22,14 @@
* THE SOFTWARE.
*
*/
import { URL } from 'url';
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { bold, cyan, yellow } from 'kleur/colors';
import { join, relative, dirname, basename } from 'path';
// @ts-ignore-next-line: has no exported member 'Input'
import { prompt, Input } from 'enquirer';
import { prompt, Input, Password } from 'enquirer';
import { getProjectApiKeyURL, progress, write as stdWrite } from '../helpers';
import {
getPackageManager,
Expand Down Expand Up @@ -60,6 +61,8 @@ export const REGULAR_FILES_PATH = [
];
export const CONFIG_PATH = 'synthetics.config.ts';

const IS_URL = new RegExp('^(https?:\\/\\/)');

export class Generator {
pkgManager = 'npm';
constructor(public projectDir: string) {}
Expand All @@ -80,29 +83,30 @@ export class Generator {
return JSON.parse(process.env.TEST_QUESTIONS);
}

const { onCloud } = await prompt<{ onCloud: string }>({
type: 'confirm',
name: 'onCloud',
initial: 'y',
message: 'Do you use Elastic Cloud',
});
const url = await new Input({
header: onCloud
? yellow(
'Get cloud.id from your deployment https://www.elastic.co/guide/en/cloud/current/ec-cloud-id.html'
)
: '',
message: onCloud
? 'What is your cloud.id'
: 'What is the url of your Kibana instance',
name: 'url',
message: 'Enter Elastic Kibana URL or Cloud ID',
required: true,
result(value) {
return onCloud ? cloudIDToKibanaURL(value) : value;
validate(value) {
try {
if (!IS_URL.test(value)) {
value = cloudIDToKibanaURL(value);
}
new URL(value);
return true;
} catch (e) {
return 'Invalid URL or Cloud ID';
}
},
result(value: string) {
if (!IS_URL.test(value)) {
value = cloudIDToKibanaURL(value);
}
return value;
},
}).run();

const auth = await new Input({
const auth = await new Password({
name: 'auth',
header: yellow(
`Generate API key from Kibana ${getProjectApiKeyURL(url)}`
Expand Down Expand Up @@ -249,7 +253,7 @@ export class Generator {
}
// Add push command
if (!pkgJSON.scripts.push) {
pkgJSON.scripts.push = 'npx @elastic/synthetics push';
pkgJSON.scripts.push = `npx @elastic/synthetics push`;
}

await this.createFile(
Expand Down Expand Up @@ -287,7 +291,7 @@ All set, you can run below commands inside: ${this.projectDir}:
)}
${yellow(
'Make sure to configure the SYNTHETICS_API_KEY before pushing monitors to Kibana.'
'Configure API Key via `SYNTHETICS_API_KEY` env variable or --auth CLI flag.'
)}
Visit https://www.elastic.co/guide/en/observability/current/synthetic-run-tests.html to learn more.
Expand Down
6 changes: 2 additions & 4 deletions src/options.ts
Expand Up @@ -184,14 +184,12 @@ export function getCommonCommandOpts() {
const params = createOption(
'-p, --params <jsonstring>',
'JSON object that gets injected to all journeys'
);
params.argParser(JSON.parse);
).argParser(JSON.parse);

const playwrightOpts = createOption(
'--playwright-options <jsonstring>',
'JSON object to pass in custom Playwright options for the agent. Options passed will be merged with Playwright options defined in your synthetics.config.js file.'
);
playwrightOpts.argParser(JSON.parse);
).argParser(JSON.parse);

const pattern = createOption(
'--pattern <pattern>',
Expand Down

0 comments on commit 7c75279

Please sign in to comment.