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 */
|
2019-06-23 03:35:23 -07:00
|
|
|
import { ChildProcess, spawn } from 'child_process';
|
2021-04-30 19:22:15 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
import { readFile as fsReadFile } from 'fs/promises';
|
|
|
|
import { write as fsWrite } from 'fs';
|
2021-04-30 19:22:15 -07:00
|
|
|
|
2019-09-19 06:22:22 -07:00
|
|
|
import { join } from 'path';
|
|
|
|
import { file } from 'tmp-promise';
|
|
|
|
import { promisify } from 'util';
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
import { UserSettings } from 'n8n-core';
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
2019-06-23 03:35:23 -07:00
|
|
|
import { IBuildOptions } from '.';
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
|
|
|
|
const copyfiles = require('copyfiles');
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
const fsReadFileAsync = promisify(fsReadFile);
|
|
|
|
const fsWriteAsync = promisify(fsWrite);
|
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
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @returns
|
|
|
|
*/
|
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
|
|
|
|
const tsconfigPath = join(__dirname, '../../src/tsconfig-build.json');
|
|
|
|
|
|
|
|
// Read the tsconfi file
|
2021-08-29 11:58:11 -07:00
|
|
|
const tsConfigString = await fsReadFile(tsconfigPath, { encoding: 'utf8' });
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
2019-09-19 06:22:22 -07:00
|
|
|
const tsConfig = JSON.parse(tsConfigString);
|
|
|
|
|
|
|
|
// 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
|
2021-09-18 14:03:52 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
2021-09-15 23:44:29 -07:00
|
|
|
const { fd, path, cleanup } = await file();
|
2019-09-19 06:22:22 -07:00
|
|
|
await fsWriteAsync(fd, Buffer.from(JSON.stringify(tsConfig, null, 2), 'utf8'));
|
|
|
|
|
|
|
|
return {
|
|
|
|
path,
|
|
|
|
cleanup,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
/**
|
|
|
|
* Builds and copies credentials and nodes
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {IBuildOptions} [options] Options to overwrite default behaviour
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
export async function buildFiles(options?: IBuildOptions): Promise<string> {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, no-param-reassign
|
2019-06-23 03:35:23 -07:00
|
|
|
options = options || {};
|
|
|
|
|
2020-09-06 12:48:14 -07:00
|
|
|
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);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-09-19 06:22:22 -07:00
|
|
|
const tsconfigData = await createCustomTsconfig();
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
const outputDirectory =
|
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
|
|
options.destinationFolder || UserSettings.getUserN8nFolderCustomExtensionPath();
|
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
|
|
|
|
} --outDir ${outputDirectory} --rootDir ${process.cwd()} --baseUrl ${nodeModulesPath}`;
|
2019-06-23 03:35:23 -07:00
|
|
|
if (options.watch === true) {
|
|
|
|
buildCommand += ' --watch';
|
|
|
|
}
|
|
|
|
|
|
|
|
let buildProcess: ChildProcess;
|
|
|
|
try {
|
2021-08-29 11:58:11 -07:00
|
|
|
buildProcess = spawn('node', buildCommand.split(' '), {
|
|
|
|
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
|
|
|
|
process.on('exit', () => {
|
|
|
|
buildProcess.kill();
|
|
|
|
});
|
|
|
|
} 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}`;
|
|
|
|
}
|
|
|
|
|
2019-09-19 06:22:22 -07:00
|
|
|
// Remove the tmp tsconfig file
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2019-09-19 06:22:22 -07:00
|
|
|
tsconfigData.cleanup();
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
throw new Error(errorMessage);
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2019-06-23 03:35:23 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-08-29 11:58:11 -07:00
|
|
|
['*.png', '*.node.json'].forEach((filenamePattern) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
copyfiles([join(process.cwd(), `./${filenamePattern}`), outputDirectory], { up: true }, () =>
|
|
|
|
resolve(outputDirectory),
|
|
|
|
);
|
2021-06-17 22:58:26 -07:00
|
|
|
});
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
buildProcess.on('exit', (code) => {
|
2019-09-19 06:22:22 -07:00
|
|
|
// Remove the tmp tsconfig file
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2019-09-19 06:22:22 -07:00
|
|
|
tsconfigData.cleanup();
|
2019-06-23 03:35:23 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|