Skip to content

Commit

Permalink
Merge pull request #54 from sibelius/feat/cli
Browse files Browse the repository at this point in the history
feat(cli): add ast-i18n cli to make life easier, see #45
  • Loading branch information
sibelius authored Mar 12, 2019
2 parents 8f1d265 + b4407f6 commit ac0f925
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ npm-debug.log
dump.rdb
bundle.js

lib
build
dist
coverage
Expand Down
3 changes: 3 additions & 0 deletions bin/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib/cli.js').run();
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "ast-i18n",
"version": "1.0.0",
"main": "index.js",
"author": "Sibelius Seraphini <sibeliusseraphini@gmai.com> (https://github.com/sibelius)",
"license": "MIT",
"bin": {
"ast-i18n": "./bin/cli"
},
"dependencies": {
"@types/prettier": "^1.16.0",
"ast-types": "^0.12.2",
Expand Down Expand Up @@ -50,10 +51,15 @@
"jest": "^24.1.0",
"jscodeshift": "^0.6.3"
},
"license": "MIT",
"main": "index.js",
"scripts": {
"b": "babel-node --extensions \".es6,.js,.es,.jsx,.mjs,.ts,.tsx\"",
"start": "yarn b src/index.ts",
"build": "rm -rf lib/* && babel src --extensions \".es6,.js,.es,.jsx,.mjs,.ts,.tsx\" --ignore __test__,__testfixtures__,*.spec.ts --out-dir lib",
"fixtures": "yarn start --src=fixtures",
"test": "jest"
"prepublish": "npm run build",
"start": "yarn b src/index.ts",
"test": "jest",
"watch": "babel --extensions \".es6,.js,.es,.jsx,.mjs,.ts,.tsx\" -w -d ./lib ./src"
}
}
31 changes: 31 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import yargs from 'yargs';
import shell from 'shelljs';

import { generateResources } from './generateResources';

type Argv = {
src: string,
keyMaxLength: number,
}

export const run = (argv: Argv) => {
argv = yargs(argv || process.argv.slice(2))
.usage(
'Extract all string inside JSXElement'
)
.default('src', process.cwd())
.describe(
'src',
'The source to collect strings'
)
.default('keyMaxLength', 40)
.describe(
'src',
'The source to collect strings'
)
.argv;

const jsFiles = shell.find(argv.src).filter(path => /\.(js|ts|tsx)$/.test(path));

generateResources(jsFiles, argv.keyMaxLength);
};
3 changes: 3 additions & 0 deletions src/generateResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ export const generateResources = (files: string[], keyMaxLength: number = 40) =>

fs.writeFileSync('resource.tsx', resource(i18nMap));

// tslint:disable-next-line
console.log('generate resource file: resource.tsx');

return i18nMap;
};

0 comments on commit ac0f925

Please sign in to comment.