Skip to content

Commit

Permalink
refactor(*): Convierte en ESM y refactoriza parseado de proyectos par…
Browse files Browse the repository at this point in the history
…a detectar locales #52
  • Loading branch information
lupomontero committed Aug 17, 2022
1 parent 941eab7 commit a6581fe
Show file tree
Hide file tree
Showing 6 changed files with 794 additions and 153 deletions.
35 changes: 15 additions & 20 deletions cmd/help.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
const help = ({ pkg, commands }) => `
export const cmd = async ({ pkg, commands }) => `
Usage: ${pkg.name} [command] [options]
Commands:
${Object.keys(commands)
.map(
cmdName => (
` ${cmdName} ${
(commands[cmdName].args || [])
.map(arg => (
(arg.required)
? `<${arg.name}>`
: `[${arg.name}]`
))
.join(' ')
} ${
(commands[cmdName].options || [])
.map(opt => (
(opt.required)
? `--${opt.name}`
: `[--${opt.name}]`
))
.join(' ')
` ${cmdName} ${(commands[cmdName].args || [])
.map(arg => (
(arg.required)
? `<${arg.name}>`
: `[${arg.name}]`
))
.join(' ')
} ${(commands[cmdName].options || [])
.map(opt => (
(opt.required)
? `--${opt.name}`
: `[--${opt.name}]`
))
.join(' ')
}`
),
)
Expand All @@ -32,6 +30,3 @@ Global options:
-h, --help Show help
-V Show version
`;


module.exports = app => Promise.resolve(help(app));
16 changes: 9 additions & 7 deletions cmd/project.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const path = require('path');
const project = require('../lib/project');
import path from 'node:path';
import { parseProject } from '../lib/project.js';

module.exports = app => project(path.resolve(app.args.shift()), app.opts);
export const cmd = ({ args, opts, pkg }) => parseProject(
path.resolve(args.shift()),
opts,
pkg,
);

module.exports.args = [
export const args = [
{ name: 'dir', required: true },
];

module.exports.options = [
export const options = [
{ name: 'repo', required: true },
{ name: 'version', required: true },
{ name: 'locale', required: true },
{ name: 'track', required: true },
{ name: 'lo', required: false },
{ name: 'suffix', required: false },
];
62 changes: 30 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#! /usr/bin/env node

const minimist = require('minimist');
const chalk = require('chalk');
const { hasOwnProperty } = require('./lib/common');
const pkg = require('./package.json');


const commands = [
'help',
'topic',
'project',
].reduce((memo, key) => ({
...memo,
[key]: require(`./cmd/${key.split(':').join('/')}`),
}), {});

import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import chalk from 'chalk';
import minimist from 'minimist';
import * as help from './cmd/help.js';
import * as project from './cmd/project.js';
// import * as topic from './cmd/topic.js';

const commands = {
help,
// topic,
project,
};

const success = (value) => {
if (value) {
Expand All @@ -23,7 +21,6 @@ const success = (value) => {
process.exit(0);
};


const error = (err) => {
if (err.path) {
console.error(err.path);
Expand All @@ -42,21 +39,20 @@ const error = (err) => {
process.exit(1);
};


module.exports = (args, opts) => {
export const main = async (args, opts) => {
const pkg = JSON.parse(await fs.readFile('./package.json'));
const cmdName = (opts.h || opts.help) ? 'help' : args.shift() || 'help';

if (opts.V) {
return success(pkg.version);
return pkg.version;
}

if (!hasOwnProperty(commands, cmdName)) {
return error('Unkown command');
if (typeof commands[cmdName]?.cmd !== 'function') {
throw new Error('Unkown command');
}

if (opts.h || opts.help || cmdName === 'help') {
return commands.help({ pkg, commands })
.then(success, error);
return commands.help.cmd({ pkg, commands });
}

const requiredArgs = (commands[cmdName].args || []).reduce(
Expand All @@ -65,21 +61,23 @@ module.exports = (args, opts) => {
);

if (args.length < requiredArgs) {
return error('Insufficient arguments');
throw new Error('Insufficient arguments');
}

return commands[cmdName]({
return commands[cmdName].cmd({
pkg,
commands,
args,
opts,
})
.then(success)
.catch(error);
});
};


if (require.main === module) {
const { _: args, ...opts } = minimist(process.argv.slice(2));
module.exports(args, opts);
if (import.meta.url.startsWith('file:')) {
const modulePath = fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
const { _: args, ...opts } = minimist(process.argv.slice(2));
main(args, opts)
.then(success)
.catch(error);
}
}
Loading

0 comments on commit a6581fe

Please sign in to comment.