Skip to content

Commit 50142d7

Browse files
authored
Pull typegen __dirname from packageInfo (#4748)
* Pull ty[pegen __dirname from packageInfo * CHANGELOG
1 parent 1ed6117 commit 50142d7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Contributed:
66

77
- Adjust typegen for `Vec<{ struct: <type> }>` (Thanks to https://github.com/zannis)
8+
- Adjust typegen __dirname for ESM (Thanks to https://github.com/drewstone)
89

910
Changes:
1011

packages/typegen/src/util/file.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import path from 'path';
66

77
import { assert } from '@polkadot/util';
88

9+
import { packageInfo } from '../packageInfo';
10+
911
export function writeFile (dest: string, generator: () => string, noLog?: boolean): void {
1012
!noLog && console.log(`${dest}\n\tGenerating`);
1113

@@ -23,12 +25,17 @@ export function writeFile (dest: string, generator: () => string, noLog?: boolea
2325
}
2426

2527
export function readTemplate (template: string): string {
28+
// Inside the api repo itself, it will be 'auto'
29+
const rootDir = packageInfo.path === 'auto'
30+
? path.join(__dirname, '..')
31+
: packageInfo.path;
32+
2633
// NOTE With cjs in a subdir, search one lower as well
27-
const file = ['../templates', '../../templates']
28-
.map((p) => path.join(__dirname, p, `${template}.hbs`))
34+
const file = ['./templates', '../templates']
35+
.map((p) => path.join(rootDir, p, `${template}.hbs`))
2936
.find((p) => fs.existsSync(p));
3037

31-
assert(file, `Unable to locate ${template}.hbs from ${__dirname}`);
38+
assert(file, `Unable to locate ${template}.hbs from ${rootDir}`);
3239

3340
return fs.readFileSync(file).toString();
3441
}

0 commit comments

Comments
 (0)