Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new checks #7

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .github/generate.contents.js

This file was deleted.

16 changes: 16 additions & 0 deletions .github/scripts/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fs = require("node:fs");
const {Buffer} = require("node:buffer")
function * getFiles(path, recursive = true){
for(const entry of fs.readdirSync(path, {withFileTypes: true}))
if(entry.isFile()) yield path + "/" + entry.name;
else if(entry.isDirectory() && recursive) yield * getFiles(path + "/" + entry.name, recursive);
}
async function downloadContent(url, headers = {}) {
return Buffer.from(
await (await fetch(url, { headers, method: 'GET' })).arrayBuffer()
);
}
module.exports = {
getFiles,
downloadContent
}
29 changes: 29 additions & 0 deletions .github/scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require("node:fs");
const {getFiles} = require("./functions");

const bypass = process.argv[2] === "--bypass";
if(!bypass && !fs.existsSync("./contents.json")) {
console.error("Console Missing Generated Contents");
process.exit(1);
}

const contents = [...getFiles(".")].filter(e=>
!e.startsWith("./.git/") &&
!e.startsWith("./node_modules/") &&
!e.endsWith("package-lock.json")
).sort();
const content = {
contents,
modules: contents.filter(e=>e.startsWith("./modules")).map(e=>e.slice(10))
};

const newData = JSON.stringify(content, null, 4);
if(!bypass){
const data = fs.readFileSync("./contents.json").toString();
if(newData !== data) {
console.error("Contents are not up to date!");
console.log(newData, data);
process.exit(1);
}
}
fs.writeFileSync("./contents.json",newData);
146 changes: 146 additions & 0 deletions .github/scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
process.exit(0);
const ts = require("typescript");
const {downloadContent, getFiles} = require("./functions");
const path = require("node:path");
const { readFileSync } = require("node:fs");
const branch = "preview";

const apis = new Map();
(async()=>{
console.log("[LOADING RESOURCES] Loading vanilla resources");
let time = Date.now();
const content = await downloadContent(`https://raw.githubusercontent.com/bedrock-apis/bds-docs/${branch}/exist.json`);
const info = JSON.parse(content.toString());
const tasks = [];
for(const key of Object.keys(info.script_modules_mapping)){
if(!key.startsWith("@")) continue;
const moduleInfo = info.script_modules_mapping[key];
const vers = moduleInfo.versions.sort(compareVersions);
const job = async (path, key)=>{
const content = (await downloadContent(`https://raw.githubusercontent.com/bedrock-apis/bds-docs/${branch}/script_types/${path}.d.ts`)).toString();
if(content === "404: Not Found") return;
apis.set(key, content.toString());
};
const stableVersion = vers.filter(e=>!e.includes("-")).at(-1);
const betaVersion = vers.filter(e=>e.includes("beta")).at(-1)??stableVersion;
const alphaVersion = vers.at(-1)??vers.filter(e=>e.includes("beta")).at(-1)??betaVersion;
tasks.push(job(`${key}_${alphaVersion}`, `${key}_alpha`));
tasks.push(job(`${key}_${betaVersion}`, `${key}_beta`));
tasks.push(job(`${key}_${stableVersion}`, `${key}_stable`));
}
await Promise.all(tasks);
console.log("[LOADING RESOURCES] Loaded vanilla resources in",Date.now() - time,"ms");
runDiagnostics([...getFiles("./modules")].filter(e=>e.endsWith(".ts") | e.endsWith(".js")));
})().catch(e=>process.exit(console.error(e)??1));



function runDiagnostics(sourceFiles) {
const options = {
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.Latest,
moduleResolution: ts.ModuleResolutionKind.Node10,
allowJs: true,
outDir: "./output",
skipLibCheck: true,
skipDefaultLibCheck: true,
strict: false
};
const host = createCompilerHost(options);
for(const file of sourceFiles) {
console.log("[DIAGNOSTIC] Diagnostics for ",file);
let data = readFileSync(file).toString();
let channel = "stable";
if(data.startsWith("////")){
channel = data.split("\n")[0].slice(4).replaceAll(" ","").toLowerCase();
data = data.split("\n").slice(1).join("\n");
}
host.getCurrentChannel = ()=>channel;
host.getSource = (fileName)=>{
return file.endsWith(fileName)?data:null;
}
const program = ts.createProgram([file], options, host);
console.log(program.emit().diagnostics);
}
//

//ts.createSourceFile("myName.d.ts", "export class Player {};", ts.ScriptTarget.Latest);
//ts.factory.updateSourceFile(ts.createSourceFile("myName.d.ts", "export class Player {};", ts.ScriptTarget.Latest))
//const checker = program.getTypeChecker();
/// do something with program..
//console.log(program.getSourceFiles().map(e=>e.fileName).filter(e=>!e.startsWith("lib.")));
//console.log(program.getSourceFile("C:/Users/samde/Documents/GitHub/bapi-tool/node_modules/@minecraft/server/index.d.ts"))
}
function createCompilerHost(options) {
let object;
return object = {
getSourceFile,
getDefaultLibFileName: () => "lib.es2022.d.ts",
writeFile(fileName, content){ console.log("[ABSTRACT] - writeFile: " + fileName);},
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
getDirectories: path => ts.sys.getDirectories(path),
getCanonicalFileName: fileName =>
ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
getNewLine: () => ts.sys.newLine,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
fileExists(fileName) { return false; },
readFile: ts.sys.readFile,
resolveModuleNames
};
function getSourceFile(fileName, languageVersion, onError) {
let sourceText
if(fileName.startsWith("lib.") && fileName.endsWith(".d.ts")){
path.dirname(require.resolve("typescript")) + fileName;
sourceText = object.readFile(path.join(path.dirname(require.resolve("typescript")), fileName));
} else if(fileName.includes("__native__")) {
const moduleName = fileName.split("__native__")[0];
const channel = object.getCurrentChannel?.()??"stable";
sourceText = apis.get(moduleName+"_"+channel);
}else{
sourceText = object.getSource?.(fileName)??object.readFile(fileName);
}
return sourceText !== undefined
? ts.createSourceFile(fileName, sourceText, languageVersion)
: undefined;
}

function resolveModuleNames(
moduleNames,
containingFile
) {
const resolvedModules = [];
for (const moduleName of moduleNames) {
if(moduleName.startsWith("@")){
resolvedModules.push({
resolvedFileName: moduleName + "__native__.d.ts"
})
continue;
}
// try to use standard resolution
let result = ts.resolveModuleName(moduleName, containingFile, options, {
fileExists,
readFile
});
if (result.resolvedModule) {
resolvedModules.push(result.resolvedModule);
console.log(result);
} else {
console.log("cant found");
}
}
return resolvedModules;
}
}

function compareVersions(ver, ver2){
const [version1, tag1] = ver.split("-");
const [version2, tag2] = ver2.split("-");
let numbers1 = version1.split(".").map(Number);
let numbers2 = version2.split(".").map(Number);
for(let i = 0; i < numbers2.length | i < numbers1.length; i++){
let num1 = numbers1[i]??0, num2 = numbers2[i]??0;
if(num1 > num2) return 1;
else if(num1 < num2) return -1;
}
return 0;
}
19 changes: 19 additions & 0 deletions .github/workflows/content_generator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Content Generator

on:
pull_request:
branches: [ main ]
jobs:
generation:
name: Content Generated
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4

- name: Running Clean Up Code
run: |
npm run generate-check
15 changes: 0 additions & 15 deletions .github/workflows/pull_request.yml

This file was deleted.

29 changes: 0 additions & 29 deletions .github/workflows/push.yml

This file was deleted.

20 changes: 20 additions & 0 deletions .github/workflows/type_checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Examples Type Chacker

on:
pull_request:
branches: [ main ]

jobs:
check:
name: Type Checker
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4

- name: Running Clean Up Code
run: |
npm run check
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock.json
node_modules
28 changes: 16 additions & 12 deletions contents.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
"date": 1718705958052,
"contents": [
".github/generate.contents.js",
".github/workflows/pull_request.yml",
".github/workflows/push.yml",
"README.md",
"contents.json",
"modules/test.json"
],
"modules": [
"modules/test.json"
]
"contents": [
"./.github/scripts/functions.js",
"./.github/scripts/generate.js",
"./.github/scripts/index.js",
"./.github/workflows/content_generator.yml",
"./.github/workflows/type_checker.yml",
"./.gitignore",
"./README.md",
"./contents.json",
"./decleration.tests.d.ts",
"./modules/test.js",
"./package.json"
],
"modules": [
"test.js"
]
}
Loading