n8n/packages/node-dev/commands/new.ts
Iván Ovejero 56c4c6991f
🎨 Set up linting and formatting (#2120)
* ⬆️ Upgrade TS to 4.3.5

* 👕 Add ESLint configs

* 🎨 Add Prettier config

* 📦 Add deps and commands

*  Adjust global .editorconfig to new ruleset

* 🔥 Remove unneeded local .editorconfig

* 📦 Update deps in editor-ui

* 🔨 Limit Prettier to only TS files

*  Add recommended VSCode extensions

* 👕 Fix build

* 🔥 Remove Vue setting from global config

*  Disable prefer-default-export per feedback

* ✏️ Add forgotten divider

* 👕 Disable no-plusplus

* 👕 Disable class-methods-use-this

* ✏️ Alphabetize overrides

* 👕 Add one-var consecutive override

*  Revert one-var consecutive override

This reverts commit b9252cf935.

* 🎨 👕 Lint and format workflow package (#2121)

* 🎨 Format /workflow package

* 👕 Lint /workflow package

* 🎨 Re-format /workflow package

* 👕 Re-lint /workflow package

* ✏️ Fix typo

*  Consolidate if-checks

* 🔥 Remove prefer-default-export exceptions

* 🔥 Remove no-plusplus exceptions

* 🔥 Remove class-methods-use-this exceptions

* 🎨 👕 Lint and format node-dev package (#2122)

* 🎨 Format /node-dev package

*  Exclude templates from ESLint config

This keeps the templates consistent with the codebase while preventing lint exceptions from being made part of the templates.

* 👕 Lint /node-dev package

* 🔥 Remove prefer-default-export exceptions

* 🔥 Remove no-plusplus exceptions

* 🎨 👕 Lint and format core package (#2123)

* 🎨 Format /core package

* 👕 Lint /core package

* 🎨 Re-format /core package

* 👕 Re-lint /core package

* 🔥 Remove prefer-default-export exceptions

* 🔥 Remove no-plusplus exceptions

* 🔥 Remove class-methods-use-this exceptions

* 🎨 👕 Lint and format cli package (#2124)

* 🎨 Format /cli package

* 👕 Exclude migrations from linting

* 👕 Lint /cli package

* 🎨 Re-format /cli package

* 👕 Re-lint /cli package

* 👕 Fix build

* 🔥 Remove prefer-default-export exceptions

*  Update exceptions in ActiveExecutions

* 🔥 Remove no-plusplus exceptions

* 🔥 Remove class-methods-use-this exceptions

* 👕 fix lint issues

* 🔧 use package specific linter, remove tslint command

* 🔨 resolve build issue, sync dependencies

* 🔧 change lint command

Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
2021-08-29 20:58:11 +02:00

162 lines
5 KiB
TypeScript

/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
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 { createTemplate } from '../src';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { promisify } = require('util');
const fsAccess = promisify(fs.access);
export class New extends Command {
static description = 'Create new credentials/node';
static examples = [`$ n8n-node-dev new`];
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
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;
if (typeAnswers.type === 'Node') {
// Create new node
getDescription = true;
const nodeTypeQuestion: inquirer.QuestionCollection = {
name: 'nodeType',
type: 'list',
default: 'Execute',
message: 'What kind of node do you want to create?',
choices: ['Execute', 'Trigger', 'Webhook'],
};
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';
}
} else {
// Create new credentials
sourceFolder = 'credentials';
defaultName = 'My Service API';
}
// 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?',
});
}
const additionalAnswers = await inquirer.prompt(
additionalQuestions as inquirer.QuestionCollection,
);
const nodeName = additionalAnswers.name;
// 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/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
`${changeCase.pascalCase(nodeName)}.${typeAnswers.type.toLowerCase()}.ts`,
);
const sourceFilePath = join(__dirname, '../../templates', sourceFolder, sourceFileName);
// Check if node with the same name already exists in target folder
// to not overwrite it by accident
try {
await fsAccess(destinationFilePath);
// 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?`,
},
];
const overwriteAnswers = await inquirer.prompt(overwriteQuestion);
if (overwriteAnswers.overwrite === false) {
this.log('\nNode creation got canceled!');
return;
}
} catch (error) {
// File does not exist. That is exactly what we want so go on.
}
// Make sure that the variables in the template file get formated
// 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 successfull:');
this.log('====================================');
this.log(`Node got created: ${destinationFilePath}`);
} catch (error) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
this.log(`\nGOT ERROR: "${error.message}"`);
this.log('====================================');
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
this.log(error.stack);
}
}
}