@mbc-cqrs-serverless/cli 0.1.50-beta.0 → 0.1.51-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.
Files changed (108) hide show
  1. package/dist/actions/generate.action.js +44 -0
  2. package/dist/actions/new.action.get-package-version.spec.js +29 -0
  3. package/dist/actions/new.action.is-latest-version.spec.js +36 -0
  4. package/dist/actions/new.action.js +12 -6
  5. package/dist/actions/new.action.spec.js +84 -0
  6. package/dist/actions/new.action.update-env-local.spec.js +35 -0
  7. package/dist/actions/new.action.use-package-version.spec.js +46 -0
  8. package/dist/actions/ui.action.js +7 -5
  9. package/dist/commands/command.input.js +2 -0
  10. package/dist/commands/generate.command.js +24 -0
  11. package/dist/commands/index.js +2 -0
  12. package/dist/index.js +13 -2
  13. package/dist/runners/abstract.runner.js +44 -0
  14. package/dist/runners/schematic.runner.js +21 -0
  15. package/dist/schematics/collection.json +34 -0
  16. package/dist/schematics/index.js +17 -0
  17. package/dist/schematics/lib/controller/controller.factory.js +40 -0
  18. package/dist/schematics/lib/controller/controller.factory.spec.js +79 -0
  19. package/dist/schematics/lib/controller/files/__name@dasherize__.controller.ts +11 -0
  20. package/dist/schematics/lib/controller/schema.json +18 -0
  21. package/dist/schematics/lib/controller/units/__name@dasherize__.controller.__specFileSuffix__.ts +19 -0
  22. package/dist/schematics/lib/dto/dto.factory.js +32 -0
  23. package/dist/schematics/lib/dto/dto.factory.spec.js +128 -0
  24. package/dist/schematics/lib/dto/files/dto/__name@dasherize__-attributes.dto.ts +6 -0
  25. package/dist/schematics/lib/dto/files/dto/__name@dasherize__-command.dto.ts +16 -0
  26. package/dist/schematics/lib/dto/files/dto/__name@dasherize__-create.dto.ts +18 -0
  27. package/dist/schematics/lib/dto/files/dto/__name@dasherize__-search.dto.ts +2 -0
  28. package/dist/schematics/lib/dto/files/dto/__name@dasherize__-update.dto.ts +34 -0
  29. package/dist/schematics/lib/dto/schema.json +18 -0
  30. package/dist/schematics/lib/entity/entity.factory.js +30 -0
  31. package/dist/schematics/lib/entity/entity.factory.spec.js +86 -0
  32. package/dist/schematics/lib/entity/files/entity/__name@dasherize__-command.entity.ts +12 -0
  33. package/dist/schematics/lib/entity/files/entity/__name@dasherize__-data-list.entity.ts +12 -0
  34. package/dist/schematics/lib/entity/files/entity/__name@dasherize__-data.entity.ts +12 -0
  35. package/dist/schematics/lib/entity/schema.json +18 -0
  36. package/dist/schematics/lib/module/files/async/__name@dasherize__.controller.ts +74 -0
  37. package/dist/schematics/lib/module/files/async/__name@dasherize__.module.ts +19 -0
  38. package/dist/schematics/lib/module/files/async/__name@dasherize__.service.ts +164 -0
  39. package/dist/schematics/lib/module/files/async/dto/__name@dasherize__-attributes.dto.ts +6 -0
  40. package/dist/schematics/lib/module/files/async/dto/__name@dasherize__-command.dto.ts +16 -0
  41. package/dist/schematics/lib/module/files/async/dto/__name@dasherize__-create.dto.ts +18 -0
  42. package/dist/schematics/lib/module/files/async/dto/__name@dasherize__-search.dto.ts +2 -0
  43. package/dist/schematics/lib/module/files/async/dto/__name@dasherize__-update.dto.ts +34 -0
  44. package/dist/schematics/lib/module/files/async/entity/__name@dasherize__-command.entity.ts +12 -0
  45. package/dist/schematics/lib/module/files/async/entity/__name@dasherize__-data-list.entity.ts +12 -0
  46. package/dist/schematics/lib/module/files/async/entity/__name@dasherize__-data.entity.ts +12 -0
  47. package/dist/schematics/lib/module/files/async/handler/__name@dasherize__-rds.handler.ts +62 -0
  48. package/dist/schematics/lib/module/files/sync/__name@dasherize__.controller.ts +74 -0
  49. package/dist/schematics/lib/module/files/sync/__name@dasherize__.module.ts +19 -0
  50. package/dist/schematics/lib/module/files/sync/__name@dasherize__.service.ts +164 -0
  51. package/dist/schematics/lib/module/files/sync/dto/__name@dasherize__-attributes.dto.ts +6 -0
  52. package/dist/schematics/lib/module/files/sync/dto/__name@dasherize__-command.dto.ts +16 -0
  53. package/dist/schematics/lib/module/files/sync/dto/__name@dasherize__-create.dto.ts +18 -0
  54. package/dist/schematics/lib/module/files/sync/dto/__name@dasherize__-search.dto.ts +2 -0
  55. package/dist/schematics/lib/module/files/sync/dto/__name@dasherize__-update.dto.ts +34 -0
  56. package/dist/schematics/lib/module/files/sync/entity/__name@dasherize__-command.entity.ts +12 -0
  57. package/dist/schematics/lib/module/files/sync/entity/__name@dasherize__-data-list.entity.ts +12 -0
  58. package/dist/schematics/lib/module/files/sync/entity/__name@dasherize__-data.entity.ts +12 -0
  59. package/dist/schematics/lib/module/files/sync/handler/__name@dasherize__-rds.handler.ts +62 -0
  60. package/dist/schematics/lib/module/module.factory.js +204 -0
  61. package/dist/schematics/lib/module/module.factory.spec.js +188 -0
  62. package/dist/schematics/lib/module/schema.json +28 -0
  63. package/dist/schematics/lib/module/units/__name@dasherize__.controller.__specFileSuffix__.ts +19 -0
  64. package/dist/schematics/lib/module/units/__name@dasherize__.service.__specFileSuffix__.ts +19 -0
  65. package/dist/schematics/lib/service/files/__name@dasherize__.service.ts +12 -0
  66. package/dist/schematics/lib/service/schema.json +18 -0
  67. package/dist/schematics/lib/service/service.factory.js +40 -0
  68. package/dist/schematics/lib/service/service.factory.spec.js +81 -0
  69. package/dist/schematics/lib/service/units/__name@dasherize__.service.__specFileSuffix__.ts +19 -0
  70. package/dist/schematics/schematic.colection.js +60 -0
  71. package/dist/schematics/schematic.option.js +44 -0
  72. package/dist/schematics/utils/check-files-exist.js +13 -0
  73. package/dist/schematics/utils/index.js +17 -0
  74. package/dist/ui/index.js +18 -0
  75. package/dist/ui/logger.js +54 -0
  76. package/dist/ui/message.js +6 -0
  77. package/dist/utils/formatting.js +18 -0
  78. package/dist/utils/index.js +18 -0
  79. package/dist/utils/local-binaries.js +20 -0
  80. package/package.json +13 -4
  81. package/templates/.env.local +1 -1
  82. package/templates/README.md +1 -1
  83. package/templates/infra/README.md +6 -1
  84. package/templates/infra/libs/infra-stack.ts +1 -1
  85. package/templates/infra-local/docker-compose.yml +1 -1
  86. package/templates/infra-local/serverless.yml +19 -21
  87. package/templates/infra-local/swagger.json +396 -0
  88. package/templates/package.json +1 -0
  89. package/templates/prisma/dynamodbs/cqrs.json +1 -1
  90. package/templates/prisma/schema.prisma +4 -7
  91. package/templates/src/helpers/id.ts +12 -0
  92. package/templates/src/helpers/index.ts +1 -0
  93. package/templates/src/main.module.ts +2 -2
  94. package/templates/src/{master/dto/master-attributes.dto.ts → sample/dto/sample-attributes.dto.ts} +2 -2
  95. package/templates/src/{master/dto/master-command.dto.ts → sample/dto/sample-command.dto.ts} +4 -4
  96. package/templates/src/sample/entity/sample-command.entity.ts +13 -0
  97. package/templates/src/sample/entity/sample-data-list.entity.ts +13 -0
  98. package/templates/src/sample/entity/sample-data.entity.ts +13 -0
  99. package/templates/src/{master/handler/master-rds.handler.ts → sample/handler/sample-rds.handler.ts} +5 -7
  100. package/templates/src/{master/master.controller.ts → sample/sample.controller.ts} +22 -22
  101. package/templates/src/sample/sample.module.ts +19 -0
  102. package/templates/src/{master/master.service.ts → sample/sample.service.ts} +12 -12
  103. package/templates/test/api.http +25 -0
  104. package/templates/tsconfig.json +1 -1
  105. package/templates/src/master/entity/master-command.entity.ts +0 -13
  106. package/templates/src/master/entity/master-data-list.entity.ts +0 -13
  107. package/templates/src/master/entity/master-data.entity.ts +0 -13
  108. package/templates/src/master/master.module.ts +0 -19
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = generateAction;
4
+ const schematic_runner_1 = require("../runners/schematic.runner");
5
+ const schematics_1 = require("../schematics");
6
+ const ui_1 = require("../ui");
7
+ /* eslint-disable no-console */
8
+ async function generateAction(schematic, name, options, command) {
9
+ ui_1.logger.info(`Executing command '${command.name()}' for application with options '${JSON.stringify(options)}'`);
10
+ const commandOptions = command.opts();
11
+ const formatOptions = [];
12
+ formatOptions.push({ name: 'dry-run', value: !!commandOptions.dryRun });
13
+ formatOptions.push({ name: 'mode', value: commandOptions.mode });
14
+ formatOptions.push({
15
+ name: 'schema',
16
+ value: !!commandOptions.schema || !!commandOptions.noSchema, // noSchema > schema
17
+ });
18
+ const inputs = [];
19
+ inputs.push({ name: 'schematic', value: schematic });
20
+ inputs.push({ name: 'name', value: name });
21
+ const fullInputs = formatOptions.concat(inputs);
22
+ const schematicOptions = mapSchematicOptions(fullInputs);
23
+ const runner = new schematic_runner_1.SchematicRunner();
24
+ const fullCommand = buildCommandLine(schematic, schematicOptions);
25
+ runner.run(fullCommand);
26
+ }
27
+ function buildCommandLine(name, options) {
28
+ return `@mbc-cqrs-serverless/cli:${name}${buildOptions(options)}`;
29
+ }
30
+ function buildOptions(options) {
31
+ return options.reduce((line, option) => {
32
+ return line.concat(` ${option.toCommandString()}`);
33
+ }, '');
34
+ }
35
+ const mapSchematicOptions = (inputs) => {
36
+ const excludedInputNames = ['schematic', 'spec', 'flat', 'specFileSuffix'];
37
+ const options = [];
38
+ inputs.forEach((input) => {
39
+ if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
40
+ options.push(new schematics_1.SchematicOption(input.name, input.value));
41
+ }
42
+ });
43
+ return options;
44
+ };
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const child_process_1 = require("child_process");
4
+ const new_action_1 = require("./new.action");
5
+ const { getPackageVersion } = new_action_1.exportsForTesting;
6
+ jest.mock('child_process', () => ({
7
+ execSync: jest.fn(),
8
+ }));
9
+ describe('getPackageVersion', () => {
10
+ const mockExecSync = child_process_1.execSync;
11
+ const packageName = '@mbc-cqrs-serverless/core';
12
+ afterEach(() => {
13
+ jest.clearAllMocks();
14
+ });
15
+ it('should return the latest version when isLatest is true', () => {
16
+ const mockLatestVersion = '1.2.3';
17
+ mockExecSync.mockReturnValue(Buffer.from(`${mockLatestVersion}\n`));
18
+ const result = getPackageVersion(packageName, true);
19
+ expect(mockExecSync).toHaveBeenCalledWith(`npm view ${packageName} dist-tags.latest`);
20
+ expect(result).toEqual([mockLatestVersion]);
21
+ });
22
+ it('should return all versions when isLatest is false', () => {
23
+ const mockVersions = ['1.0.0', '1.1.0', '1.2.0'];
24
+ mockExecSync.mockReturnValue(Buffer.from(JSON.stringify(mockVersions)));
25
+ const result = getPackageVersion(packageName, false);
26
+ expect(mockExecSync).toHaveBeenCalledWith(`npm view ${packageName} versions --json`);
27
+ expect(result).toEqual(mockVersions);
28
+ });
29
+ });
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const child_process_1 = require("child_process");
4
+ const new_action_1 = require("./new.action");
5
+ const { isLatestCli } = new_action_1.exportsForTesting;
6
+ jest.mock('child_process', () => ({
7
+ execSync: jest.fn(),
8
+ }));
9
+ jest.mock('fs', () => ({
10
+ readFileSync: jest.fn(() => JSON.stringify({ version: '1.0.0' })),
11
+ }));
12
+ jest.mock('path', () => ({
13
+ join: jest.fn(() => '/mocked/path/to/package.json'),
14
+ }));
15
+ describe('isLatestCli', () => {
16
+ const mockExecSync = child_process_1.execSync;
17
+ afterEach(() => {
18
+ jest.clearAllMocks();
19
+ });
20
+ it('should return true if the current version matches the latest version', () => {
21
+ const mockLatestVersion = ['1.0.0'];
22
+ mockExecSync.mockReturnValue(Buffer.from(`${mockLatestVersion}\n`));
23
+ // Run the function
24
+ const result = isLatestCli();
25
+ // Assert that the result is true
26
+ expect(result).toBe(true);
27
+ });
28
+ it('should return false if the current version does not match the latest version', () => {
29
+ const mockLatestVersion = ['1.2.3'];
30
+ mockExecSync.mockReturnValue(Buffer.from(`${mockLatestVersion}\n`));
31
+ // Run the function
32
+ const result = isLatestCli();
33
+ // Assert that the result is true
34
+ expect(result).toBe(false);
35
+ });
36
+ });
@@ -5,13 +5,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.exportsForTesting = void 0;
7
7
  exports.default = newAction;
8
+ const chalk_1 = __importDefault(require("chalk"));
8
9
  const child_process_1 = require("child_process");
9
10
  const fs_1 = require("fs");
10
11
  const path_1 = __importDefault(require("path"));
12
+ const ui_1 = require("../ui");
11
13
  /* eslint-disable no-console */
12
14
  async function newAction(name = '', options, command) {
13
15
  const [projectName, version = 'latest'] = name.split('@');
14
- console.log(`Executing command '${command.name()}' for application '${projectName}' with options '${JSON.stringify(options)}'`);
16
+ ui_1.logger.info(`Executing command '${command.name()}' for application '${projectName}' with options '${JSON.stringify(options)}'`);
15
17
  let packageVersion;
16
18
  if (version === 'latest') {
17
19
  packageVersion = `^${getPackageVersion('@mbc-cqrs-serverless/core', true)[0]}`; // use the latest patch and minor versions
@@ -27,12 +29,12 @@ async function newAction(name = '', options, command) {
27
29
  packageVersion = `^${matchVersions.at(-1)}`; // use the patch and minor versions
28
30
  }
29
31
  else {
30
- console.log('The specified package version does not exist. Please chose a valid version!\n', versions);
32
+ ui_1.logger.error(`The specified package version does not exist. Please chose a valid version! ${versions}`);
31
33
  return;
32
34
  }
33
35
  }
34
36
  const destDir = path_1.default.join(process.cwd(), projectName);
35
- console.log('Generating MBC cqrs serverless application in', destDir);
37
+ ui_1.logger.title('MBC', `Generating MBC cqrs serverless application in ${chalk_1.default.green(destDir)}`);
36
38
  (0, fs_1.mkdirSync)(destDir, { recursive: true });
37
39
  useTemplate(destDir);
38
40
  usePackageVersion(destDir, packageVersion, projectName);
@@ -49,12 +51,16 @@ async function newAction(name = '', options, command) {
49
51
  // cp .env.local .env
50
52
  (0, fs_1.copyFileSync)(path_1.default.join(destDir, '.env.local'), path_1.default.join(destDir, '.env'));
51
53
  // git init
54
+ ui_1.logger.title('git', 'Initializing git repository.');
52
55
  let logs = (0, child_process_1.execSync)('git init', { cwd: destDir });
53
- console.log(logs.toString());
56
+ ui_1.logger.success('Initialized a git repository.');
54
57
  // npm install
55
- console.log('Installing packages in', destDir);
56
- logs = (0, child_process_1.execSync)('npm i', { cwd: destDir });
58
+ ui_1.logger.title('deps', `Installing dependencies`);
59
+ logs = (0, child_process_1.execSync)('npm i --ignore-scripts', { cwd: destDir });
60
+ (0, child_process_1.execSync)('npx prisma generate', { cwd: destDir });
57
61
  console.log(logs.toString());
62
+ ui_1.logger.success(`Dependencies installed`);
63
+ ui_1.logger.title('MBC', `Your application was created!`);
58
64
  }
59
65
  function useTemplate(destDir) {
60
66
  if (isLatestCli()) {
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const child_process_1 = require("child_process");
7
+ const commander_1 = require("commander");
8
+ const fs_1 = require("fs");
9
+ const path_1 = __importDefault(require("path"));
10
+ const new_action_1 = __importDefault(require("./new.action"));
11
+ jest.mock('child_process', () => ({
12
+ execSync: jest.fn(),
13
+ }));
14
+ jest.mock('fs', () => ({
15
+ copyFileSync: jest.fn(),
16
+ cpSync: jest.fn(),
17
+ mkdirSync: jest.fn(),
18
+ unlinkSync: jest.fn(),
19
+ writeFileSync: jest.fn(),
20
+ readFileSync: jest.fn(() => JSON.stringify({ version: '1.2.3', dependencies: {}, devDependencies: {} })),
21
+ }));
22
+ describe('newAction', () => {
23
+ const mockExecSync = child_process_1.execSync;
24
+ const mockCommand = new commander_1.Command().name('new'); // Mock command with name 'new'
25
+ beforeEach(() => {
26
+ jest.clearAllMocks();
27
+ });
28
+ it('should generate a project with the latest version when version is not specified', async () => {
29
+ const projectName = 'test-project';
30
+ const latestVersion = '1.2.3';
31
+ mockExecSync
32
+ .mockReturnValueOnce(Buffer.from(latestVersion)) // latest core
33
+ .mockReturnValueOnce(Buffer.from(latestVersion)) // latest cli
34
+ .mockReturnValue(Buffer.from(''));
35
+ await (0, new_action_1.default)(`${projectName}`, {}, mockCommand);
36
+ expect(child_process_1.execSync).toHaveBeenCalledWith('npm view @mbc-cqrs-serverless/core dist-tags.latest');
37
+ expect(fs_1.mkdirSync).toHaveBeenCalledWith(path_1.default.join(process.cwd(), projectName), { recursive: true });
38
+ expect(fs_1.cpSync).toHaveBeenCalledWith(path_1.default.join(__dirname, '../../templates'), path_1.default.join(process.cwd(), projectName), { recursive: true });
39
+ expect(fs_1.copyFileSync).toHaveBeenCalledTimes(3); // For .gitignore, infra/.gitignore and .env.local
40
+ expect(mockExecSync).toHaveBeenCalledWith('git init', {
41
+ cwd: path_1.default.join(process.cwd(), projectName),
42
+ });
43
+ expect(mockExecSync).toHaveBeenCalledWith('npm i --ignore-scripts', {
44
+ cwd: path_1.default.join(process.cwd(), projectName),
45
+ });
46
+ expect(mockExecSync).toHaveBeenCalledWith('npx prisma generate', {
47
+ cwd: path_1.default.join(process.cwd(), projectName),
48
+ });
49
+ });
50
+ it('should use a specific version if specified', async () => {
51
+ const projectName = 'test-project';
52
+ const version = '1.0.0';
53
+ const mockVersions = ['1.0.0', '1.1.0', '1.2.0'];
54
+ mockExecSync
55
+ .mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions))) // list version core
56
+ .mockReturnValueOnce(Buffer.from('1.2.3')) // latest cli
57
+ .mockReturnValue(Buffer.from(''));
58
+ await (0, new_action_1.default)(`${projectName}@${version}`, {}, mockCommand);
59
+ expect(child_process_1.execSync).toHaveBeenCalledWith('npm view @mbc-cqrs-serverless/core versions --json');
60
+ expect(fs_1.mkdirSync).toHaveBeenCalledWith(path_1.default.join(process.cwd(), projectName), { recursive: true });
61
+ expect(fs_1.cpSync).toHaveBeenCalledWith(path_1.default.join(__dirname, '../../templates'), path_1.default.join(process.cwd(), projectName), { recursive: true });
62
+ expect(fs_1.copyFileSync).toHaveBeenCalledTimes(3); // For .gitignore, infra/.gitignore and .env.local
63
+ expect(mockExecSync).toHaveBeenCalledWith('git init', {
64
+ cwd: path_1.default.join(process.cwd(), projectName),
65
+ });
66
+ expect(mockExecSync).toHaveBeenCalledWith('npm i --ignore-scripts', {
67
+ cwd: path_1.default.join(process.cwd(), projectName),
68
+ });
69
+ expect(mockExecSync).toHaveBeenCalledWith('npx prisma generate', {
70
+ cwd: path_1.default.join(process.cwd(), projectName),
71
+ });
72
+ });
73
+ it('should throw an error for an invalid version', async () => {
74
+ const projectName = 'test-project';
75
+ const invalidVersion = '2.0.0';
76
+ const mockVersions = ['1.0.0', '1.1.0', '1.2.0'];
77
+ mockExecSync.mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions)));
78
+ const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
79
+ await (0, new_action_1.default)(`${projectName}@${invalidVersion}`, {}, mockCommand);
80
+ expect(child_process_1.execSync).toHaveBeenCalledWith('npm view @mbc-cqrs-serverless/core versions --json');
81
+ expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('The specified package version does not exist'));
82
+ consoleSpy.mockRestore();
83
+ });
84
+ });
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fs_1 = require("fs");
4
+ const new_action_1 = require("./new.action");
5
+ const { updateEnvLocal } = new_action_1.exportsForTesting;
6
+ jest.mock('fs');
7
+ describe('updateEnvLocal', () => {
8
+ const mockEnvContent = `
9
+ # name of application
10
+ APP_NAME=%%projectName%%
11
+ # name of docker compose
12
+ COMPOSE_PROJECT_NAME=%%projectName%%
13
+ `;
14
+ const envPath = './.env.local';
15
+ beforeEach(() => {
16
+ jest.clearAllMocks();
17
+ });
18
+ it('should update the specified value in the .env.local file', () => {
19
+ const searchValue = '%%projectName%%';
20
+ const replaceValue = 'new-project-name';
21
+ fs_1.readFileSync.mockReturnValue(mockEnvContent);
22
+ updateEnvLocal(envPath, searchValue, replaceValue);
23
+ expect(fs_1.readFileSync).toHaveBeenCalledWith(envPath, 'utf8');
24
+ const expectedContent = mockEnvContent.replaceAll(searchValue, replaceValue);
25
+ expect(fs_1.writeFileSync).toHaveBeenCalledWith(envPath, expectedContent);
26
+ });
27
+ it('should not change the file content if searchValue is not found', () => {
28
+ const searchValue = 'non-existent-value';
29
+ const replaceValue = 'new-project-name';
30
+ fs_1.readFileSync.mockReturnValue(mockEnvContent);
31
+ updateEnvLocal(envPath, searchValue, replaceValue);
32
+ expect(fs_1.readFileSync).toHaveBeenCalledWith(envPath, 'utf8');
33
+ expect(fs_1.writeFileSync).toHaveBeenCalledWith(envPath, mockEnvContent);
34
+ });
35
+ });
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const faker_1 = require("@faker-js/faker");
7
+ const fs_1 = require("fs");
8
+ const path_1 = __importDefault(require("path"));
9
+ const new_action_1 = require("./new.action");
10
+ const { usePackageVersion } = new_action_1.exportsForTesting;
11
+ // create testcase for usePackageVersion function in new.action.ts file
12
+ describe('usePackageVersion', () => {
13
+ const fname = path_1.default.join(__dirname, 'package.json');
14
+ const packageVersion = '1.0.0';
15
+ const packageJson = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(__dirname, '../../package.json')).toString());
16
+ beforeEach(() => {
17
+ (0, fs_1.copyFileSync)(path_1.default.join(__dirname, '../../templates/package.json'), fname);
18
+ });
19
+ afterEach(() => {
20
+ (0, fs_1.unlinkSync)(fname);
21
+ });
22
+ it('it should update deps', () => {
23
+ usePackageVersion(__dirname, packageVersion);
24
+ const tplPackageJson = JSON.parse((0, fs_1.readFileSync)(fname).toString());
25
+ expect(tplPackageJson.dependencies['@mbc-cqrs-serverless/core']).toBe(packageVersion);
26
+ expect(packageJson.version).toBe(tplPackageJson.devDependencies['@mbc-cqrs-serverless/cli']);
27
+ });
28
+ it('it should not update name', () => {
29
+ const { name } = JSON.parse((0, fs_1.readFileSync)(fname).toString());
30
+ usePackageVersion(__dirname, packageVersion);
31
+ const tplPackageJson = JSON.parse((0, fs_1.readFileSync)(fname).toString());
32
+ expect(name).toBe(tplPackageJson.name);
33
+ });
34
+ it('it should not update name with empty name', () => {
35
+ const { name } = JSON.parse((0, fs_1.readFileSync)(fname).toString());
36
+ usePackageVersion(__dirname, packageVersion, '');
37
+ const tplPackageJson = JSON.parse((0, fs_1.readFileSync)(fname).toString());
38
+ expect(name).toBe(tplPackageJson.name);
39
+ });
40
+ it('it should update name', () => {
41
+ const name = faker_1.faker.word.sample();
42
+ usePackageVersion(__dirname, packageVersion, name);
43
+ const tplPackageJson = JSON.parse((0, fs_1.readFileSync)(fname).toString());
44
+ expect(name).toBe(tplPackageJson.name);
45
+ });
46
+ });
@@ -8,18 +8,19 @@ const child_process_1 = require("child_process");
8
8
  const fs_1 = require("fs");
9
9
  const path_1 = __importDefault(require("path"));
10
10
  const rimraf_1 = require("rimraf");
11
+ const ui_1 = require("../ui");
11
12
  const repoUrl = 'https://gitlab.com/mbc-net/common/mbc-cqrs-ui-common.git';
12
13
  const componentOptions = ['all', 'appsync', 'component'];
13
14
  /* eslint-disable no-console */
14
15
  async function uiAction(options, command) {
15
- console.log(`Executing command '${command.name()}' for application with options '${JSON.stringify(options)}'`);
16
+ ui_1.logger.info(`Executing command '${command.name()}' for application with options '${JSON.stringify(options)}'`);
16
17
  const { branch, auth, component, pathDir, token = '' } = options;
17
18
  if (componentOptions.findIndex((optionName) => optionName === component) === -1) {
18
- console.error(`Please choose correct component options: ${componentOptions.join(', ')}`);
19
+ ui_1.logger.error(`Please choose correct component options: ${componentOptions.join(', ')}`);
19
20
  }
20
21
  // Check command run in base src
21
22
  if (!(0, fs_1.existsSync)(path_1.default.join(process.cwd(), 'tsconfig.json'))) {
22
- console.log('Please run command in base folder');
23
+ ui_1.logger.error('Please run command in base folder');
23
24
  return;
24
25
  }
25
26
  // Check tsconfig.json contain path @ms
@@ -27,7 +28,7 @@ async function uiAction(options, command) {
27
28
  if (tsconfig?.compilerOptions &&
28
29
  tsconfig?.compilerOptions?.paths &&
29
30
  tsconfig?.compilerOptions?.paths.hasOwnProperty('@ms/*')) {
30
- console.log('The project already contain mbc-cqrs-ui-common');
31
+ ui_1.logger.error('The project already contain mbc-cqrs-ui-common');
31
32
  return;
32
33
  }
33
34
  // Copy source
@@ -52,9 +53,10 @@ async function uiAction(options, command) {
52
53
  // Modify package.json
53
54
  modifyDependencies({ pathDir, component });
54
55
  // npm install
55
- console.log('Installing packages');
56
+ ui_1.logger.title('deps', `Installing dependencies`);
56
57
  const logs = (0, child_process_1.execSync)('npm i');
57
58
  console.log(logs.toString());
59
+ ui_1.logger.success(`Dependencies installed`);
58
60
  }
59
61
  const installTemplate = ({ auth, token, pathDir, branch, component, }) => {
60
62
  let gitUrl = repoUrl;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateCommand = generateCommand;
7
+ const generate_action_1 = __importDefault(require("../actions/generate.action"));
8
+ const schematic_colection_1 = require("../schematics/schematic.colection");
9
+ async function generateCommand(program) {
10
+ program
11
+ .command('generate <schematic> [name]')
12
+ .alias('g')
13
+ .description(buildDescription())
14
+ .option('-d, --dry-run', 'Report actions that would be taken without writing out results.')
15
+ .option('--mode <mode>', 'Specify the mode of operation: sync or async (default: async)', 'async')
16
+ .option('--schema', 'Enable schema generation (default: true)', true)
17
+ .option('--no-schema', 'Disable schema generation (default: false)', false)
18
+ .action(generate_action_1.default);
19
+ }
20
+ function buildDescription() {
21
+ return ('Generate a MBC-cqrs-serverless element.\n' +
22
+ ` Schematics available on the collection:\n` +
23
+ (0, schematic_colection_1.buildSchematicsListAsTable)());
24
+ }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = loadCommands;
4
+ const generate_command_1 = require("./generate.command");
4
5
  const new_command_1 = require("./new.command");
5
6
  const start_command_1 = require("./start.command");
6
7
  const ui_command_1 = require("./ui.command");
@@ -8,6 +9,7 @@ function loadCommands(program) {
8
9
  (0, new_command_1.newCommand)(program);
9
10
  (0, start_command_1.startCommand)(program);
10
11
  (0, ui_command_1.uiCommand)(program);
12
+ (0, generate_command_1.generateCommand)(program);
11
13
  // error handling
12
14
  program.on('command:*', () => {
13
15
  console.error(`\nInvalid command: '${program.args.join(' ')}'`);
package/dist/index.js CHANGED
@@ -6,11 +6,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const commander_1 = require("commander");
8
8
  const commands_1 = __importDefault(require("./commands"));
9
+ const utils_1 = require("./utils");
9
10
  async function bootstrap() {
10
11
  const program = new commander_1.Command();
12
+ program
13
+ .version(
11
14
  // eslint-disable-next-line @typescript-eslint/no-var-requires
12
- program.version(require('../package.json').version);
13
- (0, commands_1.default)(program);
15
+ require('../package.json').version, '-v, --version', 'Output the current version.')
16
+ .usage('<command> [options]')
17
+ .helpOption('-h, --help', 'Output usage information.');
18
+ if ((0, utils_1.localBinExists)()) {
19
+ const localCommandLoader = (0, utils_1.loadLocalBinCommandLoader)();
20
+ localCommandLoader.default(program);
21
+ }
22
+ else {
23
+ (0, commands_1.default)(program);
24
+ }
14
25
  await program.parseAsync(process.argv);
15
26
  if (!process.argv.slice(2).length) {
16
27
  program.outputHelp();
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractRunner = void 0;
4
+ const child_process_1 = require("child_process");
5
+ const ui_1 = require("../ui");
6
+ /* eslint-disable no-console */
7
+ class AbstractRunner {
8
+ constructor(binary, args = []) {
9
+ this.binary = binary;
10
+ this.args = args;
11
+ }
12
+ async run(command, collect = false, cwd = process.cwd()) {
13
+ const args = [command];
14
+ const options = {
15
+ cwd,
16
+ stdio: collect ? 'pipe' : 'inherit',
17
+ shell: true,
18
+ };
19
+ return new Promise((resolve, reject) => {
20
+ const child = (0, child_process_1.spawn)(`${this.binary}`, [...this.args, ...args], options);
21
+ if (collect) {
22
+ child.stdout.on('data', (data) => resolve(data.toString().replace(/\r\n|\n/, '')));
23
+ }
24
+ child.on('close', (code) => {
25
+ if (code === 0) {
26
+ resolve(null);
27
+ }
28
+ else {
29
+ console.error(ui_1.MESSAGES.RUNNER_EXECUTION_ERROR(`${this.binary} ${command}`));
30
+ reject();
31
+ }
32
+ });
33
+ });
34
+ }
35
+ /**
36
+ * @param command
37
+ * @returns The entire command that will be ran when calling `run(command)`.
38
+ */
39
+ rawFullCommand(command) {
40
+ const commandArgs = [...this.args, command];
41
+ return `${this.binary} ${commandArgs.join(' ')}`;
42
+ }
43
+ }
44
+ exports.AbstractRunner = AbstractRunner;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchematicRunner = void 0;
4
+ const abstract_runner_1 = require("./abstract.runner");
5
+ class SchematicRunner extends abstract_runner_1.AbstractRunner {
6
+ constructor() {
7
+ super(`node`, [`"${SchematicRunner.findClosestSchematicsBinary()}"`]);
8
+ }
9
+ static getModulePaths() {
10
+ return module.paths;
11
+ }
12
+ static findClosestSchematicsBinary() {
13
+ try {
14
+ return require.resolve('@angular-devkit/schematics-cli/bin/schematics.js', { paths: this.getModulePaths() });
15
+ }
16
+ catch {
17
+ throw new Error("'schematics' binary path could not be found!");
18
+ }
19
+ }
20
+ }
21
+ exports.SchematicRunner = SchematicRunner;
@@ -0,0 +1,34 @@
1
+ {
2
+ "$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3
+ "schematics": {
4
+ "module": {
5
+ "factory": "./lib/module/module.factory#main",
6
+ "description": "Create a mbc-cqrs-serverless module.",
7
+ "schema": "./lib/module/schema.json",
8
+ "aliases": ["mo"]
9
+ },
10
+ "controller": {
11
+ "factory": "./lib/controller/controller.factory#main",
12
+ "description": "Create a mbc-cqrs-serverless controller.",
13
+ "schema": "./lib/controller/schema.json",
14
+ "aliases": ["co"]
15
+ },
16
+ "service": {
17
+ "factory": "./lib/service/service.factory#main",
18
+ "description": "Create a mbc-cqrs-serverless service.",
19
+ "schema": "./lib/service/schema.json",
20
+ "aliases": ["se"]
21
+ },
22
+ "entity": {
23
+ "factory": "./lib/entity/entity.factory#main",
24
+ "description": "Create a mbc-cqrs-serverless entity.",
25
+ "schema": "./lib/entity/schema.json",
26
+ "aliases": ["en"]
27
+ },
28
+ "dto": {
29
+ "factory": "./lib/dto/dto.factory#main",
30
+ "description": "Create a mbc-cqrs-serverless dto.",
31
+ "schema": "./lib/dto/schema.json"
32
+ }
33
+ }
34
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./schematic.option"), exports);
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.main = main;
4
+ const core_1 = require("@angular-devkit/core");
5
+ const schematics_1 = require("@angular-devkit/schematics");
6
+ function createController(options) {
7
+ return (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files'), [
8
+ (0, schematics_1.template)({
9
+ ...core_1.strings,
10
+ ...options,
11
+ }),
12
+ (0, schematics_1.move)((0, core_1.normalize)(`/src/${core_1.strings.dasherize(options.name)}`)),
13
+ ]));
14
+ }
15
+ function createUnitTest(options) {
16
+ return (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./units'), [
17
+ (0, schematics_1.template)({
18
+ ...core_1.strings,
19
+ ...options,
20
+ specFileSuffix: 'spec',
21
+ }),
22
+ (0, schematics_1.move)((0, core_1.normalize)(`/test/unit/${core_1.strings.dasherize(options.name)}`)),
23
+ ]));
24
+ }
25
+ function main(options) {
26
+ return (tree, _context) => {
27
+ const filePath = (0, core_1.normalize)(`/src/${core_1.strings.dasherize(options.name)}/${core_1.strings.dasherize(options.name)}.controller.ts`);
28
+ const unitPath = (0, core_1.normalize)(`/test/unit/${core_1.strings.dasherize(options.name)}/${core_1.strings.dasherize(options.name)}.controller.spec.ts`);
29
+ const isFileExists = tree.exists(filePath);
30
+ const isUnitExists = tree.exists(unitPath);
31
+ if (isFileExists || isUnitExists) {
32
+ if (isFileExists)
33
+ _context.logger.info(`Controller file already exists at: ${filePath}`);
34
+ if (isUnitExists)
35
+ _context.logger.info(`Unit test file already exists at: ${unitPath}`);
36
+ return;
37
+ }
38
+ return (0, schematics_1.chain)([createController(options), createUnitTest(options)]);
39
+ };
40
+ }