Skip to content

Commit

Permalink
test - types - first
Browse files Browse the repository at this point in the history
  • Loading branch information
conmaster2112 committed Oct 4, 2024
1 parent a440b78 commit 0d04873
Show file tree
Hide file tree
Showing 21 changed files with 1,412 additions and 37,276 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
}
4 changes: 2 additions & 2 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "ES2022",
"target": "ES2022",
"module": "ESNext",
"target": "ESNext",
"allowJs": true,
"checkJs": true,
"resolveJsonModule": true,
Expand Down
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"homepage": "https://github.com/Bedrock-APIs/bds-docs#readme",
"dependencies": {
"minimatch": "^10.0.1",
"typescript": "^5.6.2",
"unzip-stream": "^0.3.4"
},
"devDependencies": {
"@types/node": "^22.7.4"
"@types/node": "^22.7.4",
"typescript": "^5.7.0-dev.20241003"
}
}
Empty file removed src/flags/decleration_modules.js
Empty file.
5 changes: 5 additions & 0 deletions src/flags/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { METADATA } from "./metadata.js";
import { SCRIPT_MODULES_MAPPING } from "./module_maping.js";
import { SCRIPT_DECLARATIONS } from "./script_decleration.js";

/**
* @type {{
Expand All @@ -14,5 +15,9 @@ export const GENERATOR_FLAGS = [
{
flagId: SCRIPT_MODULES_MAPPING.name,
method: SCRIPT_MODULES_MAPPING
},
{
flagId: SCRIPT_DECLARATIONS.name,
method: SCRIPT_DECLARATIONS
}
];
7 changes: 3 additions & 4 deletions src/flags/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FileTree } from "../functions.js";
import { readFile, writeFile } from "node:fs/promises";
import { existsSync, mkdirSync } from "node:fs";

const OUTPUT_FOLDER = "./metadata";
/**
*
* @param {string} inputDirPath
Expand Down Expand Up @@ -44,7 +45,7 @@ async function Task(input,fileName) {
// Check if buffer is valid content
if(buffer == null) return false;

const outFile = resolve(METADATA_FOLDER, fileName);
const outFile = resolve(OUTPUT_FOLDER, fileName);
const outDir = dirname(outFile);

// Has to be sync to be sure we are not about to call mkdir with same directory path
Expand All @@ -57,7 +58,7 @@ async function Task(input,fileName) {
).then(()=>true, ()=>false);

if(results) console.log("[METADATA] Generated: " + fileName);
else console.log("[METADATA] Generate: " + fileName);
else console.log("[METADATA] Generation failed: " + fileName);

// Returns if file was successfully created
return results;
Expand All @@ -80,5 +81,3 @@ async function TransformJsonModule(input) {
return JSON.stringify(data, null, 4);
}


const METADATA_FOLDER = "./metadata";
86 changes: 86 additions & 0 deletions src/flags/script_decleration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { dirname, resolve } from "node:path";
import { readFile } from "node:fs/promises";
import { createWriteStream, existsSync, mkdirSync } from "node:fs";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import { Printer } from "./ts-declarations/printers.js";
import { FileTree } from "../functions.js";

const OUTPUT_FOLDER = "./script-declerations";
/**
*
* @param {string} inputDirPath
* @returns {Promise<boolean>}
*/
export async function SCRIPT_DECLARATIONS(inputDirPath) {
// Init
const inputDir = resolve(inputDirPath, "docs/script_modules");
const tasks = [];

// Task Factory for each file in the path tree
for (const file of FileTree(inputDir)) tasks.push(
Task(inputDir, file).catch(()=>false)
);

// Return Only Succesfull Creations
const results = (await Promise.all(tasks)).filter(s=>s);

// Check if all tasks has successfully ended.
return tasks.length == results.length;
}
/**
* @param {string} input
* @param {string} fileName
* @returns {Promise<boolean>}
*/
async function Task(input,fileName) {

// Read File
/**@type {Buffer | string | null} */
let buffer = await readFile(resolve(input, fileName)).catch(()=>null);

// Check if file was properly readed
if(buffer == null) return false;

// Transform File content
if(!fileName.endsWith(".json")) return false;

// Check if buffer is valid content
if(buffer == null) return false;



const outFile = resolve(OUTPUT_FOLDER, fileName);
const outDir = dirname(outFile);

// Has to be sync to be sure we are not about to call mkdir with same directory path
if(!existsSync(outDir)) mkdirSync(outDir, {recursive: true});


const writeStream = createWriteStream(outFile.replace(/.json$/g,".d.ts"));

const readable = PrintScriptModule(buffer);

const results = await pipeline(
readable,
writeStream
).then(()=>true, ()=>false);

if(results) console.log("[SCRIPT_DECLARATIONS] Generated: " + fileName);
else console.log("[SCRIPT_DECLARATIONS] Generation failed: " + fileName);

// Returns if file was successfully created
return results;
}

/**
* @param {Buffer} input
*/
function PrintScriptModule(input) {
// Parse Conent
const data = JSON.parse(input.toString());
// Check if content was object
if(typeof data != "object") throw new TypeError("Expected JSON module is not an instance of object.s");

return Readable.from(Printer(data));
}
Loading

0 comments on commit 0d04873

Please sign in to comment.