Skip to content

Commit

Permalink
Merge pull request #10 from jalal246/dev
Browse files Browse the repository at this point in the history
fix documentations
  • Loading branch information
jalal246 authored May 5, 2020
2 parents 9ede5b4 + 07c7cc2 commit 0245b02
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 177 deletions.
175 changes: 99 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package Sorter

> Sorting a group of packages that depends on each other :nerd_face:
> Sorting a group of packages that depends on each other
Having multiple projects in workspace depending on each other is a headache. You
have to build core first, then the project depends on it, and so on. You
Expand All @@ -13,15 +13,18 @@ npm install package-sorter
## API

```js
/**
* @param {Array} [packages=[]] - packages in workspace.
* @param {string} coreDependency - core package that other packages depends on it.
*
* @returns {Object} result
* @returns {Array} result.sorted - all sorted packages
* @returns {{form: number, to: number}[]} result.sortingMap- map of indexes change due to sorting
* @returns {Array} result.unSorted - packages unsortable
*/
packageSorter(packages? Array, coreDependency? string)
```
Returns result object:
- `sorted: Array <sortedPkgJson>` - all sorted packages in order.
- `sortingMap: Array <sortingMap>`- map of package sorting contains:
- `form: number` - original package index before sorting.
- `to: number` - current package index after sorting.
- `unSorted: Array <unsortedPkgJson>` - unsortable package that's missing dependency.
```js
const { sorted, sortingMap, unSorted } = packageSorter(
packages,
coreDependency
Expand All @@ -31,132 +34,152 @@ const { sorted, sortingMap, unSorted } = packageSorter(
If `coreDependency` is not passed, `package-sorter` will extract it following
monorepo naming pattern as: `@coreDep/`
> `unSorted`
> Just in case, packages are missing the main dependency will be added to
> unSorted. Then you can figure out what's missing before production.
### Example (1) - All Sorted
```js
import packageSorter from "package-sorter";

// input packages:
const pkg0 = {
name: "@folo/withcontext",
dependencies: {}
};
// input packages

const pkg1 = {
name: "@folo/values",
dependencies: {
"@folo/withcontext": "^0.1.5"
}
name: "@pkg/first",
dependencies: {},
};

const pkg2 = {
name: "@folo/layout",
name: "@pkg/second",
dependencies: {
"@pkg/first": "^0.1.5",
},
};

const pkg3 = {
name: "@pkg/third",
dependencies: {
"@folo/values": "^0.1.5"
}
"@pkg/second": "^0.1.5",
},
};

const packages = [pkg2, pkg1, pkg0];
const packages = [pkg3, pkg2, pkg1];

// our core dependency in this case is: @folo.
const { sorted, sortingMap, unSorted } = sortPackages(packages, "@folo");
// our core dependency in this case is: @pkg.
const { sorted, sortingMap, unSorted } = packageSorter(packages, "@pkg");

// sorted: [pkg0, pkg1, pkg2];
// sortingMap: [ { from: 2, to: 0 }, { from: 1, to: 1 }, { from: 0, to: 2 } ]
// unSorted: []
// sorted = [pkg1, pkg2, pkg3];

// sortingMap = [
// { from: 2, to: 0 },
// { from: 1, to: 1 },
// { from: 0, to: 2 },
// ];

// unSorted = [];
```
### Example (2) - Mixed Packages
```js
import packageSorter from "package-sorter";

// input packages:
const pkg0 = {
name: "@folo/withcontext",
dependencies: {}
};
// input packages

const pkg1 = {
name: "@folo/values",
dependencies: {
"@folo/withcontext": "^0.1.5"
}
name: "@pkg/first",
dependencies: {},
};

const pkg2 = {
name: "@pkg/second",
dependencies: {
"@pkg/first": "^0.1.5",
},
};

const pkg3 = {
name: "unrelated",
dependencies: {}
dependencies: {},
};

const packages = [pkg2, pkg1, pkg0];
const packages = [pkg3, pkg2, pkg1];

// let the function gets core dependency (@pkg) by itself
const { sorted, sortingMap, unSorted } = packageSorter(packages);

// let's the function get core dependency.
const { sorted, sortingMap, unSorted } = sortPackages(packages);
// sorted = [pkg3, pkg1, pkg2];

// sorted: [pkg2, pkg0, pkg1]
// sortingMap: [ { from: 0, to: 0 }, { from: 2, to: 1 }, { from: 1, to: 2 } ]
// unSorted: []
// sortingMap = [
// { from: 0, to: 0 },
// { from: 2, to: 1 },
// { from: 1, to: 2 },
// ];

// unSorted = [];
```
### Example (3) - Some Unsorted
```js
import packageSorter from "package-sorter";

// input packages:
const pkg0 = {
name: "@folo/withcontext",
dependencies: {}
};
// input packages

const pkg1 = {
name: "@folo/values",
dependencies: {
"@folo/withcontext": "^0.1.5"
}
name: "@pkg/first",
dependencies: {},
};

const pkg2 = {
name: "@folo/unsortable",
name: "@pkg/second",
dependencies: {
"@pkg/first": "^0.1.5",
},
};

const pkg3 = {
name: "@pkg/unsortable",
dependencies: {
"@folo/missing": "^0.1.5"
}
"@pkg/missing": "^0.1.5",
},
};

const packages = [pkg2, pkg1, pkg0];
const packages = [pkg3, pkg2, pkg1];

const { sorted, sortingMap, unSorted } = packageSorter(packages);

// sorted = [pkg1, pkg2];

const { sorted, sortingMap, unSorted } = sortPackages(packages);
// sortingMap = [
// { from: 2, to: 0 },
// { from: 1, to: 1 },
// ];

// sorted: [pkg0, pkg1]
// sortingMap: [ { from: 2, to: 0 }, { from: 1, to: 1 } ]
// unSorted: [pkg2]
// unSorted = [pkg3];
```
## Test
```sh
npm test
```
## License
This project is licensed under the [GPL-3.0 License](https://github.com/jalal246/packageSorter/blob/master/LICENSE)
### Related projects
- [move-position](https://github.com/jalal246/move-position) - Moves element
index in given array from position A to B.
- [builderz](https://github.com/jalal246/builderz) - Building your project with zero config.
- [builderz](https://github.com/jalal246/builderz) - Zero Configuration JS bundler.
- [corename](https://github.com/jalal246/corename) - Extracts package name.
- [get-info](https://github.com/jalal246/get-info) - Utility functions for
projects production.
- [textics](https://github.com/jalal246/textics) & [textics-stream](https://github.com/jalal246/textics-stream) - Counts lines, words, chars and spaces for a given string.

### Test

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

This project is licensed under the [GPL-3.0 License](https://github.com/jalal246/packageSorter/blob/master/LICENSE)
- [folo](https://github.com/jalal246/folo) - Form & Layout Components Built with React.
8 changes: 4 additions & 4 deletions src/packageSorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function addTo(packages, at, isSorted) {
if (isSorted) {
sortingMap.push({
from: at,
to
to,
});
}

Expand Down Expand Up @@ -150,7 +150,7 @@ function packageSorter(packages = [], coreDependency) {
return {
sorted: packages,
unSorted,
sortingMap
sortingMap,
};

coreDep = coreDependency || getCoreName(packages);
Expand All @@ -162,7 +162,7 @@ function packageSorter(packages = [], coreDependency) {
return {
sorted: packages,
unSorted,
sortingMap
sortingMap,
};

const totalLength = packages.length;
Expand All @@ -186,7 +186,7 @@ function packageSorter(packages = [], coreDependency) {
return {
sorted,
unSorted,
sortingMap
sortingMap,
};
}

Expand Down
Loading

0 comments on commit 0245b02

Please sign in to comment.