Skip to content

Commit 784eb50

Browse files
authored
feat: switched prettier-config to typescript #675
BREAKING CHANGE: Changed the package to support cjs and mjs, so the type module was removed
2 parents 074699d + c014026 commit 784eb50

File tree

11 files changed

+81
-44
lines changed

11 files changed

+81
-44
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"packages/*"
5555
],
5656
"scripts": {
57-
"build:packages": "pnpm --filter \"package-json-utils\" run build && nx run-many --target=build --projects=browserslist-config-anolilab,babel-preset",
58-
"build:prod:packages": "pnpm --filter \"package-json-utils\" run build:prod && nx run-many --target=build:prod --projects=browserslist-config-anolilab,babel-preset",
57+
"build:packages": "pnpm --filter \"package-json-utils\" run build && nx run-many --target=build --projects=browserslist-config-anolilab,babel-preset,prettier-config",
58+
"build:prod:packages": "pnpm --filter \"package-json-utils\" run build:prod && nx run-many --target=build:prod --projects=browserslist-config-anolilab,babel-preset,prettier-config",
5959
"preinstall": "node verify-node-version.cjs",
6060
"postinstall": "is-ci || husky install",
6161
"lint:eslint": "cross-env NO_LOGS=true eslint -c ./.eslintrc.cjs --ext .js,.cjs,.mjs,.ts,.tsx ./packages --cache --cache-strategy content .",

packages/prettier-config/.npmignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package-lock.json
22

33
src
4+
fixture
45
__tests__
56
__stories__
6-
__fixture__
77
.rpt2_cache
8-

packages/prettier-config/package.json

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,32 @@
3131
"name": "Daniel Bannert",
3232
"email": "[email protected]"
3333
},
34-
"type": "module",
35-
"main": "index.cjs",
34+
"sideEffects": false,
35+
"exports": {
36+
".": {
37+
"types": "./dist/index.d.ts",
38+
"require": "./dist/index.js",
39+
"import": "./dist/index.mjs"
40+
},
41+
"./package.json": "./package.json"
42+
},
43+
"main": "dist/index.js",
44+
"module": "dist/index.mjs",
45+
"types": "dist/index.d.ts",
3646
"files": [
37-
"lib",
38-
"index.cjs",
47+
"dist",
3948
"README.md",
4049
"CHANGELOG.md",
4150
"LICENSE.md"
4251
],
4352
"scripts": {
44-
"postinstall": "node lib/postinstall.js"
53+
"build": "cross-env NODE_ENV=development tsup",
54+
"build:prod": "cross-env NODE_ENV=production tsup",
55+
"clean": "rimraf node_modules dist",
56+
"postinstall": "node ./skip.js || node ./dist/postinstall.js"
57+
},
58+
"dependencies": {
59+
"@anolilab/package-json-utils": "1.0.1"
4560
},
4661
"devDependencies": {
4762
"@anolilab/semantic-release-preset": "3.0.0"
@@ -56,20 +71,8 @@
5671
"access": "public",
5772
"provenance": true
5873
},
59-
"pnpm": {
60-
"overrides": {
61-
"glob-parent@<5.1.2": ">=5.1.2",
62-
"minimist@<1.2.6": ">=1.2.6",
63-
"chrono-node@<2.2.4": ">=2.2.4",
64-
"yargs-parser@>=6.0.0 <13.1.2": ">=13.1.2",
65-
"trim@<0.0.3": ">=0.0.3",
66-
"trim-newlines@<3.0.1": ">=3.0.1",
67-
"moment@<2.29.2": ">=2.29.2",
68-
"semver-regex@<3.1.4": ">=3.1.4",
69-
"got@<11.8.5": ">=11.8.5",
70-
"moment@>=2.18.0 <2.29.4": ">=2.29.4",
71-
"ansi-regex@>=3.0.0 <3.0.1": ">=3.0.1",
72-
"ansi-regex@>=4.0.0 <4.1.1": ">=4.1.1"
73-
}
74-
}
74+
"sources": [
75+
"src/index.ts",
76+
"src/postinstall.ts"
77+
]
7578
}

packages/prettier-config/project.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
3+
"sourceRoot": "packages/prettier-config/src",
4+
"projectType": "library",
5+
"tags": ["prettier-config"],
6+
"implicitDependencies": ["package-json-utils"]
7+
}

packages/prettier-config/skip.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// eslint-disable-next-line no-undef
2+
if (process.env.SKIP_BUILD) {
3+
// eslint-disable-next-line no-undef,unicorn/no-process-exit
4+
process.exit(0);
5+
} else {
6+
// eslint-disable-next-line no-undef,unicorn/no-process-exit
7+
process.exit(1);
8+
}

packages/prettier-config/index.cjs renamed to packages/prettier-config/src/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
// @ts-check
1+
import type { Options } from "prettier";
22

3-
/**
4-
* @type {import('prettier').Options}
5-
*/
6-
module.exports = {
7-
// max 120 characters per line
8-
printWidth: 120,
9-
// use 2 spaces for indentation
3+
const config: Options = {
4+
// max 164 characters per line
5+
printWidth: 164,
6+
// use 4 spaces for indentation
107
tabWidth: 4,
118
// use spaces instead of indentations
129
useTabs: false,
@@ -18,7 +15,7 @@ module.exports = {
1815
quoteProps: "as-needed",
1916
// use double quotes instead of single quotes in jsx
2017
jsxSingleQuote: false,
21-
// no comma at the end
18+
// all comma at the end
2219
trailingComma: "all",
2320
// spaces are required at the beginning and end of the braces
2421
bracketSpacing: true,
@@ -42,3 +39,5 @@ module.exports = {
4239
// formats quoted code embedded
4340
embeddedLanguageFormatting: "auto",
4441
};
42+
43+
export default config;

packages/prettier-config/lib/postinstall.js renamed to packages/prettier-config/src/postinstall.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env node
22

3+
import { packageIsTypeModule } from "@anolilab/package-json-utils";
34
import { existsSync, writeFile } from "node:fs";
45
import { join, resolve } from "node:path";
56
import { promisify } from "node:util";
67

7-
import content from "../index.cjs";
8+
import content from "./index";
89

9-
// eslint-disable-next-line no-undef
10-
if (process.env.CI) {
10+
if (process.env["CI"]) {
1111
// eslint-disable-next-line no-undef
1212
process.exit(0);
1313
}
@@ -21,13 +21,13 @@ const projectPath = resolve(process.cwd(), "..", "..", "..");
2121
console.log("Configuring @anolilab/prettier-config", projectPath, "\n");
2222

2323
/**
24-
* Writes .prettierrc.cjs if it doesn't exist. Warns if it exists.
24+
* Writes .prettierrc.${m|c}js if it doesn't exist. Warns if it exists.
2525
*/
2626
const writePrettierRc = () => {
27-
const prettierPath = join(projectPath, ".prettierrc.cjs");
27+
const prettierPath = join(projectPath, `.prettierrc.${packageIsTypeModule ? "m" : "c"}js`);
2828

29-
if (existsSync(prettierPath) || existsSync(prettierPath.replace(".cjs", ""))) {
30-
console.warn(`⚠️ .prettierrc.cjs already exists;
29+
if (existsSync(prettierPath) || existsSync(prettierPath.replace(`.${packageIsTypeModule ? "m" : "c"}js`, ""))) {
30+
console.warn(`⚠️ .prettierrc.${packageIsTypeModule ? "m" : "c"}js already exists;
3131
Make sure that it includes the following for @anolilab/prettier-config to work as it should:
3232
${JSON.stringify(content, undefined, 4)}\n`);
3333

@@ -36,7 +36,10 @@ ${JSON.stringify(content, undefined, 4)}\n`);
3636

3737
return writeFileAsync(
3838
prettierPath,
39-
`module.exports = ${JSON.stringify(content, undefined, 2).replace("rangeEnd: null,", "rangeEnd: Infinity,")}\n`,
39+
`${packageIsTypeModule ? "export default" : "module.exports ="} ${JSON.stringify(content, undefined, 2).replace(
40+
"rangeEnd: null,",
41+
"rangeEnd: Infinity,",
42+
)}\n`,
4043
"utf-8",
4144
);
4245
};
@@ -68,7 +71,7 @@ const writePrettierIgnore = () => {
6871
process.exit(0);
6972
} catch (error) {
7073
console.log("😬 something went wrong:");
71-
console.error(error.message);
74+
console.error(error);
7275

7376
// eslint-disable-next-line no-undef
7477
process.exit(1);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.json"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"display": "Node 16",
4+
"extends": "../../tsconfig.json",
5+
"baseUrl": "./",
6+
"include": ["src/**/*", "*.d.ts"]
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createConfig } from "../../tsup.config";
2+
3+
const config = createConfig();
4+
5+
export default config;

0 commit comments

Comments
 (0)