Skip to content

Commit

Permalink
feat(chore): add base commands for a first version
Browse files Browse the repository at this point in the history
- add
- addLocale
- init
- pull
- push
- rm
- rmLocale
- status
  • Loading branch information
Pegase745 committed Jun 29, 2020
1 parent d506f76 commit db98111
Show file tree
Hide file tree
Showing 27 changed files with 2,072 additions and 78 deletions.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,100 @@
> A WebTranslateIt command-line tool in Node.js.
![Node.js CI](https://github.com/Pegase745/wti/workflows/Node.js%20CI/badge.svg?branch=master)

![screenshot](screenshot.png 'How to use wti')

# Usage

**Installation**

```sh
$ yarn global add wti
```

**Configuration**

Basically, `wti` is to be run on a project root directory, and looks for a `wti-config.json` file containing your project's informations.

The command `wti init` lets you create this file.

```sh
$ wti init
What is your project's api key?: <your private key>
Initializing...... [SUCCESS] Project is initialized
```
You can find the API token in your project settings.
**Usage**
Execute `wti help` to see the usage:
```sh
USAGE
$ wti [COMMAND]
COMMANDS
add create and push a new master language file
addLocale add a new locale to the project
help display help for wti
init configure the project to sync with
pull pull target language file(s)
push push master language file
rm delete a master language file from a project
See `wti help <command>` for more information on a specific command.
```
# Sample commands
| Command | Action |
| ---------------------------------- | ------------------------------------- |
| wti add <path/to/master/file.json> | Upload a new **master** language file |
| wti rm <path/to/master/file.json> | Delete a **master** language file |
| wti push | Update a **master** language file |
| wti pull | Download target language files |
| wti addLocale fr | Add a new locale to the project |
| wti rmLocale fr | Remove a locale from the project |
| wti status | View project statistics |
# i18next example
**1. Prerequisite**
- Create a project on WebTranslateIt with a source language (such as english)
- Bootstrap a React app
**2. Install and configure i18next**
```sh
$ yarn add i18next react-i18next
$ yarn add -D i18next-parser
// Extract locales
$ i18next --config i18next-parser.config.js
```
**3. Install and configure WTI client**
```sh
$ yarn add -D wti
$ wti init
$ wti add locale fr
$ ... add other languages
$ wti add locales/en/translation.json
$ wti push
```
**4. Translate keys on WebTranslateIt, and then pull translations**
```sh
$ wti pull
```
**5. Add or remove translation keys**
```sh
$ i18next --config i18next-parser.config.js
$ wti push
```
5 changes: 4 additions & 1 deletion bin/wti
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env node

require("../dist/index.js");
require('@oclif/command')
.run()
.then(require('@oclif/command/flush'))
.catch(require('@oclif/errors/handle'));
3 changes: 3 additions & 0 deletions bin/wti.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

node "%~dp0\run" %*
5 changes: 4 additions & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module.exports = {
'*.{js?(x),ts?(x)}': (filenames) => {
const spaceSeparatedFilenames = filenames.join(' ');
return [`eslint --quiet --fix ${spaceSeparatedFilenames}`];
return [
`import-sort --write ${spaceSeparatedFilenames}`,
`eslint --quiet --fix ${spaceSeparatedFilenames}`,
];
},
'*.json': ['prettier --write'],
'package.json': ['sort-package-json'],
Expand Down
39 changes: 37 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
"prebuild": "rimraf dist",
"build": "tsc",
"commit": "git-cz",
"dev": "ts-node src/index.ts",
"lint": "eslint . --ext .ts",
"prepublishOnly": "yarn build",
"prerelease": "yarn build",
"release": "semantic-release",
"start": "./bin/wti",
"test": "jest",
"test:coverage": "jest --coverage"
},
Expand All @@ -51,13 +49,29 @@
"path": "cz-conventional-changelog"
}
},
"dependencies": {
"@oclif/command": "^1.6.1",
"@oclif/config": "^1.15.1",
"@oclif/plugin-help": "^3.1.0",
"@oclif/plugin-not-found": "^1.2.4",
"@oclif/plugin-warn-if-update-available": "^1.7.0",
"cli-ux": "^5.4.6",
"form-data": "^3.0.0",
"git-root-dir": "^1.0.2",
"listr": "^0.14.3",
"make-error": "^1.3.6",
"node-fetch": "^3.0.0-beta.7"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@oclif/dev-cli": "^1.22.2",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.0.7",
"@types/git-root-dir": "^1.0.0",
"@types/jest": "^26.0.0",
"@types/listr": "^0.14.2",
"@types/node": "8",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
Expand All @@ -68,6 +82,9 @@
"eslint-plugin-jest": "^23.13.2",
"eslint-plugin-prettier": "^3.1.4",
"husky": "^4.2.5",
"import-sort-cli": "^6.0.0",
"import-sort-parser-typescript": "^6.0.0",
"import-sort-style-leboncoin": "^1.0.3",
"jest": "^26.0.1",
"jest-junit": "^10.0.0",
"lint-staged": "^10.2.11",
Expand All @@ -85,5 +102,23 @@
"preferGlobal": true,
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"importSort": {
".ts": {
"parser": "typescript",
"style": "leboncoin"
}
},
"oclif": {
"commands": "./dist/commands",
"bin": "wti",
"plugins": [
"@oclif/plugin-help",
"@oclif/plugin-not-found",
"@oclif/plugin-warn-if-update-available"
],
"warn-if-update-available": {
"timeoutInDays": 7
}
}
}
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';
import kleur from 'kleur';

import { MasterFile } from '../models';
import { MasterFileAlreadyExistsError } from '../errors';

export default class Add extends Command {
static description = 'create and push a new master language file';

static args = [{ name: 'masterFile' }];

static usage = '$ wti add locales/en/translation.json';

async run() {
const {
args: { masterFile },
} = this.parse(Add);

if (masterFile) {
cli.action.start(`Adding ${masterFile} as a master file...`);

try {
await MasterFile.create(masterFile);

cli.action.stop();

this.log(
`${kleur.green('[SUCCESS]')} Master file ${masterFile} is being added`
);
} catch (err) {
cli.action.stop();

if (err instanceof MasterFileAlreadyExistsError) {
this.log(
`${kleur.red('[ERROR]')} Master file ${masterFile} already exist`
);
}
}
} else {
this.log(`${kleur.red('[ERROR]')} Please specify a master file to add`);
}
}
}
35 changes: 35 additions & 0 deletions src/commands/addLocale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';
import kleur from 'kleur';

import { Locale } from '../models';

export default class AddLocale extends Command {
static description = 'add a new locale to the project';

static args = [{ name: 'locale' }];

static usage = '$ wti addLocale fr';

async run() {
const {
args: { locale },
} = this.parse(AddLocale);

if (locale) {
cli.action.start(`Adding ${locale} as a locale...`);

try {
await new Locale(locale).create();

cli.action.stop(
`${kleur.green('[SUCCESS]')} Locale ${locale} is being added`
);
} catch (err) {
cli.action.stop(`${kleur.red('[ERROR]')} ${err}`);
}
} else {
this.log(`${kleur.red('[ERROR]')} Please specify a locale to add`);
}
}
}
47 changes: 47 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';

import { ConfigNotFoundError } from '../errors';
import { getConfig, updateConfig, initialConfig } from '../helpers';

export default class Init extends Command {
static description = 'configure the project to sync with';

async run() {
let config = initialConfig;

try {
// get config object
config = await getConfig();
} catch (err) {
// if it doesn't exist, create an empty one
if (err instanceof ConfigNotFoundError) {
await updateConfig(config);
}
} finally {
// if apiKey is set, ask if it's ok to override it
if (config.project.apiKey) {
const override = await cli.confirm(
'You already have a project configured. Do you want to override it? (yes/no)'
);

// exit in case of no override
if (!override) this.exit();
}

// or else ask for apiKey and save it
const apiKey = await cli.prompt("What is your project's api key?");

cli.action.start('Initializing...');

await updateConfig({
...config,
project: {
apiKey,
},
});

cli.action.stop();
}
}
}
33 changes: 33 additions & 0 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Command } from '@oclif/command';
import kleur from 'kleur';
import Listr from 'listr';

import { Project, MasterFile } from '../models';

export default class Pull extends Command {
static description = 'pull target language file(s)';

async run() {
const { projectFiles } = await Project.init();

let masterFile = projectFiles.filter(
(file) => file.master_project_file_id === null
)[0],
masterFileId = masterFile.id;

const tasks = new Listr(
projectFiles.reduce(
(acc, current) =>
acc.concat({
title: `Pulling ${current.name}`,
task: async () => await new MasterFile(masterFileId).show(current),
}),
[] as Listr.ListrTask<Listr.ListrContext>[]
)
);

tasks.run().catch((err) => {
this.log(`${kleur.red('[ERROR]')} ${err}`);
});
}
}
27 changes: 27 additions & 0 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';

import { Project, MasterFile } from '../models';

export default class Push extends Command {
static description = 'push master language file';

async run() {
const { projectFiles } = await Project.init();

let masterFile = projectFiles.filter(
(file) => file.master_project_file_id === null
)[0];

cli.action.start(`Pushing master file ${masterFile.name}...`);

await new MasterFile(masterFile.id).update(
masterFile.name,
masterFile.locale_code
);

cli.action.stop(
'done. Note that it can take up to 1 min for 1000 segments to be uploaded'
);
}
}
Loading

0 comments on commit db98111

Please sign in to comment.