Skip to content

Commit 1cba2c3

Browse files
committed
02 Make docs/.vitepress/** ESM
1 parent 0781feb commit 1cba2c3

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

docs/.vitepress/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const utils = require('./utils.ts');
1+
import { copyFile, rewriteFile } from './utils.js';
22

3-
utils.copyFile('README.md', 'index.md');
4-
utils.copyFile('CONTRIBUTING.md', 'CONTRIBUTING.md');
3+
copyFile('README.md', 'index.md');
4+
copyFile('CONTRIBUTING.md', 'CONTRIBUTING.md');
55

6-
utils.rewriteFile('../apidocs/README.md', /\(CONTRIBUTING.md\)/g, '(../CONTRIBUTING.md)');
6+
rewriteFile('../apidocs/README.md', /\(CONTRIBUTING.md\)/g, '(../CONTRIBUTING.md)');
77

88
export default {
99
title: 'line-bot-sdk-nodejs',

docs/.vitepress/utils.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
const fs = require('fs');
2-
const path = require('path');
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import {fileURLToPath} from 'node:url';
4+
import {dirname} from 'node:path';
5+
6+
// __dirname is not available in ESM, so we need to derive it
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = dirname(__filename);
39

410
function copyFile(sourceFilename, targetFilename) {
5-
const sourcePath = path.join(__dirname, '../../', sourceFilename);
6-
const targetPath = path.join(__dirname, '../', targetFilename);
7-
const md = fs.readFileSync(sourcePath, 'utf-8');
8-
fs.writeFileSync(targetPath, md);
11+
const sourcePath = path.join(__dirname, '../../', sourceFilename);
12+
const targetPath = path.join(__dirname, '../', targetFilename);
13+
const md = fs.readFileSync(sourcePath, 'utf-8');
14+
fs.writeFileSync(targetPath, md);
915
}
1016

1117
function rewriteFile(filename, regex, replacement) {
12-
console.log("Rewriting file: ", filename, " with regex: ", regex, " and replacement: ", replacement)
13-
const content = fs.readFileSync(path.join(__dirname, filename), 'utf-8');
14-
const newContent = content.replace(regex, replacement);
15-
fs.writeFileSync(path.join(__dirname, filename), newContent);
18+
console.log("Rewriting file: ", filename, " with regex: ", regex, " and replacement: ", replacement)
19+
const content = fs.readFileSync(path.join(__dirname, filename), 'utf-8');
20+
const newContent = content.replace(regex, replacement);
21+
fs.writeFileSync(path.join(__dirname, filename), newContent);
1622
}
1723

1824
export {
19-
copyFile,
20-
rewriteFile,
25+
copyFile,
26+
rewriteFile
2127
};

0 commit comments

Comments
 (0)