Skip to content

Commit

Permalink
feat: configurable alias prefix (#111)
Browse files Browse the repository at this point in the history
* feat: configurable alias prefix

fixes #109

* update docs

* v1.0.0-alpha.27
  • Loading branch information
petersalomonsen authored Jun 10, 2024
1 parent 5ed70a3 commit 12c5c59
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ When working with values that differ accross different networks, developers can
* **Account**: Defines the "owner" of the widgets in the workspace, according to network.
* Pattern: `{config_account}`
* **Aliases**: Defines patterns for replacing other account and contract references. These are particularly useful for widget sources accross environments, such as using mob.near for mainnet, and mike.testnet for testnet.
* Pattern: `${alias_key}`
* Pattern: `${alias_key}` ( note that you may also have other prefixes than `alias_` by configuring the `aliasPrefix` property )
* Example:

```json
Expand Down Expand Up @@ -187,6 +187,40 @@ When working with values that differ accross different networks, developers can
}
```

#### Custom alias prefix

If your aliases are prefixed with another keyword than `alias`, you may configure this using the `aliasPrefix` property. You may also include the prefix in the keys of your alias json file. Here is an example:

```json
{
"account": "[MAINNET_ACCOUNT_ID]",
"aliases": ["./aliases.mainnet.json"],
"aliasPrefix": "REPL",
"aliasesContainsPrefix": true,
}
```

and then with your `aliases.mainnet.json` like this:


```json
{
"REPL_NAME": "world"
}
```

If your widget file looks like this:

```tsx
export default <h1>Hello ${REPL_NAME}!</h1>;
```

Then the alias will be replaced like this:

```tsx
export default <h1>Hello world!</h1>;
```

## Customizing the Gateway

Running the bos-workspace dev server will start a local gateway with a standard [near-social-vm](https://github.com/NearSocial/VM) installed unless the `--no-gateway` flag is provided in your dev command:
Expand Down
4 changes: 4 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface BaseConfig {
format?: boolean; // option to format code on build (default true)
aliases?: string[]; // list of alias to use
index?: string; // widget to use as index
aliasPrefix?: string; // prefix to use for aliases, default is "alias"
aliasesContainsPrefix?: boolean; // aliases keys contains prefix (default is false)
}

interface NetworkConfig {
Expand Down Expand Up @@ -57,6 +59,8 @@ const baseConfigSchema = Joi.object({
}).default(DEFAULT_CONFIG.ipfs),
format: Joi.boolean().default(DEFAULT_CONFIG.format),
aliases: Joi.array().items(Joi.string()).default(DEFAULT_CONFIG.aliases),
aliasPrefix: Joi.string().allow(null),
aliasesContainsPrefix: Joi.boolean().allow(null),
index: Joi.string().allow(null),
});

Expand Down
15 changes: 8 additions & 7 deletions lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ export function evalIPFS(path: string[], ipfsMap: IPFSMap, ipfsGateway: string):
};

// Replace the alias keywords with the alias value
export function evalAlias(path: string[], aliases: Aliases): Output {
export function evalAlias(path: string[], aliases: Aliases, prefix="alias", includePrefixInPath = false): Output {
const logs: Log[] = [];
const alias_path = path.join(SYNTAX.separator);

const alias_path = includePrefixInPath ? ([prefix].concat(path)).join(SYNTAX.separator) : path.join(SYNTAX.separator);
let value = aliases?.[alias_path];
if (!aliases.hasOwnProperty(alias_path)) {
value = wrap(alias_path, "alias");
value = wrap(alias_path, prefix);
logs.push({
message: `Imported alias not found: ${value}`,
level: 'warn',
Expand All @@ -177,7 +178,7 @@ export interface EvalCustomSyntaxParams {
modules: Modules,
ipfsMap: IPFSMap,
ipfsGateway: string,
aliases: Aliases,
aliases: Aliases
};

// Replace all the custom syntax keywords with the actual values
Expand All @@ -193,8 +194,8 @@ export function evalCustomSyntax(code: Code, params: EvalCustomSyntaxParams): Ou
const keyword = expression[0];
const path = expression.slice(1);


let evl: Output;
const aliasPrefix = params.config.aliasPrefix ?? 'alias';
switch (keyword) {
case 'config':
evl = evalConfig(path, params.config);
Expand All @@ -205,8 +206,8 @@ export function evalCustomSyntax(code: Code, params: EvalCustomSyntaxParams): Ou
case 'ipfs':
evl = evalIPFS(path, params.ipfsMap, params.ipfsGateway);
break;
case 'alias':
evl = evalAlias(path, params.aliases);
case aliasPrefix:
evl = evalAlias(path, params.aliases, aliasPrefix, params.config.aliasesContainsPrefix);
break;
default:
return _match;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bos-workspace",
"version": "1.0.0-alpha.26",
"version": "1.0.0-alpha.27",
"description": "",
"bin": {
"bos-workspace": "./bin/bw.js",
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ const app_example_2_output = {
}, null, 2) + "\n",
};

const app_example_3 = {
"./bos.config.json": JSON.stringify({
...DEFAULT_CONFIG,
aliasPrefix: "REPL",
aliasesContainsPrefix: true,
account: "test.near",
}),
"./aliases.json": JSON.stringify({
"REPL_NAME": "world",
}),
"./widget/alias.tsx": "export default <h1>Hello ${REPL_NAME}!</h1>;",
};

const app_example_3_output = {
"/build/data.json": JSON.stringify({
"test.near": {}
}, null, 2) + "\n",
"/build/src/widget/alias.jsx": "return <h1>Hello world!</h1>;\n",
};

const unmockedFetch = global.fetch;
const unmockedLog = global.log;

Expand Down Expand Up @@ -203,4 +223,9 @@ describe('build', () => {
await buildApp('/app_example_2', '/build');
expect(vol.toJSON('/build')).toEqual(app_example_2_output);
})
it('should support custom alias', async () => {
vol.fromJSON(app_example_3, '/app_example_3');
await buildApp('/app_example_3', '/build');
expect(vol.toJSON('/build')).toEqual(app_example_3_output);
})
})

0 comments on commit 12c5c59

Please sign in to comment.