Skip to content

Commit

Permalink
fix: clean __dirname in code to be compliant with ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Mar 30, 2023
1 parent e38c020 commit 485bab6
Show file tree
Hide file tree
Showing 57 changed files with 277 additions and 120 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn install --frozen-lockfile --network-timeout 500000
run: yarn install --frozen-lockfile --ignore-engines --network-timeout 500000
- name: Release packages
env:
CI: true
Expand All @@ -286,7 +286,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn install --frozen-lockfile --network-timeout 500000
run: yarn install --frozen-lockfile --ignore-engines --network-timeout 500000
- name: Build pages
env:
CI: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {$log} from "@tsed/common";
import {PlatformExpress} from "@tsed/platform-express";
import filedirname from "filedirname";
import {Server} from "./server";

// FIXME remove when esm is ready
const [, rootDir] = filedirname();

// /!\ configuration file must be outside of your src directory
process.env["NODE_CONFIG_DIR"] = `${__dirname}/../config`;
process.env["NODE_CONFIG_DIR"] = `${rootDir}/../config`;
const config = require("config");

async function bootstrap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {PlatformResponse, Res} from "@tsed/common";
import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";
import {Get} from "@tsed/schema";
import filedirname from "filedirname";
import {createReadStream, ReadStream} from "fs";
import {Observable, of} from "rxjs";

// FIXME remove when esm is ready
const [, rootDir] = filedirname();

@Controller("/")
export class KindOfResponseCtrl {
@Get("/observable")
Expand All @@ -13,7 +17,7 @@ export class KindOfResponseCtrl {

@Get("/stream")
stream(): ReadStream {
return createReadStream(__dirname + "/response.txt");
return createReadStream(rootDir + "/response.txt");
}

@Get("/buffer")
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,15 @@
"dir": "./docs/.vuepress/dist",
"url": "https://github.com/tsedio/tsed",
"branch": "gh-pages",
"cname": "tsed.io"
"cname": "tsed.io",
"if": "production"
},
{
"dir": "./docs-references/.vuepress/dist",
"url": "https://github.com/tsedio/api-docs.tsed.io.git",
"branch": "main",
"cname": "api-docs.tsed.io"
"cname": "api-docs.tsed.io",
"if": "production"
}
],
"examples": {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module.exports = {
...require("@tsed/jest-config"),
coverageThreshold: {
global: {
statements: 98.43,
branches: 90.67,
functions: 94.84,
lines: 98.68
statements: 98.85,
branches: 94.36,
functions: 95.63,
lines: 98.85
}
}
};
6 changes: 5 additions & 1 deletion packages/core/src/domain/AnyToPromise.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import filedirname from "filedirname";
import {createReadStream} from "fs";
import {of} from "rxjs";
import {catchAsyncError} from "../utils/catchError";
import {isStream} from "../utils/objects/isStream";
import {AnyToPromise, AnyToPromiseStatus} from "./AnyToPromise";

// FIXME remove when esm is ready
const [, rootDir] = filedirname();

describe("AnyToPromise", () => {
it("should handle sync value", async () => {
const resolver = new AnyToPromise();
Expand Down Expand Up @@ -98,7 +102,7 @@ describe("AnyToPromise", () => {
const resolver = new AnyToPromise();

const result = await resolver.call(() => {
return createReadStream(__dirname + "/__mock__/response.txt");
return createReadStream(rootDir + "/__mock__/response.txt");
});

expect(result.type).toBe("STREAM");
Expand Down
8 changes: 4 additions & 4 deletions packages/di/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module.exports = {
...require("@tsed/jest-config"),
coverageThreshold: {
global: {
statements: 98.52,
branches: 92.95,
lines: 98.6,
functions: 98.11
statements: 98.62,
branches: 96.49,
lines: 98.62,
functions: 98.71
}
}
};
3 changes: 2 additions & 1 deletion packages/engines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"private": false,
"dependencies": {
"fs-extra": "10.0.1",
"tslib": "2.5.0"
"tslib": "2.5.0",
"filedirname": "^2.7.0"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
Expand Down
35 changes: 19 additions & 16 deletions packages/engines/src/components/VueEngine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {Engine, EngineOptions, ViewEngineOptions} from "./Engine";
import {ViewEngine} from "../decorators/viewEngine";
import {getCachedEngine, importEngine} from "../utils/cache";
import filedirname from "filedirname";
import {join, resolve} from "path";
import {promisify} from "util";
import {ViewEngine} from "../decorators/viewEngine";
import {getCachedEngine, importEngine} from "../utils/cache";
import {Engine, EngineOptions} from "./Engine";
// FIXME remove when esm is ready
const [, rootDir] = filedirname();

function requireFromString(src: string, filename: string) {
const Module: any = module.constructor;
Expand All @@ -26,16 +29,6 @@ function hashCode(str: string) {
export class VueEngine extends Engine {
#renderToString: any;

async $onInit() {
await super.$onInit();
await importEngine("vue-pronto/lib/index", "pronto");
await importEngine("vueify");
await importEngine("vue-server-renderer");
const ssrRenderer = this.vueServerRenderer.createRenderer();

this.#renderToString = promisify(ssrRenderer.renderToString.bind(this));
}

get pronto() {
return getCachedEngine("pronto");
}
Expand All @@ -48,8 +41,18 @@ export class VueEngine extends Engine {
return getCachedEngine("vue-server-renderer");
}

async $onInit() {
await super.$onInit();
await importEngine("vue-pronto/lib/index", "pronto");
await importEngine("vueify");
await importEngine("vue-server-renderer");
const ssrRenderer = this.vueServerRenderer.createRenderer();

this.#renderToString = promisify(ssrRenderer.renderToString.bind(this));
}

protected $cacheOptions(template: string, options: EngineOptions, fromFile?: boolean): EngineOptions {
const fullPath = join(__dirname, `/${hashCode(template)}`);
const fullPath = join(rootDir, `/${hashCode(template)}`);

if (!fromFile) {
return {
Expand All @@ -63,7 +66,7 @@ export class VueEngine extends Engine {

protected $compile(template: string, options: EngineOptions) {
// make up a fake path
const fullPath = join(__dirname, `/${hashCode(template)}`);
const fullPath = join(rootDir, `/${hashCode(template)}`);
const promise = this.getComponent(fullPath, template, options);

return async (options: EngineOptions) => {
Expand All @@ -74,7 +77,7 @@ export class VueEngine extends Engine {
protected async $compileFile(file: string, options: EngineOptions) {
// prontoRenderer assume that the filepath is relative to a passed "rootpath"
// and if you don't pass a rootpath it will try to find one
// based on its current __dirname
// based on its current rootDir
// https://github.com/express-vue/vue-pronto/blob/c88e380fee8656bc3ed21c7d3adb2ef331be07d5/lib/utils/findPaths.js#L10-L18
const fullPath = resolve(file);
const rootPath = process.cwd();
Expand Down
10 changes: 7 additions & 3 deletions packages/engines/src/utils/getEngines.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {join} from "path";
import {expect} from "chai";
import filedirname from "filedirname";
import {join} from "path";
import {getEngine, getEngines} from "./getEngines";

// FIXME remove when esm is ready
const [, rootDir] = filedirname();

describe("getEngines", () => {
describe("getEngine()", () => {
describe("render file", () => {
Expand All @@ -10,7 +14,7 @@ describe("getEngines", () => {

const result = await new Promise((resolve, reject) => {
render(
join(__dirname, "../../test/fixtures/ejs/user.ejs"),
join(rootDir, "../../test/fixtures/ejs/user.ejs"),
{
user: {
name: "Tobi"
Expand All @@ -29,7 +33,7 @@ describe("getEngines", () => {

const error: any = await new Promise((resolve) => {
render(
join(__dirname, "../../test/fixtures/ejs/user2.ejs"),
join(rootDir, "../../test/fixtures/ejs/user2.ejs"),
{
user: {
name: "Tobi"
Expand Down
8 changes: 5 additions & 3 deletions packages/engines/test/getEngineFixture.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {join} from "path";
import {engines} from "../src/index";
import filedirname from "filedirname";
import fs from "fs";
import sinon from "sinon";

import {Engine} from "../src/components/Engine";
import {engines} from "../src/index";

const rootDir = join(__dirname);
// FIXME remove when esm is ready
const [, rootDir] = filedirname();

interface EngineFixtureOptions {
token: string | typeof Engine;
Expand Down
8 changes: 6 additions & 2 deletions packages/engines/test/shared/dust.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {engines, requires} from "../../src/index";
import fs from "fs";
import {expect} from "chai";
import filedirname from "filedirname";
import fs from "fs";
import {join} from "path";

const rootDir = join(__dirname, "..");
// FIXME remove when esm is ready
const [, dir] = filedirname();

const rootDir = join(dir, "..");

export function test(name: string) {
const user = {name: "Tobi"};
Expand Down
5 changes: 4 additions & 1 deletion packages/engines/test/shared/filters.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {engines} from "../../src/index";
import filedirname from "filedirname";
import fs from "fs";
import {expect} from "chai";
import {join} from "path";

const rootDir = join(__dirname, "..");
// FIXME remove when esm is ready
const [, dir] = filedirname();
const rootDir = join(dir, "..");

export function test(name: string) {
const engine = engines.get(name)!;
Expand Down
6 changes: 4 additions & 2 deletions packages/engines/test/shared/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {engines} from "../../src/index";
import filedirname from "filedirname";
import handlebars from "handlebars";
import fs from "fs";
import {expect} from "chai";
import {join} from "path";

const Sqrl = require("squirrelly");

const rootDir = join(__dirname, "..");
// FIXME remove when esm is ready
const [, dir] = filedirname();
const rootDir = join(dir, "..");

const readFile = fs.readFile;
const readFileSync = fs.readFileSync;
Expand Down
5 changes: 4 additions & 1 deletion packages/engines/test/shared/includes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {engines} from "../../src/index";
import filedirname from "filedirname";
import fs from "fs";
import {expect} from "chai";
import {join} from "path";

const rootDir = join(__dirname, "..");
// FIXME remove when esm is ready
const [, dir] = filedirname();
const rootDir = join(dir, "..");

export function test(name: string) {
const engine = engines.get(name)!;
Expand Down
5 changes: 4 additions & 1 deletion packages/engines/test/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {engines, requires} from "../../src/index";
import {expect} from "chai";
import filedirname from "filedirname";
import fs from "fs";
import {join} from "path";

const rootDir = join(__dirname, "..");
// FIXME remove when esm is ready
const [, dir] = filedirname();
const rootDir = join(dir, "..");

const readFile = fs.readFile;
const readFileSync = fs.readFileSync;
Expand Down
7 changes: 5 additions & 2 deletions packages/engines/test/shared/partials.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {engines} from "../../src/index";
import filedirname from "filedirname";
import {join} from "path";
import fs from "fs";
import {expect} from "chai";

const rootDir = join(__dirname, "..");
// FIXME remove when esm is ready
const [, dir] = filedirname();
const rootDir = join(dir, "..");

const readFile = fs.readFile;
const readFileSync = fs.readFileSync;
Expand Down Expand Up @@ -38,7 +41,7 @@ export function test(name: string) {
});
it("should support absolute path partial", async () => {
const path = `${rootDir}/fixtures/${name}/partials.${name}`;
const locals = {user: user, partials: {partial: join(__dirname, "/../../test/fixtures/", name, "/user")}};
const locals = {user: user, partials: {partial: join(dir, "/../../test/fixtures/", name, "/user")}};
const html = await engine.renderFile(path, locals);
expect(html).to.equal("<p>Tobi</p>");
});
Expand Down
5 changes: 4 additions & 1 deletion packages/engines/test/shared/react.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {engines} from "../../src/index";
import {expect} from "chai";
import filedirname from "filedirname";
import fs from "fs";
import sinon from "sinon";
import {join} from "path";

const rootDir = join(__dirname, "..");
// FIXME remove when esm is ready
const [, dir] = filedirname();
const rootDir = join(dir, "..");
const sandbox = sinon.createSandbox();

const readFile = fs.readFile;
Expand Down
6 changes: 4 additions & 2 deletions packages/graphql/typegraphql/test/app/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@tsed/ajv";
import {Configuration, Constant, Inject, PlatformApplication} from "@tsed/common";
import "@tsed/typegraphql";
import "@tsed/passport";
import filedirname from "filedirname";
import {resolve} from "path";
import {User} from "./graphql/auth/User";
import {HelloController} from "./controllers/HelloController";
Expand All @@ -11,7 +12,8 @@ import "./graphql/index";
import "./services/RecipeService";
import "./services/UsersRepository";

const rootDir = resolve(__dirname);
// FIXME remove when esm is ready
const [, rootDir] = filedirname();

@Configuration({
rootDir,
Expand All @@ -28,7 +30,7 @@ const rootDir = resolve(__dirname);
default: {
path: "/api/graphql",
buildSchemaOptions: {
emitSchemaFile: resolve(__dirname, "../resources/schema.gql")
emitSchemaFile: resolve(rootDir, "../resources/schema.gql")
},
serverConfig: {
context({req, res}: any) {
Expand Down

0 comments on commit 485bab6

Please sign in to comment.