Skip to content

Commit

Permalink
feat: new infra for code snippets (#2651)
Browse files Browse the repository at this point in the history
* Starting new code-snippets app

* Porting getting-started snippets

* Ignoring new app in changesets

* Adding changeset

* Ignoring wrapped snippets files (generated)

* Ignore test files trying to import files that haven’t been built yet

* Using real code/file rather than code-string

* Adding example using `launchTestNode` and `LOCAL_NETWORK_URL` overrides

* Skipping until forc workspace has at least one member

* Renaming variable

* Fixing regex

* Ignoring lint issues individually

* Removing from browser group

* Lintfix

* Restoring console after snippet

* Keeping wrapper free from weird hacks

* Adding mock utility

* Using mocking utility

* Validating if `nertworkUrl` was properly mocked

* Mock provider instead?

* Mocking only for localhost

* Adjusting snippets

* Wrapping snippet in a try/catch

* Adding blank spaces

* Removing mocking utility

* Adding pass-through options for `console.log` calls

* Adding new slot for injecting test node launcher into wrapped snippet

* Injecting node launcher for snippets using `LOCAL_NETWORK_URL`

* Trim snippets

* Cleanup snippets tests

* Stop ignoring test for `docs-snippets2`

* Re-adding `docs-snippets2` tests to knip ignore list

* Using internal builds

* Fixing ignore rule

* Adding basic counter Sway project

* Improving scripts

* Adding sample env file

* Standardizing snippets and tests

* Validate env constants existence

* Drafting new snippet

* Adjusting script command

* Removing unnecessary wallet

* Extacting launcher into another file

* New API draft

* Syncing env and launcher template

* Better docs

* Adding new sway script

* Removing API draft

* Fixing file location

* Add script to workspace

* Improve snippet

* Using new snippets

* Add new API draft

* Matching extra blankspace

* Ignoring env file

* Adding region marks

* Avoid repetition

* Trim env consts

* Cleanup

* Playing around new API

* Tidying up

* Ignoring experimental file

* Ignoring generated files in eslint

* Adjusting wallets count

* Moving chunk around

* Adjusting ignore rule

* Improving imports manipulation

* Adding comments

* Adjusting template markers

* Tyop

* Update draft

* Update title

* Adjust deployment usage

* Updating example

* Removing temp draft

* Removing temp draft section

* Updating broken test

* Using `NETWORK_URL` across the board

* Reword comment

* Adding scripts dir to tsconfig

* Starting with more wallets

* Automatically generating tests for code snippets

* Forcing assertion failures to throw

* WIP

* Changing back to `console.log`

* Adding testing groups to template

* Renaming variable

* Removing redundancy in template handling

* Upgrading dependency

* Updating lock file

* Lintfix - keeping eslint disable at the top

* Normalizing language across all snippets

* Nit

* Nit II

* Fixing package names

* Adjusting ignored package names

* Deleting obsolete files

---------

Co-authored-by: Sérgio Torres <[email protected]>
  • Loading branch information
arboleya and Torres-ssf authored Sep 26, 2024
1 parent 3ad28bf commit fff7047
Show file tree
Hide file tree
Showing 30 changed files with 602 additions and 398 deletions.
3 changes: 2 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"demo-nextjs",
"demo-node-esm",
"demo-typegen",
"@fuel-ts/docs-snippets",
"docs-snippets",
"docs-snippets2",
"template-nextjs",
"create-fuels-counter-guide",
"demo-wallet-sdk-react",
Expand Down
4 changes: 4 additions & 0 deletions .changeset/lemon-cars-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

feat: new infra for code snippets
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ apps/demo-nextjs
apps/demo-react-cra
apps/demo-react-vite
apps/create-fuels-counter-guide
apps/docs-snippets2/src/typegend
apps/docs-snippets2/src/**/*.wrapped.ts

packages/fuels/src/cli/commands/deploy/proxy
packages/fuels/test/fixtures/project
Expand Down
1 change: 1 addition & 0 deletions .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"/apps/docs/*",
"/packages/abi-typegen/test/**",
"templates/**",
"/apps/docs-snippets2/**/*.test.ts",
"apps/create-fuels-counter-guide/**"
],
"ignoreDependencies": [
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-snippets/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@fuel-ts/docs-snippets",
"name": "docs-snippets",
"version": "0.67.0",
"description": "",
"private": true,
Expand Down
57 changes: 0 additions & 57 deletions apps/docs-snippets/src/guide/introduction/getting-started.test.ts

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions apps/docs-snippets2/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['../../.eslintrc.js'],
rules: {
'no-console': 'off',
},
};
2 changes: 2 additions & 0 deletions apps/docs-snippets2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/typegend
src/**/*.test.ts
9 changes: 9 additions & 0 deletions apps/docs-snippets2/fuels.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createConfig } from 'fuels';

export default createConfig({
workspace: './sway',
output: './src/typegend',
forcBuildFlags: ['--release'],
forcPath: 'fuels-forc',
fuelCorePath: 'fuels-core',
});
22 changes: 22 additions & 0 deletions apps/docs-snippets2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "docs-snippets2",
"version": "0.67.0",
"description": "",
"private": true,
"scripts": {
"build": "run-s wrap:snippets build:forc",
"build:forc": "pnpm fuels build",
"wrap:snippets": "tsx scripts/wrap-snippets.ts",
"pretest": "pnpm build",
"test": "cd ../.. && pnpm run test:filter apps/docs-snippets2"
},
"dependencies": {
"fuels": "workspace:*"
},
"devDependencies": {
"glob": "^10.4.5"
},
"keywords": [],
"author": "",
"license": "ISC"
}
41 changes: 41 additions & 0 deletions apps/docs-snippets2/scripts/launcher-snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { launchTestNode } from 'fuels/test-utils';

using node = await launchTestNode({ walletsConfig: { count: 5 } });

const LOCAL_NETWORK_URL = node.provider.url;

const [w1, w2, w3, w4, w5] = node.wallets;

const WALLET_ADDRESS = w1.address;
const WALLET_PVT_KEY = w1.privateKey;

const WALLET_ADDRESS_2 = w2.address;
const WALLET_PVT_KEY_2 = w2.privateKey;

const WALLET_ADDRESS_3 = w3.address;
const WALLET_PVT_KEY_3 = w3.privateKey;

const WALLET_ADDRESS_4 = w4.address;
const WALLET_PVT_KEY_4 = w4.privateKey;

const WALLET_ADDRESS_5 = w5.address;
const WALLET_PVT_KEY_5 = w5.privateKey;

const all = [
LOCAL_NETWORK_URL,
WALLET_ADDRESS,
WALLET_PVT_KEY,
WALLET_ADDRESS_2,
WALLET_PVT_KEY_2,
WALLET_ADDRESS_3,
WALLET_PVT_KEY_3,
WALLET_ADDRESS_4,
WALLET_PVT_KEY_4,
WALLET_ADDRESS_5,
WALLET_PVT_KEY_5,
];

console.assert(
all.every((x) => !!x),
`All constants should be truthy.`
);
34 changes: 34 additions & 0 deletions apps/docs-snippets2/scripts/test-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable no-global-assign */

// %IMPORTS%

const consoleBkp = { ...console };

afterAll(() => {
console = { ...consoleBkp };
});

/**
* @group node
* @group browser
*/
test('%NAME%', async () => {
// TEST NODE LAUNCHER ———>>>
// %NODE_LAUNCHER%
// <<<——— TEST NODE LAUNCHER

console = {
...console,
log(logMsg, isTruthy) {
if (!isTruthy) {
throw new Error(logMsg);
}
},
};

// SNIPPET ———>>>
// %SNIPPET%
// <<<——— SNIPPET

return Promise.resolve();
});
Loading

0 comments on commit fff7047

Please sign in to comment.