n8n/packages/node-dev/commands/new.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
4.7 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { Command } from '@oclif/core';
2019-06-23 03:35:23 -07:00
import * as changeCase from 'change-case';
import { access } from 'fs/promises';
2019-06-23 03:35:23 -07:00
import * as inquirer from 'inquirer';
import { join } from 'path';
import { createTemplate } from '../src';
:art: Set up linting and formatting (#2120) * :arrow_up: Upgrade TS to 4.3.5 * :shirt: Add ESLint configs * :art: Add Prettier config * :package: Add deps and commands * :zap: Adjust global .editorconfig to new ruleset * :fire: Remove unneeded local .editorconfig * :package: Update deps in editor-ui * :hammer: Limit Prettier to only TS files * :zap: Add recommended VSCode extensions * :shirt: Fix build * :fire: Remove Vue setting from global config * :zap: Disable prefer-default-export per feedback * :pencil2: Add forgotten divider * :shirt: Disable no-plusplus * :shirt: Disable class-methods-use-this * :pencil2: Alphabetize overrides * :shirt: Add one-var consecutive override * :rewind: Revert one-var consecutive override This reverts commit b9252cf935659ba6d76727ad484a1d3c00008fcc. * 🎨 👕 Lint and format workflow package (#2121) * :art: Format /workflow package * :shirt: Lint /workflow package * :art: Re-format /workflow package * :shirt: Re-lint /workflow package * :pencil2: Fix typo * :zap: Consolidate if-checks * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format node-dev package (#2122) * :art: Format /node-dev package * :zap: Exclude templates from ESLint config This keeps the templates consistent with the codebase while preventing lint exceptions from being made part of the templates. * :shirt: Lint /node-dev package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * 🎨 👕 Lint and format core package (#2123) * :art: Format /core package * :shirt: Lint /core package * :art: Re-format /core package * :shirt: Re-lint /core package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format cli package (#2124) * :art: Format /cli package * :shirt: Exclude migrations from linting * :shirt: Lint /cli package * :art: Re-format /cli package * :shirt: Re-lint /cli package * :shirt: Fix build * :fire: Remove prefer-default-export exceptions * :zap: Update exceptions in ActiveExecutions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 👕 fix lint issues * :wrench: use package specific linter, remove tslint command * :hammer: resolve build issue, sync dependencies * :wrench: change lint command Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
2021-08-29 11:58:11 -07:00
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 03:35:23 -07:00
if (typeAnswers.type === 'Node') {
// Create new node
getDescription = true;
const nodeTypeQuestion: inquirer.QuestionCollection = {
name: 'nodeType',
2019-06-23 03:35:23 -07:00
type: 'list',
default: 'Execute',
message: 'What kind of node do you want to create?',
choices: ['Execute', 'Trigger', 'Webhook'],
2019-06-23 03:35:23 -07: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 03:35:23 -07:00
}
} else {
// Create new credentials
2019-06-23 03:35:23 -07:00
sourceFolder = 'credentials';
defaultName = 'My Service API';
}
2019-06-23 03:35:23 -07: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) {
// 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 03:35:23 -07:00
const additionalAnswers = await inquirer.prompt(
additionalQuestions as inquirer.QuestionCollection,
);
2019-06-23 03:35:23 -07:00
const nodeName = additionalAnswers.name;
2019-06-23 03:35:23 -07:00
// Define the source file to be used and the location and name of the new
// node file
const destinationFilePath = join(
process.cwd(),
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
`${changeCase.pascalCase(nodeName)}.${typeAnswers.type.toLowerCase()}.ts`,
);
2019-06-23 03:35:23 -07:00
const sourceFilePath = join(__dirname, '../../templates', sourceFolder, sourceFileName);
2019-06-23 03:35:23 -07:00
// Check if node with the same name already exists in target folder
// to not overwrite it by accident
try {
await access(destinationFilePath);
2019-06-23 03:35:23 -07: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 03:35:23 -07:00
const overwriteAnswers = await inquirer.prompt(overwriteQuestion);
2019-06-23 03:35:23 -07:00
if (overwriteAnswers.overwrite === false) {
this.log('\nNode creation got canceled!');
return;
2019-06-23 03:35:23 -07:00
}
} catch (error) {
// File does not exist. That is exactly what we want so go on.
2019-06-23 03:35:23 -07:00
}
// Make sure that the variables in the template file get formatted
// in the correct way
const replaceValues = {
ClassNameReplace: changeCase.pascalCase(nodeName),
DisplayNameReplace: changeCase.capitalCase(nodeName),
N8nNameReplace: changeCase.camelCase(nodeName),
NodeDescriptionReplace: additionalAnswers.description,
};
await createTemplate(sourceFilePath, destinationFilePath, replaceValues);
this.log('\nExecution was successful:');
this.log('====================================');
this.log(`Node got created: ${destinationFilePath}`);
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this.log(`\nGOT ERROR: "${error.message}"`);
this.log('====================================');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this.log(error.stack);
}
}
}