diff --git a/packages/node-dev/package.json b/packages/node-dev/package.json index 7a17ea6b7e..c839a67326 100644 --- a/packages/node-dev/package.json +++ b/packages/node-dev/package.json @@ -46,6 +46,7 @@ "@oclif/dev-cli": "^1.22.2", "@types/inquirer": "^6.5.0", "@types/node": "^10.10.1", + "@types/tmp": "^0.1.0", "@types/vorpal": "^1.11.0", "tslint": "^5.17.0" }, @@ -58,6 +59,7 @@ "n8n-workflow": "^0.11.0", "replace-in-file": "^4.1.0", "request": "^2.88.0", + "tmp-promise": "^2.0.2", "typescript": "~3.5.2" }, "jest": { diff --git a/packages/node-dev/src/Build.ts b/packages/node-dev/src/Build.ts index 4758926222..b898ac05fd 100644 --- a/packages/node-dev/src/Build.ts +++ b/packages/node-dev/src/Build.ts @@ -1,5 +1,14 @@ -import { join } from 'path'; import { ChildProcess, spawn } from 'child_process'; +import { + readFile as fsReadFile, + write as fsWrite, +} from 'fs'; +import { join } from 'path'; +import { file } from 'tmp-promise'; +import { promisify } from 'util'; + +const fsReadFileAsync = promisify(fsReadFile); +const fsWriteAsync = promisify(fsWrite); import { IBuildOptions } from '.'; @@ -8,6 +17,41 @@ import { } from 'n8n-core'; +/** + * 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 + */ +export async function createCustomTsconfig () { + + // Get path to simple tsconfig file which should be used for build + const tsconfigPath = join(__dirname, '../../src/tsconfig-build.json'); + + // Read the tsconfi file + const tsConfigString = await fsReadFileAsync(tsconfigPath, { encoding: 'utf8'}) as string; + const tsConfig = JSON.parse(tsConfigString); + + // Set absolute include paths + const newIncludeFiles = []; + for (const includeFile of tsConfig.include) { + newIncludeFiles.push(join(process.cwd(), includeFile)); + } + tsConfig.include = newIncludeFiles; + + // Write new custom tsconfig file + const { fd, path, cleanup } = await file(); + await fsWriteAsync(fd, Buffer.from(JSON.stringify(tsConfig, null, 2), 'utf8')); + + return { + path, + cleanup, + }; +} + + /** * Builds and copies credentials and nodes * @@ -21,12 +65,11 @@ export async function buildFiles (options?: IBuildOptions): Promise { // Get the path of the TypeScript cli of this project const tscPath = join(__dirname, '../../node_modules/typescript/bin/tsc'); - // Get path to simple tsconfig file which should be used for build - const tsconfigPath = join(__dirname, '../../src/tsconfig-build.json'); + const tsconfigData = await createCustomTsconfig(); const outputDirectory = options.destinationFolder || UserSettings.getUserN8nFolderCustomExtensionPath(); - let buildCommand = `${tscPath} --p ${tsconfigPath} --outDir ${outputDirectory}`; + let buildCommand = `${tscPath} --p ${tsconfigData.path} --outDir ${outputDirectory} --rootDir ${process.cwd()}`; if (options.watch === true) { buildCommand += ' --watch'; } @@ -52,11 +95,16 @@ export async function buildFiles (options?: IBuildOptions): Promise { errorMessage = `${errorMessage}\nGot following output:\n${error.stdout}`; } + // Remove the tmp tsconfig file + tsconfigData.cleanup(); + throw new Error(errorMessage); } return new Promise((resolve, reject) => { buildProcess.on('exit', code => { + // Remove the tmp tsconfig file + tsconfigData.cleanup(); resolve(outputDirectory); }); }); diff --git a/packages/node-dev/src/tsconfig-build.json b/packages/node-dev/src/tsconfig-build.json index cb48078fac..d21e9bddd4 100644 --- a/packages/node-dev/src/tsconfig-build.json +++ b/packages/node-dev/src/tsconfig-build.json @@ -3,9 +3,7 @@ "lib": [ "es2017" ], - "types": [ - "node" - ], + "types": [], "module": "commonjs", "importHelpers": true, "noImplicitAny": true, @@ -18,8 +16,8 @@ "sourceMap": true }, "include": [ - "../*.credentials.ts", - "../*.node.ts" + "*.credentials.ts", + "*.node.ts" ], "exclude": [ "node_modules"