Skip to content

Commit

Permalink
Migrate to ESM (TurboWarp#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Jun 23, 2024
1 parent 68dcb60 commit 3103e09
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 75 deletions.
10 changes: 6 additions & 4 deletions development/build-production.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const pathUtil = require("path");
const Builder = require("./builder");
import pathUtil from "node:path";
import urlUtil from "node:url";
import Builder from "./builder.js";

const outputDirectory = pathUtil.join(__dirname, "../build");
const l10nOutput = pathUtil.join(__dirname, "../build-l10n");
const dirname = pathUtil.dirname(urlUtil.fileURLToPath(import.meta.url));
const outputDirectory = pathUtil.join(dirname, "../build");
const l10nOutput = pathUtil.join(dirname, "../build-l10n");

const builder = new Builder("production");
const build = builder.build();
Expand Down
66 changes: 34 additions & 32 deletions development/builder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const fs = require("fs");
const AdmZip = require("adm-zip");
const pathUtil = require("path");
const ExtendedJSON = require("@turbowarp/json");
const compatibilityAliases = require("./compatibility-aliases");
const parseMetadata = require("./parse-extension-metadata");
const { mkdirp, recursiveReadDirectory } = require("./fs-utils");
import fs from "node:fs";
import pathUtil from "node:path";
import urlUtil from "node:url";
import AdmZip from "adm-zip";
import ExtendedJSON from "@turbowarp/json";
import spdxParser from "spdx-expression-parse";
import sizeOfImage from "image-size";
import compatibilityAliases from "./compatibility-aliases.js";
import parseMetadata from "./parse-extension-metadata.js";
import renderTemplate from "./render-template.js";
import renderDocs from "./render-docs.js";
import parseTranslations from "./parse-extension-translations.js";
import fsUtils from "./fs-utils.js";

const dirname = pathUtil.dirname(urlUtil.fileURLToPath(import.meta.url));

/**
* @typedef {'development'|'production'|'desktop'} Mode
Expand Down Expand Up @@ -213,7 +221,6 @@ class ExtensionFile extends BuildFile {
);
}

const spdxParser = require("spdx-expression-parse");
try {
// Don't care about the result -- just see if it parses.
spdxParser(metadata.license);
Expand Down Expand Up @@ -264,7 +271,6 @@ class ExtensionFile extends BuildFile {
},
};

const parseTranslations = require("./parse-extension-translations");
const jsCode = fs.readFileSync(this.sourcePath, "utf-8");
const unprefixedRuntimeStrings = parseTranslations(jsCode);
const runtimeStrings = Object.fromEntries(
Expand All @@ -290,7 +296,7 @@ class HomepageFile extends BuildFile {
samples,
mode
) {
super(pathUtil.join(__dirname, "homepage-template.ejs"));
super(pathUtil.join(dirname, "homepage-template.ejs"));

/** @type {Record<string, ExtensionFile>} */
this.extensionFiles = extensionFiles;
Expand Down Expand Up @@ -344,8 +350,6 @@ class HomepageFile extends BuildFile {
}

read() {
const renderTemplate = require("./render-template");

const mostRecentExtensions = Object.entries(this.extensionFiles)
.sort((a, b) => b[1].getLastModified() - a[1].getLastModified())
.slice(0, 5)
Expand Down Expand Up @@ -470,7 +474,6 @@ class JSONMetadataFile extends BuildFile {

class ImageFile extends BuildFile {
validate() {
const sizeOfImage = require("image-size");
const contents = this.read();
const { width, height } = sizeOfImage(contents);
const aspectRatio = width / height;
Expand Down Expand Up @@ -541,7 +544,6 @@ class DocsFile extends BuildFile {
}

read() {
const renderDocs = require("./render-docs");
const markdown = super.read().toString("utf-8");
return renderDocs(markdown, this.extensionSlug);
}
Expand Down Expand Up @@ -605,7 +607,7 @@ class Build {
}

export(root) {
mkdirp(root);
fsUtils.mkdirp(root);

for (const [relativePath, file] of Object.entries(this.files)) {
const directoryName = pathUtil.dirname(relativePath);
Expand Down Expand Up @@ -659,7 +661,7 @@ class Build {
* @param {string} root
*/
exportL10N(root) {
mkdirp(root);
fsUtils.mkdirp(root);

const groups = this.generateL10N();
for (const [name, strings] of Object.entries(groups)) {
Expand All @@ -685,12 +687,12 @@ class Builder {
this.mode = mode;
}

this.extensionsRoot = pathUtil.join(__dirname, "../extensions");
this.websiteRoot = pathUtil.join(__dirname, "../website");
this.imagesRoot = pathUtil.join(__dirname, "../images");
this.docsRoot = pathUtil.join(__dirname, "../docs");
this.samplesRoot = pathUtil.join(__dirname, "../samples");
this.translationsRoot = pathUtil.join(__dirname, "../translations");
this.extensionsRoot = pathUtil.join(dirname, "../extensions");
this.websiteRoot = pathUtil.join(dirname, "../website");
this.imagesRoot = pathUtil.join(dirname, "../images");
this.docsRoot = pathUtil.join(dirname, "../docs");
this.samplesRoot = pathUtil.join(dirname, "../samples");
this.translationsRoot = pathUtil.join(dirname, "../translations");
}

build() {
Expand All @@ -708,7 +710,7 @@ class Builder {
* @type {Record<string, Record<string, Record<string, string>>>}
*/
const translations = {};
for (const [filename, absolutePath] of recursiveReadDirectory(
for (const [filename, absolutePath] of fsUtils.recursiveReadDirectory(
this.translationsRoot
)) {
if (!filename.endsWith(".json")) {
Expand All @@ -721,7 +723,7 @@ class Builder {

/** @type {Record<string, ExtensionFile>} */
const extensionFiles = {};
for (const [filename, absolutePath] of recursiveReadDirectory(
for (const [filename, absolutePath] of fsUtils.recursiveReadDirectory(
this.extensionsRoot
)) {
if (!filename.endsWith(".js")) {
Expand All @@ -742,7 +744,7 @@ class Builder {

/** @type {Record<string, ImageFile>} */
const extensionImages = {};
for (const [filename, absolutePath] of recursiveReadDirectory(
for (const [filename, absolutePath] of fsUtils.recursiveReadDirectory(
this.imagesRoot
)) {
const extension = pathUtil.extname(filename);
Expand All @@ -762,7 +764,7 @@ class Builder {

/** @type {Map<string, SampleFile[]>} */
const samples = new Map();
for (const [filename, absolutePath] of recursiveReadDirectory(
for (const [filename, absolutePath] of fsUtils.recursiveReadDirectory(
this.samplesRoot
)) {
if (!filename.endsWith(".sb3")) {
Expand All @@ -781,14 +783,14 @@ class Builder {
build.files[`/samples/${filename}`] = file;
}

for (const [filename, absolutePath] of recursiveReadDirectory(
for (const [filename, absolutePath] of fsUtils.recursiveReadDirectory(
this.websiteRoot
)) {
build.files[`/${filename}`] = new BuildFile(absolutePath);
}

if (this.mode !== "desktop") {
for (const [filename, absolutePath] of recursiveReadDirectory(
for (const [filename, absolutePath] of fsUtils.recursiveReadDirectory(
this.docsRoot
)) {
if (!filename.endsWith(".md")) {
Expand All @@ -801,7 +803,7 @@ class Builder {
}

const scratchblocksPath = pathUtil.join(
__dirname,
dirname,
"../node_modules/@turbowarp/scratchblocks/build/scratchblocks.min.js"
);
build.files["/docs-internal/scratchblocks.js"] = new BuildFile(
Expand Down Expand Up @@ -853,9 +855,9 @@ class Builder {
return null;
}

startWatcher(callback) {
async startWatcher(callback) {
// Load chokidar lazily.
const chokidar = require("chokidar");
const chokidar = await import("chokidar");
callback(this.tryBuild());
chokidar
.watch(
Expand Down Expand Up @@ -893,4 +895,4 @@ class Builder {
}
}

module.exports = Builder;
export default Builder;
12 changes: 6 additions & 6 deletions development/colors.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const enableColor = !process.env.NO_COLOR;
const color = (i) => (enableColor ? i : "");

module.exports = {
RESET: color("\x1b[0m"),
BOLD: color("\x1b[1m"),
RED: color("\x1b[31m"),
GREEN: color("\x1b[32m"),
};
const RESET = color("\x1b[0m");
const BOLD = color("\x1b[1m");
const RED = color("\x1b[31m");
const GREEN = color("\x1b[32m");

export { RESET, BOLD, RED, GREEN };
2 changes: 1 addition & 1 deletion development/compatibility-aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const extensions = {
"/LukeManiaStudios/TempVariables2.js": "/Lily/TempVariables2.js",
};

module.exports = extensions;
export default extensions;
6 changes: 3 additions & 3 deletions development/fs-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const pathUtil = require("path");
import fs from "node:fs";
import pathUtil from "node:path";

/**
* Recursively read a directory.
Expand Down Expand Up @@ -47,7 +47,7 @@ const mkdirp = (directory) => {
}
};

module.exports = {
export default {
recursiveReadDirectory,
mkdirp,
};
14 changes: 8 additions & 6 deletions development/get-credits-for-gui.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const fs = require("fs");
const path = require("path");
const https = require("https");
const fsUtils = require("./fs-utils");
const parseMetadata = require("./parse-extension-metadata");
import fs from "node:fs";
import pathUtil from "node:path";
import urlUtil from "node:url";
import https from "node:https";
import fsUtils from "./fs-utils.js";
import parseMetadata from "./parse-extension-metadata.js";

class AggregatePersonInfo {
/** @param {Person} person */
Expand Down Expand Up @@ -62,7 +63,8 @@ const run = async () => {
*/
const aggregate = new Map();

const extensionRoot = path.resolve(__dirname, "../extensions/");
const dirname = pathUtil.dirname(urlUtil.fileURLToPath(import.meta.url));
const extensionRoot = pathUtil.resolve(dirname, "../extensions/");
for (const [name, absolutePath] of fsUtils.recursiveReadDirectory(
extensionRoot
)) {
Expand Down
2 changes: 1 addition & 1 deletion development/parse-extension-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ const parseMetadata = (extensionCode) => {
return metadata;
};

module.exports = parseMetadata;
export default parseMetadata;
8 changes: 4 additions & 4 deletions development/parse-extension-translations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const espree = require("espree");
const esquery = require("esquery");
const parseMetadata = require("./parse-extension-metadata");
import * as espree from "espree";
import esquery from "esquery";
import parseMetadata from "./parse-extension-metadata.js";

/**
* @fileoverview Parses extension code to find calls to Scratch.translate() and statically
Expand Down Expand Up @@ -120,4 +120,4 @@ const parseTranslations = (js) => {
return result;
};

module.exports = parseTranslations;
export default parseTranslations;
12 changes: 7 additions & 5 deletions development/render-docs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require("path");
const MarkdownIt = require("markdown-it");
const renderTemplate = require("./render-template");
import pathUtil from "node:path";
import urlUtil from "node:url";
import MarkdownIt from "markdown-it";
import renderTemplate from "./render-template.js";

const md = new MarkdownIt({
html: true,
Expand Down Expand Up @@ -63,7 +64,8 @@ const renderDocs = (markdownSource, slug) => {

const bodyHTML = md.renderer.render(tokens, md.options, env);

return renderTemplate(path.join(__dirname, "docs-template.ejs"), {
const dirname = pathUtil.dirname(urlUtil.fileURLToPath(import.meta.url));
return renderTemplate(pathUtil.join(dirname, "docs-template.ejs"), {
slug,
headerHTML,
headerText,
Expand All @@ -72,4 +74,4 @@ const renderDocs = (markdownSource, slug) => {
});
};

module.exports = renderDocs;
export default renderDocs;
6 changes: 3 additions & 3 deletions development/render-template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const ejs = require("ejs");
import fs from "node:fs";
import ejs from "ejs";

// TODO: Investigate the value of removing dependency on `ejs` and possibly writing our own DSL.

Expand All @@ -9,4 +9,4 @@ const renderTemplate = (path, data) => {
return outputHTML;
};

module.exports = renderTemplate;
export default renderTemplate;
4 changes: 2 additions & 2 deletions development/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require("express");
const Builder = require("./builder");
import express from "express";
import Builder from "./builder.js";

let mostRecentBuild = null;
const builder = new Builder("development");
Expand Down
4 changes: 2 additions & 2 deletions development/validate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Builder = require("./builder");
const Colors = require("./colors");
import Builder from "./builder.js";
import * as Colors from "./colors.js";

const builder = new Builder("production");
const errors = builder.validate();
Expand Down
11 changes: 5 additions & 6 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const js = require('@eslint/js');
const globals = require('globals');
import js from '@eslint/js';
import globals from 'globals';

module.exports = [
export default [
// Base on eslint recommended
js.configs.recommended,

// Common for all files
{
languageOptions: {
ecmaVersion: 2022,
sourceType: 'commonjs'
ecmaVersion: 2022
},
rules: {
// Unused variables commonly indicate logic errors
Expand Down Expand Up @@ -76,7 +75,6 @@ module.exports = [
],
languageOptions: {
globals: {
...globals.commonjs,
...globals.node
}
}
Expand All @@ -88,6 +86,7 @@ module.exports = [
'extensions/**'
],
languageOptions: {
sourceType: 'script',
globals: {
...globals.browser,
Blockly: 'readonly',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@turbowarp/extensions",
"version": "0.0.1",
"type": "module",
"description": "Unsandboxed extensions for TurboWarp",
"scripts": {
"start": "node development/server.js",
Expand Down

0 comments on commit 3103e09

Please sign in to comment.