Skip to content

Commit

Permalink
Convert scripts to ES Modules (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishtahir authored Oct 30, 2023
1 parent f254759 commit 0553ade
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "Nish Tahir",
"license": "Apache-2.0",
"homepage": "https://github.com/nishtahir/language-kotlin#readme",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/nishtahir/language-kotlin.git"
Expand Down
14 changes: 7 additions & 7 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* - Kotlin.tmLanguage
*/

const fs = require("fs");
const path = require("path");
const plist = require("plist");
const json = require("format-json");
const yaml = require("yamljs");
const deepmerge = require("deepmerge");
const { safeWriteFileSync } = require("./util");
import fs from "fs";
import path from "path";
import plist from "plist";
import json from "format-json";
import yaml from "yamljs";
import deepmerge from "deepmerge";
import { __dirname, safeWriteFileSync } from "./util.js";

const SOURCE_PATH = path.resolve(__dirname, "../", "src/");
const OUTPUT_PATH = path.resolve(__dirname, "../dist/");
Expand Down
25 changes: 15 additions & 10 deletions scripts/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
* It does not consider the inner structure of the scopes, only that they are hit.
*/

const path = require("path");
const plist = require("plist");
const { safeWriteFileSync, getAllFilesInDir, readFileSync } = require("./util");

const {
import path from "path";
import plist from "plist";
import {
__dirname,
safeWriteFileSync,
getAllFilesInDir,
readFileSync,
} from "./util.js";

import {
parseGrammarTestCase,
runGrammarTestCase,
} = require("vscode-tmgrammar-test/dist/unit/index");
const { createRegistry } = require("vscode-tmgrammar-test/dist/common/index");
} from "vscode-tmgrammar-test/dist/unit/index.js";
import { createRegistry } from "vscode-tmgrammar-test/dist/common/index.js";

const lcovWrite = require("lcov-write");
const Yaml = require("yaml");
const YamlSourceMap = require("yaml-source-map");
import lcovWrite from "lcov-write";
import Yaml from "yaml";
import YamlSourceMap from "yaml-source-map";

const distDir = path.resolve(__dirname, "../", "dist/");
const buildDir = path.resolve(__dirname, "../", "build/");
Expand Down
13 changes: 7 additions & 6 deletions scripts/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
* It is used to ensure a consistent code style.
*/

const fs = require("fs");
const path = require("path");
const yaml = require("yamljs");
import path from "path";
import yaml from "yamljs";
import { readdirSync, readFileSync, writeFileSync } from "fs";
import { __dirname } from "./util.js";

const sourceDirPath = path.resolve(__dirname, "../", "src/");
const sourceFiles = fs.readdirSync(sourceDirPath);
const sourceFiles = readdirSync(sourceDirPath);

sourceFiles.forEach((file) => {
const filePath = path.resolve(sourceDirPath, file);
console.log("Formatting " + filePath);

const source = fs.readFileSync(filePath, "utf8");
const source = readFileSync(filePath, "utf8");
if (source.trim().length === 0) {
return; // skip empty files
}
Expand All @@ -24,5 +25,5 @@ sourceFiles.forEach((file) => {
indent: 2,
indentSeq: true,
});
fs.writeFileSync(filePath, yamlData);
writeFileSync(filePath, yamlData);
});
20 changes: 9 additions & 11 deletions scripts/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const fs = require("fs");
const path = require("path");
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";

export const __filename = fileURLToPath(import.meta.url);
export const __dirname = path.dirname(__filename);

/**
* Read a file from a given path
* @param {string} path file to read
*/
function readFileSync(path) {
export function readFileSync(path) {
return fs.readFileSync(path, "utf8");
}

Expand All @@ -15,7 +19,7 @@ function readFileSync(path) {
* @param {string} dest
* @param {string} content
*/
function safeWriteFileSync(dest, content) {
export function safeWriteFileSync(dest, content) {
const dir = dest.substring(0, dest.lastIndexOf("/") + 1);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
Expand All @@ -28,7 +32,7 @@ function safeWriteFileSync(dest, content) {
* @param {string} dirPath - path to directory
* @param {Array<string>} files - files in parent directory
*/
function getAllFilesInDir(dirPath, files = []) {
export function getAllFilesInDir(dirPath, files = []) {
fs.readdirSync(dirPath).forEach((file) => {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
files = getAllFilesInDir(dirPath + "/" + file, files);
Expand All @@ -38,9 +42,3 @@ function getAllFilesInDir(dirPath, files = []) {
});
return files;
}

module.exports = {
safeWriteFileSync,
getAllFilesInDir,
readFileSync,
};

0 comments on commit 0553ade

Please sign in to comment.