Skip to content

Commit

Permalink
pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 17, 2024
1 parent f584b91 commit f16bf62
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/ts-codegen/src/generators/create-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, dirname, basename, extname } from "path";
import { readFileSync, existsSync } from "fs";
import { sync as mkdirp } from "mkdirp";
import pkg from "../../package.json";
import { writeContentToFile } from "../utils/files";
import { BuilderFile, TSBuilderInput } from "../builder";
import {
Expand All @@ -10,6 +10,39 @@ import {
} from "../helpers";
import { BuilderContext } from "wasm-ast-types";

// need to search due to the dist/ folder and src/, etc.
function findPackageJson(currentDir: string) {
const filePath = join(currentDir, 'package.json');

// Check if package.json exists in the current directory
if (existsSync(filePath)) {
return filePath;
}

// Get the parent directory
const parentDir = dirname(currentDir);

// If reached the root directory, package.json is not found
if (parentDir === currentDir) {
throw new Error('package.json not found in any parent directory');
}

// Recursively look in the parent directory
return findPackageJson(parentDir);
}

function readAndParsePackageJson() {
// Start searching from the current directory
const pkgPath = findPackageJson(__dirname);

// Read and parse the package.json
const str = readFileSync(pkgPath, 'utf8');
const pkg = JSON.parse(str);
return pkg;
}

const pkg = readAndParsePackageJson();

const version = process.env.NODE_ENV === "test" ? "latest" : pkg.version;
const header = `/**
* This file and any referenced files were automatically generated by ${pkg.name}@${version}
Expand Down

0 comments on commit f16bf62

Please sign in to comment.