1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-09-20 09:27:44 +03:00
n8n/packages/node-dev/commands/new.ts

162 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-06-23 13:35:23 +03:00
import * as changeCase from 'change-case';
import * as fs from 'fs';
import * as inquirer from 'inquirer';
import { Command } from '@oclif/command';
2019-06-23 13:35:23 +03:00
import { join } from 'path';
const { promisify } = require('util');
const fsAccess = promisify(fs.access);
import {
createTemplate
} from '../src';
export class New extends Command {
static description = 'Create new credentials/node';
static examples = [
`$ n8n-node-dev new`,
];
async run() {
try {
this.log('\nCreate new credentials/node');
this.log('=========================');
// Ask for the type of not to be created
const typeQuestion: inquirer.QuestionCollection = {
name: 'type',
type: 'list',
default: 'Node',
message: 'What do you want to create?',
choices: [
'Credentials',
'Node',
],
};
const typeAnswers = await inquirer.prompt(typeQuestion);
let sourceFolder = '';
const sourceFileName = 'simple.ts';
let defaultName = '';
let getDescription = false;
2019-06-23 13:35:23 +03:00
if (typeAnswers.type === 'Node') {
// Create new node
getDescription = true;
const nodeTypeQuestion: inquirer.QuestionCollection = {
name: 'nodeType',
2019-06-23 13:35:23 +03:00
type: 'list',
default: 'Execute',
message: 'What kind of node do you want to create?',
2019-06-23 13:35:23 +03:00
choices: [
'Execute',
'Trigger',
'Webhook',
2019-06-23 13:35:23 +03:00
],
};
const nodeTypeAnswers = await inquirer.prompt(nodeTypeQuestion);
// Choose a the template-source-file depending on user input.
sourceFolder = 'execute';
defaultName = 'My Node';
if (nodeTypeAnswers.nodeType === 'Trigger') {
sourceFolder = 'trigger';
defaultName = 'My Trigger';
} else if (nodeTypeAnswers.nodeType === 'Webhook') {
sourceFolder = 'webhook';
defaultName = 'My Webhook';
2019-06-23 13:35:23 +03:00
}
} else {
// Create new credentials
2019-06-23 13:35:23 +03:00
sourceFolder = 'credentials';
defaultName = 'My Service API';
}
2019-06-23 13:35:23 +03:00
// Ask additional questions to know with what values the
// variables in the template file should be replaced with
const additionalQuestions = [
{
name: 'name',
type: 'input',
default: defaultName,
message: 'How should the node be called?',
},
];
if (getDescription === true) {
// Get also a node description
additionalQuestions.push({
name: 'description',
type: 'input',
default: 'Node converts input data to chocolate',
message: 'What should the node description be?',
});
}
2019-06-23 13:35:23 +03:00
const additionalAnswers = await inquirer.prompt(additionalQuestions as inquirer.QuestionCollection);
2019-06-23 13:35:23 +03:00
const nodeName = additionalAnswers.name;
2019-06-23 13:35:23 +03:00
// Define the source file to be used and the location and name of the new
// node file
const destinationFilePath = join(process.cwd(), `${changeCase.pascalCase(nodeName)}.${typeAnswers.type.toLowerCase()}.ts`);
2019-06-23 13:35:23 +03:00
const sourceFilePath = join(__dirname, '../../templates', sourceFolder, sourceFileName);
2019-06-23 13:35:23 +03:00
// Check if node with the same name already exists in target folder
// to not overwrite it by accident
try {
await fsAccess(destinationFilePath);
2019-06-23 13:35:23 +03:00
// File does already exist. So ask if it should be overwritten.
const overwriteQuestion: inquirer.QuestionCollection = [
{
name: 'overwrite',
type: 'confirm',
default: false,
message: `The file "${destinationFilePath}" already exists and would be overwritten. Do you want to proceed and overwrite the file?`,
},
];
2019-06-23 13:35:23 +03:00
const overwriteAnswers = await inquirer.prompt(overwriteQuestion);
2019-06-23 13:35:23 +03:00
if (overwriteAnswers.overwrite === false) {
this.log('\nNode creation got canceled!');
return;
2019-06-23 13:35:23 +03:00
}
} catch (error) {
// File does not exist. That is exactly what we want so go on.
2019-06-23 13:35:23 +03:00
}
// Make sure that the variables in the template file get formated
// in the correct way
const replaceValues = {
ClassNameReplace: changeCase.pascalCase(nodeName),
DisplayNameReplace: changeCase.titleCase(nodeName),
N8nNameReplace: changeCase.camelCase(nodeName),
NodeDescriptionReplace: additionalAnswers.description,
};
await createTemplate(sourceFilePath, destinationFilePath, replaceValues);
this.log('\nExecution was successfull:');
this.log('====================================');
this.log('Node got created: ' + destinationFilePath);
} catch (error) {
this.error('\nGOT ERROR');
this.error('====================================');
this.error(error.message);
return;
}
}
}