From 3b5ce05a191b66e6b8e068ac42c36ab715a4ea91 Mon Sep 17 00:00:00 2001 From: Rinat Date: Wed, 9 Oct 2024 14:16:31 +0200 Subject: [PATCH 01/11] feat: possibility to use bigints in objects --- src/tasks/copy-template-files.ts | 1 + src/utils/bigint-to-json.ts | 5 +++++ .../packages/nextjs/utils/scaffold-eth/bigint-reviver.ts | 5 +++++ 3 files changed, 11 insertions(+) create mode 100644 src/utils/bigint-to-json.ts create mode 100644 templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts diff --git a/src/tasks/copy-template-files.ts b/src/tasks/copy-template-files.ts index c00f43bd1..ded18e534 100644 --- a/src/tasks/copy-template-files.ts +++ b/src/tasks/copy-template-files.ts @@ -10,6 +10,7 @@ import { promisify } from "util"; import link from "../utils/link"; import { getArgumentFromExternalExtensionOption } from "../utils/external-extensions"; import { BASE_DIR, SOLIDITY_FRAMEWORKS, SOLIDITY_FRAMEWORKS_DIR } from "../utils/consts"; +import "../utils/bigint-to-json"; const EXTERNAL_EXTENSION_TMP_DIR = "tmp-external-extension"; diff --git a/src/utils/bigint-to-json.ts b/src/utils/bigint-to-json.ts new file mode 100644 index 000000000..e4850f86f --- /dev/null +++ b/src/utils/bigint-to-json.ts @@ -0,0 +1,5 @@ +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json +(BigInt.prototype as any).toJSON = function () { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + return { $bigint: this.toString() }; +}; diff --git a/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts b/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts new file mode 100644 index 000000000..8e004f331 --- /dev/null +++ b/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts @@ -0,0 +1,5 @@ +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json +export const bigintReviver = (key: string, value: unknown) => + value !== null && typeof value === "object" && "$bigint" in value && typeof value.$bigint === "string" + ? BigInt(value.$bigint) + : value; From c971352bef4a00f49fcfaaa44093a46cc54e8aef Mon Sep 17 00:00:00 2001 From: Rinat Date: Wed, 9 Oct 2024 18:08:31 +0200 Subject: [PATCH 02/11] fix: links numbers --- contributors/TEMPLATING.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 6a40a8caa..380581019 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -146,11 +146,11 @@ To avoid issues when named arguments have typos, the `withDefaults` utility will # Args files injection in Template files -For each Template file, we search on the extensions the user selected for the existence of Args files in the exact same relative path. If there are multiple Args files, we combine them into an array +For each Template file, we search on the extensions the user selected for the existence of Args files in the exact same relative path. If Args files are found, we combine them into an array. To see the list of template files and their matching args files, check [TEMPLATE-FILES.md](./TEMPLATE-FILES.md). -I've thought about how the strings should be joined, but an option is to use [tagged templates](4). We can go as crazy as we want with tagged templates. +I've thought about how the strings should be joined, but an option is to use [tagged templates](2). We can go as crazy as we want with tagged templates. # Extension folder anatomy @@ -178,7 +178,7 @@ The special files and folders are: ## Merging package.json files -The package we use to merge `package.json` files [merge-packages](3) will attempt to find intersections of dependencies. If there is a conflict, the version from the last `package.json` will be taken. +The package we use to merge `package.json` files [merge-packages](1) will attempt to find intersections of dependencies. If there is a conflict, the version from the last `package.json` will be taken. For example: @@ -206,7 +206,5 @@ The first and last files are the first and second arguments when we call the fun This is a possible improvement in the speed of the cli. I've used the sync API to avoid adding extra complexity for the proof of concept, but it might be an improvement helping parallelize tasks. For example processing templates in parallel. -[1]: https://github.com/nextauthjs/next-auth -[2]: https://www.prisma.io/ -[3]: https://github.com/zppack/merge-packages -[4]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates +[1]: https://github.com/zppack/merge-packages +[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates From 44bfaa6c2682c8f6ec5b7b921be6616ed4a3a5b9 Mon Sep 17 00:00:00 2001 From: Rinat Date: Wed, 9 Oct 2024 19:58:19 +0200 Subject: [PATCH 03/11] feat: recommended args handling --- contributors/TEMPLATING.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 380581019..fb5744edd 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -176,6 +176,22 @@ The special files and folders are: # Things worth mentioning +## Recommended way to handle complex arguments in templates + +Most of the time you will use string arguments for templating, but sometimes you will need to add arrays, objects, bigints, etc. You can handle them however you want, but we're recommending to use the table below as a helper. + +| Pattern | Template | Args | Result | +| --------------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | +| Replace an object | `const replacedObj = ${JSON.stringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | +| Replace an array | `const replacedArr = ${JSON.stringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | +| Object, add new entries | `const mergedObj = ${JSON.stringify(structuredClone({key1: 'value1', key2: 'value2', ...objToMerge[0]}))}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | +| Array, add new items | `const arrWithAdditionalItems = ${JSON.stringify(structuredClone(['a', 'b', ...arrayToSpread[0]]))};` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | +| BigInt, simple var | `const bigInt = BigInt("${someBigInt[0]}");` | `const someBigInt = 123n` | `const bigInt = BigInt("123");` | +| Simple object with bigints | `const simpleObjectWithBigInt = { key1: BigInt("${simpleObjectWithBigInt[0]}") };` | `const simpleObjectWithBigInt = { key1: 123n }` | `const simpleObjectWithBigInt = { key1: BigInt("123") };` | +| Complex object with bigints | `const objWithBigInt = JSON.parse('${JSON.stringify(objWithBigInt[0])}', bigintReviver)` | `const objWithBigInt = { key1: 123n }` | `const objWithBigInt = JSON.parse('{"key1":{"$bigint":"123"}}', bigintReviver);`, which is equal to `{ key1: 123n } ` | + +Don't forget to import `bigintReviver` from `~~/utils/scaffold-eth/bigint-reviver` in the template file for the last pattern. + ## Merging package.json files The package we use to merge `package.json` files [merge-packages](1) will attempt to find intersections of dependencies. If there is a conflict, the version from the last `package.json` will be taken. From c0655f3428e08d0b07263f2ce9686e434dbba125 Mon Sep 17 00:00:00 2001 From: Rinat Date: Wed, 9 Oct 2024 20:37:04 +0200 Subject: [PATCH 04/11] fix: simple objects with bigints --- contributors/TEMPLATING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index fb5744edd..59bb9b7f4 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -187,7 +187,7 @@ Most of the time you will use string arguments for templating, but sometimes you | Object, add new entries | `const mergedObj = ${JSON.stringify(structuredClone({key1: 'value1', key2: 'value2', ...objToMerge[0]}))}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | | Array, add new items | `const arrWithAdditionalItems = ${JSON.stringify(structuredClone(['a', 'b', ...arrayToSpread[0]]))};` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | | BigInt, simple var | `const bigInt = BigInt("${someBigInt[0]}");` | `const someBigInt = 123n` | `const bigInt = BigInt("123");` | -| Simple object with bigints | `const simpleObjectWithBigInt = { key1: BigInt("${simpleObjectWithBigInt[0]}") };` | `const simpleObjectWithBigInt = { key1: 123n }` | `const simpleObjectWithBigInt = { key1: BigInt("123") };` | +| Simple object with bigints | `const simpleObjectWithBigInt = { key1: BigInt("${simpleObjectWithBigInt[0].key1}") };` | `const simpleObjectWithBigInt = { key1: 123n }` | `const simpleObjectWithBigInt = { key1: BigInt("123") };` | | Complex object with bigints | `const objWithBigInt = JSON.parse('${JSON.stringify(objWithBigInt[0])}', bigintReviver)` | `const objWithBigInt = { key1: 123n }` | `const objWithBigInt = JSON.parse('{"key1":{"$bigint":"123"}}', bigintReviver);`, which is equal to `{ key1: 123n } ` | Don't forget to import `bigintReviver` from `~~/utils/scaffold-eth/bigint-reviver` in the template file for the last pattern. From 71989f75241cff56bca4ecd6409a4fb104bc7f4b Mon Sep 17 00:00:00 2001 From: Rinat Date: Fri, 11 Oct 2024 16:03:11 +0200 Subject: [PATCH 05/11] feat: getStringifiedContent utils + updated rules --- contributors/TEMPLATING.md | 6 ++++-- templates/utils.js | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 59bb9b7f4..33a3d49db 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -184,8 +184,10 @@ Most of the time you will use string arguments for templating, but sometimes you | --------------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | | Replace an object | `const replacedObj = ${JSON.stringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | | Replace an array | `const replacedArr = ${JSON.stringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | -| Object, add new entries | `const mergedObj = ${JSON.stringify(structuredClone({key1: 'value1', key2: 'value2', ...objToMerge[0]}))}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | -| Array, add new items | `const arrWithAdditionalItems = ${JSON.stringify(structuredClone(['a', 'b', ...arrayToSpread[0]]))};` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | +| Object, add new entries | `const mergedObj = {key1: 'value1', key2: 'value2', ${getStringifiedObjectContent(objToMerge[0])}}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | +| Array, add new items | `const arrWithAdditionalItems = ['a', 'b', ${getStringifiedArrayContent(arrayToSpread[0])}]` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | +| Object, add new entries, v2 | `const mergedObj = ${JSON.stringify(structuredClone({key1: 'value1', key2: 'value2', ...objToMerge[0]}))}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | +| Array, add new items, v2 | `const arrWithAdditionalItems = ${JSON.stringify(structuredClone(['a', 'b', ...arrayToSpread[0]]))};` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | | BigInt, simple var | `const bigInt = BigInt("${someBigInt[0]}");` | `const someBigInt = 123n` | `const bigInt = BigInt("123");` | | Simple object with bigints | `const simpleObjectWithBigInt = { key1: BigInt("${simpleObjectWithBigInt[0].key1}") };` | `const simpleObjectWithBigInt = { key1: 123n }` | `const simpleObjectWithBigInt = { key1: BigInt("123") };` | | Complex object with bigints | `const objWithBigInt = JSON.parse('${JSON.stringify(objWithBigInt[0])}', bigintReviver)` | `const objWithBigInt = { key1: 123n }` | `const objWithBigInt = JSON.parse('{"key1":{"$bigint":"123"}}', bigintReviver);`, which is equal to `{ key1: 123n } ` | diff --git a/templates/utils.js b/templates/utils.js index 26de24a9c..2ad51468d 100644 --- a/templates/utils.js +++ b/templates/utils.js @@ -23,3 +23,10 @@ export const withDefaults = return template(argsWithDefault); }; + +export const getStringifiedObjectContent = (obj) => + Object.entries(obj) + .map(([key, value]) => `${key}: ${JSON.stringify(value)},`) + .join("\n"); + +export const getStringifiedArrayContent = (arr) => arr.map(item => `${JSON.stringify(item)},`).join("\n"); From eeea6df9e53d7a261b9afafeefe317b6c6900e1d Mon Sep 17 00:00:00 2001 From: Rinat Date: Mon, 14 Oct 2024 16:02:17 +0200 Subject: [PATCH 06/11] feat: update with deepStringify --- contributors/TEMPLATING.md | 22 ++++++++----------- src/tasks/copy-template-files.ts | 1 - src/utils/bigint-to-json.ts | 5 ----- .../utils/scaffold-eth/bigint-reviver.ts | 5 ----- templates/utils.js | 9 +++----- 5 files changed, 12 insertions(+), 30 deletions(-) delete mode 100644 src/utils/bigint-to-json.ts delete mode 100644 templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 33a3d49db..6632eefa4 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -180,19 +180,15 @@ The special files and folders are: Most of the time you will use string arguments for templating, but sometimes you will need to add arrays, objects, bigints, etc. You can handle them however you want, but we're recommending to use the table below as a helper. -| Pattern | Template | Args | Result | -| --------------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | -| Replace an object | `const replacedObj = ${JSON.stringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | -| Replace an array | `const replacedArr = ${JSON.stringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | -| Object, add new entries | `const mergedObj = {key1: 'value1', key2: 'value2', ${getStringifiedObjectContent(objToMerge[0])}}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | -| Array, add new items | `const arrWithAdditionalItems = ['a', 'b', ${getStringifiedArrayContent(arrayToSpread[0])}]` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | -| Object, add new entries, v2 | `const mergedObj = ${JSON.stringify(structuredClone({key1: 'value1', key2: 'value2', ...objToMerge[0]}))}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | -| Array, add new items, v2 | `const arrWithAdditionalItems = ${JSON.stringify(structuredClone(['a', 'b', ...arrayToSpread[0]]))};` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | -| BigInt, simple var | `const bigInt = BigInt("${someBigInt[0]}");` | `const someBigInt = 123n` | `const bigInt = BigInt("123");` | -| Simple object with bigints | `const simpleObjectWithBigInt = { key1: BigInt("${simpleObjectWithBigInt[0].key1}") };` | `const simpleObjectWithBigInt = { key1: 123n }` | `const simpleObjectWithBigInt = { key1: BigInt("123") };` | -| Complex object with bigints | `const objWithBigInt = JSON.parse('${JSON.stringify(objWithBigInt[0])}', bigintReviver)` | `const objWithBigInt = { key1: 123n }` | `const objWithBigInt = JSON.parse('{"key1":{"$bigint":"123"}}', bigintReviver);`, which is equal to `{ key1: 123n } ` | - -Don't forget to import `bigintReviver` from `~~/utils/scaffold-eth/bigint-reviver` in the template file for the last pattern. +| Pattern | Template | Args | Result | +| ----------------------- | ------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------- | +| Replace an object | `const replacedObj = ${deepStringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | +| Replace an array | `const replacedArr = ${deepStringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | +| Object, add new entries | `const mergedObj = ${deepStringify({ key1: "value1", key2: "value2", ...objToMerge[0] })};` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | +| Array, add new items | `const arrWithAdditionalItems = ${deepStringify(['a', 'b', ...arrayToSpread[0]])}` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"]` | +| BigInt, simple var | `const bigInt = ${deepStringify(someBigInt[0])};` | `const someBigInt = 123n` | `const bigInt = 123n;` | +| Object with bigints | `const objectWithBigInt = ${deepStringify(objectWithBigInt[0])}` | `const objectWithBigInt = { key1: 123n }` | `const objectWithBigInt = { key1: 123n };` | +| | ## Merging package.json files diff --git a/src/tasks/copy-template-files.ts b/src/tasks/copy-template-files.ts index ded18e534..c00f43bd1 100644 --- a/src/tasks/copy-template-files.ts +++ b/src/tasks/copy-template-files.ts @@ -10,7 +10,6 @@ import { promisify } from "util"; import link from "../utils/link"; import { getArgumentFromExternalExtensionOption } from "../utils/external-extensions"; import { BASE_DIR, SOLIDITY_FRAMEWORKS, SOLIDITY_FRAMEWORKS_DIR } from "../utils/consts"; -import "../utils/bigint-to-json"; const EXTERNAL_EXTENSION_TMP_DIR = "tmp-external-extension"; diff --git a/src/utils/bigint-to-json.ts b/src/utils/bigint-to-json.ts deleted file mode 100644 index e4850f86f..000000000 --- a/src/utils/bigint-to-json.ts +++ /dev/null @@ -1,5 +0,0 @@ -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json -(BigInt.prototype as any).toJSON = function () { - // eslint-disable-next-line @typescript-eslint/no-unsafe-call - return { $bigint: this.toString() }; -}; diff --git a/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts b/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts deleted file mode 100644 index 8e004f331..000000000 --- a/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts +++ /dev/null @@ -1,5 +0,0 @@ -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json -export const bigintReviver = (key: string, value: unknown) => - value !== null && typeof value === "object" && "$bigint" in value && typeof value.$bigint === "string" - ? BigInt(value.$bigint) - : value; diff --git a/templates/utils.js b/templates/utils.js index 2ad51468d..2189577e8 100644 --- a/templates/utils.js +++ b/templates/utils.js @@ -1,3 +1,5 @@ +import { inspect } from "util"; + export const withDefaults = (template, expectedArgsDefaults, debug = false) => (receivedArgs) => { @@ -24,9 +26,4 @@ export const withDefaults = return template(argsWithDefault); }; -export const getStringifiedObjectContent = (obj) => - Object.entries(obj) - .map(([key, value]) => `${key}: ${JSON.stringify(value)},`) - .join("\n"); - -export const getStringifiedArrayContent = (arr) => arr.map(item => `${JSON.stringify(item)},`).join("\n"); +export const deepStringify = val => inspect(val, { depth: null, compact: true, maxArrayLength: null, maxStringLength: null }) \ No newline at end of file From 006435e2d994de00f4f57e580d003aeee002d0bb Mon Sep 17 00:00:00 2001 From: Rinat Date: Mon, 14 Oct 2024 16:04:26 +0200 Subject: [PATCH 07/11] fix: eof --- templates/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/utils.js b/templates/utils.js index 2189577e8..3b99e1ac2 100644 --- a/templates/utils.js +++ b/templates/utils.js @@ -26,4 +26,4 @@ export const withDefaults = return template(argsWithDefault); }; -export const deepStringify = val => inspect(val, { depth: null, compact: true, maxArrayLength: null, maxStringLength: null }) \ No newline at end of file +export const deepStringify = val => inspect(val, { depth: null, compact: true, maxArrayLength: null, maxStringLength: null }) From 3f420cf69820a0120872ba30c0efb6c65105480f Mon Sep 17 00:00:00 2001 From: Rinat Date: Tue, 15 Oct 2024 11:46:52 +0200 Subject: [PATCH 08/11] fix: remove object with bigints line --- contributors/TEMPLATING.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 6632eefa4..02eb7c584 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -186,9 +186,7 @@ Most of the time you will use string arguments for templating, but sometimes you | Replace an array | `const replacedArr = ${deepStringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | | Object, add new entries | `const mergedObj = ${deepStringify({ key1: "value1", key2: "value2", ...objToMerge[0] })};` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | | Array, add new items | `const arrWithAdditionalItems = ${deepStringify(['a', 'b', ...arrayToSpread[0]])}` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"]` | -| BigInt, simple var | `const bigInt = ${deepStringify(someBigInt[0])};` | `const someBigInt = 123n` | `const bigInt = 123n;` | -| Object with bigints | `const objectWithBigInt = ${deepStringify(objectWithBigInt[0])}` | `const objectWithBigInt = { key1: 123n }` | `const objectWithBigInt = { key1: 123n };` | -| | +| BigInt | `const bigInt = ${deepStringify(someBigInt[0])};` | `const someBigInt = 123n` | `const bigInt = 123n;` | ## Merging package.json files From 66250607bdea2caafa379412939b2e2fcbe7938a Mon Sep 17 00:00:00 2001 From: Rinat Date: Tue, 15 Oct 2024 11:55:29 +0200 Subject: [PATCH 09/11] fix: rename to stringify + added a note --- contributors/TEMPLATING.md | 20 +++++++++++++------- templates/utils.js | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 02eb7c584..71c51c755 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -180,13 +180,19 @@ The special files and folders are: Most of the time you will use string arguments for templating, but sometimes you will need to add arrays, objects, bigints, etc. You can handle them however you want, but we're recommending to use the table below as a helper. -| Pattern | Template | Args | Result | -| ----------------------- | ------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------- | -| Replace an object | `const replacedObj = ${deepStringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | -| Replace an array | `const replacedArr = ${deepStringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | -| Object, add new entries | `const mergedObj = ${deepStringify({ key1: "value1", key2: "value2", ...objToMerge[0] })};` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | -| Array, add new items | `const arrWithAdditionalItems = ${deepStringify(['a', 'b', ...arrayToSpread[0]])}` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"]` | -| BigInt | `const bigInt = ${deepStringify(someBigInt[0])};` | `const someBigInt = 123n` | `const bigInt = 123n;` | +Note: The `stringify` function used in the examples below should be imported from the `templates/utils.js` file: + +```javascript +import { stringify } from "../path/to/templates/utils.js"; +``` + +| Pattern | Template | Args | Result | +| ----------------------- | --------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------- | +| Replace an object | `const replacedObj = ${stringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | +| Replace an array | `const replacedArr = ${stringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | +| Object, add new entries | `const mergedObj = ${stringify({ key1: "value1", key2: "value2", ...objToMerge[0] })};` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | +| Array, add new items | `const arrWithAdditionalItems = ${stringify(['a', 'b', ...arrayToSpread[0]])}` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"]` | +| BigInt | `const bigInt = ${stringify(someBigInt[0])};` | `const someBigInt = 123n` | `const bigInt = 123n;` | ## Merging package.json files diff --git a/templates/utils.js b/templates/utils.js index 3b99e1ac2..febaab3fa 100644 --- a/templates/utils.js +++ b/templates/utils.js @@ -26,4 +26,4 @@ export const withDefaults = return template(argsWithDefault); }; -export const deepStringify = val => inspect(val, { depth: null, compact: true, maxArrayLength: null, maxStringLength: null }) +export const stringify = val => inspect(val, { depth: null, compact: true, maxArrayLength: null, maxStringLength: null }) From af60bb16e8bd4cdd1bffc70ad5583e4e395f906f Mon Sep 17 00:00:00 2001 From: Rinat Date: Tue, 15 Oct 2024 11:58:59 +0200 Subject: [PATCH 10/11] feat: changeset --- .changeset/purple-kangaroos-rush.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/purple-kangaroos-rush.md diff --git a/.changeset/purple-kangaroos-rush.md b/.changeset/purple-kangaroos-rush.md new file mode 100644 index 000000000..ecdd6c4d5 --- /dev/null +++ b/.changeset/purple-kangaroos-rush.md @@ -0,0 +1,5 @@ +--- +"create-eth": patch +--- + +added recommended templating rules From 376990433921e3d6c628b3adfd03c14a5d011176 Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Wed, 16 Oct 2024 18:17:31 +0700 Subject: [PATCH 11/11] change tone (I've => We've) + inline reference for tagged literals and merge-packages --- contributors/TEMPLATING.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 71c51c755..a828dc54d 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -34,7 +34,7 @@ When a package enforces commonjs imports, our templates created within those pac ### Default values -It's a bit annoying having to define an empty array as a default value for all the arguments. To solve this, I've created a utility function that receives the template and expected arguments with their default values, and takes care of it. You can find it at `templates/utils.js`, the function named `withDefaults`. +It's a bit annoying having to define an empty array as a default value for all the arguments. To solve this, We've created a utility function that receives the template and expected arguments with their default values, and takes care of it. You can find it at `templates/utils.js`, the function named `withDefaults`. As a bonus, using this function will throw an error when an [Args file](#args-file-content) is trying to send an argument with a name not expected by the template. @@ -92,7 +92,7 @@ const stringWithoutNewLines = `This string starts without a new line and ends without new lines`; ``` -If you do this, however, prettier will try to indent the backtick. To avoid that you can see I've added a bunch of `// prettier-ignore`s before the template strings. +If you do this, however, prettier will try to indent the backtick. To avoid that you can see We've added a bunch of `// prettier-ignore`s before the template strings. # Args files @@ -150,7 +150,7 @@ For each Template file, we search on the extensions the user selected for the ex To see the list of template files and their matching args files, check [TEMPLATE-FILES.md](./TEMPLATE-FILES.md). -I've thought about how the strings should be joined, but an option is to use [tagged templates](2). We can go as crazy as we want with tagged templates. +We've thought about how the strings should be joined, but an option is to use [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates). We can go as crazy as we want with tagged templates. # Extension folder anatomy @@ -196,7 +196,7 @@ import { stringify } from "../path/to/templates/utils.js"; ## Merging package.json files -The package we use to merge `package.json` files [merge-packages](1) will attempt to find intersections of dependencies. If there is a conflict, the version from the last `package.json` will be taken. +The package we use to merge `package.json` files [merge-packages](https://www.npmjs.com/package/merge-packages) will attempt to find intersections of dependencies. If there is a conflict, the version from the last `package.json` will be taken. For example: @@ -222,7 +222,4 @@ The first and last files are the first and second arguments when we call the fun ## Filesystem async methods -This is a possible improvement in the speed of the cli. I've used the sync API to avoid adding extra complexity for the proof of concept, but it might be an improvement helping parallelize tasks. For example processing templates in parallel. - -[1]: https://github.com/zppack/merge-packages -[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates +This is a possible improvement in the speed of the cli. We've used the sync API to avoid adding extra complexity for the proof of concept, but it might be an improvement helping parallelize tasks. For example processing templates in parallel.