fix(node-dev): fix the custom node build command (#4148)

* fix(node-dev): fix the custom node build command

also
* wait for the build command to finish before logging the success message
* use module relative paths for looking up `tsconfig` and `tsc` using `require.resolve`

* copy over svg files as well

* copy over the static files before the compilation step

this makes sure that these files are also copied over with `--watch`
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2022-09-21 09:36:44 +02:00
committed by GitHub
parent e20982442b
commit 4d8c380bef
5 changed files with 51 additions and 285 deletions

View File

@@ -1,40 +1,26 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { ChildProcess, spawn } from 'child_process';
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';
import glob from 'fast-glob';
import { spawn } from 'child_process';
import { copyFile, mkdir, readFile, writeFile } from 'fs/promises';
import { join, dirname, resolve as resolvePath } from 'path';
import { file as tmpFile } from 'tmp-promise';
import { UserSettings } from 'n8n-core';
// eslint-disable-next-line import/no-cycle
import { IBuildOptions } from '.';
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const copyfiles = require('copyfiles');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const fsReadFileAsync = promisify(fsReadFile);
const fsWriteAsync = promisify(fsWrite);
/**
* Create a custom tsconfig file as tsc currently has no way to define a base
* directory:
* https://github.com/Microsoft/TypeScript/issues/25430
*
* @export
* @returns
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function createCustomTsconfig() {
// Get path to simple tsconfig file which should be used for build
const tsconfigPath = join(__dirname, '../../src/tsconfig-build.json');
const tsconfigPath = join(dirname(require.resolve('n8n-node-dev/src')), 'tsconfig-build.json');
// Read the tsconfig file
const tsConfigString = await fsReadFile(tsconfigPath, { encoding: 'utf8' });
const tsConfigString = await readFile(tsconfigPath, { encoding: 'utf8' });
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const tsConfig = JSON.parse(tsConfigString);
@@ -47,10 +33,8 @@ export async function createCustomTsconfig() {
tsConfig.include = newIncludeFiles;
// Write new custom tsconfig file
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
// eslint-disable-next-line @typescript-eslint/unbound-method
const { fd, path, cleanup } = await file();
await fsWriteAsync(fd, Buffer.from(JSON.stringify(tsConfig, null, 2), 'utf8'));
const { path, cleanup } = await tmpFile();
await writeFile(path, JSON.stringify(tsConfig, null, 2));
return {
path,
@@ -60,43 +44,37 @@ export async function createCustomTsconfig() {
/**
* Builds and copies credentials and nodes
*
* @export
* @param {IBuildOptions} [options] Options to overwrite default behaviour
* @returns {Promise<string>}
*/
export async function buildFiles(options?: IBuildOptions): Promise<string> {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, no-param-reassign
options = options || {};
let typescriptPath;
// Check for OS to designate correct tsc path
if (process.platform === 'win32') {
typescriptPath = '../../node_modules/TypeScript/lib/tsc';
} else {
typescriptPath = '../../node_modules/.bin/tsc';
}
const tscPath = join(__dirname, typescriptPath);
export async function buildFiles({
destinationFolder = UserSettings.getUserN8nFolderCustomExtensionPath(),
watch,
}: IBuildOptions): Promise<string> {
const tscPath = join(dirname(require.resolve('typescript')), 'tsc');
const tsconfigData = await createCustomTsconfig();
const outputDirectory =
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
options.destinationFolder || UserSettings.getUserN8nFolderCustomExtensionPath();
await Promise.all(
['*.svg', '*.png', '*.node.json'].map(async (filenamePattern) => {
const files = await glob(`**/${filenamePattern}`);
for (const file of files) {
const src = resolvePath(process.cwd(), file);
const dest = resolvePath(destinationFolder, file);
await mkdir(dirname(dest), { recursive: true });
await copyFile(src, dest);
}
}),
);
// Supply a node base path so that it finds n8n-core and n8n-workflow
const nodeModulesPath = join(__dirname, '../../node_modules/');
let buildCommand = `${tscPath} --p ${
tsconfigData.path
} --outDir ${outputDirectory} --rootDir ${process.cwd()} --baseUrl ${nodeModulesPath}`;
if (options.watch === true) {
} --outDir ${destinationFolder} --rootDir ${process.cwd()} --baseUrl ${nodeModulesPath}`;
if (watch) {
buildCommand += ' --watch';
}
let buildProcess: ChildProcess;
try {
buildProcess = spawn('node', buildCommand.split(' '), {
const buildProcess = spawn('node', buildCommand.split(' '), {
windowsVerbatimArguments: true,
cwd: process.cwd(),
});
@@ -110,8 +88,10 @@ export async function buildFiles(options?: IBuildOptions): Promise<string> {
// Make sure that the child process gets also always terminated
// when the main process does
process.on('exit', () => {
buildProcess.kill();
process.on('exit', () => buildProcess.kill());
await new Promise<void>((resolve) => {
buildProcess.on('exit', resolve);
});
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
@@ -122,26 +102,11 @@ export async function buildFiles(options?: IBuildOptions): Promise<string> {
errorMessage = `${errorMessage}\nGot following output:\n${error.stdout}`;
}
// Remove the tmp tsconfig file
// eslint-disable-next-line @typescript-eslint/no-floating-promises
tsconfigData.cleanup();
throw new Error(errorMessage);
} finally {
// Remove the tmp tsconfig file
await tsconfigData.cleanup();
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return new Promise((resolve, reject) => {
['*.png', '*.node.json'].forEach((filenamePattern) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
copyfiles([join(process.cwd(), `./${filenamePattern}`), outputDirectory], { up: true }, () =>
resolve(outputDirectory),
);
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
buildProcess.on('exit', (code) => {
// Remove the tmp tsconfig file
// eslint-disable-next-line @typescript-eslint/no-floating-promises
tsconfigData.cleanup();
});
});
return destinationFolder;
}