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 {
readFile as fsReadFile,
} from 'fs';
import { promisify } from 'util';
} from 'fs/promises';
import { IDataObject } from 'n8n-workflow';
import { IPackageVersions } from './';
const fsReadFileAsync = promisify(fsReadFile);
let versionCache: IPackageVersions | undefined;
@ -72,7 +69,7 @@ export async function getVersions(): Promise<IPackageVersions> {
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);
versionCache = {
@ -122,7 +119,7 @@ export async function getConfigValue(configKey: string): Promise<string | boolea
let data;
try {
data = await fsReadFileAsync(fileEnvironmentVariable, 'utf8') as string;
data = await fsReadFile(fileEnvironmentVariable, 'utf8') as string;
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`The file "${fileEnvironmentVariable}" could not be found.`);

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;

View file

@ -1,9 +1,13 @@
import { ChildProcess, spawn } from 'child_process';
const copyfiles = require('copyfiles');
import {
readFile as fsReadFile,
} from 'fs/promises';
import {
write as fsWrite,
} from 'fs';
import { join } from 'path';
import { file } from 'tmp-promise';
import { promisify } from 'util';
@ -32,7 +36,7 @@ export async function createCustomTsconfig () {
const tsconfigPath = join(__dirname, '../../src/tsconfig-build.json');
// 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);
// Set absolute include paths

View file

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

View file

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

View file

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

View file

@ -13,10 +13,7 @@ import {
import {
writeFile as fsWriteFile,
} from 'fs';
import { promisify } from 'util';
const fsWriteFileAsync = promisify(fsWriteFile);
} from 'fs/promises';
export class WriteBinaryFile implements INodeType {
@ -80,7 +77,7 @@ export class WriteBinaryFile implements INodeType {
}
// 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 = {
json: {},