Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/eslint and prettier #35

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: GitHub Actions
run-name: Running necessary checks 🚀
on: [push]
jobs:
Code-Checks:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- name: 🚚 Installing dependencies
run: yarn
- name: 🏷️ running typecheck
run: yarn build
- name: 🧪 Running tests
run: yarn test
- name: 🚨 Linting code
run: yarn lint
- name: 🚨 prettier check code
run: yarn format:check
14 changes: 7 additions & 7 deletions .locorc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/** @type {import('loco-cli/types').Config} */
module.exports = {
accessKey: "<access-key>",
localesDir: "public/locales",
accessKey: '<access-key>',
localesDir: 'public/locales',
namespaces: true,
push: {
"flag-new": "provisional",
"tag-new": process.env.npm_package_version,
"delete-absent": false,
'flag-new': 'provisional',
'tag-new': process.env.npm_package_version,
'delete-absent': false
},
pull: {
fallback: "en",
},
fallback: 'en'
}
};
11 changes: 11 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Ignore everything except the code in src/.
# Based on https://stackoverflow.com/a/70715829/458193
*
!src/**/*.js
!src/**/*.jsx
!src/**/*.ts
!src/**/*.tsx
!src/**/*.json
!*/

# More specific ignores go below.
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"arrowParens": "avoid",
"bracketSameLine": false,
"printWidth": 100,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,53 +66,53 @@ Global options are passed as options in the terminal or read from a `loco.config
// loco.config.js
/** @type {import('loco-cli/types').Config} */
module.exports = {
accessKey: "<loco-full-access-key>",
localesDir: "src/app/i18n/locales",
accessKey: '<loco-full-access-key>',
localesDir: 'src/app/i18n/locales',
namespaces: false,
push: {
"flag-new": "provisional",
"tag-new": process.env.npm_package_version,
"delete-absent": false,
},
}
'flag-new': 'provisional',
'tag-new': process.env.npm_package_version,
'delete-absent': false
}
};
```

| Config key | CLI flag | Type | Description |
| ------ | ---- | ---- | ----------- |
| accessKey | `-a`, `--access-key <key>` | `string` | The API key of the Loco project you wish to sync to/from. You can find this in the Loco project under `Developer Tools › API Keys › Full Access Key` (if you do not intend to use `loco-cli push`, an `Export key` will work too). |
| localesDir | `-d`, `--locales-dir <path>` | `string` | The folder in which the JSON translation files are stored (defaults to current working dir). |
| namespaces | `-N`, `--namespaces` | `boolean` | Organize translations into namespaces (default: `false`). Set this flag to `true` when dividing translations into multiple files. The uploaded asset ID's will be prefixed with `<namespace>:`. |
| push | - | `PushOptions` | Loco API options used for `loco-cli push`. (https://localise.biz/api/docs/import/import) |
| pull | - | `PullOptions` | Loco API options used for `loco-cli pull`. (https://localise.biz/api/docs/export/exportall) |
| Config key | CLI flag | Type | Description |
| ---------- | ---------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| accessKey | `-a`, `--access-key <key>` | `string` | The API key of the Loco project you wish to sync to/from. You can find this in the Loco project under `Developer Tools › API Keys › Full Access Key` (if you do not intend to use `loco-cli push`, an `Export key` will work too). |
| localesDir | `-d`, `--locales-dir <path>` | `string` | The folder in which the JSON translation files are stored (defaults to current working dir). |
| namespaces | `-N`, `--namespaces` | `boolean` | Organize translations into namespaces (default: `false`). Set this flag to `true` when dividing translations into multiple files. The uploaded asset ID's will be prefixed with `<namespace>:`. |
| push | - | `PushOptions` | Loco API options used for `loco-cli push`. (https://localise.biz/api/docs/import/import) |
| pull | - | `PullOptions` | Loco API options used for `loco-cli pull`. (https://localise.biz/api/docs/export/exportall) |

<details>
<summary>PushOptions</summary>
<br>

- `ignore-new`: Specify that new assets will NOT be added to the project.
- `ignore-existing`: Specify that existing assets encountered in the file will NOT be updated.
- `tag-new`: Tag any NEW assets added during the import with the given tags (comma separated).
- `tag-all`: Tag ALL assets in the file with the given tags (comma separated).
- `untag-all`: Remove existing tags from any assets matched in the imported file (comma separated).
- `tag-updated`: Tag existing assets that are MODIFIED by this import.
- `untag-updated`: Remove existing tags from assets that are MODIFIED during import.
- `tag-absent`: Tag existing assets in the project that are NOT found in the imported file.
- `untag-absent`: Remove existing tags from assets NOT found in the imported file.
- `delete-absent`: Permanently DELETES project assets NOT found in the file (use with extreme caution).
- `flag-new`: Set this flag on any NEW (non-empty) translations imported into the current locale.
- `ignore-new`: Specify that new assets will NOT be added to the project.
- `ignore-existing`: Specify that existing assets encountered in the file will NOT be updated.
- `tag-new`: Tag any NEW assets added during the import with the given tags (comma separated).
- `tag-all`: Tag ALL assets in the file with the given tags (comma separated).
- `untag-all`: Remove existing tags from any assets matched in the imported file (comma separated).
- `tag-updated`: Tag existing assets that are MODIFIED by this import.
- `untag-updated`: Remove existing tags from assets that are MODIFIED during import.
- `tag-absent`: Tag existing assets in the project that are NOT found in the imported file.
- `untag-absent`: Remove existing tags from assets NOT found in the imported file.
- `delete-absent`: Permanently DELETES project assets NOT found in the file (use with extreme caution).
- `flag-new`: Set this flag on any NEW (non-empty) translations imported into the current locale.
</details>

<details>
<summary>PullOptions
</summary>
<br>

- `filter`: Filter assets by comma-separated tag names. Match any tag with `*` and negate tags by prefixing with `!`.
- `fallback`: Fallback locale for untranslated assets, specified as short code. e.g. en or en_GB.
- `order`: Export translations according to asset order.
- `status`: Export translations with a specific status or flag. Negate values by prefixing with !. e.g. "translated", or "!fuzzy".
- `charset`: Specify preferred character encoding. Alternative to Accept-Charset header but accepts a single value which must be valid.
- `breaks`: Force platform-specific line-endings. Default is Unix (LF) breaks.
- `filter`: Filter assets by comma-separated tag names. Match any tag with `*` and negate tags by prefixing with `!`.
- `fallback`: Fallback locale for untranslated assets, specified as short code. e.g. en or en_GB.
- `order`: Export translations according to asset order.
- `status`: Export translations with a specific status or flag. Negate values by prefixing with !. e.g. "translated", or "!fuzzy".
- `charset`: Specify preferred character encoding. Alternative to Accept-Charset header but accepts a single value which must be valid.
- `breaks`: Force platform-specific line-endings. Default is Unix (LF) breaks.
</details>

## Usage
Expand Down
54 changes: 25 additions & 29 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
#!/usr/bin/env node
import { Command, Option } from "commander";
import { Command, Option } from 'commander';

import pull from "./src/commands/pull";
import push from "./src/commands/push";
import status from "./src/commands/status";
import { version } from "./package.json";
import { handleAsyncErrors } from "./src/util/handleAsyncErrors";
import pull from './src/commands/pull';
import push from './src/commands/push';
import status from './src/commands/status';
import { version } from './package.json';
import { handleAsyncErrors } from './src/util/handleAsyncErrors';

const program = new Command("loco-cli")
const program = new Command('loco-cli')
.version(version)
.option("-a, --access-key <key>", "Loco API token")
.option(
"-d, --locales-dir <path>",
"The folder in which the translations are stored.",
"."
)
.option("-N, --namespaces", "Organize translations into namespaces", false);
.option('-a, --access-key <key>', 'Loco API token')
.option('-d, --locales-dir <path>', 'The folder in which the translations are stored.', '.')
.option('-N, --namespaces', 'Organize translations into namespaces', false);
program
.command("pull")
.option("-y, --yes", "Answer yes to all confirmation prompts", false)
.description("Fetch assets from Loco")
.command('pull')
.option('-y, --yes', 'Answer yes to all confirmation prompts', false)
.description('Fetch assets from Loco')
.action(handleAsyncErrors(pull));

program
.command("push")
.command('push')
.option(
"-t, --tag [tag]",
"The tag option is removed in v2, use the `push.tag-new` option in `loco.config.js` instead"
'-t, --tag [tag]',
'The tag option is removed in v2, use the `push.tag-new` option in `loco.config.js` instead'
)
.option(
"-s, --status [status]",
"The status option is removed in v2, use the `push.flag-new` option in `loco.config.js` instead"
'-s, --status [status]',
'The status option is removed in v2, use the `push.flag-new` option in `loco.config.js` instead'
)
.option("-y, --yes", "Answer yes to all confirmation prompts", false)
.description("Upload assets to Loco")
.option('-y, --yes', 'Answer yes to all confirmation prompts', false)
.description('Upload assets to Loco')
.action(handleAsyncErrors(push));

program
.command("status")
.command('status')
.addOption(
new Option("--direction [direction]", "Direction to diff the assets IDs to")
.choices(["remote", "local", "both"])
.default("both")
new Option('--direction [direction]', 'Direction to diff the assets IDs to')
.choices(['remote', 'local', 'both'])
.default('both')
)
.description("Check status of local file")
.description('Check status of local file')
.action(handleAsyncErrors(status));

program.parse(process.argv);
21 changes: 21 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import prettierConfig from "eslint-config-prettier";

const ignoreConfig = {
ignores: ["node_modules/", "dist/", ".locorc.js"],
};

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
ignoreConfig,
prettierConfig,
{
rules: {
"@typescript-eslint/no-explicit-any": "warn",
},
},
);
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
"node": "14.x || 16.x || 18.x || 20.x"
},
"scripts": {
"test": "vitest run",
"build": "tsc",
"loco-cli": "yarn build && node ./dist/cli.js"
"format:check": "prettier --check .",
"format": "prettier --write .",
"lint": "eslint .",
"loco-cli": "yarn build && node ./dist/cli.js",
"test": "vitest run"
},
"files": [
"dist",
Expand All @@ -41,10 +44,16 @@
"isomorphic-unfetch": "^3.1.0"
},
"devDependencies": {
"@eslint/js": "9.2.0",
"@types/cli-progress": "3.11.0",
"@types/inquirer": "8.2.1",
"@types/node": "14.x",
"typescript": "4.9.4",
"eslint": "9.2.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"prettier": "3.2.5",
"typescript": "5.4.5",
"typescript-eslint": "7.8.0",
"vitest": "0.26.1"
}
}
32 changes: 16 additions & 16 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Command } from "commander";
import { diff } from "../lib/diff";
import { readFiles } from "../lib/readFiles";
import { getGlobalOptions } from "../util/options";
import { apiPull as apiPull } from "../lib/api";
import inquirer from "inquirer";
import chalk from "chalk";
import { printDiff } from "../util/print";
import { writeFiles } from "../lib/writeFiles";
import { log } from "../util/logger";
import { Command } from 'commander';
import { diff } from '../lib/diff';
import { readFiles } from '../lib/readFiles';
import { getGlobalOptions } from '../util/options';
import { apiPull as apiPull } from '../lib/api';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { printDiff } from '../util/print';
import { writeFiles } from '../lib/writeFiles';
import { log } from '../util/logger';

interface CommandOptions {
yes?: boolean;
Expand All @@ -21,7 +21,7 @@ const pull = async ({ yes }: CommandOptions, program: Command) => {

const { added, updated, deleted, totalCount: count } = diff(local, remote);
if (!count) {
log.success("Everything up to date!");
log.success('Everything up to date!');
process.exit(0);
}

Expand All @@ -33,14 +33,14 @@ const pull = async ({ yes }: CommandOptions, program: Command) => {
if (!yes) {
const { confirm } = await inquirer.prompt([
{
type: "confirm",
name: "confirm",
message: "Continue?",
},
type: 'confirm',
name: 'confirm',
message: 'Continue?'
}
]);

if (!confirm) {
log.error("Nothing pulled");
log.error('Nothing pulled');
process.exit(0);
}
}
Expand Down
Loading