UNPKG

5.54 kBJavaScriptView Raw
1let yargs = require('yargs').argv;
2let logger = require('./util/logger');
3let npath = require('path');
4
5let packageFile = npath.join(__dirname, '..');
6if (packageFile.endsWith('distribution')) {
7 packageFile = npath.join(packageFile, '..');
8}
9packageFile = npath.join(packageFile, 'package.json');
10
11const COMMANDS = [{
12 name: 'version',
13 description: "Shows the current version",
14 runner: args => {
15 console.log("DataFire v" + require(packageFile).version);
16 return Promise.resolve();
17 }
18}, {
19 name: 'serve',
20 description: "Serve the DataFire project in the current directory",
21 examples: ["datafire serve --port 3000"],
22 runner: require('./commands/serve'),
23 options: [{
24 name: 'port',
25 alias: 'p',
26 description: "The port to use",
27 }, {
28 name: 'directory',
29 alias: 'd',
30 description: "Location of the DataFire project",
31 }, {
32 name: 'tasks',
33 alias: 't',
34 description: "Run tasks",
35 }]
36}, {
37 name: 'list',
38 description: "List integrations in the current project, or all available integrations if -a is used",
39 examples: ["datafire list", "datafire list -a"],
40 runner: require('./commands/list'),
41 options: [{
42 name: 'all',
43 alias: 'a',
44 description: "Show all available integrations",
45 }, {
46 name: 'query',
47 alias: 'q',
48 description: "Filter integrations by text",
49 }]
50}, {
51 name: 'integrate [integrations..]',
52 description: "Add an integration to the current project",
53 examples: ["datafire integrate --rss https://www.reddit.com/.rss"],
54 runner: require('./commands/integrate'),
55 options: [{
56 name: 'openapi',
57 description: "The URL of an Open API specification",
58 }, {
59 name: 'rss',
60 description: "The URL of an RSS feed",
61 }, {
62 name: 'raml',
63 description: "The URL of a RAML file",
64 }, {
65 name: 'wadl',
66 description: "The URL of a WADL file",
67 }, {
68 name: 'swagger_1',
69 description: "The URL of a Swagger 1.x file",
70 }, {
71 name: 'api_blueprint',
72 description: "The URL of an API Blueprint file",
73 }, {
74 name: 'io_docs',
75 description: "The URL of an I/O docs file",
76 }, {
77 name: 'google',
78 description: "The URL of a Google API specification",
79 }, {
80 name: 'name',
81 description: "An alias to use for the integration in this project",
82 required: true,
83 }]
84}, {
85 name: 'describe <action_or_integration>',
86 description: "Show details for an integration or operation",
87 examples: [
88 "datafire describe hacker_news",
89 "datafire describe hacker_news/getUser",
90 ],
91 runner: require('./commands/describe'),
92 options: [{
93 name: 'query',
94 alias: 'q',
95 description: "Filters for operations matching the query",
96 }]
97}, {
98 name: 'authenticate <integration>',
99 examples: ["datafire authenticate github"],
100 description: "Store a set of credentials for a given integration",
101 runner: require('./commands/authenticate'),
102 check: argv => {
103 if (!argv.alias) return true;
104 return /^\w+$/.test(argv.alias) || "Alias can only contain letters, numbers, and _";
105 },
106 options: [{
107 name: 'alias',
108 alias: 'a',
109 description: 'The alias of the account to edit',
110 }, {
111 name: 'client',
112 description: "With generate_token, the account alias to use as the OAuth client",
113 }, {
114 name: 'port',
115 alias: 'p',
116 description: "With generate_token, the port to listen on for the OAuth callback",
117 }]
118}, {
119 name: 'run <action>',
120 description: "Run an action",
121 runner: require('./commands/run'),
122 examples: [
123 "datafire run ./actions/doSomething.js",
124 "datafire run hacker_news/getItem -i.itemID 8863",
125 "datafire run github"
126 ],
127 options: [{
128 name: 'input',
129 alias: 'i',
130 description: "Pass input to the action",
131 }, {
132 name: 'accounts',
133 description: "Pass in credentials"
134 }, {
135 name: 'inputFile',
136 alias: 'f',
137 description: "JSON or YAML file containing input",
138 }, {
139 name: 'outputFile',
140 alias: 'o',
141 description: "Where to output the result (otherwise STDOUT)",
142 }]
143}, {
144 name: 'test <test>',
145 description: "Run a test stored in DataFire.yml",
146 runner: require('./commands/test'),
147}]
148
149let args = require('yargs')
150 .option('v', {alias: 'verbose'})
151 .global('v')
152 .recommendCommands();
153
154COMMANDS.forEach(cmd => {
155 cmd.examples = cmd.examples || [];
156 cmd.options = cmd.options || [];
157 args = args.command(
158 cmd.name,
159 cmd.description,
160 (yargs) => {
161 cmd.options.forEach(o => {
162 yargs.option(o.name, {
163 alias: o.alias,
164 describe: o.description,
165 demand: o.required,
166 })
167 })
168 },
169 (args) => {
170 if (args.action_or_integration) {
171 let slash = args.action_or_integration.indexOf('/');
172 if (slash === -1) {
173 args.integration = args.action_or_integration;
174 } else {
175 args.action = args.action_or_integration;
176 }
177 delete args.action_or_integration;
178 }
179 cmd.runner(args)
180 .then(_ => {
181 if (cmd.name !== 'serve') {
182 process.exit(0)
183 }
184 })
185 .catch(e => {
186 logger.logError(e.message);
187 if (args.verbose) logger.logError(e.stack);
188 process.exit(1);
189 })
190 });
191 if (cmd.check) args = args.check(cmd.check);
192 cmd.examples.forEach(ex => {
193 args = args.example(cmd.name, ex);
194 });
195})
196
197args = args.help('h').alias('h', 'help').strict().argv;