From 045a431272cae0f6b36b5ffbb399f28df523c74f Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Tue, 24 Oct 2023 10:54:17 +1300 Subject: [PATCH 1/4] Fix ts manifest build failing because of missing deps --- packages/cli/package.json | 1 + packages/cli/src/utils/build.ts | 18 ++++++++++-------- yarn.lock | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index ad413e6fbb..baafb85fa2 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -35,6 +35,7 @@ "terser-webpack-plugin": "^5.3.7", "ts-loader": "^9.2.6", "tslib": "^2.3.1", + "typescript": ">=5.2.2", "update-notifier": "5.1.0", "webpack": "^5.76.0", "webpack-merge": "^5.8.0", diff --git a/packages/cli/src/utils/build.ts b/packages/cli/src/utils/build.ts index b37d6b0c1b..da9c7c2639 100644 --- a/packages/cli/src/utils/build.ts +++ b/packages/cli/src/utils/build.ts @@ -1,10 +1,8 @@ // Copyright 2020-2023 SubQuery Pte Ltd authors & contributors // SPDX-License-Identifier: GPL-3.0 -import {execFile} from 'child_process'; import {assert} from 'console'; import {existsSync, lstatSync, readFileSync, writeFileSync} from 'fs'; -import util from 'node:util'; import path from 'path'; import {Command} from '@oclif/core'; import { @@ -15,6 +13,7 @@ import { } from '@subql/common'; import {MultichainProjectManifest} from '@subql/types-core'; import * as yaml from 'js-yaml'; +import ts from 'typescript'; const requireScriptWrapper = (scriptPath: string, outputPath: string): string => `import {toJsonObject} from '@subql/common';` + @@ -51,20 +50,23 @@ export async function buildManifestFromLocation(location: string, command: Comma replaceTsReferencesInMultichain(projectYamlPath); } } catch (e) { + console.log(e) throw new Error(`Failed to generate manifest from typescript ${projectManifestEntry}, ${e.message}`); } return directory; } - +// eslint-disable-next-line @typescript-eslint/require-await export async function generateManifestFromTs(projectManifestEntry: string, command: Command): Promise { assert(existsSync(projectManifestEntry), `${projectManifestEntry} does not exist`); const projectYamlPath = tsProjectYamlPath(projectManifestEntry); try { - await util.promisify(execFile)( - 'npx', - ['ts-node', '-e', requireScriptWrapper(projectManifestEntry, projectYamlPath)], - {cwd: path.dirname(projectManifestEntry)} - ); + const result = ts.transpileModule(requireScriptWrapper(projectManifestEntry, projectYamlPath), {}); + + console.log('OUTPUT', result) + + // Run the compiled output + eval(result.outputText); + command.log(`Project manifest generated to ${projectYamlPath}`); return projectYamlPath; diff --git a/yarn.lock b/yarn.lock index 3a7f50de79..be6d29bceb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6246,6 +6246,7 @@ __metadata: terser-webpack-plugin: ^5.3.7 ts-loader: ^9.2.6 tslib: ^2.3.1 + typescript: ">=5.2.2" update-notifier: 5.1.0 webpack: ^5.76.0 webpack-merge: ^5.8.0 @@ -21684,6 +21685,16 @@ __metadata: languageName: node linkType: hard +"typescript@npm:>=5.2.2": + version: 5.2.2 + resolution: "typescript@npm:5.2.2" + bin: + tsc: bin/tsc + tsserver: bin/tsserver + checksum: 7912821dac4d962d315c36800fe387cdc0a6298dba7ec171b350b4a6e988b51d7b8f051317786db1094bd7431d526b648aba7da8236607febb26cf5b871d2d3c + languageName: node + linkType: hard + "typescript@npm:^4.9.5": version: 4.9.5 resolution: "typescript@npm:4.9.5" @@ -21694,6 +21705,16 @@ __metadata: languageName: node linkType: hard +"typescript@patch:typescript@>=5.2.2#~builtin": + version: 5.2.2 + resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin::version=5.2.2&hash=493e53" + bin: + tsc: bin/tsc + tsserver: bin/tsserver + checksum: 07106822b4305de3f22835cbba949a2b35451cad50888759b6818421290ff95d522b38ef7919e70fb381c5fe9c1c643d7dea22c8b31652a717ddbd57b7f4d554 + languageName: node + linkType: hard + "typescript@patch:typescript@^4.9.5#~builtin": version: 4.9.5 resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin::version=4.9.5&hash=493e53" From 0f1418d023a6d51d7188bedd9114d01f453753c1 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Tue, 24 Oct 2023 12:52:37 +1300 Subject: [PATCH 2/4] Fix build issues --- packages/cli/package.json | 1 + packages/cli/src/utils/build.ts | 14 ++++++++------ yarn.lock | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index baafb85fa2..bc366014d4 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -34,6 +34,7 @@ "simple-git": "^3.16.0", "terser-webpack-plugin": "^5.3.7", "ts-loader": "^9.2.6", + "ts-node": "^10.9.1", "tslib": "^2.3.1", "typescript": ">=5.2.2", "update-notifier": "5.1.0", diff --git a/packages/cli/src/utils/build.ts b/packages/cli/src/utils/build.ts index da9c7c2639..207e857031 100644 --- a/packages/cli/src/utils/build.ts +++ b/packages/cli/src/utils/build.ts @@ -13,7 +13,7 @@ import { } from '@subql/common'; import {MultichainProjectManifest} from '@subql/types-core'; import * as yaml from 'js-yaml'; -import ts from 'typescript'; +import * as tsNode from 'ts-node'; const requireScriptWrapper = (scriptPath: string, outputPath: string): string => `import {toJsonObject} from '@subql/common';` + @@ -50,7 +50,7 @@ export async function buildManifestFromLocation(location: string, command: Comma replaceTsReferencesInMultichain(projectYamlPath); } } catch (e) { - console.log(e) + console.log(e); throw new Error(`Failed to generate manifest from typescript ${projectManifestEntry}, ${e.message}`); } return directory; @@ -60,12 +60,14 @@ export async function generateManifestFromTs(projectManifestEntry: string, comma assert(existsSync(projectManifestEntry), `${projectManifestEntry} does not exist`); const projectYamlPath = tsProjectYamlPath(projectManifestEntry); try { - const result = ts.transpileModule(requireScriptWrapper(projectManifestEntry, projectYamlPath), {}); + // Allows requiring TS, this allows requirng the projectManifestEntry ts file + const tsNodeService = tsNode.register({transpileOnly: true}); - console.log('OUTPUT', result) + // Compile the above script + const script = tsNodeService.compile(requireScriptWrapper(projectManifestEntry, projectYamlPath), 'inline.ts'); - // Run the compiled output - eval(result.outputText); + // Run compiled code + eval(script); command.log(`Project manifest generated to ${projectYamlPath}`); diff --git a/yarn.lock b/yarn.lock index be6d29bceb..36b394d88a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6245,6 +6245,7 @@ __metadata: simple-git: ^3.16.0 terser-webpack-plugin: ^5.3.7 ts-loader: ^9.2.6 + ts-node: ^10.9.1 tslib: ^2.3.1 typescript: ">=5.2.2" update-notifier: 5.1.0 From 22c84d2f9d8ac31c733ae00405eaca385fbf2fa0 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Tue, 24 Oct 2023 17:09:36 +1300 Subject: [PATCH 3/4] Escape \ on Windows with building manifest from TS --- packages/cli/src/utils/build.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/utils/build.ts b/packages/cli/src/utils/build.ts index 207e857031..b119eacc14 100644 --- a/packages/cli/src/utils/build.ts +++ b/packages/cli/src/utils/build.ts @@ -23,6 +23,11 @@ const requireScriptWrapper = (scriptPath: string, outputPath: string): string => `const yamlOutput = yaml.dump(project);` + `writeFileSync('${outputPath}', '# // Auto-generated , DO NOT EDIT\\n' + yamlOutput);`; +// Replaces \ in path on windows that don't work with require +function formatPath(p: string): string { + return p.replace(/\\/g, '/'); +} + export async function buildManifestFromLocation(location: string, command: Command): Promise { let directory: string; let projectManifestEntry: string; @@ -55,6 +60,7 @@ export async function buildManifestFromLocation(location: string, command: Comma } return directory; } + // eslint-disable-next-line @typescript-eslint/require-await export async function generateManifestFromTs(projectManifestEntry: string, command: Command): Promise { assert(existsSync(projectManifestEntry), `${projectManifestEntry} does not exist`); @@ -64,7 +70,10 @@ export async function generateManifestFromTs(projectManifestEntry: string, comma const tsNodeService = tsNode.register({transpileOnly: true}); // Compile the above script - const script = tsNodeService.compile(requireScriptWrapper(projectManifestEntry, projectYamlPath), 'inline.ts'); + const script = tsNodeService.compile( + requireScriptWrapper(formatPath(projectManifestEntry), formatPath(projectYamlPath)), + 'inline.ts' + ); // Run compiled code eval(script); From 044ea3ac3271a6fe7d6dd9e8cc68e16a36b966cd Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Tue, 24 Oct 2023 17:16:40 +1300 Subject: [PATCH 4/4] Update changelog --- packages/cli/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index fb8c962410..bedee02ce2 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Multichain support for TypeScript manifest (#2097) - Support for multi endpoints CLI deployment (#2117) +### Fixed +- Building TS manifest on Windows (#2118) + ## [4.0.5] - 2023-10-18 ### Changed - Update error handling on deployment commands (#2108)