nest-combo 1.2.0 → 1.2.2
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/.prettierignore +5 -0
- package/README.MD +17 -75
- package/bin/cli.js +157 -202
- package/index.html +1 -1
- package/jest.config.mjs +198 -0
- package/lib/generate.js +13 -35
- package/lib/installDependencies.js +65 -0
- package/lib/nestBynary.js +38 -0
- package/lib/outputs.js +73 -0
- package/lib/resources.js +62 -0
- package/lib/utils.js +52 -20
- package/package.json +18 -3
- package/lib/install.js +0 -22
- package/project.example.yml +0 -48
package/jest.config.mjs
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For a detailed explanation regarding each configuration property, visit:
|
|
3
|
+
* https://jestjs.io/docs/configuration
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** @type {import('jest').Config} */
|
|
7
|
+
const config = {
|
|
8
|
+
// All imported modules in your tests should be mocked automatically
|
|
9
|
+
// automock: false,
|
|
10
|
+
|
|
11
|
+
// Stop running tests after `n` failures
|
|
12
|
+
// bail: 0,
|
|
13
|
+
|
|
14
|
+
// The directory where Jest should store its cached dependency information
|
|
15
|
+
// cacheDirectory: "/tmp/jest_rs",
|
|
16
|
+
|
|
17
|
+
// Automatically clear mock calls, instances, contexts and results before every test
|
|
18
|
+
clearMocks: true,
|
|
19
|
+
|
|
20
|
+
// Indicates whether the coverage information should be collected while executing the test
|
|
21
|
+
collectCoverage: true,
|
|
22
|
+
|
|
23
|
+
// An array of glob patterns indicating a set of files for which coverage information should be collected
|
|
24
|
+
// collectCoverageFrom: undefined,
|
|
25
|
+
|
|
26
|
+
// The directory where Jest should output its coverage files
|
|
27
|
+
coverageDirectory: 'coverage',
|
|
28
|
+
|
|
29
|
+
// An array of regexp pattern strings used to skip coverage collection
|
|
30
|
+
// coveragePathIgnorePatterns: [
|
|
31
|
+
// "/node_modules/"
|
|
32
|
+
// ],
|
|
33
|
+
|
|
34
|
+
// Indicates which provider should be used to instrument code for coverage
|
|
35
|
+
coverageProvider: 'v8',
|
|
36
|
+
|
|
37
|
+
// A list of reporter names that Jest uses when writing coverage reports
|
|
38
|
+
// coverageReporters: [
|
|
39
|
+
// "json",
|
|
40
|
+
// "text",
|
|
41
|
+
// "lcov",
|
|
42
|
+
// "clover"
|
|
43
|
+
// ],
|
|
44
|
+
|
|
45
|
+
// An object that configures minimum threshold enforcement for coverage results
|
|
46
|
+
// coverageThreshold: undefined,
|
|
47
|
+
|
|
48
|
+
// A path to a custom dependency extractor
|
|
49
|
+
// dependencyExtractor: undefined,
|
|
50
|
+
|
|
51
|
+
// Make calling deprecated APIs throw helpful error messages
|
|
52
|
+
// errorOnDeprecated: false,
|
|
53
|
+
|
|
54
|
+
// The default configuration for fake timers
|
|
55
|
+
// fakeTimers: {
|
|
56
|
+
// "enableGlobally": false
|
|
57
|
+
// },
|
|
58
|
+
|
|
59
|
+
// Force coverage collection from ignored files using an array of glob patterns
|
|
60
|
+
// forceCoverageMatch: [],
|
|
61
|
+
|
|
62
|
+
// A path to a module which exports an async function that is triggered once before all test suites
|
|
63
|
+
// globalSetup: undefined,
|
|
64
|
+
|
|
65
|
+
// A path to a module which exports an async function that is triggered once after all test suites
|
|
66
|
+
// globalTeardown: undefined,
|
|
67
|
+
|
|
68
|
+
// A set of global variables that need to be available in all test environments
|
|
69
|
+
// globals: {},
|
|
70
|
+
|
|
71
|
+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
|
|
72
|
+
// maxWorkers: "50%",
|
|
73
|
+
|
|
74
|
+
// An array of directory names to be searched recursively up from the requiring module's location
|
|
75
|
+
// moduleDirectories: [
|
|
76
|
+
// "node_modules"
|
|
77
|
+
// ],
|
|
78
|
+
|
|
79
|
+
// An array of file extensions your modules use
|
|
80
|
+
// moduleFileExtensions: [
|
|
81
|
+
// "js",
|
|
82
|
+
// "mjs",
|
|
83
|
+
// "cjs",
|
|
84
|
+
// "jsx",
|
|
85
|
+
// "ts",
|
|
86
|
+
// "tsx",
|
|
87
|
+
// "json",
|
|
88
|
+
// "node"
|
|
89
|
+
// ],
|
|
90
|
+
|
|
91
|
+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
|
|
92
|
+
// moduleNameMapper: {},
|
|
93
|
+
|
|
94
|
+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
|
|
95
|
+
// modulePathIgnorePatterns: [],
|
|
96
|
+
|
|
97
|
+
// Activates notifications for test results
|
|
98
|
+
// notify: false,
|
|
99
|
+
|
|
100
|
+
// An enum that specifies notification mode. Requires { notify: true }
|
|
101
|
+
// notifyMode: "failure-change",
|
|
102
|
+
|
|
103
|
+
// A preset that is used as a base for Jest's configuration
|
|
104
|
+
// preset: undefined,
|
|
105
|
+
|
|
106
|
+
// Run tests from one or more projects
|
|
107
|
+
// projects: undefined,
|
|
108
|
+
|
|
109
|
+
// Use this configuration option to add custom reporters to Jest
|
|
110
|
+
// reporters: undefined,
|
|
111
|
+
|
|
112
|
+
// Automatically reset mock state before every test
|
|
113
|
+
// resetMocks: false,
|
|
114
|
+
|
|
115
|
+
// Reset the module registry before running each individual test
|
|
116
|
+
// resetModules: false,
|
|
117
|
+
|
|
118
|
+
// A path to a custom resolver
|
|
119
|
+
// resolver: undefined,
|
|
120
|
+
|
|
121
|
+
// Automatically restore mock state and implementation before every test
|
|
122
|
+
// restoreMocks: false,
|
|
123
|
+
|
|
124
|
+
// The root directory that Jest should scan for tests and modules within
|
|
125
|
+
// rootDir: undefined,
|
|
126
|
+
|
|
127
|
+
// A list of paths to directories that Jest should use to search for files in
|
|
128
|
+
// roots: [
|
|
129
|
+
// "<rootDir>"
|
|
130
|
+
// ],
|
|
131
|
+
|
|
132
|
+
// Allows you to use a custom runner instead of Jest's default test runner
|
|
133
|
+
// runner: "jest-runner",
|
|
134
|
+
|
|
135
|
+
// The paths to modules that run some code to configure or set up the testing environment before each test
|
|
136
|
+
// setupFiles: [],
|
|
137
|
+
|
|
138
|
+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
|
|
139
|
+
// setupFilesAfterEnv: [],
|
|
140
|
+
|
|
141
|
+
// The number of seconds after which a test is considered as slow and reported as such in the results.
|
|
142
|
+
// slowTestThreshold: 5,
|
|
143
|
+
|
|
144
|
+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
|
|
145
|
+
// snapshotSerializers: [],
|
|
146
|
+
|
|
147
|
+
// The test environment that will be used for testing
|
|
148
|
+
// testEnvironment: "jest-environment-node",
|
|
149
|
+
|
|
150
|
+
// Options that will be passed to the testEnvironment
|
|
151
|
+
// testEnvironmentOptions: {},
|
|
152
|
+
|
|
153
|
+
// Adds a location field to test results
|
|
154
|
+
// testLocationInResults: false,
|
|
155
|
+
|
|
156
|
+
// The glob patterns Jest uses to detect test files
|
|
157
|
+
// testMatch: [
|
|
158
|
+
// "**/__tests__/**/*.[jt]s?(x)",
|
|
159
|
+
// "**/?(*.)+(spec|test).[tj]s?(x)"
|
|
160
|
+
// ],
|
|
161
|
+
|
|
162
|
+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
|
|
163
|
+
// testPathIgnorePatterns: [
|
|
164
|
+
// "/node_modules/"
|
|
165
|
+
// ],
|
|
166
|
+
|
|
167
|
+
// The regexp pattern or array of patterns that Jest uses to detect test files
|
|
168
|
+
// testRegex: [],
|
|
169
|
+
|
|
170
|
+
// This option allows the use of a custom results processor
|
|
171
|
+
// testResultsProcessor: undefined,
|
|
172
|
+
|
|
173
|
+
// This option allows use of a custom test runner
|
|
174
|
+
// testRunner: "jest-circus/runner",
|
|
175
|
+
|
|
176
|
+
// A map from regular expressions to paths to transformers
|
|
177
|
+
// transform: undefined,
|
|
178
|
+
|
|
179
|
+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
|
|
180
|
+
// transformIgnorePatterns: [
|
|
181
|
+
// "/node_modules/",
|
|
182
|
+
// "\\.pnp\\.[^\\/]+$"
|
|
183
|
+
// ],
|
|
184
|
+
|
|
185
|
+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
|
|
186
|
+
// unmockedModulePathPatterns: undefined,
|
|
187
|
+
|
|
188
|
+
// Indicates whether each individual test should be reported during the run
|
|
189
|
+
// verbose: undefined,
|
|
190
|
+
|
|
191
|
+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
|
|
192
|
+
// watchPathIgnorePatterns: [],
|
|
193
|
+
|
|
194
|
+
// Whether to use watchman for file crawling
|
|
195
|
+
// watchman: true,
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export default config;
|
package/lib/generate.js
CHANGED
|
@@ -1,28 +1,6 @@
|
|
|
1
|
-
import { execSync } from
|
|
2
|
-
import
|
|
3
|
-
import path from
|
|
4
|
-
import chalk from "chalk";
|
|
5
|
-
|
|
6
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
-
const __dirname = path.dirname(__filename);
|
|
8
|
-
|
|
9
|
-
const getNestBinary = () => {
|
|
10
|
-
try {
|
|
11
|
-
execSync("nest --version", { stdio: "ignore" });
|
|
12
|
-
console.log(
|
|
13
|
-
chalk.bgMagenta("*** Using global Nest CLI Installation *** \n")
|
|
14
|
-
);
|
|
15
|
-
return "nest";
|
|
16
|
-
} catch (error) {
|
|
17
|
-
console.log(
|
|
18
|
-
chalk.bgBlueBright("*** Using local Nest CLI Installation *** \n")
|
|
19
|
-
);
|
|
20
|
-
return path.join(__dirname, "../node_modules/.bin/nest");
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const nest = getNestBinary();
|
|
25
|
-
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
import nestCli from './nestBynary.js';
|
|
3
|
+
import path from 'path';
|
|
26
4
|
/**
|
|
27
5
|
* Generates a NestJS component (module, controller, service, etc.).
|
|
28
6
|
* @param {string} type - The type of component to generate (e.g., "mo", "co", "s").
|
|
@@ -34,29 +12,29 @@ const generateComponent = (type, resourceName, options, projectName) => {
|
|
|
34
12
|
? path.join(process.cwd(), projectName)
|
|
35
13
|
: process.cwd();
|
|
36
14
|
|
|
37
|
-
const command = `${
|
|
38
|
-
Array.isArray(options) ? options.join(
|
|
15
|
+
const command = `${nestCli} ${type} ${resourceName} ${
|
|
16
|
+
Array.isArray(options) ? options.join(' ') : ''
|
|
39
17
|
}`;
|
|
40
18
|
|
|
41
|
-
execSync(command, { stdio:
|
|
19
|
+
execSync(command, { stdio: 'inherit', cwd: workingDirectory });
|
|
42
20
|
};
|
|
43
21
|
export const generateProject = (projectName, options) =>
|
|
44
|
-
generateComponent(
|
|
22
|
+
generateComponent('new', projectName, options);
|
|
45
23
|
|
|
46
24
|
export const generateModule = (resourceName, options, projectName) =>
|
|
47
|
-
generateComponent(
|
|
25
|
+
generateComponent('g mo', resourceName, options, projectName);
|
|
48
26
|
|
|
49
27
|
export const generateController = (resourceName, options, projectName) =>
|
|
50
|
-
generateComponent(
|
|
28
|
+
generateComponent('g co', resourceName, options, projectName);
|
|
51
29
|
|
|
52
30
|
export const generateService = (resourceName, options, projectName) =>
|
|
53
|
-
generateComponent(
|
|
31
|
+
generateComponent('g s', resourceName, options, projectName);
|
|
54
32
|
|
|
55
33
|
export const generateGateway = (resourceName, options, projectName) =>
|
|
56
|
-
generateComponent(
|
|
34
|
+
generateComponent('g ga', resourceName, options, projectName);
|
|
57
35
|
|
|
58
36
|
export const generateInterceptor = (resourceName, options, projectName) =>
|
|
59
|
-
generateComponent(
|
|
37
|
+
generateComponent('g itc', resourceName, options, projectName);
|
|
60
38
|
|
|
61
39
|
export const generateMiddlware = (resourceName, options, projectName) =>
|
|
62
|
-
generateComponent(
|
|
40
|
+
generateComponent('g mi', resourceName, options, projectName);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { execSync } from 'child_process';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Installs the specified dependencies into the given project using the specified package manager.
|
|
7
|
+
*
|
|
8
|
+
* This function performs the following steps:
|
|
9
|
+
* 1. Changes the working directory to the specified project folder.
|
|
10
|
+
* 2. Logs the list of dependencies to be installed.
|
|
11
|
+
* 3. Executes the appropriate package manager command to install the dependencies.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} projectName - The name of the project folder where dependencies will be installed.
|
|
14
|
+
* @param {string[]} dependencies - An array of dependency names (e.g., package names) to install.
|
|
15
|
+
* @param {string} packageManager - The package manager to use: 'npm', 'yarn', or 'pnpm'.
|
|
16
|
+
* @throws {Error} Throws an error if the installation command fails during execution.
|
|
17
|
+
*/
|
|
18
|
+
export default function installDependencies(
|
|
19
|
+
projectName,
|
|
20
|
+
dependencies,
|
|
21
|
+
packageManager = 'npm',
|
|
22
|
+
) {
|
|
23
|
+
const workingDirectory = path.join(process.cwd(), projectName);
|
|
24
|
+
|
|
25
|
+
const supportedPackageManagers = ['npm', 'yarn', 'pnpm'];
|
|
26
|
+
if (!supportedPackageManagers.includes(packageManager)) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Unsupported package manager: ${packageManager}. Supported options are: ${supportedPackageManagers.join(
|
|
29
|
+
', ',
|
|
30
|
+
)}`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
if (Array.isArray(dependencies) && dependencies.length > 0) {
|
|
36
|
+
console.log(chalk.cyan('Installing dependencies:'));
|
|
37
|
+
for (const dependency of dependencies) {
|
|
38
|
+
console.log(chalk.cyan(`- ${dependency}`));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let command;
|
|
42
|
+
switch (packageManager) {
|
|
43
|
+
case 'npm':
|
|
44
|
+
command = `npm install ${dependencies.join(' ')}`;
|
|
45
|
+
break;
|
|
46
|
+
case 'yarn':
|
|
47
|
+
command = `yarn add ${dependencies.join(' ')}`;
|
|
48
|
+
break;
|
|
49
|
+
case 'pnpm':
|
|
50
|
+
command = `pnpm add ${dependencies.join(' ')}`;
|
|
51
|
+
break;
|
|
52
|
+
default:
|
|
53
|
+
throw new Error(`Unsupported package manager: ${packageManager}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
execSync(command, { stdio: 'inherit', cwd: workingDirectory });
|
|
57
|
+
console.log(chalk.green('Dependencies installed successfully.'));
|
|
58
|
+
} else {
|
|
59
|
+
console.log(chalk.yellow('No dependencies to install.'));
|
|
60
|
+
}
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error(chalk.red(`Error installing dependencies: ${error.message}`));
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Determines the path to the Nest CLI binary to use for project generation.
|
|
10
|
+
*
|
|
11
|
+
* This function checks if the globally installed Nest CLI is available by running `nest --version`.
|
|
12
|
+
* - If the global Nest CLI is found, it logs a message indicating that the global installation will be used and returns `"nest"`.
|
|
13
|
+
* - If the global Nest CLI is not found, it falls back to using the locally cached Nest CLI binary from the `node_modules` directory.
|
|
14
|
+
*
|
|
15
|
+
* @returns {string} The command or path to the Nest CLI binary:
|
|
16
|
+
* - `"nest"` if the global Nest CLI is available.
|
|
17
|
+
* - A local path (e.g., `../node_modules/.bin/nest`) if the global Nest CLI is not available.
|
|
18
|
+
*/
|
|
19
|
+
const getNestBinary = () => {
|
|
20
|
+
try {
|
|
21
|
+
execSync('nest --version', { stdio: 'ignore' });
|
|
22
|
+
console.log(
|
|
23
|
+
chalk.bgGreenBright('*** Using your Nest CLI Installation *** \n'),
|
|
24
|
+
);
|
|
25
|
+
return 'nest';
|
|
26
|
+
} catch (_) {
|
|
27
|
+
console.log(
|
|
28
|
+
chalk.bgBlueBright(
|
|
29
|
+
'*** Using nest-combo cached Nest CLI Installation *** \n',
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
return path.join(__dirname, '../node_modules/.bin/nest');
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const nestCli = getNestBinary();
|
|
37
|
+
|
|
38
|
+
export default nestCli;
|
package/lib/outputs.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { createRequire } from 'module';
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
const { name, version } = require('../package.json');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Print the ASCII Art Banner
|
|
8
|
+
*/
|
|
9
|
+
export function printBanner() {
|
|
10
|
+
console.log(
|
|
11
|
+
chalk.magenta(`
|
|
12
|
+
███╗ ██╗███████╗███████╗████████╗ ██████╗ ██████╗ ███╗ ███╗██████╗ ██████╗
|
|
13
|
+
████╗ ██║██╔════╝██╔════╝╚══██╔══╝ ██╔════╝██╔═══██╗████╗ ████║██╔══██╗██╔═══██╗
|
|
14
|
+
██╔██╗ ██║█████╗ ███████╗ ██║ █████╗██║ ██║ ██║██╔████╔██║██████╔╝██║ ██║
|
|
15
|
+
██║╚██╗██║██╔══╝ ╚════██║ ██║ ╚════╝██║ ██║ ██║██║╚██╔╝██║██╔══██╗██║ ██║
|
|
16
|
+
██║ ╚████║███████╗███████║ ██║ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██████╔╝╚██████╔╝
|
|
17
|
+
╚═╝ ╚═══╝╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚═════╝
|
|
18
|
+
`),
|
|
19
|
+
);
|
|
20
|
+
console.log(
|
|
21
|
+
chalk.bgWhite(
|
|
22
|
+
chalk.bold(
|
|
23
|
+
`********************************************************************** version: ${version} `,
|
|
24
|
+
),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Show Instructions how to use nest-combo
|
|
31
|
+
*/
|
|
32
|
+
export function showUsage() {
|
|
33
|
+
console.log(`
|
|
34
|
+
${chalk.yellow('Usage:')} nest-combo <<project|module>-name> [options]
|
|
35
|
+
|
|
36
|
+
${chalk.yellow('Options:')}
|
|
37
|
+
${chalk.cyan('-new, --new-project')} Create a new project
|
|
38
|
+
${chalk.cyan('-m, --module')} Generate a Module
|
|
39
|
+
${chalk.cyan('-c, --controller')} Generate a Controller
|
|
40
|
+
${chalk.cyan('-s, --service')} Generate a Service
|
|
41
|
+
${chalk.cyan('-g, --gateway')} Generate a Gateway
|
|
42
|
+
${chalk.cyan('-mw, --middleware')} Generate Middleware
|
|
43
|
+
${chalk.cyan('-itc, --interceptor')} Generate an Interceptor
|
|
44
|
+
${chalk.cyan('-f, --file')} Generate project from yml file
|
|
45
|
+
|
|
46
|
+
${chalk.bgMagenta('Optional for Modules:')}
|
|
47
|
+
${chalk.cyan('-ns, --no-spec')} Do not generate spec (test) files
|
|
48
|
+
${chalk.cyan(
|
|
49
|
+
'-dr, --dry-run',
|
|
50
|
+
)} Report actions that would be taken without writing out results.
|
|
51
|
+
|
|
52
|
+
${chalk.yellow('Example:')}
|
|
53
|
+
To create a new project:
|
|
54
|
+
${chalk.cyan(
|
|
55
|
+
`nest-combo my-project-name -new ${chalk.gray(
|
|
56
|
+
'# VsCode will be opened directly into your new project',
|
|
57
|
+
)}`,
|
|
58
|
+
)}
|
|
59
|
+
|
|
60
|
+
To create a module, controller and servide in a single line command
|
|
61
|
+
${chalk.cyan('nest-combo users -m -c -s')}
|
|
62
|
+
|
|
63
|
+
To load a full project from a yml file
|
|
64
|
+
${chalk.cyan('nest-combo -f project.yml')}
|
|
65
|
+
`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Show current version
|
|
70
|
+
*/
|
|
71
|
+
export function showVersion() {
|
|
72
|
+
console.log(chalk.green(`${name} - version: ${version}`));
|
|
73
|
+
}
|
package/lib/resources.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generateModule,
|
|
3
|
+
generateController,
|
|
4
|
+
generateService,
|
|
5
|
+
generateInterceptor,
|
|
6
|
+
generateGateway,
|
|
7
|
+
generateMiddlware,
|
|
8
|
+
} from './generate.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An array of resource configurations used to define NestJS components
|
|
12
|
+
* such as modules, controllers, services, gateways, middleware, and interceptors.
|
|
13
|
+
*
|
|
14
|
+
* Each configuration object contains the following properties:
|
|
15
|
+
* @typedef {Object} ResourceConfig
|
|
16
|
+
* @property {string} name - The name of the resource (e.g., "module", "controller").
|
|
17
|
+
* @property {string} flag - The short flag used in the CLI command (e.g., "-m" for module).
|
|
18
|
+
* @property {string} longFlag - The long flag used in the CLI command (e.g., "--module" for module).
|
|
19
|
+
* @property {Function} generator - The function responsible for generating the resource.
|
|
20
|
+
*
|
|
21
|
+
* This array is used to dynamically generate resources based on user input.
|
|
22
|
+
* Each `generator` function is imported from the `./generate.js` file and corresponds
|
|
23
|
+
* to a specific NestJS component.
|
|
24
|
+
*/
|
|
25
|
+
export default [
|
|
26
|
+
{
|
|
27
|
+
name: 'module',
|
|
28
|
+
flag: '-m',
|
|
29
|
+
longFlag: '--module',
|
|
30
|
+
generator: generateModule,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'controller',
|
|
34
|
+
flag: '-c',
|
|
35
|
+
longFlag: '--controller',
|
|
36
|
+
generator: generateController,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'service',
|
|
40
|
+
flag: '-s',
|
|
41
|
+
longFlag: '--service',
|
|
42
|
+
generator: generateService,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'gateway',
|
|
46
|
+
flag: '-g',
|
|
47
|
+
longFlag: '--gateway',
|
|
48
|
+
generator: generateGateway,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: 'middleware',
|
|
52
|
+
flag: '-mw',
|
|
53
|
+
longFlag: '--middleware',
|
|
54
|
+
generator: generateMiddlware,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'interceptor',
|
|
58
|
+
flag: '-itc',
|
|
59
|
+
longFlag: '--interceptor',
|
|
60
|
+
generator: generateInterceptor,
|
|
61
|
+
},
|
|
62
|
+
];
|
package/lib/utils.js
CHANGED
|
@@ -1,26 +1,58 @@
|
|
|
1
|
-
import chalk from
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import resources from './resources.js';
|
|
3
|
+
import { showUsage } from '../lib/outputs.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validate resrouces creation
|
|
7
|
+
* @param {string} moduleName
|
|
8
|
+
* @param {Array} args - List of input arguments
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export function validateResources(moduleName, args) {
|
|
12
|
+
if (!moduleName) {
|
|
13
|
+
console.error(chalk.red('Error:') + ' Module name is required.');
|
|
14
|
+
showUsage();
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
const hasComponentFlags = resources.some(
|
|
18
|
+
(component) =>
|
|
19
|
+
args.includes(component.flag) || args.includes(component.longFlag),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
if (!hasComponentFlags) {
|
|
23
|
+
console.error(
|
|
24
|
+
chalk.red('Error:') +
|
|
25
|
+
` At least one flag (${resources
|
|
26
|
+
.map((resource) => resource.flag)
|
|
27
|
+
.join(',')}) is required.`,
|
|
28
|
+
);
|
|
29
|
+
showUsage();
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
2
34
|
/**
|
|
3
35
|
* Validate Yml File
|
|
4
36
|
* @param {Object} ymlData
|
|
5
37
|
*/
|
|
6
38
|
export function validateYml(ymlData) {
|
|
7
39
|
try {
|
|
8
|
-
if (!ymlData || typeof ymlData !==
|
|
40
|
+
if (!ymlData || typeof ymlData !== 'object' || !ymlData['nest-combo']) {
|
|
9
41
|
throw new Error("Invalid YAML file: Missing 'nest-combo' key.");
|
|
10
42
|
}
|
|
11
43
|
|
|
12
|
-
const nestCombo = ymlData[
|
|
44
|
+
const nestCombo = ymlData['nest-combo'];
|
|
13
45
|
|
|
14
46
|
if (
|
|
15
|
-
!nestCombo[
|
|
16
|
-
typeof nestCombo[
|
|
47
|
+
!nestCombo['project-name'] ||
|
|
48
|
+
typeof nestCombo['project-name'] !== 'string'
|
|
17
49
|
) {
|
|
18
50
|
throw new Error("Missing or invalid 'project-name'. Must be a string.");
|
|
19
51
|
}
|
|
20
52
|
|
|
21
53
|
if (
|
|
22
|
-
nestCombo[
|
|
23
|
-
typeof nestCombo[
|
|
54
|
+
nestCombo['package-manager'] &&
|
|
55
|
+
typeof nestCombo['package-manager'] !== 'string'
|
|
24
56
|
) {
|
|
25
57
|
throw new Error("Invalid 'package-manager'. Must be a string.");
|
|
26
58
|
}
|
|
@@ -36,7 +68,7 @@ export function validateYml(ymlData) {
|
|
|
36
68
|
validateModules(nestCombo.modules);
|
|
37
69
|
}
|
|
38
70
|
|
|
39
|
-
console.log(chalk.green(
|
|
71
|
+
console.log(chalk.green('YAML file is valid.'));
|
|
40
72
|
} catch (error) {
|
|
41
73
|
console.error(chalk.red(`Validation Error: ${error.message}`));
|
|
42
74
|
process.exit(1);
|
|
@@ -49,32 +81,32 @@ export function validateYml(ymlData) {
|
|
|
49
81
|
*/
|
|
50
82
|
function validateModules(modules) {
|
|
51
83
|
modules.forEach((module, index) => {
|
|
52
|
-
if (!module.name || typeof module.name !==
|
|
84
|
+
if (!module.name || typeof module.name !== 'string') {
|
|
53
85
|
throw new Error(
|
|
54
|
-
`Invalid module at index ${index}. Each module must have a 'name' field as a string
|
|
86
|
+
`Invalid module at index ${index}. Each module must have a 'name' field as a string.`,
|
|
55
87
|
);
|
|
56
88
|
}
|
|
57
89
|
|
|
58
90
|
if (module.resources && !Array.isArray(module.resources)) {
|
|
59
91
|
throw new Error(
|
|
60
|
-
`Invalid 'resources' for module '${module.name}'. Must be an array
|
|
92
|
+
`Invalid 'resources' for module '${module.name}'. Must be an array.`,
|
|
61
93
|
);
|
|
62
94
|
}
|
|
63
95
|
if (module.resources) {
|
|
64
96
|
const validResources = [
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
97
|
+
'module',
|
|
98
|
+
'controller',
|
|
99
|
+
'service',
|
|
100
|
+
'gateway',
|
|
101
|
+
'middleware',
|
|
102
|
+
'interceptor',
|
|
71
103
|
];
|
|
72
104
|
module.resources.forEach((resource, resIndex) => {
|
|
73
105
|
if (!validResources.includes(resource)) {
|
|
74
106
|
throw new Error(
|
|
75
107
|
`Invalid resource '${resource}' at index ${resIndex} for module '${
|
|
76
108
|
module.name
|
|
77
|
-
}'. Valid resources are: ${validResources.join(
|
|
109
|
+
}'. Valid resources are: ${validResources.join(', ')}.`,
|
|
78
110
|
);
|
|
79
111
|
}
|
|
80
112
|
});
|
|
@@ -82,13 +114,13 @@ function validateModules(modules) {
|
|
|
82
114
|
|
|
83
115
|
if (module.options && !Array.isArray(module.options)) {
|
|
84
116
|
throw new Error(
|
|
85
|
-
`Invalid 'options' for module '${module.name}'. Must be an array
|
|
117
|
+
`Invalid 'options' for module '${module.name}'. Must be an array.`,
|
|
86
118
|
);
|
|
87
119
|
}
|
|
88
120
|
|
|
89
121
|
if (module.modules && !Array.isArray(module.modules)) {
|
|
90
122
|
throw new Error(
|
|
91
|
-
`Invalid 'modules' for module '${module.name}'. Must be an array
|
|
123
|
+
`Invalid 'modules' for module '${module.name}'. Must be an array.`,
|
|
92
124
|
);
|
|
93
125
|
}
|
|
94
126
|
if (module.modules) {
|