Skip to content

Commit

Permalink
feat: cmd-update #8
Browse files Browse the repository at this point in the history
  • Loading branch information
midnqp committed Mar 9, 2023
1 parent c591f51 commit edde046
Show file tree
Hide file tree
Showing 6 changed files with 666 additions and 567 deletions.
123 changes: 65 additions & 58 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,67 @@
{
"name": "pcli",
"type": "module",
"scripts": {
"test:backend": "npx ts-node-esm test/server.ts",
"build": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json",
"build:bin": "npm run build && npm run prepack:esbuild:cjs && npm run prepack:pkg",
"build:bin:all": "npm run build && npm run prepack:esbuild:cjs && npm run prepack:pkg:all",
"dev": "tsc-watch --noClear -p tsconfig.json",
"prepack:webpack": "npx webpack --config webpack.config.cjs",
"prepack:pkg:all": "npx pkg --options 'no-warnings' -t node18-linux-x64,node18-macos-x64,node18-win-x64 ./dist/bundle.cjs -o ./bin/pcli",
"prepack:pkg": "npx pkg --options 'no-warnings' -t node18 ./dist/bundle.cjs -o ./bin/pcli",
"prepack:esbuild:cjs": "npx esbuild dist/src/index.js --bundle --outfile=dist/bundle.cjs --format=cjs --platform=node",
"prepack:esbuild:esm": "npx esbuild dist/src/index.js --bundle --outfile=dist/bundle.mjs --format=esm --platform=node --banner:js=\"import {createRequire} from 'module'; const require = createRequire(import.meta.url); import { dirname } from 'path'; import { fileURLToPath } from 'url'; const __dirname = dirname(fileURLToPath(import.meta.url));\""
},
"dependencies": {
"axios": "^0.27.2",
"browserify": "^17.0.0",
"chai": "^4.3.7",
"chalk": "^5.2.0",
"clean-stack": "^5.1.0",
"commander": "^9.4.0",
"content-type": "^1.0.4",
"dotenv": "^16.0.1",
"enquirer": "^2.3.6",
"fs-extra": "^11.1.0",
"lodash": "^4.17.21",
"newman": "^5.3.2",
"postman-collection": "^4.1.5",
"pretty-bytes": "^6.1.0",
"terser": "^5.16.5",
"uuid": "^9.0.0",
"winston": "^3.8.2"
},
"devDependencies": {
"@babel/cli": "^7.18.10",
"@babel/core": "^7.18.10",
"@inquirer/editor": "^0.0.21-alpha.0",
"@types/chai": "^4.3.4",
"@types/content-type": "^1.1.5",
"@types/express": "^4.17.17",
"@types/fs-extra": "^9.0.13",
"@types/lodash": "^4.14.182",
"@types/newman": "^5.3.1",
"@types/postman-collection": "^3.5.7",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"eslint": "^8.33.0",
"express": "^4.18.2",
"pkg": "^5.8.0",
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.2",
"tsc-watch": "^5.0.3",
"typescript": "^4.7.4"
},
"bundleDependencies": [
"terser"
]
"name": "pcli",
"type": "module",
"main": "./src/index.js",
"scripts": {
"test:backend": "npx ts-node-esm test/server.ts",
"build": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json",
"build:bin": "npm run build && npm run prepack:esbuild:cjs && npm run prepack:pkg",
"build:bin:all": "npm run build && npm run prepack:esbuild:cjs && npm run prepack:pkg:all",
"dev": "tsc-watch --noClear -p tsconfig.json",
"prepack:webpack": "npx webpack --config webpack.config.cjs",
"prepack:pkg:all": "npx pkg --options 'no-warnings' -t node18-linux-x64,node18-macos-x64,node18-win-x64 ./dist/bundle.cjs -o ./bin/pcli",
"prepack:pkg": "npx pkg --options 'no-warnings' -t node18 ./dist/bundle.cjs -o ./bin/pcli",
"prepack:esbuild:cjs": "npx esbuild dist/src/index.js --bundle --outfile=dist/bundle.cjs --format=cjs --platform=node",
"prepack:esbuild:esm": "npx esbuild dist/src/index.js --bundle --outfile=dist/bundle.mjs --format=esm --platform=node --banner:js=\"import {createRequire} from 'module'; const require = createRequire(import.meta.url); import { dirname } from 'path'; import { fileURLToPath } from 'url'; const __dirname = dirname(fileURLToPath(import.meta.url));\""
},
"dependencies": {
"axios": "^0.27.2",
"browserify": "^17.0.0",
"chai": "^4.3.7",
"chalk": "^5.2.0",
"clean-stack": "^5.1.0",
"commander": "^9.4.0",
"content-type": "^1.0.4",
"dotenv": "^16.0.1",
"enquirer": "^2.3.6",
"fs-extra": "^11.1.0",
"js-object-pretty-print": "^0.3.0",
"lodash": "^4.17.21",
"newman": "^5.3.2",
"postman-collection": "^4.1.5",
"pretty-bytes": "^6.1.0",
"terser": "^5.16.5",
"tmp": "^0.2.1",
"uuid": "^9.0.0",
"winston": "^3.8.2"
},
"devDependencies": {
"@babel/cli": "^7.18.10",
"@babel/core": "^7.18.10",
"@inquirer/editor": "^0.0.21-alpha.0",
"@types/chai": "^4.3.4",
"@types/content-type": "^1.1.5",
"@types/express": "^4.17.17",
"@types/fs-extra": "^9.0.13",
"@types/lodash": "^4.14.182",
"@types/newman": "^5.3.1",
"@types/postman-collection": "^3.5.7",
"@types/tmp": "^0.2.3",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"esbuild": "^0.17.11",
"eslint": "^8.33.0",
"express": "^4.18.2",
"open": "^8.4.2",
"open-editor": "^4.0.0",
"pkg": "^5.8.0",
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.2",
"tsc-watch": "^5.0.3",
"typescript": "^4.7.4"
},
"bundleDependencies": [
"terser"
]
}
119 changes: 92 additions & 27 deletions src/handlers/update.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,99 @@
import services from '@src/services/index.js'
import psdk from 'postman-collection'
import { PostmanCli } from '@src/types'
import { Command } from 'commander'
import editor from '@inquirer/editor'
import {PostmanCli} from '@src/types'
import {Command} from 'commander'
import util from 'node:util'
import _ from 'lodash'
import Enquirer from 'enquirer'
import openeditor from 'open-editor'
import editor from '@inquirer/editor'
import open from 'open'
import tmp from 'tmp'
import fs from 'node:fs'

function openJson(input: string) {
const {name} = tmp.fileSync({postfix: '.js'})
fs.writeFileSync(name, input)

//openeditor([{file: name}])
open(name)
return name
}

export default async function (
args: PostmanCli.Cmd.VariadicResources,
..._cmd: [PostmanCli.Cmd.Opts.Update, Command]
args: PostmanCli.Cmd.VariadicResources,
..._cmd: [PostmanCli.Cmd.Opts.Update, Command]
) {
const [optional, cmd] = _cmd
args = args.map(e => e.toLowerCase())
const co = await services.cmdopts.getOptCollection(cmd)
const resource = services.resource.getFromNested(co, args)

if (services.response.isResponse(resource)) {
const p = services.example.toPrintable(resource)
const str = util.inspect(p, {
colors: false,
maxArrayLength: null,
maxStringLength: null,
depth: 50,
})
const prompt: any = await editor({ default: str, message: '' })

new psdk.Response({
code: prompt.response.code,
responseTime: prompt.response.time,
body: prompt.response.body,
header: [{}],
})
}
const [optional, cmd] = _cmd
args = args.map(e => e.toLowerCase())
const co = await services.cmdopts.getOptCollection(cmd)
const resource = services.resource.getFromNested(co, args)

if (services.response.isResponse(resource)) {
const p = services.example.toPrintable(resource, {addParsedBody: true})

const str = util.inspect(p, {
colors: false,
maxArrayLength: null,
maxStringLength: null,
depth: 50,
})
//const prompt = await editor({default: str, message: ''})
const filename = openJson(str)
let done: boolean
try {
const enq = await Enquirer.prompt({name: 'done', required: true, initial: true, message: 'press any key to continue', type: 'confirm'}) as any
done = enq.done
} catch (err) {done = false}

if (!done) return

type ParsedPrompt = {request: PostmanCli.RequestPrintable, response: PostmanCli.ResponsePrintable}
let prompt: ParsedPrompt
try {
prompt = eval("(" + await fs.promises.readFile(filename, 'utf8') + ")")
} catch (e) {throw Error('could not parse')}

const parsed = prompt
//const parsed: ParsedPrompt = JSON.parse(prompt)

let request: psdk.RequestDefinition | undefined
if (parsed.request && !_.isEmpty(parsed.request)) {
const rawBody = JSON.stringify(parsed.request.body)
request = {
url: {path: parsed.request.url.path, query: services.common.jsonToHeaders(parsed.request.query)},
body: {mode: 'raw', raw: rawBody},
header: services.common.jsonToHeaders(parsed.request.headers),
method: parsed.request.url.method,

}
}
const response = new psdk.Response({
code: parsed.response.code,
responseTime: parsed.response.time,
body: JSON.stringify(parsed.response.body),
header: services.common.jsonToHeaders(parsed.response.headers),

originalRequest: request
})


services.example.print(response)
const fn = async () => {
try {
const {saveOrNot} = await Enquirer.prompt({
name: 'saveOrNot', type: 'select', message: 'save changes?',
choices: [{name: 'yes'}, {name: 'no'}]
}) as any

return saveOrNot
} catch (e) {return 'no'}
}
const saveOrNot = await fn()

if (saveOrNot == 'yes') {
resource.update(response)
await services.collection.save(cmd, co)
}
}
}
Loading

0 comments on commit edde046

Please sign in to comment.