Skip to content

Commit

Permalink
Merge pull request #5 from mbc-net/docs/how-to-usage
Browse files Browse the repository at this point in the history
Browser auto reloads in local development mode
  • Loading branch information
koichimurakami authored Sep 4, 2024
2 parents 63742e3 + cb8712a commit 727bee2
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm run start:watch
or
$ npm run start:watch -- --locale {{country-code}}
$ npm run start:watch {{country-code}}
```

This command starts a local development server. Most changes are reflected live without having to restart the server.
Expand Down
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"verbose": false,
"watch": ["docs", "src", "i18n", "sidebars.ts", "docusaurus.config.ts"],
"ext": "js,tsx,md,json",
"exec": "npm run translate:extract-placeholder && npm run translate:replace-placeholders && docusaurus start --no-open"
"exec": "npm run translate:extract-placeholder && npm run translate:replace-placeholders"
}
208 changes: 208 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
"start:watch": "nodemon",
"start:watch": "ts-node scripts/hot-reload.ts",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
Expand All @@ -30,6 +30,7 @@
"@docusaurus/module-type-aliases": "3.5.2",
"@docusaurus/tsconfig": "3.5.2",
"@docusaurus/types": "3.5.2",
"concurrently": "^8.2.2",
"lodash": "^4.17.21",
"nodemon": "^3.1.4",
"ts-node": "^10.9.2",
Expand Down
65 changes: 65 additions & 0 deletions scripts/hot-reload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import concurrently from "concurrently";
import config from "../docusaurus.config";
import { execSync } from "child_process";
import * as path from "path";
import * as fs from "fs";

const fallbackLanguage = "en";
const rootDir = path.resolve(__dirname, "..");

const runCommand = function (cmd: string) {
console.log(cmd);
const ret = execSync(cmd, { cwd: rootDir });
console.log(ret.toString());
};

// Get the language from the command-line arguments
const args = process.argv.slice(2);
let language = args[0];

if (!language) {
language = fallbackLanguage;
}

if (config.i18n.locales.find((lng) => lng === language) === undefined) {
console.error("Please correct the support language");
process.exit(1);
}

const outputDir = path.join(
__dirname,
`../i18n/${language}/docusaurus-plugin-content-docs/current`
);

if (!fs.existsSync(outputDir)) {
// We need to replace placeholder into i18n folder before serve content.
runCommand(`npm run translate:extract-placeholder`);
runCommand("npm run translate:replace-placeholders");
}

const { result } = concurrently(
[
{ command: "nodemon", name: "watch", prefixColor: "green" },
{
command: `docusaurus start --locale ${language}`,
name: "serve",
prefixColor: "blue",
},
],
{
prefix: "name",
killOthers: ["failure", "success"],
restartTries: 3,
cwd: rootDir,
}
);

result.then(
function onSuccess() {
process.exit();
},
function onFailure(msg) {
console.error("abc", msg);
process.exit();
}
);

0 comments on commit 727bee2

Please sign in to comment.