Skip to content

Commit

Permalink
Feat/10 publish npm (#15)
Browse files Browse the repository at this point in the history
* npm用ビルドを追加

* GitHub Actionでnpm publish

* READMEにusageを追加
  • Loading branch information
qmotas authored Jul 27, 2022
1 parent 1efe75e commit c2b3f48
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 47 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/release-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: release-npm

on:
release:
types: [published]

jobs:
release:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v2

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: Get tag version
if: startsWith(github.ref, 'refs/tags/')
id: version
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}

- name: Build for npm
run: deno task build:npm ${{steps.version.outputs.TAG_VERSION}}

- uses: JS-DevTools/npm-publish@v1
if: startsWith(github.ref, 'refs/tags/')
with:
token: ${{ secrets.NPM_TOKEN }}
package: ./npm/package.json
2 changes: 1 addition & 1 deletion .github/workflows/deno.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deno
name: test

on:
push:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.env
.env.*
Thumbs.db
cov_profile
cov_profile
npm
100 changes: 58 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
Each transformation function is non-destructive, making it suitable for working
with immutable tree structures.

## Usage

tree-fns is available for Deno and Node.js.

### Deno

```ts
import { TreeNode, walk } from 'https://deno.land/x/[email protected]/mod.ts';
```

### npm

```
npm i tree-fns
```

## Development

### Prerequisite
Expand All @@ -17,14 +33,14 @@ with immutable tree structures.

```ts
const tree: TreeNode = {
id: "root",
id: 'root',
children: [
{
id: "1",
id: '1',
children: [],
},
{
id: "2",
id: '2',
children: [],
},
],
Expand All @@ -33,8 +49,8 @@ const tree: TreeNode = {

```ts
const tree: TreeNode<{ data: string }> = {
id: "root",
data: "yay",
id: 'root',
data: 'yay',
children: [],
};
```
Expand All @@ -48,45 +64,45 @@ Only pre-order traversing is supported.
```ts
walk(
{
id: "root",
id: 'root',
children: [
{
id: "1",
children: [{ id: "1-1", children: [] }],
id: '1',
children: [{ id: '1-1', children: [] }],
},
{
id: "2",
children: [{ id: "2-1", children: [] }],
id: '2',
children: [{ id: '2-1', children: [] }],
},
],
},
(node, location) => {
console.log(node.id); // "root" -> "1" -> "1-1" -> "2" -> "2-1"
if (node.id === "1") {
if (node.id === '1') {
console.log(location); // { parentPath: ["root"], index: 0 }
}
},
}
);
```

### `findNode(tree, test)` | `findNodeById(tree, id)`

```ts
const tree: TreeNode = {
id: "root",
id: 'root',
children: [
{
id: "1",
children: [{ id: "1-1", children: [] }],
id: '1',
children: [{ id: '1-1', children: [] }],
},
{
id: "2",
children: [{ id: "2-1", children: [] }],
id: '2',
children: [{ id: '2-1', children: [] }],
},
],
};

const node = findNode(tree, (node) => node.id === "1");
const node = findNode(tree, (node) => node.id === '1');
// [
// {
// id: '1',
Expand All @@ -98,25 +114,25 @@ const node = findNode(tree, (node) => node.id === "1");
// },
// ]

findNode(tree, (node) => node.id === "x"); // undefined
findNode(tree, (node) => node.id === 'x'); // undefined
```

or

```ts
const node = findNodeById(tree, "1");
const node = findNodeById(tree, '1');
```

### `map(tree, mapNode)`

```ts
const tree: TreeNode<{ data: string }> = {
id: "1",
data: "foo",
id: '1',
data: 'foo',
children: [
{
id: "2",
data: "bar",
id: '2',
data: 'bar',
children: [],
},
],
Expand All @@ -143,8 +159,8 @@ const mapped = map<{ data: string }>(tree, (node) => ({

```ts
const tree = {
id: "1",
children: [{ id: "2", children: [] }],
id: '1',
children: [{ id: '2', children: [] }],
};

const copied = copy(tree);
Expand All @@ -160,13 +176,13 @@ console.log(tree === copied); // false

```ts
const srcTree = {
id: "1",
children: [{ id: "2", children: [] }],
id: '1',
children: [{ id: '2', children: [] }],
};

const nodeToBeAdded = { id: "3", children: [] };
const nodeToBeAdded = { id: '3', children: [] };

const destTree = addNode(srcTree, nodeToBeAdded, { parentId: "1", index: 0 });
const destTree = addNode(srcTree, nodeToBeAdded, { parentId: '1', index: 0 });
// {
// id: '1',
// children: [
Expand All @@ -180,13 +196,13 @@ const destTree = addNode(srcTree, nodeToBeAdded, { parentId: "1", index: 0 });

```ts
const srcTree = {
id: "1",
id: '1',
children: [
{ id: "2", children: [] },
{ id: "3", children: [] },
{ id: '2', children: [] },
{ id: '3', children: [] },
],
};
const [destTree, removedNode] = removeNode(srcTree, "3");
const [destTree, removedNode] = removeNode(srcTree, '3');

console.log(removedNode); // { id: '3', children: [] }
console.log(destTree);
Expand All @@ -197,21 +213,21 @@ console.log(destTree);
// ],
// }

removeNode(srcTree, "x"); // undefined
removeNode(srcTree, 'x'); // undefined
```

### `moveNode(tree, id, dest)`

```ts
const srcTree = {
id: "1",
id: '1',
children: [
{ id: "2", children: [] },
{ id: "3", children: [] },
{ id: '2', children: [] },
{ id: '3', children: [] },
],
};

const destTree = moveNode(srcTree, "2", { parentId: "3", index: 0 });
const destTree = moveNode(srcTree, '2', { parentId: '3', index: 0 });
// {
// id: '1',
// children: [
Expand All @@ -226,10 +242,10 @@ const destTree = moveNode(srcTree, "2", { parentId: "3", index: 0 });

```ts
const tree = {
id: "1",
id: '1',
children: [
{ id: "2", children: [] },
{ id: "3", children: [] },
{ id: '2', children: [] },
{ id: '3', children: [] },
],
};

Expand Down
7 changes: 4 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
},
"lint": {
"files": {
"exclude": ["cov_profile/"]
"exclude": ["cov_profile", "npm"]
}
},
"fmt": {
"files": {
"exclude": ["cov_profile"]
"exclude": ["cov_profile", "npm"]
}
},
"tasks": {
"lint": "deno fmt --check; deno lint",
"format": "deno fmt",
"test": "deno test --import-map import_map.json --coverage=cov_profile/",
"coverage": "deno coverage cov_profile/"
"coverage": "deno coverage cov_profile/",
"build:npm": "deno run -A scripts/build_npm.ts"
}
}
29 changes: 29 additions & 0 deletions scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts";

await emptyDir("./npm");

await build({
entryPoints: ["./mod.ts"],
outDir: "./npm",
importMap: "./import_map.json",
shims: {
deno: true,
},
package: {
name: "tree-fns",
version: Deno.args[0]?.replace(/^v/, ""),
description: "Simple utility functions for tree structure",
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/qmotas/tree-fns.git",
},
bugs: {
url: "https://github.com/qmotas/tree-fns/issues",
},
},
});

// post build steps
Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");

0 comments on commit c2b3f48

Please sign in to comment.