2022-07-24 08:25:01 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
2022-09-21 00:36:44 -07:00
|
|
|
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';
|
2019-09-19 06:22:22 -07:00
|
|
|
|
2022-10-24 02:21:27 -07:00
|
|
|
import { jsonParse } from 'n8n-workflow';
|
2021-08-29 11:58:11 -07:00
|
|
|
import { UserSettings } from 'n8n-core';
|
2022-12-21 01:46:26 -08:00
|
|
|
import type { IBuildOptions } from './Interfaces';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-09-19 06:22:22 -07:00
|
|
|
/**
|
|
|
|
* Create a custom tsconfig file as tsc currently has no way to define a base
|
|
|
|
* directory:
|
|
|
|
* https://github.com/Microsoft/TypeScript/issues/25430
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export async function createCustomTsconfig() {
|
2019-09-19 06:22:22 -07:00
|
|
|
// Get path to simple tsconfig file which should be used for build
|
2022-09-21 00:36:44 -07:00
|
|
|
const tsconfigPath = join(dirname(require.resolve('n8n-node-dev/src')), 'tsconfig-build.json');
|
2019-09-19 06:22:22 -07:00
|
|
|
|
2022-09-02 07:13:17 -07:00
|
|
|
// Read the tsconfig file
|
2022-09-21 00:36:44 -07:00
|
|
|
const tsConfigString = await readFile(tsconfigPath, { encoding: 'utf8' });
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
2022-10-24 02:21:27 -07:00
|
|
|
const tsConfig = jsonParse<{ include: string[] }>(tsConfigString);
|
2019-09-19 06:22:22 -07:00
|
|
|
|
|
|
|
// Set absolute include paths
|
|
|
|
const newIncludeFiles = [];
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2019-09-19 06:22:22 -07:00
|
|
|
for (const includeFile of tsConfig.include) {
|
|
|
|
newIncludeFiles.push(join(process.cwd(), includeFile));
|
|
|
|
}
|
|
|
|
tsConfig.include = newIncludeFiles;
|
|
|
|
|
|
|
|
// Write new custom tsconfig file
|
2022-09-21 00:36:44 -07:00
|
|
|
const { path, cleanup } = await tmpFile();
|
|
|
|
await writeFile(path, JSON.stringify(tsConfig, null, 2));
|
2019-09-19 06:22:22 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
path,
|
|
|
|
cleanup,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
/**
|
|
|
|
* Builds and copies credentials and nodes
|
2022-09-29 05:37:56 -07:00
|
|
|
*
|
|
|
|
* @param {IBuildOptions} [options] Options to overwrite default behaviour
|
2019-06-23 03:35:23 -07:00
|
|
|
*/
|
2022-09-21 00:36:44 -07:00
|
|
|
export async function buildFiles({
|
|
|
|
destinationFolder = UserSettings.getUserN8nFolderCustomExtensionPath(),
|
|
|
|
watch,
|
|
|
|
}: IBuildOptions): Promise<string> {
|
|
|
|
const tscPath = join(dirname(require.resolve('typescript')), 'tsc');
|
2019-09-19 06:22:22 -07:00
|
|
|
const tsconfigData = await createCustomTsconfig();
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2022-09-21 00:36:44 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-09-19 06:38:08 -07:00
|
|
|
// Supply a node base path so that it finds n8n-core and n8n-workflow
|
|
|
|
const nodeModulesPath = join(__dirname, '../../node_modules/');
|
2021-08-29 11:58:11 -07:00
|
|
|
let buildCommand = `${tscPath} --p ${
|
|
|
|
tsconfigData.path
|
2022-09-21 00:36:44 -07:00
|
|
|
} --outDir ${destinationFolder} --rootDir ${process.cwd()} --baseUrl ${nodeModulesPath}`;
|
|
|
|
if (watch) {
|
2019-06-23 03:35:23 -07:00
|
|
|
buildCommand += ' --watch';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-09-21 00:36:44 -07:00
|
|
|
const buildProcess = spawn('node', buildCommand.split(' '), {
|
2021-08-29 11:58:11 -07:00
|
|
|
windowsVerbatimArguments: true,
|
|
|
|
cwd: process.cwd(),
|
|
|
|
});
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
// Forward the output of the child process to the main one
|
|
|
|
// that the user can see what is happening
|
2021-08-29 11:58:11 -07:00
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
buildProcess.stdout.pipe(process.stdout);
|
2021-08-29 11:58:11 -07:00
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
buildProcess.stderr.pipe(process.stderr);
|
|
|
|
|
|
|
|
// Make sure that the child process gets also always terminated
|
|
|
|
// when the main process does
|
2022-09-21 00:36:44 -07:00
|
|
|
process.on('exit', () => buildProcess.kill());
|
|
|
|
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
buildProcess.on('exit', resolve);
|
2019-06-23 03:35:23 -07:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
2019-06-23 03:35:23 -07:00
|
|
|
let errorMessage = error.message;
|
|
|
|
|
|
|
|
if (error.stdout !== undefined) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
2019-06-23 03:35:23 -07:00
|
|
|
errorMessage = `${errorMessage}\nGot following output:\n${error.stdout}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(errorMessage);
|
2022-09-21 00:36:44 -07:00
|
|
|
} finally {
|
|
|
|
// Remove the tmp tsconfig file
|
|
|
|
await tsconfigData.cleanup();
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:36:44 -07:00
|
|
|
return destinationFolder;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|