Skip to content

Commit

Permalink
Merge pull request #59 from ega4432/bug/output-count
Browse files Browse the repository at this point in the history
Fixed output count
  • Loading branch information
ega4432 authored Mar 2, 2023
2 parents d57a14c + a8d2a8b commit c813a90
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 23 deletions.
69 changes: 61 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,87 @@ jobs:

- run: npm run all # exec: [ prettier, eslint, test, build, package ]

test: # make sure the action works on a clean machine without building
test-normal:
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- name: Checkout source
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Normal test
id: import
uses: ./
env:
NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}
ACTIONS_STEP_DEBUG: true
ACTIONS_RUNNER_DEBUG: true

- name: Check output
run: |
echo "exported count: ${{ steps.import.outputs.files_count }}"
- name: Validate count
run: |
if [ "$(find ./output -type f -name '*.md' | wc -l)" != "${{ steps.import.outputs.files_count }}" ]; then
find ./output -type f -name '*.md'
exit 1
fi
test-output-path:
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- name: Checkout source
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Specific output path
id: import
uses: ./
with:
output_path: some/dir
env:
NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}

- name: Check output
run: |
echo "exported count: ${{ steps.import.outputs.files_count }}"
- name: Validate count
run: |
if [ "$(find ./some/dir -type f -name '*.md' | wc -l)" != "${{ steps.import.outputs.files_count }}" ]; then
find ./some/dir -type f -name '*.md'
exit 1
fi
test-filename-property:
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- name: Checkout source
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Specific property as a markdown filename
id: import2
id: import
uses: ./
with:
filename_property: 'slug'
env:
NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}
ACTIONS_STEP_DEBUG: true
ACTIONS_RUNNER_DEBUG: true

- name: Check output
run: |
echo "Normal test: exported count: ${{ steps.import.outputs.files_count }}"
echo "Specified property test: exported count: ${{ steps.import2.outputs.files_count }}"
echo "exported count: ${{ steps.import.outputs.files_count }}"
- name: Validate count
run: |
if [ "$(find ./output -type f -name '*.md' | wc -l)" != "${{ steps.import.outputs.files_count }}" ]; then
find ./output -type f -name '*.md'
exit 1
fi
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ Database ID of target Notion page.

### `output_path`

Directory path to output files.
You can specify the directory path to output files.

- optional
- default: `output`

### `filename_property`

You can specify the column of the Notion database to be used as a filename when saving the files.

- optional
- default: `title`
15 changes: 9 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@actions/core';
import { mkdirP } from '@actions/io';
import { Client } from '@notionhq/client';
import { readdir, writeFile } from 'fs/promises';
import { writeFile } from 'fs/promises';
import get from 'axios';

import { queryDatabase } from './utils/notion';
Expand Down Expand Up @@ -51,25 +51,29 @@ const run = async (

info(`---> Successfully created directory! : ${outDir}`);

await createFiles(mdResponse, outDir);
const count = await createFiles(mdResponse, outDir);

const files = await readdir(outDir);
debug(`Output: files_count=${files.length.toString()}`);
setOutput('files_count', files.length.toString());
debug(`Output: files_count=${count}`);
setOutput('files_count', count.toString());

info('---> Successfully created markdown files!');
};

const createFiles = async (pages: MarkdownPage[], outDir: string) => {
pages.forEach(async (markdown) => {
let count = 0;

for (const markdown of pages) {
if (markdown.filename.length) {
// NOTE: 現状すでにファイルが存在していても上書きする
const filename = `${outDir}/${markdown.filename}.md`;
await writeFile(filename, markdown.body);
debug(`Created: ${filename}`);
count++;
await downloadImages(markdown.filename, outDir);
}
});
}

return count;
};

const downloadImages = async (filename: string, outDir: string) => {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const queryDatabase = async ({
});

info('---> Successfully return response from Notion via API!');
debug(
`Response from Notion API: ${JSON.stringify(response.results, null, 2)}`
);

const pages = response.results.filter(
(result) => 'properties' in result
Expand Down

0 comments on commit c813a90

Please sign in to comment.