Replace vorpal with oclif in node-dev

This commit is contained in:
Jan Oberhauser 2019-08-28 11:03:01 +02:00
parent be3177a101
commit cd75c63f83
6 changed files with 195 additions and 210 deletions

5
packages/node-dev/bin/run Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env node
require('@oclif/command').run()
.then(require('@oclif/command/flush'))
.catch(require('@oclif/errors/handle'))

3
packages/node-dev/bin/run.cmd Executable file
View file

@ -0,0 +1,3 @@
@echo off
node "%~dp0\run" %*

View file

@ -1,49 +1,59 @@
import Vorpal = require('vorpal');
import { Args } from 'vorpal';
import {
UserSettings,
} from "n8n-core";
import { Command, flags } from '@oclif/command';
import {
buildFiles,
IBuildOptions,
} from '../src';
import {
UserSettings,
} from "n8n-core";
export class Build extends Command {
static description = 'Builds credentials and nodes and copies it to n8n custom extension folder';
module.exports = (vorpal: Vorpal) => {
return vorpal
.command('build')
// @ts-ignore
.description('Builds credentials and nodes and copies it to n8n custom extension folder')
.option('--destination <folder>',
`The path to copy the compiles files to [default: ${UserSettings.getUserN8nFolderCustomExtensionPath()}]`)
.option('--watch',
'Starts in watch mode and automatically builds and copies file whenever they change')
.option('\n')
.action(async (args: Args) => {
static examples = [
`$ n8n-node-dev build`,
`$ n8n-node-dev build --destination ~/n8n-nodes`,
`$ n8n-node-dev build --watch`,
];
console.log('\nBuild credentials and nodes');
console.log('=========================');
static flags = {
help: flags.help({ char: 'h' }),
destination: flags.string({
char: 'd',
description: `The path to copy the compiles files to [default: ${UserSettings.getUserN8nFolderCustomExtensionPath()}]`,
}),
watch: flags.boolean({
description: 'Starts in watch mode and automatically builds and copies file whenever they change',
}),
};
async run() {
const { flags } = this.parse(Build);
this.log('\nBuild credentials and nodes');
this.log('=========================');
try {
const options: IBuildOptions = {};
if (args.options.destination) {
options.destinationFolder = args.options.destination;
if (flags.destination) {
options.destinationFolder = flags.destination;
}
if (args.options.watch) {
if (flags.watch) {
options.watch = true;
}
const outputDirectory = await buildFiles(options);
console.log(`The nodes got build and saved into the following folder:\n${outputDirectory}`);
this.log(`The nodes got build and saved into the following folder:\n${outputDirectory}`);
} catch (error) {
console.error('\nGOT ERROR');
console.error('====================================');
console.error(error.message);
this.error('\nGOT ERROR');
this.error('====================================');
this.error(error.message);
return;
}
});
};
}
}

View file

@ -1,9 +1,8 @@
import * as changeCase from 'change-case';
import * as fs from 'fs';
import * as inquirer from 'inquirer';
import { Command } from '@oclif/command';
import { join } from 'path';
import Vorpal = require('vorpal');
import { Args } from 'vorpal';
const { promisify } = require('util');
const fsAccess = promisify(fs.access);
@ -12,18 +11,21 @@ import {
createTemplate
} from '../src';
module.exports = (vorpal: Vorpal) => {
return vorpal
.command('new')
// @ts-ignore
.description('Create new credentials/node')
.action(async (args: Args) => {
export class New extends Command {
static description = 'Create new credentials/node';
static examples = [
`$ n8n-node-dev new`,
];
async run() {
try {
console.log('\nCreate new credentials/node');
console.log('=========================');
this.log('\nCreate new credentials/node');
this.log('=========================');
// Ask for the type of not to be created
const typeQuestion: inquirer.QuestionCollection = {
const typeQuestion: inquirer.Question = {
name: 'type',
type: 'list',
default: 'Node',
@ -47,7 +49,7 @@ module.exports = (vorpal: Vorpal) => {
getDescription = true;
const nodeTypeQuestion: inquirer.QuestionCollection = {
const nodeTypeQuestion: inquirer.Question = {
name: 'nodeType',
type: 'list',
default: 'Execute',
@ -99,7 +101,7 @@ module.exports = (vorpal: Vorpal) => {
});
}
const additionalAnswers = await inquirer.prompt(additionalQuestions as inquirer.QuestionCollection);
const additionalAnswers = await inquirer.prompt(additionalQuestions as inquirer.Questions);
const nodeName = additionalAnswers.name;
@ -115,7 +117,7 @@ module.exports = (vorpal: Vorpal) => {
await fsAccess(destinationFilePath);
// File does already exist. So ask if it should be overwritten.
const overwriteQuestion: inquirer.QuestionCollection = [
const overwriteQuestion: inquirer.Questions = [
{
name: 'overwrite',
type: 'confirm',
@ -127,7 +129,7 @@ module.exports = (vorpal: Vorpal) => {
const overwriteAnswers = await inquirer.prompt(overwriteQuestion);
if (overwriteAnswers.overwrite === false) {
console.log('\nNode creation got canceled!');
this.log('\nNode creation got canceled!');
return;
}
} catch (error) {
@ -145,16 +147,15 @@ module.exports = (vorpal: Vorpal) => {
await createTemplate(sourceFilePath, destinationFilePath, replaceValues);
console.log('\nExecution was successfull:');
console.log('====================================');
this.log('\nExecution was successfull:');
this.log('====================================');
console.log('Node got created: ' + destinationFilePath);
this.log('Node got created: ' + destinationFilePath);
} catch (error) {
console.error('\nGOT ERROR');
console.error('====================================');
console.error(error.message);
this.error('\nGOT ERROR');
this.error('====================================');
this.error(error.message);
return;
}
});
};
}
}

View file

@ -1,40 +0,0 @@
#!/usr/bin/env node
import Vorpal = require('vorpal');
if (process.argv.length === 2) {
// When no command is given choose by default help
process.argv.push('help');
}
const command = process.argv[2];
// Check if the command the user did enter is supported else stop
const supportedCommands = [
'build',
'help',
'new',
];
if (!supportedCommands.includes(command)) {
console.log(`The command "${command}" is not known!`);
process.argv.push('help');
}
const vorpal = new Vorpal();
vorpal
.use(require('./commands/build.js'))
.use(require('./commands/new.js'))
.delimiter('')
.show()
.parse(process.argv);
process
.on('unhandledRejection', (reason, p) => {
console.error(reason, 'Unhandled Rejection at Promise', p);
})
.on('uncaughtException', err => {
console.error(err, 'Uncaught Exception thrown');
process.exit(1);
});

View file

@ -13,6 +13,10 @@
},
"main": "dist/src/index",
"types": "dist/src/index.d.ts",
"oclif": {
"commands": "./dist/commands",
"bin": "run"
},
"scripts": {
"dev": "npm run watch",
"build": "tsc",
@ -21,7 +25,7 @@
"watch": "tsc --watch"
},
"bin": {
"n8n-node-dev": "./dist/index.js"
"n8n-node-dev": "./bin/run"
},
"keywords": [
"development",
@ -30,6 +34,7 @@
"n8n"
],
"files": [
"bin",
"dist"
],
"devDependencies": {
@ -39,13 +44,14 @@
"tslint": "^5.17.0"
},
"dependencies": {
"@oclif/command": "^1.5.18",
"@oclif/errors": "^1.2.2",
"change-case": "^3.1.0",
"inquirer": "^6.3.1",
"n8n-core": "^0.2.0",
"n8n-workflow": "^0.3.0",
"replace-in-file": "^4.1.0",
"typescript": "~3.5.2",
"vorpal": "^1.12.0"
"typescript": "~3.5.2"
},
"jest": {
"transform": {