Skip to content

Extracting and parsing data for project production works on a root directory with submodule/monorepo

License

Notifications You must be signed in to change notification settings

jalal246/get-info

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

get-info

Utility functions extract project(s) information. Make production easier & faster 🔎

get-info Contains functions that read packages, validate readability, return path, JSON, and used extension(js|ts) for each package found in workspace.

These functions are essential to deal with monorepos ./packages/**/src, and it works as well for a single package project ./src.

npm install get-info

API

getJsonByName

Extracts package json, extension, and resolved path for each project name. If names are not passed, it returns all json objects can be found in ./packages/**/package.json or ./package json

/**
 *
 * @param {string} names required packages name
 *
 * @returns {Object} results
 * @returns {Array} results[].json - packages json related to given path
 * @returns {Object} results[].pkgInfo - {ext, path}
 */
const { json, pkgInfo } = getJsonByName(...names);

Example(1)

import { getJsonByName } from "get-info";

const { json, pkgInfo } = getJsonByName("myFav/project", "another/project");

json
//[{name: @myFav/project, version: "1.1.1", main: "index.js", ...}, {...}]

pkgInfo[@myFav/project]
// {ext: js, path: "./myFav-project"}

const { ext, path } = pkgInfo["@myFav/project"]

// to get default entry and resolved dist path:
const srcPath = resolve(path, "src", `index.${ext}`);
const buildPath = resolve(path, "dist");

getJsonByPath

Extracts package json, extension, and resolved path for each given path. If paths are not passed, it returns all json objects can be found in ./packages/**/package.json or ./package.json

/**
 *
 * @param {sting} paths  contains paths to resolve and extracts info form.
 *
 * @returns {Object} results
 * @returns {Array} results[].json - packages json related to given path
 * @returns {Object} results[].pkgInfo - {ext, path}
 */
const { json, pkgInfo } = getJsonByPath(...paths);

Example(2)

import { getJsonByPath } from "get-info";

const { json, pkgInfo } = getJsonByPath("./myProject");

json
// [{name: myProject, version: "1.1.1", main: "index.js", ...}]

pkgInfo[myProject]
//  {ext: ts, path: "./myProject"}

getPackagesPath

Scans root directory (workspace), returns all project in there. It filters each path returns only packages contain valid src/index[ext] and have package.json

/**
 *
 * @param {string} [dir="./packages/*"]
 *
 * @returns {Object[]} results
 * @returns {Array} results[].path valid path directory
 * @returns {Array} results[].ext extension for each path (js|ts)
 */
const { path, ext } = getPackagesPath(dir);

Example(3)

import { getPackagesPath } from "get-info";

const { path, ext } = getPackagesPath();

// path [
//   "./packages/myProj1",
//   "./packages/myProj2",
//   "./packages/myProj3"
// ];

// ext ["js", "ts", "ts"];

Utils: functions used in this project exported for further use

utils.getFileExtension

Gets extension used for project/**/src/**

import { utils } from "get-info";

/**
 *
 * @param {string} dir - given directory
 * @returns {string} extension.
 */
const { getFileExtension } = utils;
const extension = getFileExtension(dir);

utils.validateAccess

Validates access readability for package.json & src.

import { utils } from "get-info";
const { validateAccess } = utils;

/**
 *
 * @param {string} [dir="."]
 * @param {string} [ext=getFileExtension(dir/src)]
 * @param {string} [srcName="src"]
 *
 * @returns {Object} result
 * @returns {boolean} result.isValid
 * @returns {string} result.ext
 */
const { isValid, ext } = validateAccess(dir, ext, srcName);

utils.filterPathAccess

Filters array of paths by validate each path. Make sure it has package.json & src.

import { utils } from "get-info";
const { filterPathAccess } = utils;

/**
 *
 * @param {Array} [pkgPath=[]]
 * @returns {Object} results[]
 * @returns {Array} results[].path filtered valid paths
 * @returns {Array} results[].ext extension for each path (js|ts)
 */
const { path, ext } = filterPathAccess(pkgPath);

Related projects

Tests

npm test

License

This project is licensed under the GPL-3.0 License