Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into ns/fix/fuels-dev-no…
Browse files Browse the repository at this point in the history
…de-cleanup
  • Loading branch information
nedsalk committed Aug 28, 2024
2 parents 02b09c2 + 01e2f0e commit 9d51cf7
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 442 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-ties-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/utils": patch
---

fix: consistent typegen outputs
5 changes: 5 additions & 0 deletions .changeset/strong-pears-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-fuels": patch
---

fix: added missing dependencies for templates
8 changes: 4 additions & 4 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"author": "",
"license": "ISC",
"dependencies": {
"@fuel-ts/versions": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"fuels": "workspace:*",
"typedoc-plugin-markdown": "^3.15.3"
"typedoc-plugin-markdown": "^4.2.0"
},
"devDependencies": {
"@types/markdown-it": "^14.1.2",
Expand All @@ -26,8 +26,8 @@
"markdown-it": "^14.1.0",
"nodemon": "^3.1.4",
"replace": "^1.2.2",
"typedoc": "^0.25.13",
"typedoc-plugin-merge-modules": "^5.1.0",
"typedoc": "^0.26.3",
"typedoc-plugin-merge-modules": "^6.0.0",
"vitepress-plugin-search": "1.0.4-alpha.22",
"vitepress": "1.3.4",
"vue": "^3.4.38"
Expand Down
252 changes: 99 additions & 153 deletions apps/docs/scripts/typedoc-postbuild.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readdirSync, mkdirSync, copyFileSync, renameSync, writeFileSync, rmSync } from 'fs';
import { join, dirname } from 'path';
import { readdirSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import replace from 'replace';
import { fileURLToPath } from 'url';

Expand All @@ -21,23 +21,12 @@ type RegexReplacement = {
const filename = fileURLToPath(import.meta.url);
const docsDir = join(dirname(filename), '../src/');
const apiDocsDir = join(docsDir, '/api');
const classesDir = join(apiDocsDir, '/classes');
const modulesDir = join(apiDocsDir, '/modules');
const interfacesDir = join(apiDocsDir, '/interfaces_typedoc');
const enumsDir = join(apiDocsDir, '/enums');

const filesToRemove = [
'api/modules.md',
'api/classes',
'api/modules',
'api/interfaces_typedoc',
'api/enums',
];

const secondaryEntryPoints = ['-index.md', '-test_utils.md', '-cli_utils.md'];
const secondaryModules: string[] = [];
const filesToRemove = ['api/_media'];

const filePathReplacements: RegexReplacement[] = [];
const secondaryEntryPoints = ['-index.md', '-test_utils.md', '-cli_utils.md'];
const toFlattern = ['classes', 'interfaces', 'enumerations'];

const { log } = console;

Expand All @@ -50,10 +39,6 @@ const removeUnwantedFiles = () =>
rmSync(fullDirPath, { recursive: true, force: true });
});

const renameInterfaces = () => {
renameSync(join(apiDocsDir, 'interfaces'), join(apiDocsDir, 'interfaces_typedoc'));
};

/**
* Generates a json file containing the links for the sidebar to be used by vitepress.
*/
Expand Down Expand Up @@ -85,137 +70,98 @@ const exportLinksJson = () => {
}
});
});

writeFileSync('.typedoc/api-links.json', JSON.stringify(links));
};

/**
* Flattens the module files generated by typedoc. Only necessary where a package
* has multiple entry points.
*/
const flattenSecondaryModules = () => {
const modulesFiles = readdirSync(modulesDir);
const classesFiles = readdirSync(classesDir);
const interfacesFiles = readdirSync(interfacesDir);
const enumsFiles = readdirSync(enumsDir);

const files = [
...classesFiles.map((c) => ({ name: c, path: classesDir })),
...interfacesFiles.map((i) => ({ name: i, path: interfacesDir })),
...enumsFiles.map((e) => ({ name: e, path: enumsDir })),
];

// Extract secondary modules
secondaryModules.push(
...modulesFiles
.filter((file) => secondaryEntryPoints.some((entryPoint) => file.includes(entryPoint)))
.map((file) => file.replace('fuel_ts_', ''))
.map((file) => file.split('.')[0])
);

// Move files to the primary module
secondaryModules.forEach((secondaryModule) => {
const primaryModule = secondaryModule.split('-')[0];
files.forEach(({ name, path }) => {
if (name.includes(secondaryModule)) {
const nameWithPrimaryModule = name.replace(secondaryModule, primaryModule);
renameSync(join(path, name), join(path, nameWithPrimaryModule));

// Regenerate internal links for primary module
const digRecursively = (startingPath: string, rootPackagePath: string, rootPackageName: string) => {
const subDirs = readdirSync(startingPath);
subDirs.forEach((dirName) => {
const secondaryDirPath = join(startingPath, dirName);
if (toFlattern.includes(dirName)) {
const secondaryDirFiles = readdirSync(secondaryDirPath);

secondaryDirFiles.forEach((file) => {
renameSync(join(secondaryDirPath, file), join(rootPackagePath, file));
const capitalRootPackageName =
rootPackageName.charAt(0).toUpperCase() + rootPackageName.slice(1);
const filePathToReplace = startingPath.replace(rootPackagePath, rootPackageName);
filePathReplacements.push({
regex: name.replace('.md', ''),
replacement: nameWithPrimaryModule.replace('.md', ''),
regex: `${filePathToReplace}/${dirName}/${file}`,
replacement: `${capitalRootPackageName}/${file}`,
});
});

rmSync(secondaryDirPath, { recursive: true, force: true });
} else {
if (statSync(secondaryDirPath).isDirectory()) {
digRecursively(secondaryDirPath, rootPackagePath, rootPackageName);
}
});

if (dirName === 'index.md') {
const pathAfterRoot = secondaryDirPath.replace(`${rootPackagePath}/`, '');
const pathSegments = pathAfterRoot.split('/');
if (pathSegments.length - 1 > 0) {
const newIndexFileName =
pathSegments[pathSegments.length - 2] === 'index'
? `src-index.md`
: `${pathSegments[pathSegments.length - 2]}-index.md`;
renameSync(secondaryDirPath, join(rootPackagePath, newIndexFileName));
filePathReplacements.push({
regex: `${pathAfterRoot}`,
replacement: `./${newIndexFileName}`,
});
}
}
}
});
};

/**
* Alters the typedoc generated file structure to be more semantic.
* Flattens the module files generated by typedoc. Only necessary where a package
* has multiple entry points.
*/
const alterFileStructure = () => {
const modulesFiles = readdirSync(modulesDir);
const classesFiles = readdirSync(classesDir);
const interfacesFiles = readdirSync(interfacesDir);
const enumsFiles = readdirSync(enumsDir);

const files = [
...classesFiles.map((c) => ({ name: c, path: classesDir })),
...interfacesFiles.map((i) => ({ name: i, path: interfacesDir })),
...enumsFiles.map((e) => ({ name: e, path: enumsDir })),
];

modulesFiles.forEach((modulesFile) => {
// Create a new directory for each module
const newDirName = modulesFile.split('.')[0];
const newDirPath = join(apiDocsDir, newDirName);
mkdirSync(newDirPath);

// Prepare new module directory to remove 'fuel_ts_' prefix
const formattedDirName = newDirPath.split('fuel_ts_')[1];
const capitalisedDirName = formattedDirName.charAt(0).toUpperCase() + formattedDirName.slice(1);

files.forEach(({ name, path }) => {
if (name.startsWith(newDirName)) {
const newFilePath = join(newDirPath, name);
copyFileSync(join(path, name), newFilePath);

// Rename the file to remove module prefix
const newName = name.split('-')[1];
renameSync(newFilePath, join(newDirPath, newName));
// Push a replacement for internal links cleanup
filePathReplacements.push({
regex: name,
replacement: `/api/${capitalisedDirName}/${newName}`,
});
}
});

// Move module index file
copyFileSync(join(modulesDir, modulesFile), join(newDirPath, 'index.md'));

// Push a replacement for internal links cleanup
filePathReplacements.push({
regex: modulesFile,
replacement: `/api/${capitalisedDirName}/index.md`,
});

// Rename module directory to capitalised name
renameSync(newDirPath, join(apiDocsDir, capitalisedDirName));
const flattenSecondaryModules = () => {
const primaryDirs = readdirSync(apiDocsDir);
primaryDirs.forEach((primaryDirName) => {
const primaryDirPath = join(apiDocsDir, primaryDirName);
if (statSync(primaryDirPath).isDirectory()) {
digRecursively(primaryDirPath, primaryDirPath, primaryDirName);
}
});
};

/**
* Cleans up the secondary modules generated by typedoc.
* Capitalise the Primary Directories
*/
const cleanupSecondaryModules = () => {
secondaryModules.forEach((secondaryModule) => {
const primaryModule = secondaryModule.split('-')[0];
const capitalisedSecondaryModuleName =
secondaryModule.charAt(0).toUpperCase() + secondaryModule.slice(1);
const capitalisedPrimaryModuleName =
primaryModule.charAt(0).toUpperCase() + primaryModule.slice(1);

const oldFilePath = join(apiDocsDir, capitalisedSecondaryModuleName, 'index.md');

// Format the name so there isn't multiple index files for a single package
let newFileName = secondaryModule.split('-')[1];
newFileName = newFileName === 'index' ? 'src-index' : `${newFileName.replace('_', '-')}-index`;
const newFilePath = join(apiDocsDir, capitalisedPrimaryModuleName, `${newFileName}.md`);

// Move the secondary module index file to the primary module
renameSync(oldFilePath, newFilePath);

// Regenerate links for the secondary module
filePathReplacements.push({
regex: `${capitalisedSecondaryModuleName}/index`,
replacement: `${capitalisedPrimaryModuleName}/${newFileName}`,
const capitalisePrimaryDirs = () => {
const primaryDirs = readdirSync(apiDocsDir);
primaryDirs
.filter((directory) => !directory.includes('.md'))
.forEach((primaryDirName) => {
const capitalise = primaryDirName.charAt(0).toUpperCase() + primaryDirName.slice(1);
const primaryDirPath = join(apiDocsDir, primaryDirName);
renameSync(primaryDirPath, join(apiDocsDir, capitalise));
filePathReplacements.push({
regex: `${primaryDirName}/index.md`,
replacement: `${capitalise}/index.md`,
});
});
};

// Remove the secondary module
rmSync(join(apiDocsDir, capitalisedSecondaryModuleName), { recursive: true, force: true });
/**
* Remove empty directories
*/
const cleanupDirectories = (dirPath: string) => {
const primaryDirs = readdirSync(dirPath);
primaryDirs.forEach((dir) => {
const fullPath = join(dirPath, dir);
if (statSync(fullPath).isDirectory()) {
cleanupDirectories(fullPath);
}
});
if (readdirSync(dirPath).length === 0) {
rmdirSync(dirPath);
}
};

/**
Expand All @@ -226,38 +172,38 @@ const recreateInternalLinks = () => {

const prefixReplacements: RegexReplacement[] = [
// Prefix/Typedoc cleanups
{ regex: '../modules/', replacement: '/api/' },
{ regex: '../classes/', replacement: '/api/' },
{ regex: '../interfaces/', replacement: '/api/' },
{ regex: '../enums/', replacement: '/api/' },
{ regex: 'fuel_ts_', replacement: '' },
{ regex: '/api//api/', replacement: '/api/' },
{ regex: 'classes/', replacement: './' },
{ regex: 'interfaces/', replacement: './' },
{ regex: 'enumerations/', replacement: './' },
{ regex: '../../../', replacement: '../' },
{ regex: '../../', replacement: '../' },
{ regex: 'index/index', replacement: 'index' },
{ regex: '.././', replacement: './' },
// Resolves `[plugin:vite:vue] Element is missing end tag.` error
{ regex: '<', replacement: '&lt;' },
];

topLevelDirs
.filter((directory) => !directory.endsWith('.md'))
.forEach((dir) => {
[...filePathReplacements, ...prefixReplacements].forEach(({ regex, replacement }) => {
replace({
regex,
replacement,
paths: [join(apiDocsDir, dir)],
recursive: true,
silent: true,
});
const internalLinksReplacement = [...filePathReplacements, ...prefixReplacements];

topLevelDirs.forEach((dir) => {
internalLinksReplacement.forEach(({ regex, replacement }) => {
replace({
regex,
replacement,
paths: [join(apiDocsDir, dir)],
recursive: true,
silent: true,
});
});
});
};

const main = () => {
log('Cleaning up API docs.');
renameInterfaces();
flattenSecondaryModules();
alterFileStructure();
cleanupSecondaryModules();
removeUnwantedFiles();
flattenSecondaryModules();
capitalisePrimaryDirs();
cleanupDirectories(apiDocsDir);
exportLinksJson();
recreateInternalLinks();
};
Expand Down
7 changes: 4 additions & 3 deletions apps/docs/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"cacheBust": true,
"hideGenerator": true,
"plugin": ["typedoc-plugin-markdown", "typedoc-plugin-merge-modules"],
"filenameSeparator": "-",
// ********************** //
// Markdown Config
// ********************** //
"hideBreadcrumbs": true,
"hideInPageTOC": true,
"entryDocument": "index.md",
"membersWithOwnFile": ["Class", "Enum", "Interface"],
"excludeScopesInPaths": true,
"entryFileName": "index.md",
"modulesFileName": "index",
// ******************** //
// Merge Modules Config
// ******************** //
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/src/utils/bytecode.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { arrayify } from './arrayify';
import { compressBytecode, decompressBytecode } from './bytecode';
import { sleep } from './sleep';

/**
* We are using a base64 encoded bytecode here to avoid having to
Expand All @@ -20,6 +21,13 @@ describe('bytecode utils', () => {
expect(compressedBytecode.length).toBeLessThan(bytecodeBinary.length);
});

test('should compress to the same value', async () => {
const compressedBytecode = compressBytecode(bytecodeBinary);
await sleep(1000);
const compressedBytecode2 = compressBytecode(bytecodeBinary);
expect(compressedBytecode).toEqual(compressedBytecode2);
});

test('should decompress bytecode', () => {
const compressedBytecode = compressBytecode(bytecodeBinary);
const decompressedBytecode = decompressBytecode(compressedBytecode);
Expand Down
Loading

0 comments on commit 9d51cf7

Please sign in to comment.