-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐ new(cli): squeeze and infuse commands
- Loading branch information
Showing
14 changed files
with
951 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as yargs from 'yargs' | ||
|
||
yargs | ||
.usage('Usage: $0 <command> [options]') | ||
.commandDir('./commands', { | ||
extensions: process.env.NODE_ENV === 'development' ? ['js', 'ts'] : ['js'] | ||
}) | ||
.demandCommand() | ||
.help() | ||
.version() | ||
.argv | ||
|
||
process.on('uncaughtException', err => { | ||
console.error(`uncaught exception: ${err}\n`) | ||
process.exit(1) | ||
}) | ||
|
||
process.on('unhandledRejection', (reason, p) => { | ||
console.error('unhandled rejection at:', p, 'reason:', reason) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Arguments, Argv } from 'yargs' | ||
|
||
import { resolve, readSFC } from '../utils' | ||
import infuse from '../infuser' | ||
import fs from 'fs' | ||
import { LocaleMessages, SFCFileInfo } from '../../types' | ||
|
||
type InfuseOptions = { | ||
target: string | ||
messages: string | ||
} | ||
|
||
export const command = 'infuse' | ||
export const aliases = 'inf' | ||
export const describe = 'infuse locale messages to single-file components' | ||
|
||
export const builder = (args: Argv): Argv<InfuseOptions> => { | ||
return args | ||
.option('target', { | ||
type: 'string', | ||
alias: 't', | ||
describe: 'target path that single-file components is stored', | ||
demandOption: true | ||
}) | ||
.option('messages', { | ||
type: 'string', | ||
alias: 'o', | ||
describe: 'locale messages path to be infused', | ||
demandOption: true | ||
}) | ||
} | ||
|
||
export const handler = (args: Arguments<InfuseOptions>): void => { | ||
const targetPath = resolve(args.target) | ||
const messagesPath = resolve(args.messages) | ||
const newSources = infuse(targetPath, readSFC(targetPath), readLocaleMessages(messagesPath)) | ||
writeSFC(newSources) | ||
} | ||
|
||
function readLocaleMessages (path: string): LocaleMessages { | ||
// TODO: async implementation | ||
const data = fs.readFileSync(path, { encoding: 'utf8' }) | ||
return JSON.parse(data) as LocaleMessages | ||
} | ||
|
||
function writeSFC (sources: SFCFileInfo[]) { | ||
// TODO: async implementation | ||
sources.forEach(({ path, content }) => { | ||
fs.writeFileSync(path, content) | ||
}) | ||
} | ||
|
||
export default { | ||
command, | ||
aliases, | ||
describe, | ||
builder, | ||
handler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Arguments, Argv } from 'yargs' | ||
import { LocaleMessages } from '../../types' | ||
|
||
import { resolve, readSFC } from '../utils' | ||
import squeeze from '../squeezer' | ||
import fs from 'fs' | ||
|
||
type SqueezeOptions = { | ||
target: string | ||
output: string | ||
} | ||
|
||
export const command = 'squeeze' | ||
export const aliases = 'sqz' | ||
export const describe = 'squeeze locale messages from single-file components' | ||
|
||
export const builder = (args: Argv): Argv<SqueezeOptions> => { | ||
const outputDefault = `${process.cwd()}/messages.json` | ||
return args | ||
.option('target', { | ||
type: 'string', | ||
alias: 't', | ||
describe: 'target path that single-file components is stored', | ||
demandOption: true | ||
}) | ||
.option('output', { | ||
type: 'string', | ||
alias: 'o', | ||
default: outputDefault, | ||
describe: 'path to output squeezed locale messages' | ||
}) | ||
} | ||
|
||
export const handler = (args: Arguments<SqueezeOptions>): void => { | ||
const targetPath = resolve(args.target) | ||
const messages = squeeze(targetPath, readSFC(targetPath)) | ||
writeLocaleMessages(resolve(args.output), messages) | ||
} | ||
|
||
function writeLocaleMessages (output: string, messages: LocaleMessages) { | ||
// TODO: async implementation | ||
fs.writeFileSync(output, JSON.stringify(messages, null, 2)) | ||
} | ||
|
||
export default { | ||
command, | ||
aliases, | ||
describe, | ||
builder, | ||
handler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`infuse command output help 1`] = ` | ||
"processChild.js infuse | ||
infuse locale messages to single-file components | ||
Options: | ||
--version Show version number [boolean] | ||
--help Show help [boolean] | ||
--target, -t target path that single-file components is stored | ||
[string] [required] | ||
--messages, -o locale messages path to be infused [string] [required]" | ||
`; | ||
|
||
exports[`squeeze command output help 1`] = ` | ||
"processChild.js squeeze | ||
squeeze locale messages from single-file components | ||
Options: | ||
--version Show version number [boolean] | ||
--help Show help [boolean] | ||
--target, -t target path that single-file components is stored | ||
[string] [required] | ||
--output, -o path to output squeezed locale messages | ||
[string] [default: \\"/path/to/project1/messages.json\\"]" | ||
`; | ||
|
||
exports[`top output help 1`] = ` | ||
"Usage: processChild.js <command> [options] | ||
Commands: | ||
processChild.js squeeze squeeze locale messages from single-file components | ||
[aliases: sqz] | ||
processChild.js infuse infuse locale messages to single-file components | ||
[aliases: inf] | ||
Options: | ||
--version Show version number [boolean] | ||
--help Show help [boolean]" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as yargs from 'yargs' | ||
import squeeze from '../src/commands/squeeze' | ||
import infuse from '../src/commands/infuse' | ||
|
||
let orgCwd | ||
beforeEach(async () => { | ||
orgCwd = process.cwd | ||
process.cwd = jest.fn(() => '/path/to/project1') | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
process.cwd = orgCwd | ||
}) | ||
|
||
test('top output help', async () => { | ||
const cmd = yargs | ||
.usage('Usage: $0 <command> [options]') | ||
.command(squeeze) | ||
.command(infuse) | ||
.demandCommand() | ||
.locale('en') | ||
.help() | ||
const output = await new Promise(resolve => { | ||
// eslint-disable-next-line handle-callback-err | ||
cmd.parse('--help', (err, argv, output) => { | ||
resolve(output) | ||
}) | ||
}) | ||
|
||
expect(output).toMatchSnapshot() | ||
}) | ||
|
||
test('squeeze command output help', async () => { | ||
const cmd = yargs | ||
.command(squeeze) | ||
.locale('en') | ||
.help() | ||
const output = await new Promise(resolve => { | ||
// eslint-disable-next-line handle-callback-err | ||
cmd.parse('squeeze --help', (err, argv, output) => { | ||
resolve(output) | ||
}) | ||
}) | ||
|
||
expect(output).toMatchSnapshot() | ||
}) | ||
|
||
test('infuse command output help', async () => { | ||
const cmd = yargs | ||
.command(infuse) | ||
.locale('en') | ||
.help() | ||
const output = await new Promise(resolve => { | ||
// eslint-disable-next-line handle-callback-err | ||
cmd.parse('infuse --help', (err, argv, output) => { | ||
resolve(output) | ||
}) | ||
}) | ||
|
||
expect(output).toMatchSnapshot() | ||
}) |
Oops, something went wrong.