UNPKG

sb-mig

Version:

CLI to rule the world. (and handle stuff related to Storyblok CMS)

146 lines (145 loc) 3.85 kB
#! /usr/bin/env node import meow from "meow"; import { pipe, prop } from "../utils/main.js"; import { backupDescription, debugDescription, mainDescription, syncDescription, removeDescription, initDescription, discoverDescription, migrateDescription, revertDescription, migrationsDescription, copyDescription, } from "./cli-descriptions.js"; import { backup } from "./commands/backup.js"; import { copyCommand } from "./commands/copy.js"; import { debug } from "./commands/debug.js"; import { discover } from "./commands/discover.js"; import { init } from "./commands/init.js"; import { migrate } from "./commands/migrate.js"; import { migrations } from "./commands/migrations.js"; import { remove } from "./commands/remove.js"; import { revert } from "./commands/revert.js"; import { sync } from "./commands/sync.js"; import { testCommand } from "./commands/test.js"; const app = () => ({ cli: meow(mainDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => cli.showHelp(), }); app.sync = () => ({ cli: meow(syncDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { sync(cli); }, }); app.copy = () => ({ cli: meow(copyDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { copyCommand(cli); }, }); app.migrate = () => ({ cli: meow(migrateDescription, { importMeta: import.meta, booleanDefault: undefined, flags: { from: { type: "string", }, to: { type: "string", }, migrateFrom: { type: "string", default: "space", isRequired: true, }, }, }), action: (cli) => { migrate(cli); }, }); app.revert = () => ({ cli: meow(revertDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { revert(cli); }, }); app.discover = () => ({ cli: meow(discoverDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { discover(cli); }, }); app.migrations = () => ({ cli: meow(migrationsDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { migrations(cli); }, }); app.remove = () => ({ cli: meow(removeDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { remove(cli); }, }); app.backup = () => ({ cli: meow(backupDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { backup(cli); }, }); app.debug = () => ({ cli: meow(debugDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: () => { debug(); }, }); app.init = () => ({ cli: meow(initDescription, { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { init(cli); }, }); app.test = () => ({ cli: meow("nothing", { importMeta: import.meta, booleanDefault: undefined, }), action: (cli) => { testCommand(cli); }, }); const getSubcommand = (cliObject, level) => pipe(prop("input"), prop(level), (name) => prop(name)(cliObject))(prop("cli")(cliObject())); const cli = (cliObject, level = 0) => { const { cli: nextCli, action } = cliObject(); const subCommand = getSubcommand(cliObject, level); return subCommand ? cli(subCommand, level + 1) : nextCli.flags.help ? nextCli.showHelp() : action(nextCli); }; cli(app);