Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

use Prettier API to format files #57

Merged
merged 1 commit into from
Jan 17, 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
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.1.0",
"version": "3.2.0",
"license": "MIT",
"name": "@mollie/crowdin-cli",
"author": {
Expand Down Expand Up @@ -57,10 +57,13 @@
"chalk": "^4.0.0",
"commander": "^7.0.0",
"dotenv": "^8.2.0",
"glob": "^10.3.10",
"fast-glob": "^3.3.2",
"mkdirp": "^1.0.4",
"shelljs": "^0.8.5"
},
"peerDependencies": {
"prettier": "^3.2.2"
},
"devDependencies": {
"@types/commander": "^2.12.2",
"@types/mkdirp": "^1.0.1",
Expand Down
8 changes: 4 additions & 4 deletions src/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import config from "./config";
import log from "./utils/logging";
import prettify from "./utils/prettify";
import { extract } from "@formatjs/cli-lib";
import { glob } from "glob";
import fg from "fast-glob";
import fs from "fs";

export default async (globString: string) => {
export default async (globPattern: string) => {
log.info("Extracting messages");
sync(config.INTL_DIR);

const files = await glob(globString);
const files = await fg.glob(globPattern);
if (files.length === 0) {
log.error("No files found, check your glob pattern");
process.exit(1);
Expand All @@ -20,5 +20,5 @@ export default async (globString: string) => {
});
fs.writeFileSync(config.TRANSLATIONS_FILE, resultAsString);

prettify(config.TRANSLATIONS_FILE);
await prettify(config.TRANSLATIONS_FILE);
};
9 changes: 7 additions & 2 deletions src/delete-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ export default async ({ branchName, deleteTasks }: DeleteBranchOptions) => {
log.info("Deleting branch from Crowdin");

const branches = await listBranches(branchName);

if (branches.data.length === 0) {
return log.error(`Couldn’t find a branch with the name: "${branchName}"`);
}

const branchId = branches.data[0].data.id;
const files = await listFiles(branchId);

try {
if (deleteTasks) {
const files = await listFiles(branchId);
const tasks = await listTasks({ branchId });

await Promise.allSettled(
Expand All @@ -41,7 +46,7 @@ export default async ({ branchName, deleteTasks }: DeleteBranchOptions) => {
}

await deleteBranch(branchId);
log.success("Branch deleted");
log.success(`Deleted branch: ${branchName} (${branchId})`);
} catch (error) {
log.error(error as string);
}
Expand Down
2 changes: 1 addition & 1 deletion src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
const response = exportFileResponses[i];

if (!response || response.statusText !== "OK") {
return;

Check warning on line 78 in src/download.ts

View workflow job for this annotation

GitHub Actions / Run ESLint checks

Arrow function expected a return value
}

const keyValueObject = convertToKeyValue(response.data);
Expand All @@ -89,7 +89,7 @@
})
);

prettify(`${options.translationsDir}/*.+(${fileExtension})`);
await prettify(`${options.translationsDir}/*.${fileExtension}`);

log.success("Translations updated");
};
48 changes: 32 additions & 16 deletions src/utils/prettify.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import path from "path";
import fs from "fs";
import shell from "shelljs";
import fg from "fast-glob";
import log from "./logging";
import * as prettier from "prettier";

const prettify = (glob: string) => {
const prettierExecutable = path.join(
process.cwd(),
"node_modules",
".bin",
"prettier"
);
const defaultConfig: prettier.Options = {
printWidth: 100,
singleQuote: true,
trailingComma: "all",
bracketSpacing: true,
};

const prettify = async (globPattern: string) => {
const configFilePath = await prettier.resolveConfigFile();
const configFileContents = await prettier.resolveConfig(configFilePath || "");
const config = configFileContents || defaultConfig;

try {
const files = await fg.glob(globPattern);

if (fs.existsSync(prettierExecutable)) {
log.info(`Formatting ${glob} files with Prettier`);
shell.exec(
[prettierExecutable, "--loglevel silent", "--write", `"${glob}"`].join(
" "
)
);
for (const file of files) {
const data = fs.readFileSync(file, "utf-8");
try {
const pretty = await prettier.format(data, {
...config,
parser: "typescript",
});
fs.writeFileSync(file, pretty);
log.success(`Prettified ${file}`);
} catch (error) {
log.error(`Something went wrong while prettifying the file: ${file}`);
}
}
} catch {
log.error("No files found, check your glob pattern");
process.exit(1);
}
};

Expand Down
10 changes: 4 additions & 6 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ describe("CLI", () => {
});

it("correctly handles `collect` command", async () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce("");
await program(["node", "test", "collect", mockGlob]);
expect(collect).toHaveBeenCalledTimes(1);
expect(collect).toHaveBeenCalledWith(mockGlob);
});

it("correctly handles `upload` command", async () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce("");
await program(["node", "test", "upload", mockGlob]);
expect(collect).toHaveBeenCalledWith(mockGlob);
expect(upload).toHaveBeenCalledWith(
Expand Down Expand Up @@ -178,6 +180,7 @@ describe("CLI", () => {
});

it("correctly handles `upload` command with pre-translate and create-tasks options", async () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce("");
await program([
"node",
"test",
Expand All @@ -192,13 +195,8 @@ describe("CLI", () => {
expect(createTasks).toHaveBeenCalledTimes(1);
});

it("correctly handles `collect` command", async () => {
await program(["node", "test", "collect", mockGlob]);
expect(collect).toHaveBeenCalledTimes(1);
expect(collect).toHaveBeenCalledWith(mockGlob);
});

it("correctly handles `download` command", async () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce("");
sync(`${config.INTL_DIR}`);
fs.writeFileSync(`${config.INTL_DIR}/english.source.json`, "");

Expand Down
Loading
Loading