Utility functions for projects production

栏目: IT技术 · 发布时间: 4年前

内容简介:Utility functions deal with files in your project. Make production easierIf not passed path or ext (more likely you don't need to), then all functions can automatically read the current project directory whether it is

get-info

Utility functions deal with files in your project. Make production easier :mag_right:

get-info Contains a bunch od functions read packages in root project, validate each package, return the path, JSON, and extension(js|ts) used for each one. As it is essentially created to deal with monorepos, it works as well for a single package project.

npm install get-info

API

If not passed path or ext (more likely you don't need to), then all functions can automatically read the current project directory whether it is packages/**/src or ./src

getPackagesInfo

/**
 * Gets package full info by passed name of packages.
 *
 * @param {Object} input
 * @param {Array} input.path Array contains paths to each package
 * @param {Array} input.ext Array contains extension associated to each package
 * @param {string} [input.buildName="dist"]
 * @returns {Object[]} results
 * @returns {Array} results[].ext
 * @returns {Array} results[].json
 * @returns {Array} results[].path
 */
const { json, ext } = getPackagesInfo({ buildName, path, ext })(
  ...packagesName
);

Example(1)

import { getPackagesInfo } from "get-info";

const { ext, json, path } = getPackagesInfo()("myFav/project another/project");

expect(json).to.be.an("Array");

expect(json.length).to.be.equal(2);

expect(json[0].name).to.be.equal("@myFav/project");
expect(json[1].name).to.be.equal("@another/project");

getJsonByPath

/**
 * Gets package json by path. Reads each passed directory. Then, returns
 * objects extracted form these json files including source and distention path.
 *
 * Note: this function validate accessability and throw error if there's
 * something wrong in src/index.ext
 *
 * @param {Array} path Array contains paths to each package
 * @param {Array} ext Array contains extension associated to each package
 *
 * @returns {Object[]} pkgInfo is an object of arrays
 * @returns {string} pkgInfo[].sourcePath
 * @returns {string} pkgInfo[].distPath
 * @returns {string} pkgInfo[].name
 * @returns {Object} pkgInfo[].peerDependencies
 * @returns {Object} pkgInfo[].dependencies
 * @returns {...*}   other
 */
const { json, ext } = getJsonByPath({ path, ext });

Example(2)

import { getJsonByPath } from "get-info";

const { json, ext } = getJsonByPath();

expect(json).to.be.an("Array");
expect(json.length).to.be.equal(1);
expect(json[0].name).to.be.equal("get-info");

getPackagesPath

/**
 * Gets packages path for a given project source root. It filters each path
 * returns only packages contain valid src/index and have package.json
 *
 * @param {string} dir packages main directory [path="./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();

const expectedPaths = [
  "./packages/myProj1",
  "./packages/myProj2",
  "./packages/myProj3"
];

const expectedExtensions = ["js", "ts", "ts"];

expect(path).to.deep.equal(expectedPaths);
expect(ext).to.deep.equal(expectedExtensions);

Utils

Utility functions used in this project are also exported for further use.

utils.getFileExtension

import { utils } from "get-info";

/**
 * Loop inside a given directory looking for index. When find it, gets its
 * extension.
 *
 * @param {string} dir - given directory
 * @returns {string} extension.
 */
const { getFileExtension } = utils;
const extension = getFileExtension(dir);

utils.validateAccessability

import { utils } from "get-info";

/**
 * Validates access readability `package.json` & `src` for given path.
 *
 * @param {string} dir
 * @param {string} [srcName="src"]
 * @returns {string} extension
 */
const { validateAccessability } = utils;
const extension = validateAccessability(dir, srcName);

utils.filterPathAccessability

import { utils } from "get-info";

/**
 * Filters array of path by validate each path. Make sure it has `package.json`
 * and `src`.
 *
 * @param {Array} [pkgPath=[]]
 * @returns {Object} results[]
 * @returns {Array} results[].path filtered valid paths
 * @returns {Array} results[].ext extension for each path (js|ts)
 */
const { filterPathAccessability } = utils;
const { path, ext } = filterPathAccessability(pkgPath);

Related projects

  • packageSorter - Sorting packages for monorepos production.

  • builderz - Building your project with zero config.

Tests

npm test

License

This project is licensed under the GPL-3.0 License


以上所述就是小编给大家介绍的《Utility functions for projects production》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java5.0Tiger程序高手秘笈

Java5.0Tiger程序高手秘笈

BrettMclaughlin / 东南大学出版社 / 2005-10 / 28.00元

代号为 “Tiger”的下一个 Java 版本,不只是个小改动版。在语言核心中有超过 100 项以上的变动,同时有大量的对 library 与 API 所做的加强,让开发者取得许多新的功能、工具与技术。但在如此多的变化下,应该从何处开始着手?也许可以从既长又无趣的语言规范说明书开始看起;或等待最少 500 页的概念与理论巨著出版;甚至还可以直接把玩新的 JDK 看看能够有什么发现;或者借由《Jav......一起来看看 《Java5.0Tiger程序高手秘笈》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换