Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [18.x]
node-version: [20.x]

steps:
- name: Checkout
Expand All @@ -21,7 +21,7 @@ jobs:
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
- run: npm install
- name: Setup Chomp
uses: guybedford/chomp-action@v1
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 0 additions & 27 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ jest.mock('@jspm/generator', () => ({
parseUrlPkg: jest.fn(),
}))

jest.mock("../utils", () => {
const actual = jest.requireActual("../utils");
return {
__esModule: true,
...actual,
};
});
import * as utils from "../utils";

jest.mock('../config')

describe("parseNodeModuleCachePath", () => {
Expand All @@ -52,22 +43,4 @@ describe("parseNodeModuleCachePath", () => {
const result = await parseNodeModuleCachePath("modulePath", cachePath);
expect(result).toBe(cachePath);
});

test("should make directories and write file if cachePath does not exist", async () => {
jest.spyOn(fs, "existsSync").mockReturnValue(false);
const ensureFileSyncSpy = jest.spyOn(utils, "ensureFileSync");
await parseNodeModuleCachePath("modulePath", cachePath);
expect(ensureFileSyncSpy).toBeCalled();
});

test("should return empty string if there is an error", async () => {
jest.spyOn(fs, "existsSync").mockReturnValue(false);
await jest.spyOn(fs, "writeFileSync").mockImplementation(() => {
throw new Error("error");
});
const errorSpy = await jest.spyOn(console, "error").mockImplementation(() => undefined);
const result = await parseNodeModuleCachePath("modulePath", cachePath);
expect(result).toBe("file:///path/to/cache");
expect(errorSpy).toHaveBeenCalled();
});
});
12 changes: 1 addition & 11 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,23 @@ import { ExactModule } from '@jspm/generator/lib/install/package';

test("ensureDirSync has dir", () => {
const dir = "/path/to/dir";
const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(true);
const dirnameMock = jest.spyOn(path, 'dirname')
ensureDirSync(dir);
expect(existsSyncMock).toBeCalledWith(dir);
expect(dirnameMock).not.toBeCalled();
});

test("ensureDirSync has parent dir", () => {
const dir = "/path/to/dir";
const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const dirnameMock = jest.spyOn(path, 'dirname').mockReturnValue("/path/to/dir");
const mkdirSyncMock = jest.spyOn(fs, 'mkdirSync')
ensureDirSync(dir);
expect(existsSyncMock).toBeCalledWith(dir);
expect(dirnameMock).toBeCalledWith(dir);
expect(mkdirSyncMock).toHaveBeenCalledTimes(1);
});

test("ensureDirSync to have recursion", () => {
const dir = "/path/to/dir";
const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const dirnameMock = jest.spyOn(path, 'dirname').mockReturnValue("/path/");
const mkdirSyncMock = jest.spyOn(fs, 'mkdirSync')
ensureDirSync(dir);
expect(existsSyncMock).toBeCalledWith(dir);
expect(dirnameMock).toBeCalledWith(dir);
expect(mkdirSyncMock).toHaveBeenCalledTimes(2);
expect(mkdirSyncMock).toHaveBeenCalledTimes(1);
});

test("ensureFileSync has file", () => {
Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readFileSync } from "node:fs";
import { existsSync, readFileSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { ImportMap } from "@jspm/import-map";
Expand All @@ -20,6 +20,8 @@ export const root = fileURLToPath(`file://${wd}`);
export const cacheMap = new Map();
export const nodeImportMapPath = join(root, "node.importmap");
export const cache = join(root, ".cache");
const hasCacheFoler = existsSync(cache);
if (!hasCacheFoler) mkdirSync(cache);
const map = existsSync(nodeImportMapPath) ? JSON.parse(readFileSync(nodeImportMapPath, { encoding: "utf8" })) : {};
export const importmap = new ImportMap({ rootUrl: import.meta.url, map });

Expand All @@ -28,4 +30,4 @@ export const options = parseArgs({
options: { debugNodeImportmapLoader: { alias: "d", type: "boolean", default: false } },
});

export const isDebuggingEnabled = Boolean(options.debugNodeImportmapLoader);
export const isDebuggingEnabled = Boolean(options?.debugNodeImportmapLoader);
14 changes: 8 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ import { logger } from "./logger";
const log = logger({ file: "loader", isLogging });

export const ensureDirSync = (dirPath: string) => {
if (existsSync(dirPath)) return;
const parentDir = dirname(dirPath);
if (parentDir !== dirPath) ensureDirSync(parentDir);
mkdirSync(dirPath);
try {
mkdirSync(dirPath, { recursive: true });
} catch (err) {
log.error(`ensureDirSync: Failed in creating ${dirPath}`, { error: err });
throw err;
}
};

export const ensureFileSync = (path: string) => {
const dirPath = dirname(path);
if (!existsSync(dirPath)) ensureDirSync(dirPath);
try {
const dirPath = dirname(path);
if (!existsSync(dirPath)) ensureDirSync(dirPath);
writeFileSync(path, "", { flag: "wx" });
} catch {
log.error(`ensureDirSync: Failed in creating ${path}`);
Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as TeleportProjectGeneratorPreact from "@teleporthq/teleport-project-generator-preact";
import * as TeleportProjectGeneratorReact from "@teleporthq/teleport-project-generator-react";
import * as TeleportTypes from "@teleporthq/teleport-types";
// import * as TeleportProjectGeneratorPreact from "@teleporthq/teleport-project-generator-preact";
// import * as TeleportProjectGeneratorReact from "@teleporthq/teleport-project-generator-react";
// import * as TeleportTypes from "@teleporthq/teleport-types";
import * as react from "react";
import * as reactRouter from "react-router";
import * as reactRouterDom from "react-router-dom";
import assert from "assert";

// Write main module code here, or as a separate file with a "src" attribute on the module script.
console.log(
TeleportProjectGeneratorPreact,
TeleportProjectGeneratorReact,
TeleportTypes,
// TeleportProjectGeneratorPreact,
// TeleportProjectGeneratorReact,
// TeleportTypes,
react,
reactRouter,
reactRouterDom,
Expand Down