Use native fs promise where possible (#1684)

This commit is contained in:
lublak
2021-05-01 04:22:15 +02:00
committed by GitHub
parent b7a074abd7
commit c83c05456d
7 changed files with 21 additions and 38 deletions

View File

@@ -14,15 +14,9 @@ import {
readdir as fsReaddir,
readFile as fsReadFile,
stat as fsStat,
} from 'fs';
} from 'fs/promises';
import * as glob from 'glob-promise';
import * as path from 'path';
import { promisify } from 'util';
const fsAccessAsync = promisify(fsAccess);
const fsReaddirAsync = promisify(fsReaddir);
const fsReadFileAsync = promisify(fsReadFile);
const fsStatAsync = promisify(fsStat);
class LoadNodesAndCredentialsClass {
@@ -49,7 +43,7 @@ class LoadNodesAndCredentialsClass {
];
for (const checkPath of checkPaths) {
try {
await fsAccessAsync(checkPath);
await fsAccess(checkPath);
// Folder exists, so use it.
this.nodeModulesPath = path.dirname(checkPath);
break;
@@ -102,13 +96,13 @@ class LoadNodesAndCredentialsClass {
const getN8nNodePackagesRecursive = async (relativePath: string): Promise<string[]> => {
const results: string[] = [];
const nodeModulesPath = `${this.nodeModulesPath}/${relativePath}`;
for (const file of await fsReaddirAsync(nodeModulesPath)) {
for (const file of await fsReaddir(nodeModulesPath)) {
const isN8nNodesPackage = file.indexOf('n8n-nodes-') === 0;
const isNpmScopedPackage = file.indexOf('@') === 0;
if (!isN8nNodesPackage && !isNpmScopedPackage) {
continue;
}
if (!(await fsStatAsync(nodeModulesPath)).isDirectory()) {
if (!(await fsStat(nodeModulesPath)).isDirectory()) {
continue;
}
if (isN8nNodesPackage) { results.push(`${relativePath}${file}`); }
@@ -234,7 +228,7 @@ class LoadNodesAndCredentialsClass {
const packagePath = path.join(this.nodeModulesPath, packageName);
// Read the data from the package.json file to see if any n8n data is defiend
const packageFileString = await fsReadFileAsync(path.join(packagePath, 'package.json'), 'utf8');
const packageFileString = await fsReadFile(path.join(packagePath, 'package.json'), 'utf8');
const packageFile = JSON.parse(packageFileString);
if (!packageFile.hasOwnProperty('n8n')) {
return;