Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
feat(i18n): set up i18n plus baseline en.json locale
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jun 10, 2017
1 parent c50a398 commit b67bb3a
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 22 deletions.
10 changes: 6 additions & 4 deletions auto-fallback.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict'

const Y = require('./y.js')

function mkPosix (opts) {
return `
command_not_found_${opts.isBash ? 'handle' : 'handler'}() {
# Do not run within a pipe
if test ! -t 1; then
>&2 echo "command not found: $1"
>&2 echo "${Y`command not found: ${'$1'}`}"
return 127
fi
echo "$1 not found. Trying with npx..." >&2
echo "${`Y${'$1'} not found. Trying with npx...`}" >&2
if ! [[ $1 =~ @ ]]; then
npx --no-install "$@"
else
Expand All @@ -25,7 +27,7 @@ function __fish_command_not_found_on_interactive --on-event fish_prompt
functions --erase __fish_command_not_found_setup
function __fish_command_not_found_handler --on-event fish_command_not_found
echo "$argv[1] not found. Trying with npx..." >&2
echo "${Y`${'$argv[1]'} not found. Trying with npx...`}" >&2
if string match -q -r @ $argv[1]
npx $argv
else
Expand Down Expand Up @@ -55,5 +57,5 @@ function autoFallback (shell, fromEnv, opts) {
return autoFallback(fromEnv, null, opts)
}

console.error('Only Bash, Zsh, and Fish shells are supported :(')
console.error(Y`Only Bash, Zsh, and Fish shells are supported :(`)
}
5 changes: 3 additions & 2 deletions child.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ const BB = require('bluebird')

const cp = require('child_process')
const path = require('path')
const Y = require('./y.js')

module.exports.runCommand = runCommand
function runCommand (cmdPath, cmdOpts, opts) {
return spawn(cmdPath, cmdOpts, {
shell: opts.shell || !!opts.call,
stdio: opts.stdio || 'inherit'
}).catch({code: 'ENOENT'}, () => {
const err = new Error(`npx: command not found: ${path.basename(cmdPath)}`)
const err = new Error(Y`npx: command not found: ${path.basename(cmdPath)}`)
err.exitCode = 127
throw err
})
Expand All @@ -24,7 +25,7 @@ function spawn (cmd, args, opts) {
child.on('error', cb)
child.on('close', code => {
if (code) {
const err = new Error(`Command failed: ${cmd} ${args.join(' ')}`)
const err = new Error(Y`Command failed: ${cmd} ${args.join(' ')}`)
err.exitCode = code
cb(err)
} else {
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const pkg = require('./package.json')
let rimraf
const updateNotifier = require('update-notifier')
const which = BB.promisify(require('which'))
const Y = require('./y.js')

const PATH_SEP = process.platform === 'win32' ? ';' : ':'

Expand Down Expand Up @@ -145,7 +146,7 @@ function installPackages (specs, prefix, npmOpts) {
stdio: [0, 'ignore', 2]
}).catch(err => {
if (err.exitCode) {
err.message = `Install for ${specs} failed with code ${err.exitCode}`
err.message = Y`Install for ${specs} failed with code ${err.exitCode}`
}
throw err
})
Expand Down
20 changes: 20 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Execute binaries from npm packages.\n%s": "Execute binaries from npm packages.\n%s",
"Package to be installed.": "Package to be installed.",
"Location of the npm cache.": "Location of the npm cache.",
"Skip installation if a package is missing.": "Skip installation if a package is missing.",
"Path to user npmrc.": "Path to user npmrc.",
"Execute string as if inside `npm run-script`.": "Execute string as if inside `npm run-script`.",
"Shell to execute the command with, if any.": "Shell to execute the command with, if any.",
"Generate shell code to use npx as the \"command not found\" fallback.": "Generate shell code to use npx as the \"command not found\" fallback.",
"Ignores existing binaries in $PATH, or in the local project. This forces npx to do a temporary install and use the latest version.": "Ignores existing binaries in $PATH, or in the local project. This forces npx to do a temporary install and use the latest version.",
"npm binary to use for internal operations.": "npm binary to use for internal operations.",
"For the full documentation, see the manual page for npx(1).": "For the full documentation, see the manual page for npx(1).",
"Unable to guess a binary name from . Please use --package.": "Unable to guess a binary name from . Please use --package.",
"\nERROR: You must supply a command.\n": "\nERROR: You must supply a command.\n",
"Command failed: %s %s": "Command failed: %s %s",
"Install for %s failed with code %s": "Install for %s failed with code %s",
"Command failed: %s ": "Command failed: %s ",
"%s not found. Trying with npx...": "%s not found. Trying with npx...",
"command not found: %s": "command not found: %s"
}
34 changes: 33 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"postrelease": "npm publish && git push --follow-tags",
"pretest": "standard",
"release": "standard-version -s",
"test": "tap -J --coverage test/*.js",
"test": "cross-env NPX_UPDATE_LOCALE_FILES=true LC_ALL=en nyc --all -- tap -J test/*.js",
"update-coc": "weallbehave -o . && git add CODE_OF_CONDUCT.md && git commit -m 'docs(coc): updated CODE_OF_CONDUCT.md'",
"update-contrib": "weallcontribute -o . && git add CONTRIBUTING.md && git commit -m 'docs(contributing): updated CONTRIBUTING.md'"
},
Expand All @@ -42,6 +42,7 @@
"rimraf": "^2.6.1",
"update-notifier": "^2.2.0",
"which": "^1.2.14",
"y18n": "^3.2.1",
"yargs": "^8.0.1"
},
"bundleDependencies": [
Expand All @@ -52,9 +53,11 @@
"update-notifier",
"which",
"yargs",
"dotenv"
"dotenv",
"y18n"
],
"devDependencies": {
"cross-env": "^5.0.1",
"marked-man": "^0.2.1",
"mkdirp": "^0.5.1",
"nyc": "^11.0.2",
Expand Down
25 changes: 13 additions & 12 deletions parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const npa = require('npm-package-arg')
const path = require('path')
const yargs = require('yargs')
const Y = require('./y.js')

const usage = `
$0 [options] <command>[@version] [command-arg]...
Expand All @@ -18,56 +19,56 @@ module.exports = parseArgs
function parseArgs (argv) {
argv = argv || process.argv
const parser = yargs
.usage(`Execute binaries from npm packages.\n${usage}`)
.usage(Y`Execute binaries from npm packages.\n${usage}`)
.option('package', {
alias: 'p',
type: 'string',
describe: 'Package to be installed.'
describe: Y`Package to be installed.`
})
.option('cache', {
type: 'string',
describe: 'Location of the npm cache.'
describe: Y`Location of the npm cache.`
})
.option('install', {
type: 'boolean',
describe: 'Skip installation if a package is missing.',
describe: Y`Skip installation if a package is missing.`,
default: true
})
.option('userconfig', {
type: 'string',
describe: 'Path to user npmrc.'
describe: Y`Path to user npmrc.`
})
.option('call', {
alias: 'c',
type: 'string',
describe: 'Execute string as if inside `npm run-script`.'
describe: Y`Execute string as if inside \`npm run-script\`.`
})
.option('shell', {
alias: 's',
type: 'string',
describe: 'Shell to execute the command with, if any.',
describe: Y`Shell to execute the command with, if any.`,
default: false
})
.option('shell-auto-fallback', {
choices: ['', 'bash', 'fish', 'zsh'],
describe: 'Generate shell code to use npx as the "command not found" fallback.',
describe: Y`Generate shell code to use npx as the "command not found" fallback.`,
requireArg: false,
type: 'string'
})
.option('ignore-existing', {
describe: 'Ignores existing binaries in $PATH, or in the local project. This forces npx to do a temporary install and use the latest version.',
describe: Y`Ignores existing binaries in $PATH, or in the local project. This forces npx to do a temporary install and use the latest version.`,
type: 'boolean'
})
.option('npm', {
describe: 'npm binary to use for internal operations.',
describe: Y`npm binary to use for internal operations.`,
type: 'string',
default: path.resolve(__dirname, 'node_modules', '.bin', 'npm')
})
.version()
.alias('version', 'v')
.help()
.alias('help', 'h')
.epilogue('For the full documentation, see the manual page for npx(1).')
.epilogue(Y`For the full documentation, see the manual page for npx(1).`)

const opts = parser.getOptions()
const bools = new Set(opts.boolean)
Expand Down Expand Up @@ -165,6 +166,6 @@ function guessCmdName (spec) {
return path.basename(spec.fetchSpec, ext).replace(/-\d+\.\d+\.\d+(?:-[a-z0-9.\-+]+)?$/i, '')
}

console.error(`Unable to guess a binary name from ${spec.raw}. Please use --package.`)
console.error(Y`Unable to guess a binary name from ${spec.raw}. Please use --package.`)
return null
}
22 changes: 22 additions & 0 deletions y.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const path = require('path')
const yargs = require('yargs')
const y18n = require('y18n')({
directory: path.join(__dirname, 'locales'),
locale: yargs.locale(),
updateFiles: process.env.NPX_UPDATE_LOCALE_FILES === 'true'
})

module.exports = yTag
function yTag (parts) {
let str = ''
parts.forEach((part, i) => {
const arg = arguments[i + 1]
str += part
if (arg) {
str += '%s'
}
})
return y18n.__.apply(null, [str].concat([].slice.call(arguments, 1)))
}

0 comments on commit b67bb3a

Please sign in to comment.