Skip to content

Commit

Permalink
watcher extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Jan 23, 2025
1 parent 1579be7 commit 4a2249a
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 120 deletions.
40 changes: 27 additions & 13 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"workspaces": [
"packages/config",
"packages/core",
"packages/watch",
"packages/nextjs-dev-translate-plugin",
"packages/vite-plugin-dev-translate",
"packages/cli",
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aexol/dev-translate",
"version": "0.1.2",
"version": "0.1.3",
"private": false,
"main": "./lib/index.js",
"author": "Aexol, Artur Czemiel",
Expand All @@ -17,8 +17,8 @@
"lib"
],
"dependencies": {
"@aexol/dev-translate-config": "^0.1.2",
"@aexol/dev-translate-core": "^0.1.2",
"@aexol/dev-translate-config": "^0.1.3",
"@aexol/dev-translate-core": "^0.1.3",
"chalk": "^5.3.0",
"chokidar": "^3.6.0",
"commander": "^11.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aexol/dev-translate-config",
"version": "0.1.2",
"version": "0.1.3",
"private": false,
"main": "./lib/index.js",
"author": "Aexol, Artur Czemiel",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aexol/dev-translate-core",
"version": "0.1.2",
"version": "0.1.3",
"private": false,
"main": "./lib/index.js",
"author": "Aexol, Artur Czemiel",
Expand Down
48 changes: 2 additions & 46 deletions packages/nextjs-dev-translate-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
import chokidar from 'chokidar';
import path from 'path';
import { NextConfig } from 'next';
import { LangPair, translateLocaleFolder, Languages } from '@aexol/dev-translate-core';

export type DevTranslateOptions = {
apiKey: string;
folderName: string;
lang: LangPair['lang'];
localeDir: string;
context?: string;
};

const setupFileWatcher = async (opts: DevTranslateOptions) => {
const { apiKey, folderName, lang, localeDir, context } = opts;
const directoryToWatch = path.join(process.cwd(), localeDir, opts.folderName);
const translate = async () => {
await translateLocaleFolder({
srcLang: {
folderName,
lang,
},
apiKey,
context,
cwd: process.cwd(),
localeDir,
});
};
const watcher = chokidar.watch(directoryToWatch, {
persistent: true,
});

watcher.on('change', () => {
translate();
});

watcher.on('add', () => {
translate();
});

watcher.on('unlink', () => {
translate();
});

console.log(`Watching for file changes in ${directoryToWatch}`);
};
import { watch, Languages, DevTranslateOptions } from '@aexol/dev-translate-watch';

// Plugin function to be used in next.config.js
export function withDevTranslate(nextConfig: NextConfig = {}, options: DevTranslateOptions): NextConfig {
const env = process.env.NODE_ENV;
if (env !== 'development') {
return nextConfig;
}
setupFileWatcher(options);
watch(options);
return {
...nextConfig,
webpack(config, options) {
Expand Down
5 changes: 2 additions & 3 deletions packages/nextjs-dev-translate-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aexol/nextjs-dev-translate-plugin",
"version": "0.1.2",
"version": "0.1.3",
"private": false,
"main": "./lib/index.js",
"author": "Aexol, Artur Czemiel",
Expand All @@ -15,8 +15,7 @@
"commonjs"
],
"dependencies": {
"@aexol/dev-translate-core": "^0.1.2",
"chokidar": "^3.6.0"
"@aexol/dev-translate-watch": "^0.1.3"
},
"peerDependencies": {
"next": ">=13"
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs-dev-translate-plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"exclude": [
"lib",
"commonjs",
"node_modules",
"jest.config.js",
]
Expand Down
4 changes: 2 additions & 2 deletions packages/testground/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aexol/testground-dev-translate",
"version": "0.1.2",
"version": "0.1.3",
"private": true,
"main": "./lib/index.js",
"author": "Aexol, Artur Czemiel",
Expand All @@ -14,6 +14,6 @@
"lib"
],
"dependencies": {
"@aexol/dev-translate": "^0.1.2"
"@aexol/dev-translate": "^0.1.3"
}
}
50 changes: 2 additions & 48 deletions packages/vite-plugin-dev-translate/index.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,5 @@
import chokidar from 'chokidar';
import path from 'path';
import { Plugin } from 'vite';
import { LangPair, translateLocaleFolder, Languages } from '@aexol/dev-translate-core';

export type DevTranslateOptions = {
apiKey: string;
folderName: string;
lang: LangPair['lang'];
localeDir: string;
context?: string;
};

const setupFileWatcher = async (opts: DevTranslateOptions) => {
const { apiKey, folderName, lang, localeDir, context } = opts;
const directoryToWatch = path.join(process.cwd(), localeDir, opts.folderName);
const translate = async () => {
await translateLocaleFolder({
srcLang: {
folderName,
lang,
},
apiKey,
cwd: process.cwd(),
localeDir,
context,
});
};
const watcher = chokidar.watch(directoryToWatch, {
persistent: true,
});

watcher.on('change', () => {
translate();
});

watcher.on('add', () => {
translate();
});

watcher.on('unlink', () => {
translate();
});

console.log(`Watching for file changes in ${directoryToWatch}`);

return watcher;
};
import { watch, DevTranslateOptions, Languages } from '@aexol/dev-translate-watch';

export default function devTranslatePlugin(options: DevTranslateOptions): Plugin {
return {
Expand All @@ -54,7 +8,7 @@ export default function devTranslatePlugin(options: DevTranslateOptions): Plugin

configureServer(server) {
// Initialize the watcher
setupFileWatcher(options).then((watcher) => {
watch(options).then((watcher) => {
// Clean up the watcher when the server closes
server.httpServer?.on('close', () => {
watcher.close();
Expand Down
5 changes: 2 additions & 3 deletions packages/vite-plugin-dev-translate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aexol/vite-plugin-dev-translate",
"version": "0.1.2",
"version": "0.1.3",
"private": false,
"main": "./lib/index.js",
"author": "Aexol, Artur Czemiel",
Expand All @@ -15,8 +15,7 @@
"commonjs"
],
"dependencies": {
"@aexol/dev-translate-core": "^0.1.2",
"chokidar": "^3.6.0"
"@aexol/dev-translate-watch": "^0.1.3"
},
"peerDependencies": {
"vite": ">=5"
Expand Down
8 changes: 8 additions & 0 deletions packages/watch/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"../../.eslintrc.json"
],
"rules":{
"@typescript-eslint/no-explicit-any":"off"
}
}
6 changes: 6 additions & 0 deletions packages/watch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.env
/lib
/commonjs
node_modules
.graphql-editor-auth.json
tsconfig.tsbuildinfo
48 changes: 48 additions & 0 deletions packages/watch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import chokidar from 'chokidar';
import path from 'path';
import { LangPair, translateLocaleFolder, Languages } from '@aexol/dev-translate-core';

export type DevTranslateOptions = {
apiKey: string;
folderName: string;
lang: LangPair['lang'];
localeDir: string;
context?: string;
};

export const watch = async (opts: DevTranslateOptions) => {
const { apiKey, folderName, lang, localeDir, context } = opts;
const directoryToWatch = path.join(process.cwd(), localeDir, opts.folderName);
const translate = async () => {
try {
await translateLocaleFolder({
srcLang: {
folderName,
lang,
},
apiKey,
cwd: process.cwd(),
localeDir,
context,
});
} catch (error) {
console.log(error);
}
};
const watcher = chokidar.watch(directoryToWatch, {
persistent: true,
});
watcher.on('change', () => {
translate();
});
watcher.on('add', () => {
translate();
});
watcher.on('unlink', () => {
translate();
});
console.log(`Watching for file changes in ${directoryToWatch}`);
return watcher;
};

export { Languages };
Loading

0 comments on commit 4a2249a

Please sign in to comment.