diff --git a/bin/lcui.js b/bin/lcui.js index b269684..a62f7ca 100755 --- a/bin/lcui.js +++ b/bin/lcui.js @@ -26,7 +26,7 @@ program .action(wrapAction(create)); program - .command('generate ') + .command('generate [name]') .description('generate files with template of specified type') .action(wrapAction(generate)); diff --git a/generators/i18n/index.js b/generators/i18n/index.js new file mode 100644 index 0000000..f260deb --- /dev/null +++ b/generators/i18n/index.js @@ -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; diff --git a/test/fixtures/i18n/config.js b/generators/i18n/templates/i18n.js similarity index 100% rename from test/fixtures/i18n/config.js rename to generators/i18n/templates/i18n.js diff --git a/generators/makefile/index.js b/generators/makefile/index.js index 8422f74..2a7ffc8 100644 --- a/generators/makefile/index.js +++ b/generators/makefile/index.js @@ -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'); diff --git a/generators/view/index.js b/generators/view/index.js index a9184cb..57d16d9 100644 --- a/generators/view/index.js +++ b/generators/view/index.js @@ -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), diff --git a/generators/widget/index.js b/generators/widget/index.js index d41140d..f52cea7 100644 --- a/generators/widget/index.js +++ b/generators/widget/index.js @@ -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), diff --git a/lib/generator.js b/lib/generator.js index c5df6b4..8cc513b 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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) { diff --git a/test/i18n.test.js b/test/i18n.test.js index 2a64b4e..58bf96b 100644 --- a/test/i18n.test.js +++ b/test/i18n.test.js @@ -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); });