Skip to content

Commit 79423cc

Browse files
committed
fix: e2e/test works!
1 parent 958851d commit 79423cc

File tree

7 files changed

+35
-27
lines changed

7 files changed

+35
-27
lines changed

src/config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import { ImportMap } from "@jspm/import-map";
1111
*
1212
* ******************************************************
1313
*/
14-
export const rootUrl = import.meta.url;
15-
export const root = fileURLToPath(new URL(".", rootUrl));
14+
export const root = fileURLToPath(`file://${process.cwd()}`);
1615
export const cacheMap = new Map();
1716
export const nodeImportMapPath = join(root, "node.importmap");
1817
export const cache = join(root, ".cache");
1918
export const map = existsSync(nodeImportMapPath)
2019
? JSON.parse(readFileSync(nodeImportMapPath, { encoding: "utf8" }))
2120
: {};
22-
export const importMap = new ImportMap({ rootUrl, map });
21+
export const importMap = new ImportMap({ rootUrl: import.meta.url, map });

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const NO_CACHE_MAP_DEFINED =
22
"No parentURL provided. No cachePath was defined. Couldn't get cachePath from cacheMap.";
33
export const ALL_CACHE_MAP_REQUIREMENTS_MUST_BE_DEFINED = "All cacheMap requirements must be defined.";
4-
export const IS_DEBUGGING = Boolean(process.env.DEBUG) || false;
4+
// export const IS_DEBUGGING = Boolean(process.env.DEBUG) || false;
5+
export const IS_DEBUGGING = true;

src/loader.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { cacheMap, nodeImportMapPath } from "src/config";
1+
import { cacheMap, nodeImportMapPath } from "./config";
22
import {
3-
isNodeOrFileProtocol,
3+
checkIfNodeOrFileProtocol,
44
resolveNodeModuleCachePath,
55
resolveModulePath,
66
resolveParsedModulePath,
7-
} from "src/utils";
8-
import { Context, NextResolve } from "src/types";
7+
} from "./utils";
8+
import { Context, NextResolve } from "./types";
99

1010
/**
1111
* ******************************************************
@@ -33,8 +33,10 @@ import { Context, NextResolve } from "src/types";
3333
*/
3434
export const resolve = async (specifier: string, { parentURL }: Context, nextResolve: NextResolve) => {
3535
if (!parentURL || !nodeImportMapPath) return nextResolve(specifier);
36-
const modulePath = resolveModulePath(specifier, cacheMap.get(parentURL) || parentURL);
37-
if (isNodeOrFileProtocol(modulePath)) return nextResolve(specifier);
36+
const cacheMapPath = cacheMap.get(parentURL) || parentURL;
37+
const modulePath = resolveModulePath(specifier, cacheMapPath);
38+
const isNodeOrFileProtocol = checkIfNodeOrFileProtocol(modulePath);
39+
if (isNodeOrFileProtocol) return nextResolve(specifier);
3840
const nodeModuleCachePath = await resolveNodeModuleCachePath(modulePath);
3941
if (!nodeModuleCachePath) return nextResolve(specifier);
4042
cacheMap.set(`file://${nodeModuleCachePath}`, modulePath);

src/logger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ export const logger = ({ file, isLogging = false }: LoggerOptions) => ({
99
error: (msg: string, ...args: unknown[]) => {
1010
if (args) console.error(`jspm:[${file}]: ${msg}`, ...args);
1111
else console.error(`jspm:[${file}]: ${msg}`);
12-
},
13-
setLogger: (isLogging = false) => logger({ file, isLogging }),
12+
}
1413
});

src/parser.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { existsSync, writeFileSync } from "node:fs";
22
import fetch from "node-fetch";
3-
import { ensureDirSync } from "src/utils";
3+
import { ensureFileSync } from "src/utils";
4+
import { IS_DEBUGGING } from "src/constants";
45
import { logger } from "src/logger";
56

6-
const log = logger({ file: "utils" });
7+
const log = logger({ file: "parser", isLogging: IS_DEBUGGING });
78

89
/**
910
* parseNodeModuleCachePath
@@ -13,12 +14,13 @@ const log = logger({ file: "utils" });
1314
* @returns {string}
1415
*/
1516
export const parseNodeModuleCachePath = async (modulePath: string, cachePath: string) => {
17+
log.debug('parseNodeModuleCachePath', cachePath, modulePath)
18+
if (existsSync(cachePath)) return cachePath;
1619
try {
17-
if (existsSync(cachePath)) return cachePath;
1820
const resp = await fetch(modulePath);
1921
if (!resp.ok) throw Error(`404: Module not found: ${modulePath}`);
2022
const code = await resp.text();
21-
ensureDirSync(cachePath);
23+
ensureFileSync(cachePath);
2224
writeFileSync(cachePath, code);
2325
return cachePath;
2426
} catch (err) {

src/utils.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, mkdirSync } from "node:fs";
1+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
22
import { dirname, join } from "node:path";
33
import { parseUrlPkg } from "@jspm/generator";
44
import { parseNodeModuleCachePath } from "src/parser";
@@ -17,8 +17,7 @@ import { logger } from "src/logger";
1717
* ******************************************************
1818
*/
1919

20-
const log = logger({ file: "loader" });
21-
log.setLogger(IS_DEBUGGING);
20+
const log = logger({ file: "loader", isLogging: IS_DEBUGGING });
2221

2322
export const ensureDirSync = (dirPath: string) => {
2423
if (existsSync(dirPath)) return;
@@ -27,7 +26,17 @@ export const ensureDirSync = (dirPath: string) => {
2726
mkdirSync(dirPath);
2827
};
2928

30-
export const isNodeOrFileProtocol = (modulePath: string) => {
29+
export const ensureFileSync = (path: string) => {
30+
const dirPath = dirname(path);
31+
if (!existsSync(dirPath)) ensureDirSync(dirPath);
32+
try {
33+
writeFileSync(path, '', { flag: 'wx' });
34+
} catch {
35+
log.error(`ensureDirSync: Failed in creating ${path}`)
36+
}
37+
}
38+
39+
export const checkIfNodeOrFileProtocol = (modulePath: string) => {
3140
const { protocol = "" } = new URL(modulePath);
3241
const isNode = protocol === "node:";
3342
const isFile = protocol === "file:";
@@ -47,7 +56,7 @@ export const resolveNodeModuleCachePath = async (modulePath: string) => {
4756
const version = moduleMetadata?.pkg?.version;
4857
const moduleFile = modulePath.split("/").reverse()[0] || "";
4958
const nodeModuleCachePath = join(cache, `${name}@${version}`, moduleFile);
50-
log.debug("resolveNodeModuleCachePath:", { name, version, nodeModuleCachePath });
59+
log.debug("resolveNodeModuleCachePath:", { moduleMetadata, name, version, nodeModuleCachePath });
5160
return nodeModuleCachePath;
5261
} catch (err) {
5362
log.error("resolveNodeModuleCachePath:", err);

tests/e2e/test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import * as TeleportProjectGeneratorPreact from "@teleporthq/teleport-project-generator-preact";
22
import * as TeleportProjectGeneratorReact from "@teleporthq/teleport-project-generator-react";
33
import * as TeleportTypes from "@teleporthq/teleport-types";
4-
import * as chalk from "chalk";
5-
import * as express from "express";
6-
import * as morgan from "morgan";
74
import * as react from "react";
8-
import * as reactDom from "react-dom";
95
import * as reactRouter from "react-router";
106
import * as reactRouterDom from "react-router-dom";
117

128
// Write main module code here, or as a separate file with a "src" attribute on the module script.
13-
console.log(TeleportProjectGeneratorPreact, TeleportProjectGeneratorReact, TeleportTypes, chalk, express, morgan, react, reactDom, reactRouter, reactRouterDom);
14-
console.log(chalk.default.green("Hello......."));
9+
console.log(TeleportProjectGeneratorPreact, TeleportProjectGeneratorReact, TeleportTypes, react, reactRouter, reactRouterDom);
10+
console.log("Hello.......");

0 commit comments

Comments
 (0)