savory
Version:
A command-line interface for operating your Codefresh account
35 lines (33 loc) • 1.44 kB
JavaScript
const
fp = require('lodash/fp'),
fs = require('fs'),
path = require('path'),
kefir = require('kefir'),
jsYaml = require('js-yaml'),
{ errorFormatter } = require('../style.js'),
{ middleware: apiMiddleware } = require('../../lib/api_client');
module.exports = {
command: "create",
describe: "Creates a new YAML pipeline file",
builder: (yargs)=> yargs
.positional('filename', {
type: "string",
describe: "The path to Codefresh's YAML pipeline file. If a filename is not supplied, YAML is expected through stdin"
}),
handler: fp.pipe(apiMiddleware, ({ _httpClient, filename })=> {
(!filename
? kefir
.fromEvents(process.stdin, 'data')
.takeUntilBy(kefir.fromEvents(process.stdin, 'close'))
.scan(fp.concat, [])
.last()
.map(Buffer.concat)
: kefir
.fromNodeCallback((cb)=> fs.readFile(path.resolve(process.cwd(), filename), cb)))
.map(fp.pipe(fp.toString, jsYaml.safeLoad))
.flatMap(fp.pipe(fp.partial(_httpClient, ["pipelines", {}, 'POST']), kefir.fromPromise))
.map(()=> `Created successfully`)
.onValue(console.log)
.onError((err = {})=> console.warn((err["name"] === "DUPLICATION_ERROR" ? fp.constant('Pipeline already exists') : errorFormatter)(err)));
})
};