Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
- Watcher feature added.
  • Loading branch information
gerceker committed Aug 8, 2023
1 parent d850b33 commit 2f1342a
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 21 deletions.
37 changes: 25 additions & 12 deletions bin/cli/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,19 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = void 0;
exports.compile = exports.create_output = void 0;
var get_the_config_1 = require("../utils/get-the-config");
var validate_config_1 = require("../utils/validate-config");
var read_lang_files_1 = require("../functions/read-lang-files");
var fs_1 = require("fs");
var log_message_1 = require("../utils/log-message");
function compile(option) {
var watcher_1 = require("../functions/watcher");
function create_output(config) {
return __awaiter(this, void 0, void 0, function () {
var config_file, config, status, result;
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
config_file = option.config;
config = (0, get_the_config_1.get_the_config)(config_file);
if (!config)
return [2 /*return*/];
status = (0, validate_config_1.validate_config)(config);
if (!status)
return [2 /*return*/];
return [4 /*yield*/, (0, read_lang_files_1.read_lang_files)(config)];
case 0: return [4 /*yield*/, (0, read_lang_files_1.read_lang_files)(config)];
case 1:
result = _a.sent();
(0, log_message_1.log_message)('success', 'compiling_successfully', { output_file: config['output-file'] });
Expand All @@ -65,4 +58,24 @@ function compile(option) {
});
});
}
exports.create_output = create_output;
function compile(option) {
return __awaiter(this, void 0, void 0, function () {
var config_file, config, status;
return __generator(this, function (_a) {
config_file = option.config;
config = (0, get_the_config_1.get_the_config)(config_file);
if (!config)
return [2 /*return*/];
status = (0, validate_config_1.validate_config)(config);
if (!status)
return [2 /*return*/];
create_output(config);
if (option.watch) {
(0, watcher_1.watch_file)(config);
}
return [2 /*return*/];
});
});
}
exports.compile = compile;
27 changes: 27 additions & 0 deletions bin/functions/watcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.watch_file = void 0;
var fs_1 = require("fs");
var compile_1 = require("../cli/compile");
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 {
(0, fs_1.watch)(process.cwd() + '/' + config['input-dir'] + '/' + files[name] + '/' + lang + '.json', function (event, filename) {
if (filename) {
(0, compile_1.create_output)(config);
}
});
}
catch (error) { }
next_lang();
}, next_name);
});
}
exports.watch_file = watch_file;
1 change: 1 addition & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ var compile_1 = require("./cli/compile");
program.command('compile')
.description('Converts the files in the input folder into a single file output.')
.requiredOption('-c,--config <char>', 'config file.')
.option('-w, --watch', 'Automatically saves changes.')
.action(compile_1.compile);
program.parse();
4 changes: 3 additions & 1 deletion i18n/messages/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"test":"b"
"test":"b",
"merhaba":"a",
"yumurta":"b"
}
15 changes: 14 additions & 1 deletion output.json
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
{"en":{"messages":{"test":"b"}},"tr":{"messages":{"test":"a"}}}
{
"en": {
"messages": {
"test": "b",
"merhaba": "a",
"yumurta": "b"
}
},
"tr": {
"messages": {
"test": "a"
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "localizer-js",
"version": "1.0.6-beta",
"version": "1.1.0-beta",
"description": "Language support development tool for apps.",
"main": "./bin/index.js",
"scripts": {
Expand Down
17 changes: 11 additions & 6 deletions src/cli/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ 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';

export async function create_output(config) {
var result = await read_lang_files(config);
log_message('success','compiling_successfully' , {output_file:config['output-file']});
writeFileSync(process.cwd() + '/' + config['output-file'],JSON.stringify(result));
}

export async function compile(option) {

const config_file = option.config;
const config = get_the_config(config_file);
if (!config) return;
var status = validate_config(config);
if (!status) return;
var result = await read_lang_files(config);


log_message('success','compiling_successfully' , {output_file:config['output-file']});
writeFileSync(process.cwd() + '/' + config['output-file'],JSON.stringify(result));
create_output(config);
if (option.watch) {
watch_file(config);
}
}
32 changes: 32 additions & 0 deletions src/functions/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { watch } from 'fs';
import { create_output } from '../cli/compile';
import { config_type } from '../types/config-type';

export function watch_file(config: config_type) {

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) {}



next_lang();
}, next_name);

});

}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ const program = new Command();
import {compile} from './cli/compile';
// commands



program.command('compile')
.description('Converts the files in the input folder into a single file output.')
.requiredOption('-c,--config <char>', 'config file.')
.option('-w, --watch','Automatically saves changes.')
.action(compile);



program.parse();
1 change: 1 addition & 0 deletions test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dasf

0 comments on commit 2f1342a

Please sign in to comment.