Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

05 Add document for ESM project #20

Draft
wants to merge 24 commits into
base: 04-support-cjs-again
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ jobs:
node-version: 18
registry-url: 'https://registry.npmjs.org'
- run: npm install
- name: Update version in package.json, package-lock.json
- name: Update version in package.json, package-lock.json, and lib/version.ts
run: |
VERSION=${{ github.event.release.tag_name }}
VERSION=${VERSION#v}
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" package.json
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" package-lock.json
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" lib/version.ts
- run: npm run release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
15 changes: 13 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,21 @@ jobs:
run: export NODE_OPTIONS=--openssl-legacy-provider; npm run apidocs
- name: Test building docs
run: export NODE_OPTIONS=--openssl-legacy-provider; npm run docs:build
- name: Test building examples
- name: Test building examples (CJS)
run: |
cd examples/echo-bot-ts
cd examples/echo-bot-ts-cjs
npm run build-sdk
npm install
npm run build
cd -
- name: Test building examples (ESM)
run: |
cd examples/echo-bot-ts-esm
npm run build-sdk
npm install
npm run build
cd -
- name: publint
run: npx publint
- name: validate package
run: npx @arethetypeswrong/cli $(npm pack)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Github Action](https://github.com/line/line-bot-sdk-nodejs/actions/workflows/test.yml/badge.svg)](https://github.com/line/line-bot-sdk-nodejs/actions/workflows/test.yml)
[![npmjs](https://badge.fury.io/js/%40line%2Fbot-sdk.svg)](https://www.npmjs.com/package/@line/bot-sdk)

TODO: Add native esm sample in example directory

## Introduction
The LINE Messaging API SDK for nodejs makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes.
Expand Down
8 changes: 4 additions & 4 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const utils = require('./utils.ts');
import { copyFile, rewriteFile } from './utils.js';

utils.copyFile('README.md', 'index.md');
utils.copyFile('CONTRIBUTING.md', 'CONTRIBUTING.md');
copyFile('README.md', 'index.md');
copyFile('CONTRIBUTING.md', 'CONTRIBUTING.md');

utils.rewriteFile('../apidocs/README.md', /\(CONTRIBUTING.md\)/g, '(../CONTRIBUTING.md)');
rewriteFile('../apidocs/README.md', /\(CONTRIBUTING.md\)/g, '(../CONTRIBUTING.md)');

export default {
title: 'line-bot-sdk-nodejs',
Expand Down
30 changes: 18 additions & 12 deletions docs/.vitepress/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const fs = require('fs');
const path = require('path');
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {dirname} from 'node:path';

// __dirname is not available in ESM, so we need to derive it
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

function copyFile(sourceFilename, targetFilename) {
const sourcePath = path.join(__dirname, '../../', sourceFilename);
const targetPath = path.join(__dirname, '../', targetFilename);
const md = fs.readFileSync(sourcePath, 'utf-8');
fs.writeFileSync(targetPath, md);
const sourcePath = path.join(__dirname, '../../', sourceFilename);
const targetPath = path.join(__dirname, '../', targetFilename);
const md = fs.readFileSync(sourcePath, 'utf-8');
fs.writeFileSync(targetPath, md);
}

function rewriteFile(filename, regex, replacement) {
console.log("Rewriting file: ", filename, " with regex: ", regex, " and replacement: ", replacement)
const content = fs.readFileSync(path.join(__dirname, filename), 'utf-8');
const newContent = content.replace(regex, replacement);
fs.writeFileSync(path.join(__dirname, filename), newContent);
console.log("Rewriting file: ", filename, " with regex: ", regex, " and replacement: ", replacement)
const content = fs.readFileSync(path.join(__dirname, filename), 'utf-8');
const newContent = content.replace(regex, replacement);
fs.writeFileSync(path.join(__dirname, filename), newContent);
}

export {
copyFile,
rewriteFile,
copyFile,
rewriteFile
};
1 change: 1 addition & 0 deletions docs/getting-started/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ line.middleware({
Here is a synopsis of echoing webhook server with [Express](https://expressjs.com/):

``` js
// fix here
const express = require('express');
const line = require('@line/bot-sdk');

Expand Down
1 change: 1 addition & 0 deletions docs/guide/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Express, please refer to its documentation.
Here is an example of an HTTP server built with Express.

``` js
// fix here
const express = require('express')

const app = express()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# LINE Echo Bot with TypeScript
# Echo Bot for LINE using TypeScript and CJS

An example LINE bot to echo message with TypeScript. The bot is coded according to TypeScript's best practices.
Welcome to this simple guide on how to create an Echo Bot for the LINE messaging platform using TypeScript and CommonJS (CJS).
An Echo Bot is a basic bot that replies to a user's message with the same content.
This tutorial will help you set up a LINE Echo Bot from scratch.

## Prerequisite

- Git
- Node.js version 10 and up
- LINE Developers Account for the bot
- Node.js version 18 or higher
- You've created a channel in the LINE Developers Console, and got your channel access token and channel secret.
- Read https://developers.line.biz/en/docs/messaging-api/getting-started/#using-console if you haven't done this yet.

## Installation

Expand All @@ -19,7 +21,7 @@ git clone https://github.com/line/line-bot-sdk-nodejs.git
- Change directory to the example.

```bash
cd line-bot-sdk-nodejs/examples/echo-bot-ts
cd line-bot-sdk-nodejs/examples/echo-bot-ts-cjs
```

- Install all dependencies.
Expand All @@ -43,7 +45,7 @@ export PORT=<YOUR_PORT>
https://example.com/callback
```

- Compile the TypeScript files.
- Build the application.

```bash
npm run build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const clientConfig: ClientConfig = {
};

const middlewareConfig: MiddlewareConfig = {
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET || '',
};

Expand Down
Loading