Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ts manifest build failing because of missing deps #2118

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"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",
"webpack": "^5.76.0",
"webpack-merge": "^5.8.0",
Expand Down
25 changes: 19 additions & 6 deletions packages/cli/src/utils/build.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,6 +13,7 @@ import {
} from '@subql/common';
import {MultichainProjectManifest} from '@subql/types-core';
import * as yaml from 'js-yaml';
import * as tsNode from 'ts-node';

const requireScriptWrapper = (scriptPath: string, outputPath: string): string =>
`import {toJsonObject} from '@subql/common';` +
Expand All @@ -24,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<string> {
let directory: string;
let projectManifestEntry: string;
Expand Down Expand Up @@ -51,20 +55,29 @@ 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<string> {
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)}
// Allows requiring TS, this allows requirng the projectManifestEntry ts file
const tsNodeService = tsNode.register({transpileOnly: true});

// Compile the above script
const script = tsNodeService.compile(
requireScriptWrapper(formatPath(projectManifestEntry), formatPath(projectYamlPath)),
'inline.ts'
);

// Run compiled code
eval(script);

command.log(`Project manifest generated to ${projectYamlPath}`);

return projectYamlPath;
Expand Down
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6245,7 +6245,9 @@ __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
webpack: ^5.76.0
webpack-merge: ^5.8.0
Expand Down Expand Up @@ -21684,6 +21686,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"
Expand All @@ -21694,6 +21706,16 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:typescript@>=5.2.2#~builtin<compat/typescript>":
version: 5.2.2
resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin<compat/typescript>::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<compat/typescript>":
version: 4.9.5
resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=493e53"
Expand Down
Loading