@postmate/cli 0.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 +1 -0
- package/dist/index.js +104 -0
- package/dist/utils/cliLogger.js +20 -0
- package/dist/utils/project.js +16 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# postmateli
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import { findPostmateRoot } from './utils/project.js';
|
|
4
|
+
import { CoreContext } from '@postmate/core';
|
|
5
|
+
import { cliLogger } from "./utils/cliLogger.js";
|
|
6
|
+
import fs from "fs";
|
|
7
|
+
import path from "path";
|
|
8
|
+
const program = new Command();
|
|
9
|
+
program
|
|
10
|
+
.name('pmc')
|
|
11
|
+
.description('Postmate CLI')
|
|
12
|
+
.version('0.1.0');
|
|
13
|
+
const runCommand = program
|
|
14
|
+
.command('run')
|
|
15
|
+
.description('Run a Postmate collection')
|
|
16
|
+
.argument('[collection]', 'collection name')
|
|
17
|
+
.argument('[env]', 'environment name')
|
|
18
|
+
.argument('[data]', 'data table name')
|
|
19
|
+
.option('-c, --collection <name>', 'collection name')
|
|
20
|
+
.option('-e, --env <name>', 'environment name')
|
|
21
|
+
.option('-d, --data <name>', 'data table name')
|
|
22
|
+
.action(async (collectionArg, envArg, dataArg, options) => {
|
|
23
|
+
try {
|
|
24
|
+
const collection = options.collection ?? collectionArg;
|
|
25
|
+
const env = options.env ?? envArg;
|
|
26
|
+
const data = options.data ?? dataArg;
|
|
27
|
+
if (!collection) {
|
|
28
|
+
console.error('Error: Collection name is required');
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
const baseDir = findPostmateRoot();
|
|
32
|
+
const coreContext = new CoreContext({
|
|
33
|
+
baseDir,
|
|
34
|
+
logger: cliLogger // simple logger for CLI
|
|
35
|
+
});
|
|
36
|
+
const runner = coreContext.collectionRunner;
|
|
37
|
+
let hasFailures = false;
|
|
38
|
+
const collectionObj = await coreContext.collectionManager.getCollectionByName(collection);
|
|
39
|
+
if (!collectionObj) {
|
|
40
|
+
throw new Error(`Collection not found: ${collection}`);
|
|
41
|
+
}
|
|
42
|
+
await runner.run({
|
|
43
|
+
collectionName: collection,
|
|
44
|
+
containerId: collectionObj.id,
|
|
45
|
+
requestIds: [],
|
|
46
|
+
env: env,
|
|
47
|
+
iterations: 1,
|
|
48
|
+
delayMs: 0,
|
|
49
|
+
dataTableName: data
|
|
50
|
+
}, {
|
|
51
|
+
onRunStart: (info) => {
|
|
52
|
+
console.log(`š Running ${info.collectionName}`);
|
|
53
|
+
console.log(`Env: ${info.env}`);
|
|
54
|
+
console.log(`Iterations: ${info.iterations}`);
|
|
55
|
+
console.log(`Total Requests: ${info.totalRequests}`);
|
|
56
|
+
},
|
|
57
|
+
onRequestEnd: (info) => {
|
|
58
|
+
const testResult = info.response.testResult;
|
|
59
|
+
const statusCode = info.response?.status ?? 0;
|
|
60
|
+
const passed = statusCode >= 200 && statusCode < 300;
|
|
61
|
+
const symbol = passed ? 'ā' : 'ā';
|
|
62
|
+
console.log(`[Itr. ${info.iteration}] ${info.request.name} [${statusCode}]`);
|
|
63
|
+
if (testResult?.length)
|
|
64
|
+
console.warn('---------------Tests Results-------------');
|
|
65
|
+
testResult?.forEach(tr => {
|
|
66
|
+
let tcPass = tr.status == 'Passed' ? 'ā' : 'ā';
|
|
67
|
+
console.info(`[${tcPass}] ${tr.description} `);
|
|
68
|
+
});
|
|
69
|
+
if (!passed) {
|
|
70
|
+
hasFailures = true;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
onComplete: (info) => {
|
|
74
|
+
const seconds = Math.floor(info.durationMs / 1000);
|
|
75
|
+
console.log(`\nFinished in ${seconds}s`);
|
|
76
|
+
console.log(`Total Requests: ${info.totalRequests}`);
|
|
77
|
+
// 1ļøā£ Create reports folder
|
|
78
|
+
const reportsDir = path.join(process.cwd(), "reports");
|
|
79
|
+
if (!fs.existsSync(reportsDir)) {
|
|
80
|
+
fs.mkdirSync(reportsDir, { recursive: true });
|
|
81
|
+
}
|
|
82
|
+
// 2ļøā£ Generate filename
|
|
83
|
+
const timestamp = new Date()
|
|
84
|
+
.toISOString()
|
|
85
|
+
.replace(/:/g, "-");
|
|
86
|
+
const fileName = `${collection}-${env}-${timestamp}.json`;
|
|
87
|
+
const filePath = path.join(reportsDir, fileName);
|
|
88
|
+
// 3ļøā£ Write JSON
|
|
89
|
+
fs.writeFileSync(filePath, JSON.stringify(info.reportJson, null, 2), "utf-8");
|
|
90
|
+
console.log(`Report saved: ${filePath}`);
|
|
91
|
+
},
|
|
92
|
+
onError: (err) => {
|
|
93
|
+
console.error('Run failed:', err);
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
process.exit(hasFailures ? 1 : 0);
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
console.error(err.message || err);
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const cliLogger = {
|
|
2
|
+
onRequestStart(info) {
|
|
3
|
+
console.log(`\nā ${info.method} ${info.url}`);
|
|
4
|
+
},
|
|
5
|
+
onRequestEnd(info) {
|
|
6
|
+
const status = info.status;
|
|
7
|
+
const time = info.responseTime;
|
|
8
|
+
const symbol = status >= 200 && status < 300 ? 'ā' : 'ā';
|
|
9
|
+
console.log(`${symbol} ${status} (${time}ms)`);
|
|
10
|
+
},
|
|
11
|
+
onScriptLog(msg) {
|
|
12
|
+
console.log(` [log] ${msg}`);
|
|
13
|
+
},
|
|
14
|
+
onScriptWarn(msg) {
|
|
15
|
+
console.warn(` [warn] ${msg}`);
|
|
16
|
+
},
|
|
17
|
+
onScriptError(msg) {
|
|
18
|
+
console.error(` [error] ${msg}`);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export function findPostmateRoot(startDir = process.cwd()) {
|
|
4
|
+
let current = startDir;
|
|
5
|
+
while (true) {
|
|
6
|
+
const candidate = path.join(current, '.postmate');
|
|
7
|
+
if (fs.existsSync(candidate)) {
|
|
8
|
+
return candidate;
|
|
9
|
+
}
|
|
10
|
+
const parent = path.dirname(current);
|
|
11
|
+
if (parent === current) {
|
|
12
|
+
throw new Error('No .postmate directory found');
|
|
13
|
+
}
|
|
14
|
+
current = parent;
|
|
15
|
+
}
|
|
16
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@postmate/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Postmate CLI - Run API collections from terminal",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"api",
|
|
10
|
+
"cli",
|
|
11
|
+
"testing",
|
|
12
|
+
"postman-alternative",
|
|
13
|
+
"automation"
|
|
14
|
+
],
|
|
15
|
+
"author": "Shyam Naryan Yadav",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@postmate/core": "^0.1.15",
|
|
19
|
+
"chalk": "^5.6.2",
|
|
20
|
+
"commander": "^14.0.3"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^25.2.1",
|
|
24
|
+
"rimraf": "^6.1.2",
|
|
25
|
+
"ts-node": "^10.9.2",
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
},
|
|
28
|
+
"bin": {
|
|
29
|
+
"pmc": "dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc",
|
|
33
|
+
"dev": "ts-node src/index.ts",
|
|
34
|
+
"clean": "rimraf dist node_modules package-lock.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
},
|
|
42
|
+
"clean": "rimraf dist"
|
|
43
|
+
}
|