Skip to content

Commit

Permalink
The watcher feature has been improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
gerceker committed Aug 8, 2023
1 parent 2f1342a commit 1c9c0fe
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 44 deletions.
1 change: 1 addition & 0 deletions bin/cli/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function compile(option) {
return [2 /*return*/];
create_output(config);
if (option.watch) {
(0, watcher_1.watch_config)(config_file);
(0, watcher_1.watch_file)(config);
}
return [2 /*return*/];
Expand Down
42 changes: 34 additions & 8 deletions bin/functions/watcher.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.watch_file = void 0;
exports.watch_file = exports.watch_config = void 0;
var fs_1 = require("fs");
var compile_1 = require("../cli/compile");
var get_the_config_1 = require("../utils/get-the-config");
var validate_config_1 = require("../utils/validate-config");
var watched_files = [];
function watch_config(config_file) {
try {
(0, fs_1.watch)(process.cwd() + '/' + config_file, function (event, filename) {
if (filename) {
setTimeout(function () {
var config = (0, get_the_config_1.get_the_config)(config_file);
if (!config)
return;
var status = (0, validate_config_1.validate_config)(config);
if (!status)
return;
watch_file(config);
}, 250);
}
});
}
catch (error) { }
}
exports.watch_config = watch_config;
function watch_file(config) {
var files = config.files;
var langs = config.langs;
Expand All @@ -12,14 +34,18 @@ function watch_file(config) {
var file_names = Object.keys(files);
file_names.syncForEach(function (name, next_name) {
langs.syncForEach(function (lang, next_lang) {
try {
(0, fs_1.watch)(process.cwd() + '/' + config['input-dir'] + '/' + files[name] + '/' + lang + '.json', function (event, filename) {
if (filename) {
(0, compile_1.create_output)(config);
}
});
var file_name = process.cwd() + '/' + config['input-dir'] + '/' + name + '/' + lang + '.json';
if (watched_files.indexOf(file_name) == -1) {
watched_files.push(file_name);
try {
(0, fs_1.watch)(file_name, function (event, filename) {
if (filename) {
(0, compile_1.create_output)(config);
}
});
}
catch (error) { }
}
catch (error) { }
next_lang();
}, next_name);
});
Expand Down
6 changes: 4 additions & 2 deletions i18n/messages/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"test":"b",
"test":"selam",
"merhaba":"a",
"yumurta":"b"
"yumurta":"b",
"tost":"a",
"most":"asdfadsf"
}
3 changes: 3 additions & 0 deletions i18n/test/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test":"hello"
}
3 changes: 3 additions & 0 deletions i18n/test/tr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test":"merhaba"
}
3 changes: 2 additions & 1 deletion localize.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"input-dir": "i18n",
"output-file": "output.json",
"files": {
"messages": "messages"
"messages": "messages",
"test":"test"
},
"schema": "schema.json",
"global": true
Expand Down
15 changes: 1 addition & 14 deletions output.json
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
{
"en": {
"messages": {
"test": "b",
"merhaba": "a",
"yumurta": "b"
}
},
"tr": {
"messages": {
"test": "a"
}
}
}
{"en":{"messages":{"test":"selam","merhaba":"a","yumurta":"b","tost":"a","most":"asdfadsf"},"test":{"test":"hello"}},"tr":{"messages":{"test":"a"},"test":{"test":"merhaba"}}}
5 changes: 3 additions & 2 deletions src/cli/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { validate_config } from '../utils/validate-config';
import { read_lang_files } from '../functions/read-lang-files';
import { writeFileSync } from 'fs';
import { log_message } from '../utils/log-message';
import { watch_file } from '../functions/watcher';
import { watch_file,watch_config } from '../functions/watcher';

export async function create_output(config) {
var result = await read_lang_files(config);
Expand All @@ -19,6 +19,7 @@ export async function compile(option) {
if (!status) return;
create_output(config);
if (option.watch) {
watch_file(config);
watch_config(config_file);
watch_file(config);
}
}
54 changes: 38 additions & 16 deletions src/functions/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
import { watch } from 'fs';
import { create_output } from '../cli/compile';
import { config_type } from '../types/config-type';

export function watch_file(config: config_type) {
import { get_the_config } from '../utils/get-the-config';
import { validate_config } from '../utils/validate-config';

var watched_files:string[] = [];

export function watch_config(config_file) {
try {
watch(process.cwd() + '/' + config_file,
(event, filename) => {
if (filename) {
setTimeout(() => {
const config = get_the_config(config_file);
if (!config) return;
var status = validate_config(config);
if (!status) return;

watch_file(config)
}, 250);
}
});
} catch (error) {}
}

export function watch_file(config) {

var files = config.files;
var langs = config.langs;
if (config.global) {langs.push('global');}

var file_names = Object.keys(files);

file_names.syncForEach(function (name, next_name) {

langs.syncForEach(function (lang, next_lang) {

try {
watch(process.cwd() + '/' + config['input-dir'] + '/' + files[name] + '/' + lang + '.json', (event, filename) => {
if (filename) {
create_output(config);
}
});
} catch (error) {}

var file_name = process.cwd() + '/' + config['input-dir'] + '/' + name + '/' + lang + '.json';

if (watched_files.indexOf(file_name) == -1) {
watched_files.push(file_name);
try {
watch(file_name,
(event, filename) => {
if (filename) {
create_output(config);
}
});
} catch (error) {}
}


next_lang();
}, next_name);

});

}
1 change: 0 additions & 1 deletion test.log

This file was deleted.

0 comments on commit 1c9c0fe

Please sign in to comment.