stepzen 0.11.0 → 0.13.0-beta.0

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/README.md CHANGED
@@ -29,7 +29,7 @@ $ npm install -g stepzen
29
29
  $ stepzen COMMAND
30
30
  running command...
31
31
  $ stepzen (-v|--version|version)
32
- stepzen/0.11.0 darwin-x64 node-v14.19.1
32
+ stepzen/0.13.0-beta.0 darwin-x64 node-v14.19.1
33
33
  $ stepzen --help [COMMAND]
34
34
  USAGE
35
35
  $ stepzen COMMAND
@@ -27,10 +27,19 @@ class Import extends command_1.Command {
27
27
  'In order to use the --name flag please import each schema separately.');
28
28
  }
29
29
  // Get the working directory and workspace
30
+ let workspace;
30
31
  const directory = utils_1.getDirectory(flags.dir);
31
- let workspace = workspace_1.getWorkspace(directory);
32
- if (!workspace) {
33
- workspace = await init_1.default.run([directory]);
32
+ const maybeWorkspace = workspace_1.getWorkspace(directory);
33
+ if (maybeWorkspace) {
34
+ workspace = maybeWorkspace;
35
+ }
36
+ else {
37
+ try {
38
+ workspace = await init_1.default.run([directory]);
39
+ }
40
+ catch (error) {
41
+ throw new errors_1.CLIError(`Could not create a StepZen workspace in the ${flags.dir ? directory : 'current'} directory.\n` + error.message);
42
+ }
34
43
  }
35
44
  // Select an import execution flow:
36
45
  // - v1 with `stepzen/engines` and cloud function
@@ -1,4 +1,7 @@
1
1
  import { Command, flags } from '@oclif/command';
2
+ export declare const validateWorkspaceName: (directory: string, name: string) => boolean;
3
+ export declare const generateWorkspaceName: (directory: string) => string;
4
+ export declare const guessSchemaRoot: (directory: string) => string;
2
5
  export default class Init extends Command {
3
6
  static description: string;
4
7
  static hidden: boolean;
@@ -7,5 +10,5 @@ export default class Init extends Command {
7
10
  hidden: boolean;
8
11
  name: string;
9
12
  }[];
10
- run(): Promise<any>;
13
+ run(): Promise<import("../shared/types").Workspace>;
11
14
  }
@@ -1,53 +1,85 @@
1
1
  "use strict";
2
2
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.guessSchemaRoot = exports.generateWorkspaceName = exports.validateWorkspaceName = void 0;
4
5
  const command_1 = require("@oclif/command");
5
6
  const errors_1 = require("@oclif/errors");
6
7
  const fs = require("fs");
7
8
  const glob = require("glob");
8
9
  const inquirer = require("inquirer");
10
+ const os = require("os");
9
11
  const path = require("path");
10
12
  const utils_1 = require("../shared/utils");
11
13
  const moniker_1 = require("../shared/moniker");
12
14
  const workspace_1 = require("../shared/workspace");
15
+ exports.validateWorkspaceName = (directory, name) => {
16
+ return !fs.existsSync(path.join(directory, name));
17
+ };
18
+ exports.generateWorkspaceName = (directory) => {
19
+ let name = 'hello-stepzen';
20
+ while (!exports.validateWorkspaceName(directory, name)) {
21
+ name = `hello-${moniker_1.getRandomDescriptor()}-stepzen`;
22
+ }
23
+ return name;
24
+ };
25
+ exports.guessSchemaRoot = (directory) => {
26
+ const existing = [
27
+ ...glob.sync('*/**/config.yaml', {
28
+ cwd: directory,
29
+ ignore: '**/node_modules/**',
30
+ }),
31
+ ...glob.sync('*/**/index.graphql', {
32
+ cwd: directory,
33
+ ignore: '**/node_modules/**',
34
+ }),
35
+ ];
36
+ return existing.length > 0 ? path.dirname(existing[0]) : '';
37
+ };
13
38
  class Init extends command_1.Command {
14
39
  async run() {
15
40
  const { args, flags } = this.parse(Init);
16
41
  // Get the correct directory
17
- const directory = utils_1.getDirectory(args.directory);
42
+ let directory = utils_1.getDirectory(args.directory);
18
43
  // Make sure it is not already a workspace
19
- const w = workspace_1.getWorkspace(directory);
20
- if (w)
21
- throw new errors_1.CLIError('This is already a workspace, cannot init');
22
- // If you've passed an endpoint, validate it, and throw an error straight away if needed
44
+ if (workspace_1.getWorkspace(directory)) {
45
+ throw new errors_1.CLIError(`This directory is already a StepZen workspace: ${directory}.` +
46
+ ' Please select a different directory.');
47
+ }
48
+ const isHomeDir = directory === os.homedir();
49
+ // Prevent init from running in the home directory if the directory was
50
+ // explicitly provided as an aargument to `stepzen init`.
51
+ // StepZen CLI sometimes would enumerate all files in the workspace folder
52
+ // doing so in the home directory is likely to fail.
53
+ if (args.directory && isHomeDir) {
54
+ throw new errors_1.CLIError('Using the home directory as a StepZen workspace is not supported.' +
55
+ ' Please select a different directory.');
56
+ }
57
+ // Make a suggestion for the workspace name (if running in the HOME
58
+ // directory)
59
+ const name = isHomeDir
60
+ ? exports.generateWorkspaceName(directory)
61
+ : path.basename(directory);
62
+ // See if we think there's a StepZen schema already
63
+ const root = isHomeDir ? '' : exports.guessSchemaRoot(directory);
64
+ // If you've passed an endpoint, validate it, and throw an error
65
+ // straight away if needed
23
66
  if (flags.endpoint) {
24
67
  const error = utils_1.validateEndpoint(flags.endpoint);
25
- if (typeof error === 'string')
68
+ if (typeof error === 'string') {
26
69
  throw new errors_1.CLIError(error);
70
+ }
27
71
  }
28
72
  // Make a suggestion for the endpoint
29
73
  const endpoint = flags.endpoint || `api/${moniker_1.default()}`;
30
- // See if we think there's a StepZen schema already
31
- let root;
32
- const existing = [
33
- ...glob.sync('*/**/config.yaml', {
34
- cwd: directory,
35
- ignore: '**/node_modules/**',
36
- }),
37
- ...glob.sync('*/**/index.graphql', {
38
- cwd: directory,
39
- ignore: '**/node_modules/**',
40
- }),
41
- ];
42
- if (existing.length > 0) {
43
- root = existing[0]
44
- .replace('config.yaml', '')
45
- .replace('index.graphql', '')
46
- .replace(/\/*$/, '');
47
- }
48
- const hasRoot = existing.length > 0;
49
74
  // What questions will we ask?
50
75
  const questions = [
76
+ {
77
+ default: name,
78
+ message: 'What would you like to call your workspace?',
79
+ name: 'name',
80
+ validate: (name) => exports.validateWorkspaceName(directory, name),
81
+ when: isHomeDir && !flags.yes,
82
+ },
51
83
  {
52
84
  default: endpoint,
53
85
  message: 'What would you like your endpoint to be called?',
@@ -59,14 +91,22 @@ class Init extends command_1.Command {
59
91
  message: `We have detected a schema in this directory. Set the schema root to "${root}"?`,
60
92
  name: 'use-root',
61
93
  type: 'confirm',
62
- when: hasRoot && !flags.yes,
94
+ when: Boolean(root) && !flags.yes,
63
95
  },
64
96
  ];
65
97
  // Get the answers
66
- const answers = Object.assign({ endpoint, 'use-root': true }, (await inquirer.prompt(questions)));
98
+ const answers = Object.assign({ name,
99
+ endpoint, 'use-root': true }, (await inquirer.prompt(questions)));
100
+ // Append the suggested workspace name to the directory (if running
101
+ // in the HOME directory)
102
+ if (isHomeDir) {
103
+ // eslint-disable-next-line require-atomic-updates
104
+ directory = path.join(directory, answers.name);
105
+ fs.mkdirSync(directory);
106
+ }
67
107
  // Create the workspace
68
108
  const workspace = { endpoint: answers.endpoint };
69
- if (hasRoot && answers['use-root'])
109
+ if (root && answers['use-root'])
70
110
  workspace.root = root;
71
111
  // Write the file
72
112
  const file = path.join(directory, 'stepzen.config.json');
@@ -74,7 +114,7 @@ class Init extends command_1.Command {
74
114
  // Fetch the newly created workspace
75
115
  const created = workspace_1.getWorkspace(directory);
76
116
  // Done!
77
- this.log(`Created ${path.join(created.directory, 'stepzen.config.json')}`);
117
+ this.log(`Created a StepZen workspace in ${created.directory}`);
78
118
  return created;
79
119
  }
80
120
  }
@@ -25,14 +25,23 @@ class Start extends command_1.Command {
25
25
  throw new errors_1.CLIError('You are probably not logged in.');
26
26
  }
27
27
  // Get the working directory and workspace
28
+ let workspace;
28
29
  const directory = utils_1.getDirectory(flags.dir);
29
- let workspace = workspace_1.getWorkspace(directory);
30
- if (!workspace) {
30
+ const maybeWorkspace = workspace_1.getWorkspace(directory);
31
+ if (maybeWorkspace) {
32
+ workspace = maybeWorkspace;
33
+ }
34
+ else {
31
35
  const initArgs = [directory];
32
- if (flags.endpoint)
36
+ if (flags.endpoint) {
33
37
  initArgs.push('--endpoint', flags.endpoint);
34
- // eslint-disable-next-line require-atomic-updates
35
- workspace = await init_1.default.run(initArgs);
38
+ }
39
+ try {
40
+ workspace = await init_1.default.run(initArgs);
41
+ }
42
+ catch (error) {
43
+ throw new errors_1.CLIError(`Could not create a StepZen workspace in the ${flags.dir ? directory : 'current'} directory.\n` + error.message);
44
+ }
36
45
  }
37
46
  // If you have overridden the endpoint, set it
38
47
  if (flags.endpoint) {
@@ -1,4 +1,6 @@
1
1
  import type { Answers, DistinctQuestion } from 'inquirer';
2
+ export declare const createGeneratorFilesDirectly: (id: string, details: any) => Promise<string>;
3
+ export declare const createGeneratorFilesViaStepZen: (id: string, details: any) => Promise<string>;
2
4
  export declare const createGeneratorFiles: (id: string, details: any) => Promise<string>;
3
5
  export declare type GeneratorConfiguration = {
4
6
  questions: DistinctQuestion<Answers>[];
@@ -6,6 +8,8 @@ export declare type GeneratorConfiguration = {
6
8
  errors: Record<string, any>;
7
9
  status: number;
8
10
  };
11
+ export declare const getConfigurationDirectly: (id: string, details?: any) => Promise<GeneratorConfiguration | null>;
12
+ export declare const getConfigurationViaStepZen: (id: string, details?: any) => Promise<GeneratorConfiguration | null>;
9
13
  export declare const getConfiguration: (id: string, details?: any) => Promise<GeneratorConfiguration | null>;
10
14
  export declare const getSchemaList: (arg: string) => string[];
11
15
  export declare const getTemplates: () => Promise<string>;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.askTemplateQuestions = exports.askGeneratorQuestions = exports.getTemplates = exports.getSchemaList = exports.getConfiguration = exports.createGeneratorFiles = void 0;
4
+ exports.askTemplateQuestions = exports.askGeneratorQuestions = exports.getTemplates = exports.getSchemaList = exports.getConfiguration = exports.getConfigurationViaStepZen = exports.getConfigurationDirectly = exports.createGeneratorFiles = exports.createGeneratorFilesViaStepZen = exports.createGeneratorFilesDirectly = void 0;
5
5
  const errors_1 = require("@oclif/errors");
6
6
  const chalk = require("chalk");
7
7
  const debug = require("debug");
@@ -16,10 +16,40 @@ const configuration_1 = require("../shared/configuration");
16
16
  const constants_1 = require("../shared/constants");
17
17
  const errors_2 = require("../shared/errors");
18
18
  const { version } = require('../../package.json');
19
- exports.createGeneratorFiles = async (id, details) => {
19
+ exports.createGeneratorFilesDirectly = async (id, details) => {
20
+ const url = `${constants_1.STEPZEN_DIRECT_GENERATOR_ENGINES_URL}/create`;
21
+ debug('stepzen:generate')(`calling the generator engine ${id} via ${url}`);
22
+ try {
23
+ const response = await node_fetch_1.default(url, {
24
+ body: JSON.stringify({
25
+ id,
26
+ details: JSON.stringify(details.answers),
27
+ }),
28
+ headers: {
29
+ 'Content-Type': 'application/json',
30
+ 'User-Agent': `stepzen-cli/${version}`,
31
+ },
32
+ method: 'POST',
33
+ });
34
+ const files = await response.json();
35
+ const tmp = path.join(os.tmpdir(), `stepzen-generated-schema-${Date.now()}`);
36
+ fs.ensureDirSync(tmp);
37
+ for (const file of files) {
38
+ const dir = path.join(tmp, file.name);
39
+ fs.writeFileSync(dir, file.content);
40
+ }
41
+ return tmp;
42
+ }
43
+ catch (error) {
44
+ debug('stepzen:generate')(error);
45
+ throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
46
+ }
47
+ };
48
+ exports.createGeneratorFilesViaStepZen = async (id, details) => {
20
49
  const config = configuration_1.readConfiguration();
21
50
  const domain = constants_1.STEPZEN_DOMAIN.replace('.io', '.net');
22
51
  const url = `https://${config.account}.${domain}/${constants_1.STEPZEN_GENERATOR_ENGINES_ENDPOINT}/__graphql`;
52
+ debug('stepzen:generate')(`calling the generator engine ${id} via ${url}`);
23
53
  const response = await node_fetch_1.default(url, {
24
54
  body: JSON.stringify({
25
55
  query: `
@@ -51,10 +81,45 @@ exports.createGeneratorFiles = async (id, details) => {
51
81
  }
52
82
  return tmp;
53
83
  };
54
- exports.getConfiguration = async (id, details = {}) => {
84
+ exports.createGeneratorFiles = async (id, details) => {
85
+ const fn = constants_1.STEPZEN_DIRECT_GENERATOR_ENGINES_URL
86
+ ? exports.createGeneratorFilesDirectly
87
+ : exports.createGeneratorFilesViaStepZen;
88
+ return fn(id, details);
89
+ };
90
+ exports.getConfigurationDirectly = async (id, details = {}) => {
91
+ const url = `${constants_1.STEPZEN_DIRECT_GENERATOR_ENGINES_URL}/configure`;
92
+ debug('stepzen:generate')(`calling the generator engine ${id} via ${url}`);
93
+ try {
94
+ const response = await node_fetch_1.default(url, {
95
+ body: JSON.stringify({
96
+ id,
97
+ details: JSON.stringify(details),
98
+ }),
99
+ headers: {
100
+ 'Content-Type': 'application/json',
101
+ 'User-Agent': `stepzen-cli/${version}`,
102
+ },
103
+ method: 'POST',
104
+ });
105
+ const { answers, errors, questions, status } = await response.json();
106
+ return {
107
+ answers: JSON.parse(answers),
108
+ errors: JSON.parse(errors),
109
+ questions: JSON.parse(questions),
110
+ status: JSON.parse(status),
111
+ };
112
+ }
113
+ catch (error) {
114
+ debug('stepzen:generate')(error);
115
+ throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
116
+ }
117
+ };
118
+ exports.getConfigurationViaStepZen = async (id, details = {}) => {
55
119
  const config = configuration_1.readConfiguration();
56
120
  const domain = constants_1.STEPZEN_DOMAIN.replace('.io', '.net');
57
121
  const url = `https://${config.account}.${domain}/${constants_1.STEPZEN_GENERATOR_ENGINES_ENDPOINT}/__graphql`;
122
+ debug('stepzen:generate')(`calling the generator engine ${id} via ${url}`);
58
123
  const response = await node_fetch_1.default(url, {
59
124
  body: JSON.stringify({
60
125
  query: `
@@ -100,6 +165,12 @@ exports.getConfiguration = async (id, details = {}) => {
100
165
  }
101
166
  return data.configure;
102
167
  };
168
+ exports.getConfiguration = async (id, details = {}) => {
169
+ const fn = constants_1.STEPZEN_DIRECT_GENERATOR_ENGINES_URL
170
+ ? exports.getConfigurationDirectly
171
+ : exports.getConfigurationViaStepZen;
172
+ return fn(id, details);
173
+ };
103
174
  exports.getSchemaList = (arg) => {
104
175
  const schemas = arg.split(',').map((schema) => schema.trim());
105
176
  // Make sure they're unique, otherwise, complain
@@ -5,6 +5,7 @@ export declare const STEPZEN_DOMAIN: string;
5
5
  export declare const STEPZEN_SERVER_URL: string;
6
6
  export declare const STEPZEN_GENERATOR_ENGINES_SCHEMA: string;
7
7
  export declare const STEPZEN_GENERATOR_ENGINES_ENDPOINT = "stepzen-generator/engines";
8
+ export declare const STEPZEN_DIRECT_GENERATOR_ENGINES_URL: string | undefined;
8
9
  export declare const STEPZEN_API_TEMPLATES_REPOSITORY = "https://github.com/steprz/stepzen-schemas";
9
10
  export declare const ADMIN_DEPLOY_URL = "/cli/admin/deploy";
10
11
  export declare const ADMIN_LIST_URL = "/cli/admin/list";
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.STEPZEN_DISCORD_URL = exports.STEPZEN_JSON2SDL_SERVER_URL = exports.ADMIN_VERIFY_URL = exports.ADMIN_UPLOAD_URL = exports.ADMIN_LIST_URL = exports.ADMIN_DEPLOY_URL = exports.STEPZEN_API_TEMPLATES_REPOSITORY = exports.STEPZEN_GENERATOR_ENGINES_ENDPOINT = exports.STEPZEN_GENERATOR_ENGINES_SCHEMA = exports.STEPZEN_SERVER_URL = exports.STEPZEN_DOMAIN = exports.STEPZEN_CONFIG_FILE = exports.STEPZEN_LAST_UPDATE_CHECK_TIMESTAMP = exports.STEPZEN_CONFIG_DIRECTORY = void 0;
4
+ exports.STEPZEN_DISCORD_URL = exports.STEPZEN_JSON2SDL_SERVER_URL = exports.ADMIN_VERIFY_URL = exports.ADMIN_UPLOAD_URL = exports.ADMIN_LIST_URL = exports.ADMIN_DEPLOY_URL = exports.STEPZEN_API_TEMPLATES_REPOSITORY = exports.STEPZEN_DIRECT_GENERATOR_ENGINES_URL = exports.STEPZEN_GENERATOR_ENGINES_ENDPOINT = exports.STEPZEN_GENERATOR_ENGINES_SCHEMA = exports.STEPZEN_SERVER_URL = exports.STEPZEN_DOMAIN = exports.STEPZEN_CONFIG_FILE = exports.STEPZEN_LAST_UPDATE_CHECK_TIMESTAMP = exports.STEPZEN_CONFIG_DIRECTORY = void 0;
5
5
  // This file contains constants and all magic strings
6
6
  const dotenv = require("dotenv");
7
7
  const os = require("os");
@@ -9,7 +9,7 @@ const path = require("path");
9
9
  // This allows you to set environment variables in a `.env` file.
10
10
  // This file needs to be in your working directory.
11
11
  dotenv.config();
12
- const { STEPZEN_CONFIG_FILE: ENV_VAR_STEPZEN_CONFIG_FILE, STEPZEN_DOMAIN: ENV_VAR_STEPZEN_DOMAIN, STEPZEN_GENERATOR_ENGINES_SCHEMA: ENV_VAR_STEPZEN_GENERATOR_ENGINES_SCHEMA, STEPZEN_SERVER_URL: ENV_VAR_STEPZEN_SERVER_URL, STEPZEN_JSON2SDL_SERVER_URL: ENV_VAR_STEPZEN_JSON2SDL_SERVER_URL, } = process.env;
12
+ const { STEPZEN_CONFIG_FILE: ENV_VAR_STEPZEN_CONFIG_FILE, STEPZEN_DOMAIN: ENV_VAR_STEPZEN_DOMAIN, STEPZEN_GENERATOR_ENGINES_SCHEMA: ENV_VAR_STEPZEN_GENERATOR_ENGINES_SCHEMA, STEPZEN_SERVER_URL: ENV_VAR_STEPZEN_SERVER_URL, STEPZEN_JSON2SDL_SERVER_URL: ENV_VAR_STEPZEN_JSON2SDL_SERVER_URL, STEPZEN_DIRECT_GENERATOR_ENGINES_URL: ENV_VAR_STEPZEN_DIRECT_GENERATOR_ENGINES_URL, } = process.env;
13
13
  // Where your authentication details are stored locally
14
14
  exports.STEPZEN_CONFIG_DIRECTORY = path.join(os.homedir(), '.stepzen');
15
15
  exports.STEPZEN_LAST_UPDATE_CHECK_TIMESTAMP = path.join(exports.STEPZEN_CONFIG_DIRECTORY, 'last_update_check.timestamp');
@@ -23,6 +23,8 @@ exports.STEPZEN_SERVER_URL = ENV_VAR_STEPZEN_SERVER_URL || 'https://{account}.st
23
23
  exports.STEPZEN_GENERATOR_ENGINES_SCHEMA = ENV_VAR_STEPZEN_GENERATOR_ENGINES_SCHEMA || 'stepzen/engines';
24
24
  // Generator Engines schema: target API endpoint
25
25
  exports.STEPZEN_GENERATOR_ENGINES_ENDPOINT = 'stepzen-generator/engines';
26
+ // If defined, call the generator engines cloud function directly, skipping zenctl
27
+ exports.STEPZEN_DIRECT_GENERATOR_ENGINES_URL = ENV_VAR_STEPZEN_DIRECT_GENERATOR_ENGINES_URL;
26
28
  // API Templates repository
27
29
  exports.STEPZEN_API_TEMPLATES_REPOSITORY = 'https://github.com/steprz/stepzen-schemas';
28
30
  // Paths to the endpoints used (only for tests)
@@ -1,2 +1,4 @@
1
+ export declare const getRandomAnimal: () => string;
2
+ export declare const getRandomDescriptor: () => string;
1
3
  declare const _default: () => string;
2
4
  export default _default;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getRandomDescriptor = exports.getRandomAnimal = void 0;
4
5
  const lodash_1 = require("lodash");
5
6
  const animals = [
6
7
  'aardvark',
@@ -637,8 +638,6 @@ const descriptors = [
637
638
  'zinc',
638
639
  'zooming',
639
640
  ];
640
- exports.default = () => {
641
- const animal = animals[lodash_1.random(0, animals.length - 1)];
642
- const descriptor = descriptors[lodash_1.random(0, descriptors.length - 1)];
643
- return `${descriptor}-${animal}`;
644
- };
641
+ exports.getRandomAnimal = () => animals[lodash_1.random(0, animals.length - 1)];
642
+ exports.getRandomDescriptor = () => descriptors[lodash_1.random(0, descriptors.length - 1)];
643
+ exports.default = () => `${exports.getRandomDescriptor()}-${exports.getRandomAnimal()}`;
@@ -3,11 +3,20 @@ export interface MachineConfiguration {
3
3
  adminkey: string;
4
4
  apikey?: string;
5
5
  }
6
+ /**
7
+ * On-disk representation of a workspace config file, `stepzen.config.json`
8
+ */
6
9
  export interface WorkspaceConfiguration {
7
- directory?: string;
8
10
  endpoint: string;
9
11
  root?: string;
10
- schema?: string;
12
+ }
13
+ /**
14
+ * In-memory representation of a workspace config
15
+ */
16
+ export interface Workspace {
17
+ endpoint: string;
18
+ directory: string;
19
+ schema: string;
11
20
  }
12
21
  export interface ZenCtlResponse {
13
22
  errors?: Array<string>;
@@ -68,11 +68,12 @@ exports.ensureApiKey = async () => {
68
68
  exports.getDirectory = (d = process.cwd()) => {
69
69
  let directory = d;
70
70
  // If it starts with `~`, expand this
71
- if (directory.startsWith('~'))
71
+ if (directory.startsWith('~')) {
72
72
  directory = directory.replace('~', os.homedir());
73
- // If it does not now start with `/`, then get the absolute path
74
- if (directory.startsWith('/') === false)
75
- directory = path.resolve(directory);
73
+ }
74
+ // If it does not now start with `/`, then get the absolute path.
75
+ // Also, remove any `..` and `.` segments.
76
+ directory = path.resolve(directory);
76
77
  // If the path does not exist, throw an error
77
78
  if (!fs.existsSync(directory)) {
78
79
  throw new errors_1.CLIError(`Cannot find ${directory}`);
@@ -1 +1,2 @@
1
- export declare const getWorkspace: (directory: string) => any;
1
+ import { Workspace } from './types';
2
+ export declare const getWorkspace: (directory: string) => Workspace | null;
@@ -4,50 +4,53 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.getWorkspace = void 0;
5
5
  const errors_1 = require("@oclif/errors");
6
6
  const fs = require("fs");
7
+ const os = require("os");
7
8
  const path = require("path");
8
9
  const utils_1 = require("./utils");
9
10
  exports.getWorkspace = (directory) => {
10
- let workspace = {
11
- directory: '',
12
- endpoint: '',
13
- root: '',
14
- schema: '',
15
- };
16
- // Loop through this and all parent directories to find the root
17
- let parts = directory.split('/');
18
- do {
19
- const d = `${parts.join('/')}`.replace(/\/*$/, '');
11
+ let workspaceRoot;
12
+ const parts = path
13
+ // Remove trailing slashes and any `..` and `.` segments
14
+ .resolve(directory)
15
+ // split into segments
16
+ .split(path.sep);
17
+ // Loop through this and all parent directories up to $HOME to find the root
18
+ for (; parts.length > 0; parts.pop()) {
19
+ const d = parts.join(path.sep);
20
20
  const f = path.join(d, 'stepzen.config.json');
21
21
  if (fs.existsSync(f)) {
22
- workspace.directory = d;
23
- parts = [];
24
- }
25
- parts.pop();
26
- } while (parts.length > 0);
27
- // If it's found a workspace, then parse the configuration file
28
- if (workspace.directory) {
29
- const filepath = path.join(workspace.directory, 'stepzen.config.json');
30
- const file = fs.readFileSync(filepath, 'utf8');
31
- try {
32
- const config = JSON.parse(file);
33
- workspace = Object.assign(Object.assign({}, workspace), config);
22
+ workspaceRoot = d;
23
+ break;
34
24
  }
35
- catch (_a) {
36
- throw new errors_1.CLIError(`Cannot parse configuration from ${filepath}`);
25
+ if (d === os.homedir()) {
26
+ break;
37
27
  }
38
28
  }
39
- // Add the 'schema' property, which is the directory + the 'root'
40
- workspace.schema = path.join(workspace.directory, workspace.root);
29
+ if (!workspaceRoot) {
30
+ return null;
31
+ }
32
+ const filepath = path.join(workspaceRoot, 'stepzen.config.json');
33
+ const file = fs.readFileSync(filepath, 'utf8');
34
+ let config;
35
+ try {
36
+ config = JSON.parse(file);
37
+ }
38
+ catch (_a) {
39
+ throw new errors_1.CLIError(`Cannot parse configuration from ${filepath}`);
40
+ }
41
41
  // Validate the workspace
42
- if (workspace.directory) {
43
- const error = utils_1.validateEndpoint(workspace.endpoint);
44
- if (typeof error === 'string')
45
- throw new errors_1.CLIError(error);
46
- const schemaExists = fs.existsSync(workspace.schema);
47
- if (!schemaExists)
48
- throw new errors_1.CLIError(`Cannot find workspace schema folder ${workspace.schema}`);
49
- return workspace;
42
+ const error = utils_1.validateEndpoint(config.endpoint);
43
+ if (typeof error === 'string') {
44
+ throw new errors_1.CLIError(error);
45
+ }
46
+ // Add the 'schema' property, which is the directory + the 'root'
47
+ const schema = path.join(workspaceRoot, config.root || '');
48
+ if (!fs.existsSync(schema)) {
49
+ throw new errors_1.CLIError(`Cannot find workspace schema folder ${schema}`);
50
50
  }
51
- // This is not a workspace folder
52
- return false;
51
+ return {
52
+ directory: workspaceRoot,
53
+ endpoint: config.endpoint,
54
+ schema: schema,
55
+ };
53
56
  };
@@ -1 +1 @@
1
- {"version":"0.11.0","commands":{"deploy":{"id":"deploy","description":"deploy to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"configurationsets":{"name":"configurationsets","type":"option","description":"Configurationsets to use","default":""},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"schema":{"name":"schema","type":"option","description":"Schema to use","required":true},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"destination","description":"destination","required":true}]},"import":{"id":"import","description":"Import a schema for an external data source or a API endpoint to your GraphQL API.","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"working directory"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"silent":{"name":"silent","type":"boolean","hidden":true,"allowNo":false},"name":{"name":"name","type":"option","description":"subfolder inside the workspace folder to save the imported schema files, defaults to the imported schema name"},"prefix":{"name":"prefix","type":"option","description":"[curl] prefix to add every type in the generated schema."},"query-name":{"name":"query-name","type":"option","description":"[curl] property name to add to the Query type as a way to access the imported cURL endpoint."},"query-type":{"name":"query-type","type":"option","description":"[curl] name for the type returned by the cURL endpoint in the generated schema. The name specified by --query-type is not prefixed by --prefix if both flags are present."},"path-params":{"name":"path-params","type":"option","description":"[curl] specifies path parameters in the URL path. Can be formed by taking the original path and replacing the variable segments with $paramName placeholders.\n\nExample:\nstepzen import curl https://example.com/users/jane/posts/12 --path-params '/users/$userId/posts/$postId'"}},"args":[{"name":"schemas","required":true}]},"init":{"id":"init","description":"stepzen init","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"endpoint":{"name":"endpoint","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"yes":{"name":"yes","type":"boolean","hidden":true,"allowNo":false}},"args":[{"name":"directory","hidden":true}]},"lint":{"id":"lint","description":"stepzen lint","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"dir":{"name":"dir","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"list":{"id":"list","description":"list your items","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"type","description":"type","required":true,"options":["configurationsets","schemas"]}]},"login":{"id":"login","description":"log in to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"account":{"name":"account","type":"option","char":"a","hidden":true},"adminkey":{"name":"adminkey","type":"option","char":"k","hidden":true},"config":{"name":"config","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"logout":{"id":"logout","description":"log out of stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"start":{"id":"start","description":"upload and deploy your schema","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"working directory"},"endpoint":{"name":"endpoint","type":"option","description":"Override workspace endpoint"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"no-console":{"name":"no-console","type":"boolean","hidden":true,"allowNo":false},"no-dashboard":{"name":"no-dashboard","type":"boolean","hidden":true,"allowNo":false},"no-init":{"name":"no-init","type":"boolean","hidden":true,"allowNo":false},"no-server":{"name":"no-server","type":"boolean","hidden":true,"allowNo":false},"no-validate":{"name":"no-validate","type":"boolean","hidden":true,"allowNo":false},"no-watcher":{"name":"no-watcher","type":"boolean","hidden":true,"allowNo":false},"port":{"name":"port","type":"option","default":5001}},"args":[]},"transpile":{"id":"transpile","description":"transpile a graphql schema","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"config":{"name":"config","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"hide-output":{"name":"hide-output","type":"boolean","hidden":true,"allowNo":false},"inspect":{"name":"inspect","type":"boolean","char":"i","hidden":true,"allowNo":false},"inspect-after":{"name":"inspect-after","type":"boolean","hidden":true,"allowNo":false},"output-configuration":{"name":"output-configuration","type":"boolean","allowNo":false},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"folder","required":true}]},"upload":{"id":"upload","description":"upload to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"A directory to upload"},"file":{"name":"file","type":"option","description":"A file to upload"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"type","description":"type","required":true,"options":["configurationset","schema"]},{"name":"destination","description":"destination","required":true}]},"validate":{"id":"validate","description":"validate a graphql schema","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"folder","required":true}]},"whoami":{"id":"whoami","description":"stepzen whoami","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"showkeys":{"name":"showkeys","type":"boolean","allowNo":false},"apikey":{"name":"apikey","type":"boolean","allowNo":false},"adminkey":{"name":"adminkey","type":"boolean","allowNo":false}},"args":[]}}}
1
+ {"version":"0.13.0-beta.0","commands":{"deploy":{"id":"deploy","description":"deploy to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"configurationsets":{"name":"configurationsets","type":"option","description":"Configurationsets to use","default":""},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"schema":{"name":"schema","type":"option","description":"Schema to use","required":true},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"destination","description":"destination","required":true}]},"import":{"id":"import","description":"Import a schema for an external data source or a API endpoint to your GraphQL API.","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"working directory"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"silent":{"name":"silent","type":"boolean","hidden":true,"allowNo":false},"name":{"name":"name","type":"option","description":"subfolder inside the workspace folder to save the imported schema files, defaults to the imported schema name"},"prefix":{"name":"prefix","type":"option","description":"[curl] prefix to add every type in the generated schema."},"query-name":{"name":"query-name","type":"option","description":"[curl] property name to add to the Query type as a way to access the imported cURL endpoint."},"query-type":{"name":"query-type","type":"option","description":"[curl] name for the type returned by the cURL endpoint in the generated schema. The name specified by --query-type is not prefixed by --prefix if both flags are present."},"path-params":{"name":"path-params","type":"option","description":"[curl] specifies path parameters in the URL path. Can be formed by taking the original path and replacing the variable segments with $paramName placeholders.\n\nExample:\nstepzen import curl https://example.com/users/jane/posts/12 --path-params '/users/$userId/posts/$postId'"}},"args":[{"name":"schemas","required":true}]},"init":{"id":"init","description":"stepzen init","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"endpoint":{"name":"endpoint","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"yes":{"name":"yes","type":"boolean","hidden":true,"allowNo":false}},"args":[{"name":"directory","hidden":true}]},"lint":{"id":"lint","description":"stepzen lint","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"dir":{"name":"dir","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"list":{"id":"list","description":"list your items","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"type","description":"type","required":true,"options":["configurationsets","schemas"]}]},"login":{"id":"login","description":"log in to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"account":{"name":"account","type":"option","char":"a","hidden":true},"adminkey":{"name":"adminkey","type":"option","char":"k","hidden":true},"config":{"name":"config","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"logout":{"id":"logout","description":"log out of stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"start":{"id":"start","description":"upload and deploy your schema","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"working directory"},"endpoint":{"name":"endpoint","type":"option","description":"Override workspace endpoint"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"no-console":{"name":"no-console","type":"boolean","hidden":true,"allowNo":false},"no-dashboard":{"name":"no-dashboard","type":"boolean","hidden":true,"allowNo":false},"no-init":{"name":"no-init","type":"boolean","hidden":true,"allowNo":false},"no-server":{"name":"no-server","type":"boolean","hidden":true,"allowNo":false},"no-validate":{"name":"no-validate","type":"boolean","hidden":true,"allowNo":false},"no-watcher":{"name":"no-watcher","type":"boolean","hidden":true,"allowNo":false},"port":{"name":"port","type":"option","default":5001}},"args":[]},"transpile":{"id":"transpile","description":"transpile a graphql schema","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"config":{"name":"config","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"hide-output":{"name":"hide-output","type":"boolean","hidden":true,"allowNo":false},"inspect":{"name":"inspect","type":"boolean","char":"i","hidden":true,"allowNo":false},"inspect-after":{"name":"inspect-after","type":"boolean","hidden":true,"allowNo":false},"output-configuration":{"name":"output-configuration","type":"boolean","allowNo":false},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"folder","required":true}]},"upload":{"id":"upload","description":"upload to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"A directory to upload"},"file":{"name":"file","type":"option","description":"A file to upload"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"type","description":"type","required":true,"options":["configurationset","schema"]},{"name":"destination","description":"destination","required":true}]},"validate":{"id":"validate","description":"validate a graphql schema","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"folder","required":true}]},"whoami":{"id":"whoami","description":"stepzen whoami","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"showkeys":{"name":"showkeys","type":"boolean","allowNo":false},"apikey":{"name":"apikey","type":"boolean","allowNo":false},"adminkey":{"name":"adminkey","type":"boolean","allowNo":false}},"args":[]}}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "stepzen",
3
3
  "description": "The StepZen CLI",
4
- "version": "0.11.0",
4
+ "version": "0.13.0-beta.0",
5
5
  "license": "MIT",
6
6
  "author": "Darren Waddell <darren@stepzen.com>",
7
7
  "contributors": [
@@ -79,6 +79,7 @@
79
79
  "@types/unzipper": "^0.10.4",
80
80
  "chai": "^4.2.0",
81
81
  "copyfiles": "^2.4.1",
82
+ "cross-env": "^7.0.3",
82
83
  "eslint": "7.11.0",
83
84
  "eslint-config-oclif": "3.1.0",
84
85
  "eslint-config-oclif-typescript": "0.1.0",
@@ -118,9 +119,9 @@
118
119
  "postpack": "rm -f oclif.manifest.json",
119
120
  "posttest": "eslint . --ignore-path .prettierignore",
120
121
  "prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
121
- "test": "tsc -b && STEPZEN_DOMAIN=steprz.io STEPZEN_SERVER_URL=http://localhost nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
122
+ "test": "tsc -b && cross-env STEPZEN_DOMAIN=steprz.io STEPZEN_SERVER_URL=http://localhost nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
122
123
  "test:single:example": "npm run test:single -- test/src/shared/curl-parser.test.ts",
123
- "test:single": "tsc -b && STEPZEN_DOMAIN=steprz.io STEPZEN_SERVER_URL=http://localhost mocha --forbid-only test/_global.test.ts",
124
+ "test:single": "tsc -b && cross-env STEPZEN_DOMAIN=steprz.io STEPZEN_SERVER_URL=http://localhost mocha --forbid-only test/_global.test.ts",
124
125
  "version": "oclif-dev readme && git add README.md",
125
126
  "prepare": "husky install"
126
127
  },