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

feat: add import order sorting via prettier #1392

Merged
merged 26 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
need to test this but.
  • Loading branch information
FinnDore committed May 13, 2023
commit a665f393f9ce8687cad97dd471536cc964f20fb2
16 changes: 16 additions & 0 deletions cli/src/installers/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from "fs-extra";
import path from "path";
import { PKG_ROOT } from "~/consts.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";
import { addPackageScript } from "~/utils/addPackageScript.js";

export const prettier: Installer = ({ projectDir, packages }) => {
const packeagesToInstall: AvailableDependencies[] = [
Expand All @@ -22,6 +23,21 @@ export const prettier: Installer = ({ projectDir, packages }) => {
devMode: true,
});

addPackageScript({
projectDir,
scripts: [
{
name: "format",
value: "pnpm format:check --write",
},
{
name: "format:check",
value:
"pnpm prettier --check --plugin-search-dir=. **/*.{cjs,mjs,ts,tsx,md,json} --ignore-path ../.gitignore --ignore-unknown --no-error-on-unmatched-pattern",
},
],
});

if (packages?.tailwind.inUse) {
fs.copySync(
path.join(
Expand Down
26 changes: 26 additions & 0 deletions cli/src/utils/addPackageScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "fs-extra";
import path from "path";
import sortPackageJson from "sort-package-json";
import { type PackageJson } from "type-fest";

export const addPackageScript = (opts: {
scripts: { name: string; value: string }[];
projectDir: string;
}) => {
const pkgJson = fs.readJSONSync(
path.join(opts.projectDir, "package.json"),
) as PackageJson;

if (!pkgJson.scripts) {
pkgJson.scripts = {};
}

for (const script of opts.scripts) {
pkgJson.scripts[script.name] = script.value;
}
const sortedPkgJson = sortPackageJson(pkgJson);

fs.writeJSONSync(path.join(opts.projectDir, "package.json"), sortedPkgJson, {
spaces: 2,
});
};