Skip to content

Commit

Permalink
Use import.meta.url instead of process.cwd() (polkadot-js#5675)
Browse files Browse the repository at this point in the history
* Use import.meta.url instead of process.cwd()

* Bump dev
  • Loading branch information
jacogr authored Jun 16, 2023
1 parent 56fb5c9 commit 61a8b40
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 144 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"test:one": "polkadot-dev-run-test --env node"
},
"devDependencies": {
"@polkadot/dev": "^0.75.19",
"@polkadot/dev": "^0.75.21",
"@polkadot/typegen": "workspace:packages/typegen",
"@types/node": "^20.2.5"
"@types/node": "^20.3.1"
},
"resolutions": {
"typescript": "^5.0.4"
Expand Down
10 changes: 3 additions & 7 deletions packages/api-contract/src/Abi/Abi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import type { Registry } from '@polkadot/types/types';

import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';

import { TypeDefInfo } from '@polkadot/types/types';
Expand Down Expand Up @@ -44,12 +43,9 @@ interface JSONAbi {
}
}

// FIXME When Jest is removed with ESM tests, this should be converted to use import.meta.url
const cmpPath = path.join(process.cwd(), 'packages/api-contract/src/test/compare');

function stringifyInfo (key: string, value: unknown): unknown {
return key === 'info'
? TypeDefInfo[value as number]
return key === 'info' && typeof value === 'number'
? TypeDefInfo[value]
: value;
}

Expand Down Expand Up @@ -92,7 +88,7 @@ describe('Abi', (): void => {
it(`initializes from a contract ABI: ${abiName}`, (): void => {
const abi = new Abi(abiJson);
const registryJson = stringifyJson(abi.registry);
const cmpFile = path.join(cmpPath, `${abiName}.test.json`);
const cmpFile = new URL(`../test/compare/${abiName}.test.json`, import.meta.url);

try {
expect(
Expand Down
5 changes: 1 addition & 4 deletions packages/api-contract/src/base/Code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
/// <reference types="@polkadot/dev-test/globals.d.ts" />

import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';

import { toPromiseMethod } from '@polkadot/api';

Expand All @@ -15,8 +13,7 @@ import v1contractFlipper from '../test/contracts/ink/v1/flipper.contract.json' a
import { Code } from './Code.js';
import { mockApi } from './mock.js';

// FIXME When Jest is removed with ESM tests, this should be converted to use import.meta.url
const v0wasmFlipper = fs.readFileSync(path.join(process.cwd(), 'packages/api-contract/src/test/contracts/ink/v0/flipper.wasm'), 'utf-8');
const v0wasmFlipper = fs.readFileSync(new URL('../test/contracts/ink/v0/flipper.wasm', import.meta.url), 'utf-8');

describe('Code', (): void => {
it('can construct with an individual ABI/WASM combo', (): void => {
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"tslib": "^2.5.3"
},
"devDependencies": {
"@substrate/connect": "0.7.26"
"@substrate/connect": "0.7.29"
},
"optionalDependencies": {
"@substrate/connect": "0.7.26"
"@substrate/connect": "0.7.29"
}
}
6 changes: 5 additions & 1 deletion packages/rpc-provider/src/substrate-connect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import type * as Sc from '@substrate/connect';
import type { HealthChecker, SmoldotHealth } from './types.js';

import { stringify } from '@polkadot/util';

import { ScProvider } from './index.js';

interface MockChain extends Sc.Chain {
Expand Down Expand Up @@ -100,7 +102,9 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
_spec: () => spec,
_triggerCallback: (response) => {
callback(
typeof response === 'string' ? response : JSON.stringify(response)
typeof response === 'string'
? response
: stringify(response)
);
},
remove: () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/typegen/src/generate/tsDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function tsResult (registry: Registry, definitions: Record<string, ModuleTypes>,
/** @internal */
function tsSi (_registry: Registry, _definitions: Record<string, ModuleTypes>, typeDef: TypeDef, _imports: TypeImports): string {
// FIXME
return `// SI: ${JSON.stringify(typeDef)}`;
return `// SI: ${stringify(typeDef)}`;
}

/** @internal */
Expand Down
3 changes: 2 additions & 1 deletion packages/typegen/src/util/wsMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { HexString } from '@polkadot/util/types';

import { promiseTracker } from '@polkadot/api/promise/decorateMethod';
import { stringify } from '@polkadot/util';
import { WebSocket } from '@polkadot/x-ws';

async function getWsData <T> (endpoint: string, method: 'rpc_methods' | 'state_getMetadata' | 'state_getRuntimeVersion'): Promise<T> {
Expand All @@ -20,7 +21,7 @@ async function getWsData <T> (endpoint: string, method: 'rpc_methods' | 'state_g
};

websocket.onerror = (event: unknown): void => {
tracker.reject(new Error(`WebSocket error:: ${JSON.stringify(event)}`));
tracker.reject(new Error(`WebSocket error:: ${stringify(event)}`));
};

websocket.onopen = (): void => {
Expand Down
8 changes: 4 additions & 4 deletions packages/types-codec/src/base/Enum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Registry } from '@polkadot/types-codec/types';

import { TypeRegistry } from '@polkadot/types';
import { Enum, Null, Text, U32 } from '@polkadot/types-codec';
import { u8aToHex } from '@polkadot/util';
import { stringify, u8aToHex } from '@polkadot/util';

import { perf } from '../test/performance.js';

Expand Down Expand Up @@ -358,15 +358,15 @@ describe('Enum', (): void => {
it('has a sane output for basic enums', (): void => {
expect(
new Enum(registry, ['foo', 'bar']).toRawType()
).toEqual(JSON.stringify({ _enum: ['foo', 'bar'] }));
).toEqual(stringify({ _enum: ['foo', 'bar'] }));
});

it('has a sane output for typed enums', (): void => {
expect(
// eslint-disable-next-line sort-keys
new Enum(registry, { foo: Text, bar: U32 }).toRawType()
// eslint-disable-next-line sort-keys
).toEqual(JSON.stringify({ _enum: { foo: 'Text', bar: 'u32' } }));
).toEqual(stringify({ _enum: { foo: 'Text', bar: 'u32' } }));
});

it('re-creates via rawType (c-like)', (): void => {
Expand Down Expand Up @@ -401,7 +401,7 @@ describe('Enum', (): void => {
});

it('creates proper raw structure', (): void => {
expect(new Test(registry).toRawType()).toEqual(JSON.stringify({
expect(new Test(registry).toRawType()).toEqual(stringify({
_enum: {
A: 5,
B: 42,
Expand Down
3 changes: 2 additions & 1 deletion packages/types-codec/src/native/Set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { TypeRegistry } from '@polkadot/types';
import { CodecSet } from '@polkadot/types-codec';
import { stringify } from '@polkadot/util';

// TODO actually import these from definitions, don't re-define here
const SET_FIELDS = {
Expand Down Expand Up @@ -108,7 +109,7 @@ describe('Set', (): void => {
it('has a sane toRawType representation', (): void => {
expect(
new CodecSet(registry, { a: 1, b: 2, c: 345 }).toRawType()
).toEqual(JSON.stringify({
).toEqual(stringify({
_set: { a: 1, b: 2, c: 345 }
}));
});
Expand Down
5 changes: 3 additions & 2 deletions packages/types-codec/src/native/Struct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { CodecTo } from '@polkadot/types-codec/types';

import { TypeRegistry } from '@polkadot/types';
import { Enum, Struct, Text, U32, Vec } from '@polkadot/types-codec';
import { stringify } from '@polkadot/util';

import { TEST_A } from './Struct.data.js';

Expand Down Expand Up @@ -314,7 +315,7 @@ describe('Struct', (): void => {
counter: U32,
vector: Vec.with('AccountId')
}).toRawType()
).toEqual(JSON.stringify({
).toEqual(stringify({
accountId: 'AccountId',
balanceCompact: 'Compact<Balance>', // Override in Uint
blockNumber: 'BlockNumber',
Expand All @@ -333,7 +334,7 @@ describe('Struct', (): void => {

expect(
new Type(registry).toRawType()
).toEqual(JSON.stringify({
).toEqual(stringify({
accountId: 'AccountId',
balance: 'Balance' // Override in Uint
}));
Expand Down
5 changes: 3 additions & 2 deletions packages/types-create/src/util/getTypeDef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { TypeRegistry } from '@polkadot/types';
import { getTypeDef, TypeDefInfo } from '@polkadot/types-create';
import { stringify } from '@polkadot/util';

describe('getTypeDef', (): void => {
it('maps empty tuples to empty tuple', (): void => {
Expand Down Expand Up @@ -439,7 +440,7 @@ describe('getTypeDef', (): void => {

it('creates a nested enum with tuple/struct', (): void => {
expect(
getTypeDef(JSON.stringify({
getTypeDef(stringify({
_enum: {
A: 'u32',
B: '(u32, bool)',
Expand Down Expand Up @@ -513,7 +514,7 @@ describe('getTypeDef', (): void => {

it('creates a nested struct with struct/tuple', (): void => {
expect(
getTypeDef(JSON.stringify({
getTypeDef(stringify({
a: 'u32',
b: '(u32, bool)',
c: {
Expand Down
4 changes: 2 additions & 2 deletions packages/types-create/src/util/getTypeDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AnyString } from '@polkadot/types-codec/types';
import type { TypeDef } from '@polkadot/types-create/types';

import { sanitize } from '@polkadot/types-codec';
import { isNumber, isString, objectSpread } from '@polkadot/util';
import { isNumber, isString, objectSpread, stringify } from '@polkadot/util';

import { TypeDefInfo } from '../types/index.js';
import { typeSplit } from './typeSplit.js';
Expand Down Expand Up @@ -36,7 +36,7 @@ const KNOWN_INTERNALS = ['_alias', '_fallback'];
function getTypeString (typeOrObj: any): string {
return isString(typeOrObj)
? typeOrObj.toString()
: JSON.stringify(typeOrObj);
: stringify(typeOrObj);
}

function isRustEnum (details: Record<string, string> | Record<string, number>): details is Record<string, string> {
Expand Down
3 changes: 2 additions & 1 deletion packages/types-known/src/upgrades/e2e/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { ChainUpgradesExpanded } from '../types.js';
import fs from 'node:fs';

import { ApiPromise, WsProvider } from '@polkadot/api';
import { stringify } from '@polkadot/util';

import * as allMan from '../manual/index.js';
import * as allGen from './index.js';
Expand Down Expand Up @@ -39,7 +40,7 @@ for (const chain of keys) {
import type { ChainUpgradesExpanded } from '../types.js';
export const upgrades: ChainUpgradesExpanded = ${JSON.stringify(final, null, 2)};
export const upgrades: ChainUpgradesExpanded = ${stringify(final, 2)};
`);
await api.disconnect();
});
Expand Down
3 changes: 2 additions & 1 deletion packages/types/src/generic/Block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { BlockValue } from './Block.js';

import block00300 from '@polkadot/types-support/json/SignedBlock.003.00.json' assert { type: 'json' };
import metadataStatic from '@polkadot/types-support/metadata/static-substrate';
import { stringify } from '@polkadot/util';

import { TypeRegistry } from '../create/index.js';
import { Metadata } from '../metadata/index.js';
Expand All @@ -32,7 +33,7 @@ describe('Block', (): void => {
new Block(registry).toRawType()
).toEqual(
// each of the containing structures have been stringified on their own
JSON.stringify({
stringify({
header: 'Header',
extrinsics: 'Vec<Extrinsic>'
})
Expand Down
5 changes: 2 additions & 3 deletions packages/types/src/metadata/util/testUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { MetaVersionAll } from '../versions.js';
import type { Check } from './types.js';

import fs from 'node:fs';
import path from 'node:path';

import { hexToU8a, stringCamelCase, stringify, u8aToHex } from '@polkadot/util';

Expand All @@ -37,8 +36,8 @@ interface MetadataJson {
}
}

function getJsonName (version: number, type: string, sub: 'json' | 'types'): string {
return path.join(process.cwd(), `packages/types-support/src/metadata/v${version}/${type}-${sub}.json`);
function getJsonName (version: number, type: string, sub: 'json' | 'types'): URL {
return new URL(`../../../../types-support/src/metadata/v${version}/${type}-${sub}.json`, import.meta.url);
}

function writeJson (json: unknown, version: number, type: string, sub: 'json' | 'types'): void {
Expand Down
5 changes: 3 additions & 2 deletions packages/types/src/metadata/util/toCallsOnly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import staticLatest from '@polkadot/types-support/metadata/static-substrate';
import staticV13 from '@polkadot/types-support/metadata/v13/substrate-hex';
import { stringify } from '@polkadot/util';

import { TypeRegistry } from '../../create/index.js';
import { Metadata } from '../Metadata.js';
Expand All @@ -18,7 +19,7 @@ describe('toCallsOnly', (): void => {
try {
expect(stripped).toBeDefined();
} catch (error) {
console.error(JSON.stringify(stripped));
console.error(stringify(stripped));

throw error;
}
Expand All @@ -30,7 +31,7 @@ describe('toCallsOnly', (): void => {
try {
expect(stripped).toBeDefined();
} catch (error) {
console.error(JSON.stringify(stripped));
console.error(stringify(stripped));

throw error;
}
Expand Down
Loading

0 comments on commit 61a8b40

Please sign in to comment.