Skip to content

Commit

Permalink
Merge pull request #12 from jalal246/dev
Browse files Browse the repository at this point in the history
Add ability to get packages without json
  • Loading branch information
jalal246 authored May 5, 2020
2 parents f9c1897 + 15efeb9 commit 48bcc20
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 75 deletions.
184 changes: 121 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Utility functions extract project(s) Json by providing> project root path or package names.
`get-info` Works with monorepos `./packages/**/` as well as for a single package project `./MyFiles`.
`get-info` Works with monorepos `./packages/**/` as well as for a single package project `./myFiles`.

```bash
npm install get-info
Expand All @@ -17,13 +17,15 @@ not passed, it returns all json objects can be found in
`./packages/**/package.json` or `./package json`

```js
/**
* @param {string} names required packages name
*
* @returns {Object} results
* @returns {Array} results[].json - packages json related to given name
* @returns {Object} results[].pkgInfo - {path}
*/
getJsonByName(...packNames?string)
```
The result object:
- `json: Array <packJson>` - Contains objects of all retrieved package.json based on given names
- `pkgInfo: Array <packPath>` - Contains objects of package paths based on package name
```js
const { json, pkgInfo } = getJsonByName(...names);
```
Expand All @@ -32,31 +34,51 @@ const { json, pkgInfo } = getJsonByName(...names);
```js
import { getJsonByName } from "get-info";

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

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

// pkgInfo[@myFav/project]
// {path: "root/to/myFav-project"}

// Note: to get default entry and resolved dist path:

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

const srcPath = resolve(path, "src", `index.js`);
const buildPath = resolve(path, "dist");
// workspace
//
// ├───foo
// │ ├───src
// │ └───package.json
// ├───bar
// │ ├───src
// │ └───package.json
// ├───foobar
// │ ├───src
//

const { json, pkgInfo } = getJsonByName("foo", "bar");

// json = [
// { name: "foo", version: "1.0.0", main: "index.js" },
// { name: "bar", version: "2.1.1", main: "bundle.js" },
// ];

// pkgInfo = {
// foo: { path: "path/to/foo" },
// bar: { path: "path/to/bar" },
// };
```
What if passed invalid name? It returns empty array `[]`
#### Example(2)
```js
const { json, pkgInfo } = getJsonByName("pkg-no-valid-json");

// json
// []
// workspace
//
// ├───foo
// │ ├───src
// │ └───package.json
// ├───bar
// │ ├───src
// │ └───package.json
// ├───foobar
// │ ├───src
//

const { json, pkgInfo } = getJsonByName("baz");

// json =[]

if (json.length === 0) console.log("do something");
```
Expand All @@ -68,51 +90,94 @@ passed, it returns all json objects can be found in `./packages/**/package.json`
or `./package.json`
```js
/**
*
* @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 - {path}
*/
const { json, pkgInfo } = getJsonByPath(...paths);
getJsonByPath(...paths?string)
```
The result object:
- `json: Array <packJson>` - Contains objects of all retrieved package.json based on given paths
- `pkgInfo: Array <packPath>` - Contains objects of package paths based on package path
- `unfoundJson: Array <string>` - List of paths don't have valid package.json
```js
const { json, pkgInfo, unfoundJson } = getJsonByPath(...paths);
```
#### Example(3)
```js
import { getJsonByPath } from "get-info";

const { json, pkgInfo } = getJsonByPath(`${__dirname}/myProject`);
// workspace
//
// ├───foo
// │ ├───src
// │ └───package.json
// ├───bar
// │ ├───src
// │ └───package.json
// ├───foobar
// │ ├───src
//

const { json, pkgInfo, unfoundJson } = getJsonByPath(
`${__dirname}/foo`,
`${__dirname}/bar`
);

// json = [
// { name: "foo", version: "1.0.0", main: "index.js" },
// { name: "bar", version: "2.1.1", main: "bundle.js" },
// ];

// json
// [{name: myProject, version: "1.1.1", main: "index.js", ...}]
// pkgInfo = {
// foo: { path: "path/to/foo" },
// bar: { path: "path/to/bar" },
// };

// pkgInfo[myProject]
// {path: "root/to/myProject"}
// unfoundJson = ["path/to/foobar"];
```
How it works with monorepo?

#### Example(4)
By default, `getJsonByPath` returns all package in `workspace` tree.
```js
const { json, pkgInfo } = getJsonByPath(
`${__dirname}/myProject1`,
`${__dirname}/myProject2`
);
// workspace
//
// ├───foo
// │ ├───src
// │ └───package.json
// ├───bar
// │ ├───src
// │ └───package.json
// ├───foobar
// │ └───src
//
// │───package.json

const { json, pkgInfo, unfoundJson } = getJsonByPath();

// json = [
// { name: "foo", version: "1.0.0", main: "index.js" },
// { name: "bar", version: "2.1.1", main: "bundle.js" },
// ];

// pkgInfo = {
// foo: { path: "path/to/foo" },
// bar: { path: "path/to/bar" },
// };

// unfoundJson = ["path/to/foobar"];
```
// json
// [{name: myProject1, version: "1.1.1", main: "index.js", ...}]
## Tests
// Since path is exported and associated with package name you can easily do:
```sh
npm test
```
const { path } = pkgInfo["myProject1"];
## License
const srcPath = resolve(path, "src", `index.js`);
const buildPath = resolve(path, "dist");
```
This project is licensed under the [GPL-3.0 License](https://github.com/jalal246/get-info/blob/master/LICENSE)
### Related projects
Expand All @@ -131,12 +196,5 @@ const buildPath = resolve(path, "dist");
- [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
npm test
```

## License

This project is licensed under the [GPL-3.0 License](https://github.com/jalal246/get-info/blob/master/LICENSE)
- [folo](https://github.com/jalal246/folo) - Form & Layout Components Built with
React.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
"fileSystem",
"filter"
],
"dependencies": {
"glob": "^7.1.6"
},
"dependencies": {},
"devDependencies": {},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
9 changes: 6 additions & 3 deletions src/getJsonByName.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ function getJsonByName(...names) {
/**
* extract all then filters it.
*/
const { json, pkgInfo } = getJsonByPath();
const { json, pkgInfo /** unfoundJson * */ } = getJsonByPath();

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

/**
* returns all.
*/
return {
json,
pkgInfo,
/** unfoundJson, * */
};
}

Expand Down Expand Up @@ -57,6 +59,7 @@ function getJsonByName(...names) {
return {
json: filteredJson,
pkgInfo: filteredPkgInfo,
/** unfoundJson */
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/getJsonByPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function getJsonByPath(...defaultPaths) {
}

const pkgInfo = {};
const unfoundJson = [];

const packagesJson = foundPaths
.map((pkgPath) => {
Expand All @@ -46,6 +47,7 @@ function getJsonByPath(...defaultPaths) {
const isValid = fs.existsSync(pkgJson);

if (!isValid) {
unfoundJson.push(pkgJson);
return null;
}

Expand All @@ -71,7 +73,7 @@ function getJsonByPath(...defaultPaths) {
})
.filter(Boolean);

return { json: packagesJson, pkgInfo };
return { json: packagesJson, pkgInfo, unfoundJson };
}

module.exports = getJsonByPath;
9 changes: 6 additions & 3 deletions test/getJsonByName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const { getJsonByName } = require("../src");

describe("getJsonByName", () => {
it("Default: gets current path and json", () => {
const { json, pkgInfo } = getJsonByName();
const { json, pkgInfo /** unfoundJson * */ } = getJsonByName();

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

expect(json.length).to.be.equal(1);
// expect(unfoundJson.length).to.be.equal(0);

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

Expand All @@ -27,10 +28,11 @@ describe("getJsonByName", () => {
});

it("gets array of json with default path", () => {
const { json } = getJsonByName("get-info");
const { json /** unfoundJson * */ } = getJsonByName("get-info");

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

expect(json[0].name).to.be.equal("get-info");
});
Expand Down Expand Up @@ -61,9 +63,10 @@ describe("getJsonByName", () => {
// });

it("returns empty array when name is wrong", () => {
const { json } = getJsonByName("nothingTrue");
const { json /** unfoundJson * */ } = getJsonByName("nothingTrue");

expect(json).to.be.an("Array");
expect(json.length).to.be.equal(0);
// expect(unfoundJson.length).to.be.equal(0);
});
});
6 changes: 4 additions & 2 deletions test/getJsonByPath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ const rootPath = fs

describe("getJsonByPath", () => {
it("Default: Checks if monorepo or not and gets the json info", () => {
const { json, pkgInfo } = getJsonByPath();
const { json, pkgInfo, unfoundJson } = getJsonByPath();

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

expect(json[0]).to.have.own.property("name");
expect(json[0]).to.have.own.property("files");
Expand All @@ -32,9 +33,10 @@ describe("getJsonByPath", () => {
});

it("returns all valid packages contains json for given path", () => {
const { json, pkgInfo } = getJsonByPath(...rootPath);
const { json, pkgInfo, unfoundJson } = getJsonByPath(...rootPath);

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

json.forEach(({ name }) => {
expect(name).to.be.an("string");
Expand Down

0 comments on commit 48bcc20

Please sign in to comment.