mockapi-msi 1.0.1 → 1.1.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
@@ -92,4 +92,18 @@ log: verbose
92
92
  #error <- Only exposes internal errors
93
93
  #verbose <- Logs debug, information and errors
94
94
  #none <- Turn off the logs
95
- ```
95
+ ```
96
+
97
+ ## MockAPI CLI
98
+
99
+ MockAPI provides a small but helpful CLI. Type ```mockapi --help``` to get the available commands from the CLI once MockAPI is installed.
100
+
101
+ #### The ```init``` command
102
+
103
+ Once MockAPI is globally installed, you will need a configuration file with minimal information to be able of start mocking the API. The ```init``` command argument will lead you through different basic questions helping you to initialize this configuration file.
104
+
105
+ ```powershell
106
+ mockapi --init
107
+ ```
108
+
109
+ You can skip every question which will assign some default values to the configuration file. Later you could change these values using any text editor.
package/main.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  'use strict';
4
4
 
@@ -9,9 +9,17 @@ const constants = require('./modules/constants');
9
9
  const readers = require('./modules/readers');
10
10
  const parser = require('./modules/urlParser');
11
11
  const handlerInjector = require('./modules/configurationParser');
12
- const ModuleProxy = require('./modules/moduleProxy')
13
-
12
+ const ModuleProxy = require('./modules/moduleProxy');
13
+ const CLI = require('./modules/cli');
14
14
  const rootPath = __dirname;
15
+
16
+ const cli = new CLI(`${rootPath}/${constants.CONFIG_FILE_NAME}`);
17
+
18
+ if (cli.hasCommands()) {
19
+ cli.executeCommandLine();
20
+ return false;
21
+ }
22
+
15
23
  const configFile = readers.text_reader(`${rootPath}/${constants.CONFIG_FILE_NAME}`);
16
24
  const parsedConfiguration = YAML.parse(configFile());
17
25
 
package/modules/cli.js ADDED
@@ -0,0 +1,107 @@
1
+ const readline = require('readline');
2
+ const YAML = require('yaml');
3
+ const fs = require('fs');
4
+
5
+ class CLI {
6
+
7
+ _configurationFilePath = "";
8
+ _arguments = [];
9
+ _commands = {
10
+ "--help": this._helpCommand,
11
+ "help": this._helpCommand,
12
+ "init": this._initCommand,
13
+ "--init": this._initCommand
14
+ };
15
+ _configTemplate = {
16
+ port: 8080,
17
+ enableCors: true,
18
+ data: {
19
+ myRows: { path: 'YOUR FOLDER', reader: 'folder' }
20
+ },
21
+ endpoints: {
22
+ '/data': {
23
+ verb: 'get',
24
+ data: 'myRows',
25
+ responseStatus: 200,
26
+ responseContentType: 'application/json'
27
+ }
28
+ },
29
+ log: 'verbose'
30
+ };
31
+
32
+ constructor(configurationFilePath) {
33
+ this._configurationFilePath = configurationFilePath;
34
+ this._arguments = process.argv.slice(2);
35
+ }
36
+
37
+ _initCommand() {
38
+ let configTemplate = this._configTemplate;
39
+
40
+ const rl = readline.createInterface({
41
+ input: process.stdin,
42
+ output: process.stdout
43
+ });
44
+
45
+ console.log("Creating a basic configuration file");
46
+ console.log("");
47
+
48
+ rl.question(`Set MockAPI listening port (8080): `, (port) => {
49
+ rl.question(`Do you want to enable CORS? Y/n: `, (enableCors) => {
50
+ rl.question(`Do you want to include default endpoint? Y/n: `, (defaultEndpoint) => {
51
+ const configPort = port || 8080;
52
+ const configEnableCors = enableCors === "Y" || enableCors === "y" || enableCors === "";
53
+ const configDefaultEndpoint = defaultEndpoint === "Y" || defaultEndpoint === "y" || defaultEndpoint === "";
54
+
55
+ configTemplate.port = configPort;
56
+ configTemplate.enableCors = configEnableCors;
57
+
58
+ if (configDefaultEndpoint === false) {
59
+ delete configTemplate.endpoints;
60
+ delete configTemplate.data;
61
+ }
62
+
63
+ const configFile = YAML.stringify(configTemplate);
64
+
65
+ fs.writeFile(this._configurationFilePath, configFile, (err) => {
66
+ if (err) {
67
+ console.log(err);
68
+ } else {
69
+ console.log("Configuration file created");
70
+ }
71
+ });
72
+
73
+ rl.close();
74
+ });
75
+ });
76
+ });
77
+
78
+ }
79
+
80
+ _helpCommand() {
81
+ console.log("Run MockAPI:");
82
+ console.log("mockapi\n");
83
+ console.log("Command execution:\n");
84
+ console.log("mockapi <command>");
85
+ console.log("");
86
+ console.log("--help, help shows this help");
87
+ console.log("--init, init creates a basic configuration file");
88
+ console.log("");
89
+ }
90
+
91
+ hasCommands() { return this._arguments.length > 0; }
92
+
93
+ executeCommandLine() {
94
+ if (this._arguments.length > 0) {
95
+
96
+ let command = this._commands["--help"];
97
+
98
+ if (this._commands[this._arguments[0]] !== undefined) {
99
+ command = this._commands[this._arguments[0]];
100
+ }
101
+
102
+ command.apply(this);
103
+ }
104
+ }
105
+ }
106
+
107
+ module.exports = CLI;
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "mockapi-msi",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Mock API is a lightweight configurable HTTP API for testing and prototyping.",
5
5
  "main": "main.js",
6
6
  "scripts": {
7
+ "start": "node ./main.js",
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },
9
10
  "author": "Matias Iacono",
@@ -11,6 +12,9 @@
11
12
  "bin": {
12
13
  "mockapi": "main.js"
13
14
  },
15
+ "engines": {
16
+ "node": ">=16"
17
+ },
14
18
  "dependencies": {
15
19
  "yaml": "2.0.0-5"
16
20
  }