Skip to content

Commit

Permalink
fix(prettier-config): removed @anolilab/package-json-utils
Browse files Browse the repository at this point in the history
Signed-off-by: prisis <[email protected]>
  • Loading branch information
prisis committed Dec 17, 2024
1 parent c4df0ab commit 804c365
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
3 changes: 0 additions & 3 deletions packages/prettier-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@
"build:prod": "packem build --production",
"clean": "rimraf node_modules dist"
},
"dependencies": {
"@anolilab/package-json-utils": "3.0.9"
},
"devDependencies": {
"@anolilab/semantic-release-preset": "9.0.2",
"@visulima/packem": "^1.10.0",
Expand Down
58 changes: 31 additions & 27 deletions packages/prettier-config/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { existsSync, writeFile } from "node:fs";
import { existsSync } from "node:fs";
import { writeFile, readFile } from "node:fs/promises";
import { join } from "node:path";
import { env, exit } from "node:process";
import { promisify } from "node:util";

import { packageIsTypeModule, projectPath } from "@anolilab/package-json-utils";
import { exit } from "node:process";

import content from ".";

if (env["CI"] !== undefined) {
exit(0);
}

const writeFileAsync = promisify(writeFile);

console.log("Configuring @anolilab/prettier-config", projectPath, "\n");

const configFile = ".prettierrc";

/**
* Writes .prettierrc.${m|c}js if it doesn't exist. Warns if it exists.
*/
const writePrettierRc = async () => {
const writePrettierRc = async (cwd: string, isTypeModule: boolean) => {
// eslint-disable-next-line no-restricted-syntax,no-loops/no-loops
for (const filename of [
configFile,
Expand All @@ -37,7 +27,7 @@ const writePrettierRc = async () => {
"prettier.config.cjs",
]) {
// eslint-disable-next-line security/detect-non-literal-fs-filename
if (existsSync(join(projectPath, filename))) {
if (existsSync(join(cwd, filename))) {
console.warn(`⚠️ ${filename} already exists;
Make sure that it includes the following for @anolilab/prettier-config to work as it should:
${JSON.stringify(content, undefined, 4)}\n`);
Expand All @@ -46,13 +36,13 @@ ${JSON.stringify(content, undefined, 4)}\n`);
}
}

const prettierPath = join(projectPath, ".prettierrc.js");
const prettierPath = join(cwd, ".prettierrc.js");

await writeFileAsync(
await writeFile(
prettierPath,
`${packageIsTypeModule ? 'import config from "@anolilab/prettier-config";' : 'var config = require("@anolilab/prettier-config");'}
`${isTypeModule ? 'import config from "@anolilab/prettier-config";' : 'var config = require("@anolilab/prettier-config");'}
${packageIsTypeModule ? "export default" : "module.exports ="} {
${isTypeModule ? "export default" : "module.exports ="} {
...config,
}
`,
Expand All @@ -63,8 +53,8 @@ ${packageIsTypeModule ? "export default" : "module.exports ="} {
/**
* Writes .prettierignore if it doesn't exist. Warns if it exists.
*/
const writePrettierIgnore = async () => {
const prettierPath = join(projectPath, ".prettierignore");
const writePrettierIgnore = async (cwd: string) => {
const prettierPath = join(cwd, ".prettierignore");

// eslint-disable-next-line security/detect-non-literal-fs-filename
if (existsSync(prettierPath)) {
Expand All @@ -73,7 +63,7 @@ const writePrettierIgnore = async () => {
return;
}

await writeFileAsync(
await writeFile(
prettierPath,
`${["*.md", "*.sh", "*.yml", "*.svg", "*.gif", "*.log", ".DS_Store", "CNAME", "AUTHORS", "LICENSE", "es/", "lib/", "dist/", "coverage/"].join("\n")}\n`,
"utf8",
Expand All @@ -82,16 +72,30 @@ const writePrettierIgnore = async () => {

// eslint-disable-next-line unicorn/prefer-top-level-await
(async () => {
const cwd = process.cwd();

const packageJsonPath = join(cwd, "package.json");

if (!existsSync(packageJsonPath)) {
console.error("No package.json found in the current directory. You need to run this command in a directory with a package.json file.");

exit(1);
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf-8"));

console.log("Configuring @anolilab/prettier-config", cwd, "\n");

try {
await writePrettierRc();
await writePrettierIgnore();
await writePrettierRc(cwd, packageJson.type === "module");
await writePrettierIgnore(cwd);

console.log("😎 Everything went well, have fun!");
console.log("Everything went well, have fun!");

exit(0);
} catch (error) {
console.log("😬 something went wrong:");
console.error(error);
console.error("Something went wrong:", error);

exit(1);
}
Expand Down

0 comments on commit 804c365

Please sign in to comment.