Skip to content

Commit 73e3c99

Browse files
committed
fix: harkfile resolution
1 parent 436a97e commit 73e3c99

File tree

9 files changed

+66
-33
lines changed

9 files changed

+66
-33
lines changed

bootstrap-hark.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// process.env["HARK_HARD_CORE_TRACE"] = "1";
22
require("@babel/register")({
33
babelrc: true,
4-
extensions: [".js", ".jsx", ".csj", ".mjs", ".ts", ".tsx"],
4+
extensions: [".csj", ".es", ".es6", ".js", ".jsx", ".mjs", ".ts", ".tsx"],
55
rootMode: "root",
66
});
7-
87
const { cliStartExit } = require("@hark/cli");
98
cliStartExit(process.argv.slice(2), {
109
context: {

example/bootstrap-hark-example.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// process.env["HARK_HARD_CORE_TRACE"] = "1";
22
require("@babel/register")({
33
babelrc: true,
4-
extensions: [".js", ".jsx", ".csj", ".mjs", ".ts", ".tsx"],
4+
extensions: [".csj",".es",".es6",".js",".jsx",".mjs",".ts",".tsx"],
55
rootMode: "upward",
66
only: [
77
function(filepath) {

harkfile.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export class MyPackage extends MyProject {
267267

268268
// babelTranspileSelf
269269
this.registerTask("babelTranspileSelf", (gc) =>
270-
transpile({ watchMode: gc.watchMode, srcDir: `${path}/src`, outDir: `${path}/dist`, comments: true }),
270+
transpile({ watchMode: gc.watchMode, srcDir: `${path}/src`, outDir: `${path}/dist/lib`, comments: true }),
271271
);
272272

273273
// tsconfigData
@@ -277,7 +277,7 @@ export class MyPackage extends MyProject {
277277
compilerOptions: {
278278
baseUrl: ".",
279279
rootDir: "src",
280-
outDir: "dist",
280+
outDir: "dist/lib",
281281
isolatedModules: true,
282282
skipLibCheck: true,
283283
sourceMap: false,
@@ -286,7 +286,7 @@ export class MyPackage extends MyProject {
286286
composite: true,
287287
},
288288
include: ["src"],
289-
exclude: ["**/*.test.tsx?"],
289+
exclude: ["**/*.test.ts", "**/*.test.tsx"],
290290
}),
291291
);
292292

@@ -390,8 +390,8 @@ export const copyPackageJson = ({
390390
jsonParse(),
391391
packageJsonClean({
392392
overwrite: (json) => ({
393-
main: "index.js",
394-
types: "index.d.ts",
393+
main: "./lib/index.js",
394+
types: "./lib/index.d.ts",
395395
...(gitRepositoryUrl
396396
? {
397397
repository: {

packages/cli/bin/hark.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
#!/usr/bin/env node
2-
require("../_cli.js");
2+
"use strict";
3+
4+
// Prefer the local installation of Hark
5+
if (require("import-local")(__filename)) {
6+
// Using local install of Hark
7+
} else {
8+
require("../lib/_cli.js");
9+
}

packages/cli/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"@hark/reporter-verbose": "^0.9.1",
1515
"@hark/task-group": "^0.9.1",
1616
"chalk": "3.0.0",
17-
"clipanion": "^2.1.6"
17+
"clipanion": "^2.1.6",
18+
"import-local": "~3.0.2"
1819
},
1920
"peerDependencies": {
2021
"@babel/register": "*",

packages/cli/src/_cli.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const HARK_REGISTER_BABEL = process.env["HARK_REGISTER_BABEL"];
44

55
cliStartExit(process.argv.slice(2), {
66
registerBabelOptions:
7-
HARK_REGISTER_BABEL == null || (HARK_REGISTER_BABEL as any) === true || /^yes|true|1|on$/i.test(HARK_REGISTER_BABEL)
7+
HARK_REGISTER_BABEL == null || (HARK_REGISTER_BABEL as any) === true || /^(yes|true|1|on|)$/i.test(HARK_REGISTER_BABEL)
88
? {}
9+
: /^\{[\s\S\n]*\}$/i.test(HARK_REGISTER_BABEL)
10+
? JSON.parse(HARK_REGISTER_BABEL)
911
: null,
1012
context: {
1113
stdin: process.stdin,

packages/cli/src/cli-start.ts

+43-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
11
import { BaseContext } from "clipanion/lib/advanced";
2+
import { promises as fsp } from "fs";
23
import * as _path from "path";
34
import { cliInit } from "./cli-init";
45
import { HarkCliStartOptions, HarkfileFunction } from "./models";
5-
import { existsSync, promises as fsp } from "fs";
66

77
export async function cliStart<T>(args: string[], options?: HarkCliStartOptions<T>) {
8-
const harkfilePath = _path.resolve(options?.harkfilePath || "./harkfile");
98
const cliVars = await cliInit(options);
10-
const extensions = [".js", ".jsx", ".csj", ".mjs", ".ts", ".tsx"];
9+
const extensions: string[] = options?.registerBabelOptions ?? [
10+
//
11+
".ts",
12+
".tsx",
13+
".mjs",
14+
".es",
15+
".es6",
16+
".csj",
17+
".js",
18+
".jsx",
19+
];
20+
if (extensions.length === 0) {
21+
throw new Error("No extensions given");
22+
}
1123
if (options?.registerBabelOptions != null) {
12-
require("@babel/register")({
24+
let babelRegister;
25+
try {
26+
babelRegister = require("@babel/register");
27+
} catch (ex) {
28+
console.error(
29+
"\n\n@hark/cli: PeerDependency for @babel/register not met. Please set HARK_REGISTER_BABEL=0 if this is intended.\n\n",
30+
);
31+
throw ex;
32+
}
33+
babelRegister({
1334
babelrc: true,
1435
extensions,
1536
rootMode: "root",
1637
...options?.registerBabelOptions,
1738
});
1839
}
19-
let harkFileFn: HarkfileFunction<any, any>;
2040

21-
if (await fileExists(harkfilePath, [...extensions, ""])) {
22-
try {
23-
harkFileFn = (await import(harkfilePath)).default;
24-
} catch (ex) {
25-
console.error(`\nError while loading harkfile at "${harkfilePath}".\n`);
26-
throw ex;
27-
}
28-
} else {
29-
console.error(`\nNo harkfile detected at "${harkfilePath}".\n`);
30-
harkFileFn = () => undefined;
41+
const harkfilePath = _path.resolve(options?.harkfilePath || "./harkfile");
42+
const possibleHarkfiles = [harkfilePath, harkfilePath + _path.sep + "index"].flatMap((p) => extensions.map((e) => p + e));
43+
const foundHarkfile = await findFirstExistingFile(possibleHarkfiles);
44+
if (foundHarkfile == null) {
45+
throw new Error(`\nNo harkfile detected at any of these paths:\n- ` + possibleHarkfiles.join("- \n"));
46+
}
47+
48+
let harkFileFn: HarkfileFunction<any, any>;
49+
try {
50+
harkFileFn = (await import(foundHarkfile)).default;
51+
} catch (ex) {
52+
console.error(`\nError while loading harkfile at "${harkfilePath}".\n`);
53+
throw ex;
3154
}
3255

3356
let harkfileContext: any;
@@ -52,12 +75,12 @@ export async function cliStartExit<T>(args: string[], options?: HarkCliStartOpti
5275
process.exit(code);
5376
}
5477

55-
async function fileExists(filepath: string, extensions: string[] = [""]) {
56-
for (const extension of extensions) {
78+
async function findFirstExistingFile(filepaths: string[]): Promise<string | undefined> {
79+
for (const filepath of filepaths) {
5780
try {
58-
await fsp.access(`${filepath}${extension}`);
59-
return true;
81+
await fsp.access(filepath);
82+
return filepath;
6083
} catch {}
6184
}
62-
return false;
85+
return undefined;
6386
}

release/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"name": "hark-root-monorepo-release",
44
"version": "0.0.0",
55
"scripts": {
6-
"hark": "hark"
6+
"hark": "hark",
7+
"hark-parent": "node ../bootstrap-hark.js"
78
},
89
"dependencies": {
910
"@babel/core": "^7.8.6",

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4596,7 +4596,7 @@ import-local@^2.0.0:
45964596
pkg-dir "^3.0.0"
45974597
resolve-cwd "^2.0.0"
45984598

4599-
import-local@^3.0.2:
4599+
import-local@^3.0.2, import-local@~3.0.2:
46004600
version "3.0.2"
46014601
resolved "https://registry.yarnpkg.com/import-local/-/import-local-3.0.2.tgz#a8cfd0431d1de4a2199703d003e3e62364fa6db6"
46024602
integrity sha512-vjL3+w0oulAVZ0hBHnxa/Nm5TAurf9YLQJDhqRZyqb+VKGOB6LU8t9H1Nr5CIo16vh9XfJTOoHwU0B71S557gA==

0 commit comments

Comments
 (0)