diff --git a/bin/cli/compile.js b/bin/cli/compile.js index 5099591..54a75c6 100644 --- a/bin/cli/compile.js +++ b/bin/cli/compile.js @@ -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'] }); @@ -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; diff --git a/bin/functions/watcher.js b/bin/functions/watcher.js new file mode 100644 index 0000000..d454394 --- /dev/null +++ b/bin/functions/watcher.js @@ -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; diff --git a/bin/index.js b/bin/index.js index fb5179e..0591ecd 100644 --- a/bin/index.js +++ b/bin/index.js @@ -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 ', 'config file.') + .option('-w, --watch', 'Automatically saves changes.') .action(compile_1.compile); program.parse(); diff --git a/i18n/messages/en.json b/i18n/messages/en.json index 3d7dd95..fcad6ae 100644 --- a/i18n/messages/en.json +++ b/i18n/messages/en.json @@ -1,3 +1,5 @@ { - "test":"b" + "test":"b", + "merhaba":"a", + "yumurta":"b" } \ No newline at end of file diff --git a/output.json b/output.json index 038c997..02505fc 100644 --- a/output.json +++ b/output.json @@ -1 +1,14 @@ -{"en":{"messages":{"test":"b"}},"tr":{"messages":{"test":"a"}}} \ No newline at end of file +{ + "en": { + "messages": { + "test": "b", + "merhaba": "a", + "yumurta": "b" + } + }, + "tr": { + "messages": { + "test": "a" + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index ff5eb12..f18217e 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/cli/compile.ts b/src/cli/compile.ts index acc9f82..d89edf9 100644 --- a/src/cli/compile.ts +++ b/src/cli/compile.ts @@ -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); + } } \ No newline at end of file diff --git a/src/functions/watcher.ts b/src/functions/watcher.ts new file mode 100644 index 0000000..49d373f --- /dev/null +++ b/src/functions/watcher.ts @@ -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); + + }); + +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 0046c61..f6414de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 ', 'config file.') + .option('-w, --watch','Automatically saves changes.') .action(compile); + + program.parse(); diff --git a/test.log b/test.log new file mode 100644 index 0000000..17ccb08 --- /dev/null +++ b/test.log @@ -0,0 +1 @@ +dasf \ No newline at end of file