mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Also created a command that lists workflows so it can be used by other applications that wish to interact with n8n via CLI.
67 lines
1.4 KiB
TypeScript
67 lines
1.4 KiB
TypeScript
import {
|
|
Command, flags,
|
|
} from '@oclif/command';
|
|
|
|
import {
|
|
IDataObject
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
Db,
|
|
} from "../../src";
|
|
|
|
|
|
export class ListWorkflowCommand extends Command {
|
|
static description = '\nList workflows';
|
|
|
|
static examples = [
|
|
`$ n8n list:workflow`,
|
|
`$ n8n list:workflow --active=true --onlyId`,
|
|
`$ n8n list:workflow --active=false`,
|
|
];
|
|
|
|
static flags = {
|
|
help: flags.help({ char: 'h' }),
|
|
active: flags.string({
|
|
description: 'Filters workflows by active status. Can be true or false',
|
|
}),
|
|
onlyId: flags.boolean({
|
|
description: 'Outputs workflow IDs only, one per line.',
|
|
}),
|
|
};
|
|
|
|
async run() {
|
|
const { flags } = this.parse(ListWorkflowCommand);
|
|
|
|
if (flags.active !== undefined && !['true', 'false'].includes(flags.active)) {
|
|
this.error('The --active flag has to be passed using true or false');
|
|
}
|
|
|
|
try {
|
|
await Db.init();
|
|
|
|
const findQuery: IDataObject = {};
|
|
if (flags.active !== undefined) {
|
|
findQuery.active = flags.active === 'true';
|
|
}
|
|
|
|
const workflows = await Db.collections.Workflow!.find(findQuery);
|
|
if (flags.onlyId) {
|
|
workflows.map(workflow => console.log(workflow.id));
|
|
} else {
|
|
workflows.map(workflow => console.log(workflow.id + "|" + workflow.name));
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
console.error('\nGOT ERROR');
|
|
console.log('====================================');
|
|
console.error(e.message);
|
|
console.error(e.stack);
|
|
this.exit(1);
|
|
}
|
|
|
|
this.exit();
|
|
}
|
|
}
|