Skip to content

Commit

Permalink
Merge pull request #50 from fullstackedorg/typescript
Browse files Browse the repository at this point in the history
TypeScript
  • Loading branch information
cplepage authored May 14, 2024
2 parents 187fef3 + b68c4d0 commit 6fea947
Show file tree
Hide file tree
Showing 42 changed files with 2,191 additions and 2,074 deletions.
71 changes: 55 additions & 16 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ import { build, merge } from "./platform/node/src/build";
import { scan } from "./editor/api/projects/scan";
import esbuild from "esbuild";
import zip from "./editor/api/projects/zip";
import child_process from "child_process";

// TypeScript fix for JSC (Safari/WebKit) memory leak
// Refer to this for more info: https://github.com/microsoft/TypeScript/issues/58137
// Remove if ever fixed
const codeToLookup = "program = createProgram(options);";
const codeToAdd = "options.oldProgram = undefined;";
const tsFilePath = "node_modules/typescript/lib/typescript.js";
const tsFileContent = fs.readFileSync(tsFilePath, { encoding: "utf-8" });
const re = new RegExp(
`${codeToLookup.replace(/(\(|\))/g, (c) => (c === "(" ? "\\(" : "\\)"))}(${codeToAdd})*`
);
const textBlockToUpdate = tsFileContent.match(re);
if (textBlockToUpdate) {
if (!textBlockToUpdate[0].endsWith(codeToAdd)) {
fs.writeFileSync(
tsFilePath,
tsFileContent.replace(re, codeToLookup + codeToAdd)
);
}
} else {
throw "Could not find typescript code block to patch.";
}

const baseFile = "src/js/index.js";

Expand All @@ -29,29 +52,34 @@ const compileScss = async (scssFile: string) => {
const compilePromises = scssFiles.map(compileScss);
await Promise.all(compilePromises);

const editorEntry = await merge(
baseFile,
path.resolve("editor/index.ts"),
".cache"
);
const buildErrors = build(
esbuild.buildSync,
editorEntry,
"index",
"editor/build",
undefined,
false,
false
);
fs.rmSync(editorEntry);
const toBuild = [
["editor/index.ts", "index"],
["editor/typescript/worker.ts", "worker-ts"]
];

let buildErrors = [];
for (const [input, output] of toBuild) {
const editorEntry = await merge(baseFile, path.resolve(input), ".cache");
const errors = build(
esbuild.buildSync,
editorEntry,
output,
"editor/build",
undefined,
"external",
false
);
fs.rmSync(editorEntry);
if (errors) buildErrors.push(errors);
}

// cleanup
scssFiles.forEach((scssFile) => {
const cssFile = scssFile.slice(0, -4) + "css";
if (fs.existsSync(cssFile)) fs.rmSync(cssFile);
});

if (buildErrors) throw buildErrors;
if (buildErrors.length) throw buildErrors;

fs.cpSync("editor/index.html", "editor/build/index.html");
fs.cpSync("editor/assets", "editor/build/assets", {
Expand All @@ -68,3 +96,14 @@ if (fs.existsSync(sampleDemoDir)) {
);
await fs.promises.writeFile("editor/build/Demo.zip", zipData);
}

fs.cpSync("node_modules/typescript/lib", "editor/build/tsLib", {
recursive: true
});

child_process.execSync(
"tsc --declaration --skipLibCheck --module system --outfile editor/build/tsLib/fullstacked.js src/adapter/fullstacked.ts",
{
stdio: "inherit"
}
);
2 changes: 1 addition & 1 deletion editor-sample-demo
6 changes: 1 addition & 5 deletions editor/api/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,7 @@ export default {
onAuth: requestGitAuth
});
} catch (e) {
// unreacheable
if (e.cause?.code == "ENOTFOUND") {
unreacheable = true;
}
unreacheable = true;
}

return {
Expand All @@ -301,7 +298,6 @@ export default {
if (e.cause?.code === "ENOTFOUND") {
return;
}
console.log(e);

return e;
}
Expand Down
5 changes: 5 additions & 0 deletions editor/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export default {
name: usernameAndEmail?.username
}
});

if (searchParams.branch) {
await git.pull(project);
await git.checkout(project, searchParams.branch);
}
}

return project;
Expand Down
59 changes: 0 additions & 59 deletions editor/api/packages/index.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,6 @@
import rpc from "../../rpc";
import gzip from "gzip-js";
import untar from "js-untar";

export default {
async install(
packageName: string,
progress: (current: number, total: number) => void,
version = "latest"
) {
const packageInfoStr = (
await rpc().fetch(
`https://registry.npmjs.org/${packageName}/${version}`,
{
encoding: "utf8"
}
)
).body as string;
const packageInfo = JSON.parse(packageInfoStr);
const tarbalUrl = packageInfo.dist.tarball;
const tarballData = (await rpc().fetch(tarbalUrl)).body as Uint8Array;
const tarData = new Uint8Array(gzip.unzip(tarballData));
const nodeModulesDirectory = await rpc().directories.nodeModules();
await rpc().fs.mkdir(nodeModulesDirectory + "/" + packageName, {
absolutePath: true
});
const files: {
name: string;
buffer: ArrayBufferLike;
type: string; // https://en.wikipedia.org/wiki/Tar_(computing)#UStar_format
}[] = await untar(tarData.buffer);
const directoriesToCreate = new Set<string>();
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (file.type === "5") continue;
const pathComponents = file.name
.slice("package/".length)
.split("/");
const filename = pathComponents.pop();
const directory = pathComponents.join("/");

const directoryToCreate =
nodeModulesDirectory + "/" + packageName + "/" + directory;
if (!directoriesToCreate.has(directoryToCreate)) {
directoriesToCreate.add(directoryToCreate);
await rpc().fs.mkdir(directoryToCreate, { absolutePath: true });
}

await rpc().fs.writeFile(
nodeModulesDirectory +
"/" +
packageName +
"/" +
directory +
"/" +
filename,
new Uint8Array(file.buffer),
{ absolutePath: true }
);
if (progress) progress(i, files.length);
}
},
async count() {
const nodeModulesDirectory = await rpc().directories.nodeModules();
if (
Expand Down
2 changes: 1 addition & 1 deletion editor/api/projects/scan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Adapter } from "../../../src/adapter";
import type { Adapter } from "../../../src/adapter/fullstacked";
import { Dirent } from "../../../src/adapter/fs";

export const scan = async (
Expand Down
2 changes: 1 addition & 1 deletion editor/api/projects/zip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter } from "../../../src/adapter";
import { Adapter } from "../../../src/adapter/fullstacked";
import { scan } from "./scan";
import * as zip from "@zip.js/zip.js";

Expand Down
3 changes: 3 additions & 0 deletions editor/assets/icons/typescript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions editor/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export const DELETE_ALL_PACKAGES_ID = "delete-all-packages";
export const SETTINGS_BUTTON_ID = "settings";
export const BACK_BUTTON_ID = "back-button";
export const PROJECT_TITLE_ID = "project-title";
export const PACKAGE_INSTALLER_ID = "package-installer";
export const TYPESCRIPT_ICON_ID = "typescript-icon";
5 changes: 5 additions & 0 deletions editor/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ a {
transform: rotate(1turn);
}
}

.cm-search label,
.cm-button {
display: inline-block;
}
28 changes: 28 additions & 0 deletions editor/packages/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@import "../style/spacing";
@import "../style/colors";
@import "../style/typography";

#package-installer {
ul {
padding: 0;
}

li {
div:last-child {
text-align: right;
font-size: $font-size-s;
color: colorOpacity(white, 0.7);
}

&:not(:last-child) {
border-bottom: 1px solid colorOpacity(white, 0.2);
padding-bottom: spacing(2);
margin-bottom: spacing(2);
}
}

.button-group {
display: flex;
justify-content: flex-end;
}
}
Loading

0 comments on commit 6fea947

Please sign in to comment.