Skip to content

Commit

Permalink
Fix ts manifest build failing because of missing deps (#2118)
Browse files Browse the repository at this point in the history
* Fix ts manifest build failing because of missing deps

* Fix build issues

* Escape \ on Windows with building manifest from TS

* Update changelog
  • Loading branch information
stwiname authored Oct 24, 2023
1 parent 99689cb commit 29a2c51
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 @@ -33,7 +33,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 @@ -6244,7 +6244,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 @@ -21655,6 +21657,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 @@ -21665,6 +21677,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

0 comments on commit 29a2c51

Please sign in to comment.