Skip to content

Commit 0553ade

Browse files
authored
Convert scripts to ES Modules (nishtahir#74)
1 parent f254759 commit 0553ade

File tree

5 files changed

+39
-34
lines changed

5 files changed

+39
-34
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"author": "Nish Tahir",
66
"license": "Apache-2.0",
77
"homepage": "https://github.com/nishtahir/language-kotlin#readme",
8+
"type": "module",
89
"repository": {
910
"type": "git",
1011
"url": "git+https://github.com/nishtahir/language-kotlin.git"

scripts/build.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* - Kotlin.tmLanguage
77
*/
88

9-
const fs = require("fs");
10-
const path = require("path");
11-
const plist = require("plist");
12-
const json = require("format-json");
13-
const yaml = require("yamljs");
14-
const deepmerge = require("deepmerge");
15-
const { safeWriteFileSync } = require("./util");
9+
import fs from "fs";
10+
import path from "path";
11+
import plist from "plist";
12+
import json from "format-json";
13+
import yaml from "yamljs";
14+
import deepmerge from "deepmerge";
15+
import { __dirname, safeWriteFileSync } from "./util.js";
1616

1717
const SOURCE_PATH = path.resolve(__dirname, "../", "src/");
1818
const OUTPUT_PATH = path.resolve(__dirname, "../dist/");

scripts/coverage.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@
99
* It does not consider the inner structure of the scopes, only that they are hit.
1010
*/
1111

12-
const path = require("path");
13-
const plist = require("plist");
14-
const { safeWriteFileSync, getAllFilesInDir, readFileSync } = require("./util");
15-
16-
const {
12+
import path from "path";
13+
import plist from "plist";
14+
import {
15+
__dirname,
16+
safeWriteFileSync,
17+
getAllFilesInDir,
18+
readFileSync,
19+
} from "./util.js";
20+
21+
import {
1722
parseGrammarTestCase,
1823
runGrammarTestCase,
19-
} = require("vscode-tmgrammar-test/dist/unit/index");
20-
const { createRegistry } = require("vscode-tmgrammar-test/dist/common/index");
24+
} from "vscode-tmgrammar-test/dist/unit/index.js";
25+
import { createRegistry } from "vscode-tmgrammar-test/dist/common/index.js";
2126

22-
const lcovWrite = require("lcov-write");
23-
const Yaml = require("yaml");
24-
const YamlSourceMap = require("yaml-source-map");
27+
import lcovWrite from "lcov-write";
28+
import Yaml from "yaml";
29+
import YamlSourceMap from "yaml-source-map";
2530

2631
const distDir = path.resolve(__dirname, "../", "dist/");
2732
const buildDir = path.resolve(__dirname, "../", "build/");

scripts/format.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
* It is used to ensure a consistent code style.
44
*/
55

6-
const fs = require("fs");
7-
const path = require("path");
8-
const yaml = require("yamljs");
6+
import path from "path";
7+
import yaml from "yamljs";
8+
import { readdirSync, readFileSync, writeFileSync } from "fs";
9+
import { __dirname } from "./util.js";
910

1011
const sourceDirPath = path.resolve(__dirname, "../", "src/");
11-
const sourceFiles = fs.readdirSync(sourceDirPath);
12+
const sourceFiles = readdirSync(sourceDirPath);
1213

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

17-
const source = fs.readFileSync(filePath, "utf8");
18+
const source = readFileSync(filePath, "utf8");
1819
if (source.trim().length === 0) {
1920
return; // skip empty files
2021
}
@@ -24,5 +25,5 @@ sourceFiles.forEach((file) => {
2425
indent: 2,
2526
indentSeq: true,
2627
});
27-
fs.writeFileSync(filePath, yamlData);
28+
writeFileSync(filePath, yamlData);
2829
});

scripts/util.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
const fs = require("fs");
2-
const path = require("path");
1+
import fs from "fs";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
4+
5+
export const __filename = fileURLToPath(import.meta.url);
6+
export const __dirname = path.dirname(__filename);
37

48
/**
59
* Read a file from a given path
610
* @param {string} path file to read
711
*/
8-
function readFileSync(path) {
12+
export function readFileSync(path) {
913
return fs.readFileSync(path, "utf8");
1014
}
1115

@@ -15,7 +19,7 @@ function readFileSync(path) {
1519
* @param {string} dest
1620
* @param {string} content
1721
*/
18-
function safeWriteFileSync(dest, content) {
22+
export function safeWriteFileSync(dest, content) {
1923
const dir = dest.substring(0, dest.lastIndexOf("/") + 1);
2024
if (!fs.existsSync(dir)) {
2125
fs.mkdirSync(dir);
@@ -28,7 +32,7 @@ function safeWriteFileSync(dest, content) {
2832
* @param {string} dirPath - path to directory
2933
* @param {Array<string>} files - files in parent directory
3034
*/
31-
function getAllFilesInDir(dirPath, files = []) {
35+
export function getAllFilesInDir(dirPath, files = []) {
3236
fs.readdirSync(dirPath).forEach((file) => {
3337
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
3438
files = getAllFilesInDir(dirPath + "/" + file, files);
@@ -38,9 +42,3 @@ function getAllFilesInDir(dirPath, files = []) {
3842
});
3943
return files;
4044
}
41-
42-
module.exports = {
43-
safeWriteFileSync,
44-
getAllFilesInDir,
45-
readFileSync,
46-
};

0 commit comments

Comments
 (0)