Skip to content

Commit

Permalink
Merge pull request #7 from jalal246/dev
Browse files Browse the repository at this point in the history
Extract extra info by name
  • Loading branch information
jalal246 authored Mar 17, 2020
2 parents 352d29c + 36fb6f2 commit d89c9e1
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 95 deletions.
21 changes: 0 additions & 21 deletions .eslintrc.js

This file was deleted.

53 changes: 24 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,24 @@ can automatically read the current project directory whether it is
*
* @returns {Object[]} results
* @returns {Array} results[].json - packages json related to given package-name
* @returns {Array} results[].ext - extension (js|ts) related to every package-name
* @returns {Array} results[].distPath - resolved distention path for every package-name
* @returns {Object} results[].pkgInfo - {dist, ext}
*/

const { json, ext, distPath } = getJsonByName(buildName, ...path)(...names);
const { json, pkgInfo } = getJsonByName(buildName, ...path)(...names);
```

#### Example(1)

```js
import { getJsonByName } from "get-info";

const { ext, json, path } = getJsonByName()("myFav/project another/project");
const { json, pkgInfo } = getJsonByName()("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");
// json>> [{name: @myFav/project, version: "1.1.1", main: "index.js", ...}, {...}]
//
// pkgInfo>> {@myFav/project, ...}
//
// pkgInfo[@myFav/project]>> {ext: js, dist: "root/**/myFav-project/dist/"}
```

### getJsonByPath
Expand All @@ -64,25 +62,23 @@ expect(json[1].name).to.be.equal("@another/project");
*
* @returns {Object[]} results
* @returns {Array} results[].json - packages json related to given path
* @returns {Array} results[].ext - extension (js|ts) related to every path
* @returns {Array} results[].distPath - resolved distention path for every path
* @returns {Object} results[].pkgInfo - {dist, ext}
*/
const { json, ext, distPath } = getJsonByPath(buildName)(...paths);
const { json, pkgInfo } = getJsonByPath(buildName)(...paths);
```

#### Example(2)

```js
import { getJsonByPath } from "get-info";

const { json, ext, distPath } = getJsonByPath()();

expect(json).to.be.an("Array");
expect(json.length).to.be.equal(1);
const { json, pkgInfo } = getJsonByPath()();

expect(json[0].name).to.be.equal("get-info");
expect(ext[0]).to.be.equal("js");
expect(distPath[0]).to.be.equal(`${__dirname}/dist`);
// json>> [{name: get-info, version: "1.1.1", main: "index.js", ...}]
//
// pkgInfo>> {get-info}
//
// pkgInfo[get-info]>> {ext: js, dist: "root/**/get-info/dist/"}
```

### getPackagesPath
Expand All @@ -108,16 +104,13 @@ import { getPackagesPath } from "get-info";

const { path, ext } = getPackagesPath();

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

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

expect(path).to.deep.equal(expectedPaths);
expect(ext).to.deep.equal(expectedExtensions);
// ext>> ["js", "ts", "ts"];
```

### Utils
Expand Down Expand Up @@ -190,6 +183,8 @@ const { path, ext } = filterPathAccess(pkgPath);
- [move-position](https://github.com/jalal246/move-position) - Moves element
index in an array.

- [textics](https://github.com/jalal246/textics) & [textics-stream](https://github.com/jalal246/textics-stream) - Counts lines, words, chars and spaces for a given string.

## Tests

```sh
Expand Down
10 changes: 1 addition & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@
"@mytools/print": "^0.1.3",
"glob": "^7.1.6"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.0",
"mocha": "^7.0.1",
"prettier": "^1.19.1"
},
"devDependencies": {},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
Expand Down
23 changes: 11 additions & 12 deletions src/getJsonByName.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const { msg, success } = require("@mytools/print");
const getJsonByPath = require("./getJsonByPath");

let json;
let ext;
let distPath;
let pkgInfo;

/**
* Extracts package json, extension, and resolved distention path for each given
Expand All @@ -15,21 +14,23 @@ let distPath;
*
* @returns {Object[]} results
* @returns {Array} results[].json - packages json related to given path
* @returns {Array} results[].ext - extension (js|ts) related to every path
* @returns {Object} results[].pkgInfo - {dist, ext}
*/
function byName(names) {
const filteredExt = [];
const filteredJson = [];
const filteredDistPath = [];
const filteredPkgInfo = {};

names.forEach(packageName => {
for (let j = 0; j < json.length; j += 1) {
const { name } = json[j];

if (name.includes(packageName)) {
filteredJson.push(json[j]);
filteredExt.push(ext[j]);
filteredDistPath.push(distPath[j]);

/**
* Add extracted extra info to pkgInfo and keep pkgJson as it is.
*/
filteredPkgInfo[name] = pkgInfo[name];

// remove element from array so we don't check it again.
json.splice(j, 1);
Expand All @@ -43,8 +44,7 @@ function byName(names) {

return {
json: filteredJson,
ext: filteredExt,
distPath: filteredDistPath
pkgInfo: filteredPkgInfo
};
}

Expand All @@ -60,15 +60,14 @@ function getJsonByName(buildName, ...paths) {
/**
* extract json form each package.
*/
({ json, ext, distPath } = getJsonByPath(buildName)(...paths));
({ json, pkgInfo } = getJsonByPath(buildName)(...paths));

if (defaultNames.length === 0) {
msg(`Getting all packages`);

return {
json,
ext,
distPath
pkgInfo
};
}

Expand Down
18 changes: 10 additions & 8 deletions src/getJsonByPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ let ext = [];
*
* @returns {Object[]} results
* @returns {Array} results[].json - packages json related to given path
* @returns {Array} results[].ext - extension (js|ts) related to every path
* @returns {Array} results[].distPath - resolved distention path for every path
* @returns {Object} results[].pkgInfo - {dist, ext}
*/
function byPath(defaultPaths) {
const filteredExt = [];
const distPath = [];
const pkgInfo = {};

const packagesJson = defaultPaths.map((pkgPath, i) => {
const pkgJson = resolve(pkgPath, "package.json");
Expand All @@ -41,9 +39,13 @@ function byPath(defaultPaths) {

const dist = resolve(pkgPath, buildName);

distPath.push(dist);

filteredExt.push(pkgExt);
/**
* Add extracted extra info to pkgInfo and keep pkgJson as it is.
*/
pkgInfo[name] = {
dist,
ext: pkgExt
};

return {
sourcePath,
Expand All @@ -62,7 +64,7 @@ function byPath(defaultPaths) {

success(`> Done extracting ${filteredPkgJson.length} packages json`);

return { json: filteredPkgJson, ext: filteredExt, distPath };
return { json: filteredPkgJson, pkgInfo };
}

/**
Expand Down
14 changes: 8 additions & 6 deletions test/getJsonByName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ const { getJsonByName, getPackagesPath, setIsSilent } = require("../src");
describe("getJsonByName", () => {
setIsSilent(true);
it("Default: gets current path with ext and json", () => {
const { ext, json, distPath } = getJsonByName()();
const { json, pkgInfo } = getJsonByName()();

expect(json).to.be.an("Array");
expect(ext).to.be.an("Array");
expect(distPath).to.be.an("Array");
expect(pkgInfo).to.be.an("Object");

expect(json.length).to.be.equal(1);
expect(ext.length).to.be.equal(1);
expect(distPath.length).to.be.equal(1);

expect(ext[0]).to.be.equal("js");
expect(json[0]).to.have.own.property("sourcePath");
expect(json[0]).to.have.own.property("dependencies");

/**
* By default, will read the project scr and package json.
*/
expect(json[0].name).to.be.equal("get-info");
const { name } = json[0];

const { dist, ext } = pkgInfo[name];

expect(dist).to.be.an("string");
expect(ext).to.be.equal("js");
});

it("gets array of json with default path", () => {
Expand Down
28 changes: 18 additions & 10 deletions test/getJsonByPath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@ const { getPackagesPath, getJsonByPath, setIsSilent } = require("../src");
describe("getJsonByPath", () => {
setIsSilent(true);
it("Default: Checks if monorepo or not and gets the json info", () => {
const { json, ext, distPath } = getJsonByPath()();
const { json, pkgInfo } = getJsonByPath()();

expect(json).to.be.an("Array");
expect(json.length).to.be.equal(1);

expect(ext).to.be.an("Array");
expect(ext.length).to.be.equal(1);

expect(distPath).to.be.an("Array");
expect(distPath.length).to.be.equal(1);

expect(json[0]).to.have.own.property("name");
expect(json[0]).to.have.own.property("sourcePath");
expect(json[0]).to.have.own.property("dependencies");

/**
* By default, will read the project scr and package json.
*/
expect(json[0].name).to.be.equal("get-info");
expect(ext[0]).to.be.equal("js");
const { name } = json[0];

const { dist, ext } = pkgInfo[name];

expect(dist).to.be.an("string");
expect(ext).to.be.equal("js");
});

it("returns all packages for given path", () => {
Expand All @@ -44,7 +42,7 @@ describe("getJsonByPath", () => {
it("filters unfiltered paths then get packages Json for each", () => {
const { path } = getPackagesPath("./test/packages-invalid/*");

const { json } = getJsonByPath()(...path);
const { json, pkgInfo } = getJsonByPath()(...path);

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

Expand All @@ -53,5 +51,15 @@ describe("getJsonByPath", () => {
expect(json[0]).to.have.own.property("dependencies");

expect(json[0].name).to.be.equal("@folo/forms");

/**
* By default, will read the project scr and package json.
*/
const { name } = json[0];

const { dist, ext } = pkgInfo[name];

expect(dist).to.be.an("string");
expect(ext).to.be.equal("js");
});
});

0 comments on commit d89c9e1

Please sign in to comment.