From c2304c67f0124da2fa8670fdf0717c67fe3379c2 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Sun, 9 Jun 2024 14:15:18 +0000 Subject: [PATCH 1/3] feat: configurable alias prefix fixes #109 --- lib/config.ts | 4 ++++ lib/parser.ts | 15 ++++++++------- tests/unit/build.ts | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/config.ts b/lib/config.ts index 76a8c2c..b91f387 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -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 { @@ -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), }); diff --git a/lib/parser.ts b/lib/parser.ts index 4e20d79..e21d240 100644 --- a/lib/parser.ts +++ b/lib/parser.ts @@ -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', @@ -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 @@ -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); @@ -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; diff --git a/tests/unit/build.ts b/tests/unit/build.ts index 1ae0cbd..7aa81bb 100644 --- a/tests/unit/build.ts +++ b/tests/unit/build.ts @@ -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

Hello ${REPL_NAME}!

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

Hello world!

;\n", +}; + const unmockedFetch = global.fetch; const unmockedLog = global.log; @@ -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); + }) }) From 220f79533607941a5c9c73e6b0aeeb684240ad3f Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Sun, 9 Jun 2024 14:29:53 +0000 Subject: [PATCH 2/3] update docs --- README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ac5c83..8f2fb53 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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

Hello ${REPL_NAME}!

; +``` + +Then the alias will be replaced like this: + +```tsx +export default

Hello world!

; +``` + ## 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: From 0c09c2083a0c32bc885ca98b8ae2451af4aeaab2 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Mon, 10 Jun 2024 17:51:48 +0000 Subject: [PATCH 3/3] v1.0.0-alpha.27 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0dee003..8cb1cbe 100644 --- a/package.json +++ b/package.json @@ -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",