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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 38 deletions

View file

@ -3,14 +3,11 @@ import * as express from 'express';
import { join as pathJoin } from 'path'; import { join as pathJoin } from 'path';
import { import {
readFile as fsReadFile, readFile as fsReadFile,
} from 'fs'; } from 'fs/promises';
import { promisify } from 'util';
import { IDataObject } from 'n8n-workflow'; import { IDataObject } from 'n8n-workflow';
import { IPackageVersions } from './'; import { IPackageVersions } from './';
const fsReadFileAsync = promisify(fsReadFile);
let versionCache: IPackageVersions | undefined; let versionCache: IPackageVersions | undefined;
@ -72,7 +69,7 @@ export async function getVersions(): Promise<IPackageVersions> {
return versionCache; return versionCache;
} }
const packageFile = await fsReadFileAsync(pathJoin(__dirname, '../../package.json'), 'utf8') as string; const packageFile = await fsReadFile(pathJoin(__dirname, '../../package.json'), 'utf8') as string;
const packageData = JSON.parse(packageFile); const packageData = JSON.parse(packageFile);
versionCache = { versionCache = {
@ -122,7 +119,7 @@ export async function getConfigValue(configKey: string): Promise<string | boolea
let data; let data;
try { try {
data = await fsReadFileAsync(fileEnvironmentVariable, 'utf8') as string; data = await fsReadFile(fileEnvironmentVariable, 'utf8') as string;
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
throw new Error(`The file "${fileEnvironmentVariable}" could not be found.`); throw new Error(`The file "${fileEnvironmentVariable}" could not be found.`);

View file

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

View file

@ -1,9 +1,13 @@
import { ChildProcess, spawn } from 'child_process'; import { ChildProcess, spawn } from 'child_process';
const copyfiles = require('copyfiles'); const copyfiles = require('copyfiles');
import { import {
readFile as fsReadFile, readFile as fsReadFile,
} from 'fs/promises';
import {
write as fsWrite, write as fsWrite,
} from 'fs'; } from 'fs';
import { join } from 'path'; import { join } from 'path';
import { file } from 'tmp-promise'; import { file } from 'tmp-promise';
import { promisify } from 'util'; import { promisify } from 'util';
@ -32,7 +36,7 @@ export async function createCustomTsconfig () {
const tsconfigPath = join(__dirname, '../../src/tsconfig-build.json'); const tsconfigPath = join(__dirname, '../../src/tsconfig-build.json');
// Read the tsconfi file // Read the tsconfi file
const tsConfigString = await fsReadFileAsync(tsconfigPath, { encoding: 'utf8'}) as string; const tsConfigString = await fsReadFile(tsconfigPath, { encoding: 'utf8'}) as string;
const tsConfig = JSON.parse(tsConfigString); const tsConfig = JSON.parse(tsConfigString);
// Set absolute include paths // Set absolute include paths

View file

@ -1,9 +1,6 @@
import { import {
readFile as fsReadFile, readFile as fsReadFile,
} from 'fs'; } from 'fs/promises';
import { promisify } from 'util';
const fsReadFileAsync = promisify(fsReadFile);
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
@ -162,7 +159,7 @@ export class ExecuteWorkflow implements INodeType {
let workflowJson; let workflowJson;
try { try {
workflowJson = await fsReadFileAsync(workflowPath, { encoding: 'utf8' }) as string; workflowJson = await fsReadFile(workflowPath, { encoding: 'utf8' }) as string;
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
throw new NodeOperationError(this.getNode(), `The file "${workflowPath}" could not be found.`); throw new NodeOperationError(this.getNode(), `The file "${workflowPath}" could not be found.`);

View file

@ -8,10 +8,7 @@ import {
import { import {
readFile as fsReadFile, readFile as fsReadFile,
} from 'fs'; } from 'fs/promises';
import { promisify } from 'util';
const fsReadFileAsync = promisify(fsReadFile);
export class ReadBinaryFile implements INodeType { export class ReadBinaryFile implements INodeType {
@ -64,7 +61,7 @@ export class ReadBinaryFile implements INodeType {
let data; let data;
try { try {
data = await fsReadFileAsync(filePath) as Buffer; data = await fsReadFile(filePath) as Buffer;
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
throw new NodeOperationError(this.getNode(), `The file "${filePath}" could not be found.`); throw new NodeOperationError(this.getNode(), `The file "${filePath}" could not be found.`);

View file

@ -9,10 +9,7 @@ import * as path from 'path';
import { import {
readFile as fsReadFile, readFile as fsReadFile,
} from 'fs'; } from 'fs/promises';
import { promisify } from 'util';
const fsReadFileAsync = promisify(fsReadFile);
export class ReadBinaryFiles implements INodeType { export class ReadBinaryFiles implements INodeType {
@ -61,7 +58,7 @@ export class ReadBinaryFiles implements INodeType {
let item: INodeExecutionData; let item: INodeExecutionData;
let data: Buffer; let data: Buffer;
for (const filePath of files) { for (const filePath of files) {
data = await fsReadFileAsync(filePath) as Buffer; data = await fsReadFile(filePath) as Buffer;
item = { item = {
binary: { binary: {

View file

@ -13,10 +13,7 @@ import {
import { import {
writeFile as fsWriteFile, writeFile as fsWriteFile,
} from 'fs'; } from 'fs/promises';
import { promisify } from 'util';
const fsWriteFileAsync = promisify(fsWriteFile);
export class WriteBinaryFile implements INodeType { export class WriteBinaryFile implements INodeType {
@ -80,7 +77,7 @@ export class WriteBinaryFile implements INodeType {
} }
// Write the file to disk // Write the file to disk
await fsWriteFileAsync(fileName, Buffer.from(item.binary[dataPropertyName].data, BINARY_ENCODING), 'binary'); await fsWriteFile(fileName, Buffer.from(item.binary[dataPropertyName].data, BINARY_ENCODING), 'binary');
const newItem: INodeExecutionData = { const newItem: INodeExecutionData = {
json: {}, json: {},