Skip to content

Commit

Permalink
feat: add i18n config file generator
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed May 10, 2021
1 parent 69e698c commit fe4702a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/lcui.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ program
.action(wrapAction(create));

program
.command('generate <type> <name>')
.command('generate <type> [name]')
.description('generate files with template of specified type')
.action(wrapAction(generate));

Expand Down
35 changes: 35 additions & 0 deletions generators/i18n/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');

class Generator {
constructor(name, options) {
this.name = name;
this.cwd = options.cwd;
this.templatesDir = path.join(__dirname, 'templates');
this.configDir = path.resolve(this.cwd, 'config');
if (!fs.existsSync(this.configDir)) {
throw new Error(`${this.configDir} is not exists!`);
}
}

writeFile(file, content) {
console.log(chalk.green('writing'), path.relative(this.cwd, file));
return fs.writeFileSync(file, content);
}

readTemplateFile(name) {
const templateFile = path.join(this.templatesDir, name);
return fs.readFileSync(templateFile, { encoding: 'utf-8' });
}

generateFile(input, output) {
this.writeFile(output, this.readTemplateFile(input));
}

generate() {
this.generateFile('i18n.js', path.join(this.configDir, 'i18n.js'));
}
}

module.exports = Generator;
File renamed without changes.
2 changes: 1 addition & 1 deletion generators/makefile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const xmakeFile = 'xmake.lua';

class Generator {
constructor(name, options) {
this.name = name;
this.name = ['xmake', 'cmake'].includes(name) ? name : 'cmake';
this.cwd = options.cwd;
this.templatesDir = path.join(__dirname, 'templates');
this.sourceDir = path.join(this.cwd, 'src');
Expand Down
3 changes: 2 additions & 1 deletion generators/view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const { snakeCase, pascalCase, paramCase } = require('change-case');
const { format } = require('../../lib/utils');

class Generator {
constructor(name, options) {
constructor(inputName, options) {
const name = inputName || 'view';
this.data = {
className: `${pascalCase(name)}View`,
variableName: snakeCase(name),
Expand Down
3 changes: 2 additions & 1 deletion generators/widget/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const { snakeCase, pascalCase, paramCase } = require('change-case');
const { format } = require('../../lib/utils');

class Generator {
constructor(name, options) {
constructor(inputName, options) {
const name = inputName || 'widget';
this.data = {
className: pascalCase(name),
variableName: snakeCase(name),
Expand Down
4 changes: 3 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const WidgetGenerator = require('../generators/widget');
const ViewGenerator = require('../generators/view');
const MakefileGenerator = require('../generators/makefile');
const I18nGenerator = require('../generators/i18n');

const generators = {
widget: WidgetGenerator,
view: ViewGenerator,
makefile: MakefileGenerator
makefile: MakefileGenerator,
i18n: I18nGenerator
};

function generate(type, name) {
Expand Down
3 changes: 2 additions & 1 deletion test/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ describe('i18n', () => {
fs.emptyDirSync(path.join(projectDir, 'src'));
fs.mkdirSync(path.join(projectDir, 'src', 'lib'));
fs.copyFileSync(path.join(cwd, 'app.c'), path.join(projectDir, 'src', 'app.c'));
fs.copyFileSync(path.join(cwd, 'config.js'), path.join(projectDir, 'config', 'i18n.js'));
});
it('the configuration file should be compiled successfully', function () {
this.timeout(10000);
assert.strictEqual(projectCreated, true);
execSync('lcui generate i18n', { cwd: projectDir });
execSync('lcui compile i18n', { cwd: projectDir });
assert.strictEqual(fs.existsSync(path.join(projectDir, 'config', 'i18n.js')), true);
assert.strictEqual(fs.existsSync(path.join(projectDir, 'src', 'lib', 'i18n.c')), true);
assert.strictEqual(fs.existsSync(path.join(projectDir, 'src', 'lib', 'i18n.h')), true);
});
Expand Down

0 comments on commit fe4702a

Please sign in to comment.