@cedarjs/cli 1.0.0-canary.12969 → 1.0.0-canary.12970

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.
@@ -1,4 +1,3 @@
1
- import path from "path";
2
1
  import camelcase from "camelcase";
3
2
  import execa from "execa";
4
3
  import { Listr } from "listr2";
@@ -6,11 +5,7 @@ import prompts from "prompts";
6
5
  import { recordTelemetryAttributes } from "@cedarjs/cli-helpers";
7
6
  import { getConfig } from "@cedarjs/project-config";
8
7
  import c from "../../../lib/colors.js";
9
- import {
10
- getPaths,
11
- writeFilesTask,
12
- transformTSToJS
13
- } from "../../../lib/index.js";
8
+ import { writeFilesTask, transformTSToJS } from "../../../lib/index.js";
14
9
  import {
15
10
  prepareForRollback,
16
11
  addFunctionToRollback
@@ -25,28 +20,23 @@ const files = async ({ name, typescript = false, type, tests }) => {
25
20
  throw new Error("You must specify a directive type");
26
21
  }
27
22
  const camelName = camelcase(name);
28
- const outputFilename = `${camelName}.${typescript ? "ts" : "js"}`;
23
+ const extension = typescript ? ".ts" : ".js";
29
24
  const directiveFile = await templateForComponentFile({
30
25
  name,
31
- extension: typescript ? ".ts" : ".js",
26
+ extension,
32
27
  generator: "directive",
28
+ apiPathSection: "directives",
33
29
  templatePath: `${type}.directive.ts.template`,
34
- outputPath: path.join(getPaths().api.directives, camelName, outputFilename),
35
30
  templateVars: { camelName }
36
31
  });
37
32
  const files2 = [directiveFile];
38
33
  if (tests) {
39
- const testOutputFilename = `${camelcase(name)}.test.${typescript ? "ts" : "js"}`;
40
34
  const testFile = await templateForComponentFile({
41
35
  name,
42
- extension: typescript ? ".test.ts" : ".test.js",
36
+ extension: `.test${extension}`,
43
37
  generator: "directive",
38
+ apiPathSection: "directives",
44
39
  templatePath: `${type}.directive.test.ts.template`,
45
- outputPath: path.join(
46
- getPaths().api.directives,
47
- camelName,
48
- testOutputFilename
49
- ),
50
40
  templateVars: { camelName }
51
41
  });
52
42
  files2.push(testFile);
@@ -1,15 +1,9 @@
1
- import path from "path";
2
- import camelcase from "camelcase";
3
1
  import { Listr } from "listr2";
4
2
  import { terminalLink } from "termi-link";
5
3
  import { recordTelemetryAttributes } from "@cedarjs/cli-helpers";
6
4
  import { errorTelemetry } from "@cedarjs/telemetry";
7
5
  import c from "../../../lib/colors.js";
8
- import {
9
- getPaths,
10
- transformTSToJS,
11
- writeFilesTask
12
- } from "../../../lib/index.js";
6
+ import { transformTSToJS, writeFilesTask } from "../../../lib/index.js";
13
7
  import { prepareForRollback } from "../../../lib/rollback.js";
14
8
  import { validateName } from "../helpers.js";
15
9
  import { templateForComponentFile } from "../yargsHandlerHelpers.js";
@@ -20,51 +14,32 @@ const files = async ({
20
14
  ...rest
21
15
  }) => {
22
16
  const extension = generateTypescript ? ".ts" : ".js";
23
- const functionName = camelcase(name);
24
17
  const outputFiles = [];
25
18
  const functionFiles = await templateForComponentFile({
26
- name: functionName,
27
- componentName: functionName,
19
+ name,
28
20
  extension,
29
21
  apiPathSection: "functions",
30
22
  generator: "function",
31
23
  templatePath: "function.ts.template",
32
- templateVars: { ...rest, typescript: generateTypescript },
33
- outputPath: path.join(
34
- getPaths().api.functions,
35
- functionName,
36
- `${functionName}${extension}`
37
- )
24
+ templateVars: { ...rest, typescript: generateTypescript }
38
25
  });
39
26
  outputFiles.push(functionFiles);
40
27
  if (generateTests) {
41
28
  const testFile = await templateForComponentFile({
42
- name: functionName,
43
- componentName: functionName,
44
- extension,
29
+ name,
30
+ extension: `.test${extension}`,
45
31
  apiPathSection: "functions",
46
32
  generator: "function",
47
33
  templatePath: "test.ts.template",
48
- templateVars: { ...rest },
49
- outputPath: path.join(
50
- getPaths().api.functions,
51
- functionName,
52
- `${functionName}.test${extension}`
53
- )
34
+ templateVars: { ...rest }
54
35
  });
55
36
  const scenarioFile = await templateForComponentFile({
56
- name: functionName,
57
- componentName: functionName,
58
- extension,
37
+ name,
38
+ extension: `.scenarios${extension}`,
59
39
  apiPathSection: "functions",
60
40
  generator: "function",
61
41
  templatePath: "scenarios.ts.template",
62
- templateVars: { ...rest },
63
- outputPath: path.join(
64
- getPaths().api.functions,
65
- functionName,
66
- `${functionName}.scenarios${extension}`
67
- )
42
+ templateVars: { ...rest }
68
43
  });
69
44
  outputFiles.push(testFile);
70
45
  outputFiles.push(scenarioFile);
@@ -19,7 +19,7 @@ import { logger } from 'src/lib/logger'
19
19
  * function, and execution environment.
20
20
  */
21
21
  export const handler = async (event: APIGatewayEvent, _context: Context) => {
22
- logger.info(`<%= '$' %>{event.httpMethod} <%= '$' %>{event.path}: <%= name %> function`)
22
+ logger.info(`<%= '$' %>{event.httpMethod} <%= '$' %>{event.path}: <%= camelName %> function`)
23
23
 
24
24
  return {
25
25
  statusCode: 200,
@@ -27,7 +27,7 @@ export const handler = async (event: APIGatewayEvent, _context: Context) => {
27
27
  'Content-Type': 'application/json',
28
28
  },
29
29
  body: JSON.stringify({
30
- data: '${name} function',
30
+ data: '${camelName} function',
31
31
  }),
32
32
  }
33
33
  }
@@ -13,70 +13,61 @@ import {
13
13
  } from "../../../lib/index.js";
14
14
  import { prepareForRollback } from "../../../lib/rollback.js";
15
15
  import { validateName } from "../helpers.js";
16
- import { templateForComponentFile } from "../yargsHandlerHelpers.js";
16
+ import { templateForFile } from "../yargsHandlerHelpers.js";
17
17
  const normalizeName = (name) => {
18
18
  return changeCase.pascalCase(name).replace(/Job$/, "");
19
19
  };
20
20
  const files = async ({
21
21
  name,
22
22
  queueName,
23
- typescript: generateTypescript,
23
+ typescript,
24
24
  tests: generateTests = true,
25
25
  ...rest
26
26
  }) => {
27
- const extension = generateTypescript ? ".ts" : ".js";
28
- const outputFiles = [];
29
27
  const jobName = normalizeName(name);
30
- const jobFiles = await templateForComponentFile({
28
+ const componentName = `${jobName}Job`;
29
+ const extension = typescript ? ".ts" : ".js";
30
+ const jobFiles = await templateForFile({
31
31
  name: jobName,
32
- componentName: jobName,
33
- extension,
34
- apiPathSection: "jobs",
32
+ side: "api",
33
+ sidePathSection: "jobs",
35
34
  generator: "job",
35
+ outputPath: path.join(componentName, componentName + extension),
36
36
  templatePath: "job.ts.template",
37
- templateVars: { name: jobName, queueName, ...rest },
38
- outputPath: path.join(
39
- getPaths().api.jobs,
40
- `${jobName}Job`,
41
- `${jobName}Job${extension}`
42
- )
37
+ // TODO: Remove `name` here. It's already passed to the template by the
38
+ // helper function we're using
39
+ templateVars: { name: jobName, queueName, ...rest }
43
40
  });
41
+ const outputFiles = [];
44
42
  outputFiles.push(jobFiles);
45
43
  if (generateTests) {
46
- const testFile = await templateForComponentFile({
44
+ const testFile = await templateForFile({
47
45
  name: jobName,
48
- componentName: jobName,
49
- extension,
50
- apiPathSection: "jobs",
46
+ side: "api",
47
+ sidePathSection: "jobs",
51
48
  generator: "job",
49
+ outputPath: path.join(componentName, componentName + `.test${extension}`),
52
50
  templatePath: "test.ts.template",
53
- templateVars: { ...rest },
54
- outputPath: path.join(
55
- getPaths().api.jobs,
56
- `${jobName}Job`,
57
- `${jobName}Job.test${extension}`
58
- )
51
+ templateVars: { ...rest }
59
52
  });
60
- const scenarioFile = await templateForComponentFile({
53
+ const scenarioFile = await templateForFile({
61
54
  name: jobName,
62
- componentName: jobName,
63
- extension,
64
- apiPathSection: "jobs",
55
+ side: "api",
56
+ sidePathSection: "jobs",
65
57
  generator: "job",
66
- templatePath: "scenarios.ts.template",
67
- templateVars: { ...rest },
68
58
  outputPath: path.join(
69
- getPaths().api.jobs,
70
- `${jobName}Job`,
71
- `${jobName}Job.scenarios${extension}`
72
- )
59
+ componentName,
60
+ componentName + `.scenarios${extension}`
61
+ ),
62
+ templatePath: "scenarios.ts.template",
63
+ templateVars: { ...rest }
73
64
  });
74
65
  outputFiles.push(testFile);
75
66
  outputFiles.push(scenarioFile);
76
67
  }
77
68
  return outputFiles.reduce(async (accP, [outputPath, content]) => {
78
69
  const acc = await accP;
79
- const template = generateTypescript ? content : await transformTSToJS(outputPath, content);
70
+ const template = typescript ? content : await transformTSToJS(outputPath, content);
80
71
  return {
81
72
  [outputPath]: template,
82
73
  ...acc
@@ -1,4 +1,3 @@
1
- import path from "path";
2
1
  import ansis from "ansis";
3
2
  import boxen from "boxen";
4
3
  import camelcase from "camelcase";
@@ -8,12 +7,7 @@ import { generate as generateTypes } from "@cedarjs/internal/dist/generate/gener
8
7
  import { getConfig } from "@cedarjs/project-config";
9
8
  import { errorTelemetry } from "@cedarjs/telemetry";
10
9
  import c from "../../../lib/colors.js";
11
- import {
12
- generateTemplate,
13
- transformTSToJS,
14
- getPaths,
15
- writeFilesTask
16
- } from "../../../lib/index.js";
10
+ import { transformTSToJS, writeFilesTask } from "../../../lib/index.js";
17
11
  import {
18
12
  prepareForRollback,
19
13
  addFunctionToRollback
@@ -26,7 +20,7 @@ import {
26
20
  } from "../../../lib/schemaHelpers.js";
27
21
  import { relationsForModel } from "../helpers.js";
28
22
  import { files as serviceFiles } from "../service/serviceHandler.js";
29
- import { customOrDefaultTemplatePath } from "../yargsHandlerHelpers.js";
23
+ import { templateForFile } from "../yargsHandlerHelpers.js";
30
24
  const DEFAULT_IGNORE_FIELDS_FOR_INPUT = ["createdAt", "updatedAt"];
31
25
  const missingIdConsoleMessage = () => {
32
26
  const line1 = ansis.bold.yellow("WARNING") + ": Cannot generate CRUD SDL without an `@id` database column.";
@@ -164,52 +158,25 @@ const files = async ({
164
158
  tests,
165
159
  typescript
166
160
  }) => {
167
- const {
168
- modelName,
169
- modelDescription,
170
- query,
171
- createInput,
172
- updateInput,
173
- idInput,
174
- idType: idType2,
175
- idName: idName2,
176
- relations,
177
- enums
178
- } = await sdlFromSchemaModel(name, crud, docs);
179
- const templatePath = customOrDefaultTemplatePath({
161
+ const extension = typescript ? "ts" : "js";
162
+ const sdlData = await sdlFromSchemaModel(name, crud, docs);
163
+ const [outputPath, content] = await templateForFile({
164
+ name,
180
165
  side: "api",
166
+ sidePathSection: "graphql",
181
167
  generator: "sdl",
182
- templatePath: "sdl.ts.template"
183
- });
184
- let template = await generateTemplate(templatePath, {
185
- docs,
186
- modelName,
187
- modelDescription,
188
- name,
189
- crud,
190
- query,
191
- createInput,
192
- updateInput,
193
- idInput,
194
- idType: idType2,
195
- idName: idName2,
196
- enums
168
+ templatePath: "sdl.ts.template",
169
+ templateVars: { docs, name, crud, ...sdlData },
170
+ outputPath: `${camelcase(pluralize(name))}.sdl.${extension}`
197
171
  });
198
- const extension = typescript ? "ts" : "js";
199
- let outputPath = path.join(
200
- getPaths().api.graphql,
201
- `${camelcase(pluralize(name))}.sdl.${extension}`
202
- );
203
- if (typescript) {
204
- template = await transformTSToJS(outputPath, template);
205
- }
172
+ const template = typescript ? content : await transformTSToJS(outputPath, content);
206
173
  return {
207
174
  [outputPath]: template,
208
175
  ...await serviceFiles({
209
176
  name,
210
177
  crud,
211
178
  tests,
212
- relations,
179
+ relations: sdlData.relations,
213
180
  typescript
214
181
  })
215
182
  };
@@ -1,12 +1,10 @@
1
+ import path from "node:path";
1
2
  import camelcase from "camelcase";
2
3
  import { transformTSToJS } from "../../../lib/index.js";
3
4
  import { pluralize, singularize } from "../../../lib/rwPluralize.js";
4
5
  import { getSchema, verifyModelName } from "../../../lib/schemaHelpers.js";
5
6
  import { relationsForModel } from "../helpers.js";
6
- import {
7
- createHandler,
8
- templateForComponentFile
9
- } from "../yargsHandlerHelpers.js";
7
+ import { createHandler, templateForFile } from "../yargsHandlerHelpers.js";
10
8
  const DEFAULT_SCENARIO_NAMES = ["one", "two"];
11
9
  const parseSchema = async (model) => {
12
10
  const schema = await getSchema(model);
@@ -221,24 +219,23 @@ const files = async ({
221
219
  const componentName = camelcase(pluralize(name));
222
220
  const model = name;
223
221
  const idName = await getIdName(model);
224
- const extension = "ts";
225
222
  const modelRelations = relations || relationsForModel(await getSchema(model));
226
- const serviceFile = await templateForComponentFile({
223
+ const serviceFile = await templateForFile({
227
224
  name,
228
- componentName,
229
- extension: `.${extension}`,
230
- apiPathSection: "services",
225
+ side: "api",
226
+ sidePathSection: "services",
231
227
  generator: "service",
232
- templatePath: `service.${extension}.template`,
228
+ outputPath: path.join(componentName, componentName + ".ts"),
229
+ templatePath: "service.ts.template",
233
230
  templateVars: { relations: modelRelations, idName, ...rest }
234
231
  });
235
- const testFile = await templateForComponentFile({
232
+ const testFile = await templateForFile({
236
233
  name,
237
- componentName,
238
- extension: `.test.${extension}`,
239
- apiPathSection: "services",
234
+ side: "api",
235
+ sidePathSection: "services",
240
236
  generator: "service",
241
- templatePath: `test.${extension}.template`,
237
+ outputPath: path.join(componentName, componentName + ".test.ts"),
238
+ templatePath: "test.ts.template",
242
239
  templateVars: {
243
240
  relations: relations || [],
244
241
  create: await fieldsToInput(model),
@@ -252,13 +249,13 @@ const files = async ({
252
249
  ...rest
253
250
  }
254
251
  });
255
- const scenariosFile = await templateForComponentFile({
252
+ const scenariosFile = await templateForFile({
256
253
  name,
257
- componentName,
258
- extension: `.scenarios.${extension}`,
259
- apiPathSection: "services",
254
+ side: "api",
255
+ sidePathSection: "services",
260
256
  generator: "service",
261
- templatePath: `scenarios.${extension}.template`,
257
+ outputPath: path.join(componentName, componentName + ".scenarios.ts"),
258
+ templatePath: "scenarios.ts.template",
262
259
  templateVars: {
263
260
  scenario: await buildScenario(model),
264
261
  stringifiedScenario: await buildStringifiedScenario(model),
@@ -1,5 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ import { camelCase } from "change-case";
3
4
  import { Listr } from "listr2";
4
5
  import pascalcase from "pascalcase";
5
6
  import { recordTelemetryAttributes } from "@cedarjs/cli-helpers";
@@ -35,34 +36,56 @@ const customOrDefaultTemplatePath = ({
35
36
  return defaultPath;
36
37
  }
37
38
  };
38
- const templateForComponentFile = async ({
39
+ const templateForFile = async ({
39
40
  name,
40
- suffix = "",
41
- extension = ".js",
42
- webPathSection,
43
- apiPathSection,
41
+ side,
42
+ sidePathSection,
44
43
  generator,
44
+ outputPath,
45
45
  templatePath,
46
- templateVars,
47
- componentName,
48
- outputPath
46
+ templateVars
49
47
  }) => {
50
- const basePath = webPathSection ? getPaths().web[webPathSection] : getPaths().api[apiPathSection];
51
- const outputComponentName = componentName || pascalcase(name) + suffix;
52
- const componentOutputPath = outputPath || path.join(basePath, outputComponentName, outputComponentName + extension);
48
+ const basePath = getPaths()[side][sidePathSection];
49
+ const fullOutputPath = path.join(basePath, outputPath);
53
50
  const fullTemplatePath = customOrDefaultTemplatePath({
54
51
  generator,
55
52
  templatePath,
56
- side: webPathSection ? "web" : "api"
53
+ side
57
54
  });
58
55
  const content = await generateTemplate(fullTemplatePath, {
59
56
  name,
60
57
  outputPath: ensurePosixPath(
61
- `./${path.relative(getPaths().base, componentOutputPath)}`
58
+ `./${path.relative(getPaths().base, fullOutputPath)}`
62
59
  ),
63
60
  ...templateVars
64
61
  });
65
- return [componentOutputPath, content];
62
+ return [fullOutputPath, content];
63
+ };
64
+ const templateForComponentFile = async ({
65
+ name,
66
+ suffix = "",
67
+ extension = ".js",
68
+ webPathSection,
69
+ apiPathSection,
70
+ generator,
71
+ templatePath,
72
+ templateVars
73
+ }) => {
74
+ const side = webPathSection ? "web" : "api";
75
+ const caseFn = side === "web" ? pascalcase : camelCase;
76
+ const componentName = caseFn(name) + suffix;
77
+ const outputPath = path.join(componentName, componentName + extension);
78
+ return templateForFile({
79
+ name,
80
+ suffix,
81
+ extension,
82
+ side,
83
+ sidePathSection: webPathSection || apiPathSection,
84
+ generator,
85
+ outputPath,
86
+ templatePath,
87
+ templateVars
88
+ });
66
89
  };
67
90
  const validateName = (name) => {
68
91
  if (name.match(/^\W/)) {
@@ -151,5 +174,6 @@ export {
151
174
  createYargsForComponentGeneration,
152
175
  customOrDefaultTemplatePath,
153
176
  templateForComponentFile,
177
+ templateForFile,
154
178
  validateName
155
179
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/cli",
3
- "version": "1.0.0-canary.12969+fa4218aac",
3
+ "version": "1.0.0-canary.12970+a0b9793f6",
4
4
  "description": "The CedarJS Command Line",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,15 +31,15 @@
31
31
  "dependencies": {
32
32
  "@babel/preset-typescript": "7.28.5",
33
33
  "@babel/runtime-corejs3": "7.28.4",
34
- "@cedarjs/api-server": "1.0.0-canary.12969",
35
- "@cedarjs/cli-helpers": "1.0.0-canary.12969",
36
- "@cedarjs/fastify-web": "1.0.0-canary.12969",
37
- "@cedarjs/internal": "1.0.0-canary.12969",
38
- "@cedarjs/prerender": "1.0.0-canary.12969",
39
- "@cedarjs/project-config": "1.0.0-canary.12969",
40
- "@cedarjs/structure": "1.0.0-canary.12969",
41
- "@cedarjs/telemetry": "1.0.0-canary.12969",
42
- "@cedarjs/web-server": "1.0.0-canary.12969",
34
+ "@cedarjs/api-server": "1.0.0-canary.12970",
35
+ "@cedarjs/cli-helpers": "1.0.0-canary.12970",
36
+ "@cedarjs/fastify-web": "1.0.0-canary.12970",
37
+ "@cedarjs/internal": "1.0.0-canary.12970",
38
+ "@cedarjs/prerender": "1.0.0-canary.12970",
39
+ "@cedarjs/project-config": "1.0.0-canary.12970",
40
+ "@cedarjs/structure": "1.0.0-canary.12970",
41
+ "@cedarjs/telemetry": "1.0.0-canary.12970",
42
+ "@cedarjs/web-server": "1.0.0-canary.12970",
43
43
  "@listr2/prompt-adapter-enquirer": "2.0.16",
44
44
  "@opentelemetry/api": "1.8.0",
45
45
  "@opentelemetry/core": "1.22.0",
@@ -101,5 +101,5 @@
101
101
  "publishConfig": {
102
102
  "access": "public"
103
103
  },
104
- "gitHead": "fa4218aac5522b9d324ecb7c819876503d4f3ef5"
104
+ "gitHead": "a0b9793f6d5043998be093ca949610cc3f6b19dd"
105
105
  }