From 02921f89b6f4e9bdccb997dbee09a4194f028402 Mon Sep 17 00:00:00 2001 From: wangshouren <1115808717@qq.com> Date: Fri, 26 Jul 2024 17:33:41 +0800 Subject: [PATCH] chore(@npcs/get-changed-packages,@npcs/cli,@npcs/rollup-config,@npcs/template,npcs): add commit lint --- README.md | 1 + apps/template/src/app/layout.tsx | 2 +- commitlint.config.js | 10 +++ configs/rollup/rollup.config.mjs | 19 +++--- libs/cli/package.json | 3 - package.json | 3 +- pnpm-lock.yaml | 55 +++++++++++++--- pnpm-workspace.yaml | 1 + tools/get-changed-packages/.eslintrc.json | 3 + tools/get-changed-packages/CHANGELOG.md | 40 +++++++++++ tools/get-changed-packages/README.md | 3 + tools/get-changed-packages/package.json | 35 ++++++++++ tools/get-changed-packages/rollup.config.mjs | 8 +++ tools/get-changed-packages/src/pnpm.ts | 20 ++++++ tools/get-changed-packages/src/shared.ts | 42 ++++++++++++ .../get-changed-packages/tests/shared.test.ts | 66 +++++++++++++++++++ tools/get-changed-packages/tsconfig.json | 11 ++++ tools/get-changed-packages/vitest.config.mts | 3 + 18 files changed, 301 insertions(+), 24 deletions(-) create mode 100644 tools/get-changed-packages/.eslintrc.json create mode 100644 tools/get-changed-packages/CHANGELOG.md create mode 100644 tools/get-changed-packages/README.md create mode 100644 tools/get-changed-packages/package.json create mode 100644 tools/get-changed-packages/rollup.config.mjs create mode 100644 tools/get-changed-packages/src/pnpm.ts create mode 100644 tools/get-changed-packages/src/shared.ts create mode 100644 tools/get-changed-packages/tests/shared.test.ts create mode 100644 tools/get-changed-packages/tsconfig.json create mode 100644 tools/get-changed-packages/vitest.config.mts diff --git a/README.md b/README.md index a8fae3e..899ade0 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,4 @@ Next.js practical cases. | Extract shared next config to plugin | | | | Extract eslint for libs | | | | Replace clerk.js key to production | | | +| Fix .js => .mjs of esm format | | | diff --git a/apps/template/src/app/layout.tsx b/apps/template/src/app/layout.tsx index 27bafba..c5b7cc5 100644 --- a/apps/template/src/app/layout.tsx +++ b/apps/template/src/app/layout.tsx @@ -10,7 +10,7 @@ const inter = Inter({ subsets: ["latin"] }); export const metadata: Metadata = { title: "Npcs Template", - description: "A template project in next.js-practical-cases", + description: "A template project in Next.js practical cases", }; export default async function RootLayout({ diff --git a/commitlint.config.js b/commitlint.config.js index dcd0c9d..e7f2e9e 100644 --- a/commitlint.config.js +++ b/commitlint.config.js @@ -1,4 +1,14 @@ +const scopes = import("@npcs/get-changed-packages/esm/pnpm.js").then((x) => + x.getChangedPackages() +); + /** @type {import('cz-git').UserConfig} */ module.exports = { extends: ["@commitlint/config-conventional"], + prompt: { + scopes, + enableMultipleScopes: true, + defaultScope: scopes, + allowEmptyScopes: false, + }, }; diff --git a/configs/rollup/rollup.config.mjs b/configs/rollup/rollup.config.mjs index 728b0e2..b036c8d 100644 --- a/configs/rollup/rollup.config.mjs +++ b/configs/rollup/rollup.config.mjs @@ -1,5 +1,3 @@ -// @ts-check - /* eslint-disable import/no-anonymous-default-export */ import resolve from "@rollup/plugin-node-resolve"; @@ -14,17 +12,18 @@ import { typescriptPaths } from "rollup-plugin-typescript-paths"; /** @type {import('rollup').RollupOptions} */ export default { input: fg.sync(["./src/**/*.{ts,tsx,css}", "!**/stories/**"]), - output: { - format: "esm", - preserveModules: true, - preserveModulesRoot: "./src", - dir: "./esm", - sourcemap: true, - }, + output: [ + { + format: "esm", + preserveModules: true, + preserveModulesRoot: "./src", + dir: "./esm", + sourcemap: true, + }, + ], plugins: [ typescript({ jsx: "react", - outputToFilesystem: true, compilerOptions: { declaration: true, outDir: "./esm", diff --git a/libs/cli/package.json b/libs/cli/package.json index beb9598..a3635d2 100644 --- a/libs/cli/package.json +++ b/libs/cli/package.json @@ -75,9 +75,6 @@ "@npcs/tailwind-config": "workspace:*", "@npcs/typescript-config": "workspace:*", "@npcs/vitest-config": "workspace:^", - "@rollup/plugin-commonjs": "^26.0.1", - "@rollup/plugin-node-resolve": "^15.2.3", - "@rollup/plugin-typescript": "^11.1.6", "@storybook/addon-essentials": "8.2.4", "@storybook/addon-interactions": "8.2.4", "@storybook/addon-links": "8.2.4", diff --git a/package.json b/package.json index dae1dbf..52037c6 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,13 @@ "private": true, "packageManager": "pnpm@9.4.0+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a", "scripts": { - "prepare": "husky && turbo prisma-generate", + "prepare": "husky && turbo prisma-generate & turbo compile", "commit": "git-cz" }, "devDependencies": { "@commitlint/cli": "^19.3.0", "@commitlint/config-conventional": "^19.2.2", + "@npcs/get-changed-packages": "workspace:^", "cz-git": "^1.9.4", "husky": "^9.1.1", "lerna": "^8.1.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b93ccd1..8f7438e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: '@commitlint/config-conventional': specifier: ^19.2.2 version: 19.2.2 + '@npcs/get-changed-packages': + specifier: workspace:^ + version: link:tools/get-changed-packages cz-git: specifier: ^1.9.4 version: 1.9.4 @@ -1056,15 +1059,6 @@ importers: '@npcs/vitest-config': specifier: workspace:^ version: link:../../configs/vitest - '@rollup/plugin-commonjs': - specifier: ^26.0.1 - version: 26.0.1(rollup@4.18.1) - '@rollup/plugin-node-resolve': - specifier: ^15.2.3 - version: 15.2.3(rollup@4.18.1) - '@rollup/plugin-typescript': - specifier: ^11.1.6 - version: 11.1.6(rollup@4.18.1)(tslib@2.6.3)(typescript@5.5.3) '@storybook/addon-essentials': specifier: 8.2.4 version: 8.2.4(storybook@8.2.4(@babel/preset-env@7.24.8(@babel/core@7.24.9))(bufferutil@4.0.8)(utf-8-validate@6.0.3)) @@ -1807,6 +1801,49 @@ importers: specifier: ^1.6.0 version: 1.6.0(@edge-runtime/vm@3.2.0)(@types/node@20.14.10)(jsdom@24.1.0(bufferutil@4.0.8)(utf-8-validate@6.0.3))(terser@5.31.2) + tools/get-changed-packages: + dependencies: + '@pnpm/filter-workspace-packages': + specifier: ^10.0.1 + version: 10.0.1(@pnpm/logger@5.0.0) + '@pnpm/find-workspace-dir': + specifier: ^7.0.1 + version: 7.0.1 + '@pnpm/workspace.find-packages': + specifier: ^4.0.1 + version: 4.0.1(@pnpm/logger@5.0.0) + rollup: + specifier: ^4.18.1 + version: 4.18.1 + devDependencies: + '@npcs/eslint-config': + specifier: workspace:* + version: link:../../configs/eslint + '@npcs/rollup-config': + specifier: workspace:* + version: link:../../configs/rollup + '@npcs/tailwind-config': + specifier: workspace:* + version: link:../../configs/tailwind + '@npcs/typescript-config': + specifier: workspace:* + version: link:../../configs/typescript + '@npcs/vitest-config': + specifier: workspace:^ + version: link:../../configs/vitest + '@types/node': + specifier: ^20.14.10 + version: 20.14.11 + eslint: + specifier: ^8.57.0 + version: 8.57.0 + typescript: + specifier: 5.5.3 + version: 5.5.3 + vitest: + specifier: ^1.6.0 + version: 1.6.0(@edge-runtime/vm@3.2.0)(@types/node@20.14.11)(jsdom@24.1.0)(terser@5.31.2) + packages: '@adobe/css-tools@4.4.0': diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 32ac0c0..c13f8bb 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -3,3 +3,4 @@ packages: - "./configs/*" - "./libs/*" - "./dbs/*" + - "./tools/*" diff --git a/tools/get-changed-packages/.eslintrc.json b/tools/get-changed-packages/.eslintrc.json new file mode 100644 index 0000000..4c33734 --- /dev/null +++ b/tools/get-changed-packages/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": ["@npcs/eslint-config"] +} diff --git a/tools/get-changed-packages/CHANGELOG.md b/tools/get-changed-packages/CHANGELOG.md new file mode 100644 index 0000000..cbff95b --- /dev/null +++ b/tools/get-changed-packages/CHANGELOG.md @@ -0,0 +1,40 @@ +# Change Log + +All notable changes to this project will be documented in this file. +See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. + +# [0.4.0](https://github.com/xiaosen7/npcs/compare/v0.3.1...v0.4.0) (2024-07-26) + +**Note:** Version bump only for package @npcs/cli + + + + + +## [0.3.1](https://github.com/xiaosen7/npcs/compare/v0.3.0...v0.3.1) (2024-07-26) + +**Note:** Version bump only for package @npcs/cli + + + + + +# [0.3.0](https://github.com/xiaosen7/next.js-practical-cases/compare/v0.2.0...v0.3.0) (2024-07-26) + +**Note:** Version bump only for package @npcs/cli + + + + + +# [0.2.0](https://github.com/xiaosen7/next.js-practical-cases/compare/v0.1.0...v0.2.0) (2024-07-26) + +**Note:** Version bump only for package @npcs/cli + + + + + +# [0.1.0](https://github.com/xiaosen7/next.js-practical-cases/compare/v0.0.1...v0.1.0) (2024-07-26) + +**Note:** Version bump only for package @npcs/cli diff --git a/tools/get-changed-packages/README.md b/tools/get-changed-packages/README.md new file mode 100644 index 0000000..62e05e1 --- /dev/null +++ b/tools/get-changed-packages/README.md @@ -0,0 +1,3 @@ +# @npcs/get-changed-packages + +Get the changed packages from the last commit inside a workspace. diff --git a/tools/get-changed-packages/package.json b/tools/get-changed-packages/package.json new file mode 100644 index 0000000..3847219 --- /dev/null +++ b/tools/get-changed-packages/package.json @@ -0,0 +1,35 @@ +{ + "name": "@npcs/get-changed-packages", + "version": "0.4.0", + "description": "Get the changed packages from the last commit inside a workspace.", + "repository": { + "url": "https://github.com/xiaosen7/npcs" + }, + "type": "module", + "scripts": { + "check-types": "tsc --noEmit", + "lint": "eslint .", + "compile:watch": "rollup -c --watch", + "compile": "rollup -c", + "test": "vitest --run", + "test:watch": "vitest watch", + "test:changed": "vitest run --changed" + }, + "dependencies": { + "@pnpm/filter-workspace-packages": "^10.0.1", + "@pnpm/find-workspace-dir": "^7.0.1", + "@pnpm/workspace.find-packages": "^4.0.1", + "rollup": "^4.18.1" + }, + "devDependencies": { + "@npcs/eslint-config": "workspace:*", + "@npcs/rollup-config": "workspace:*", + "@npcs/tailwind-config": "workspace:*", + "@npcs/typescript-config": "workspace:*", + "@npcs/vitest-config": "workspace:^", + "@types/node": "^20.14.10", + "eslint": "^8.57.0", + "typescript": "5.5.3", + "vitest": "^1.6.0" + } +} diff --git a/tools/get-changed-packages/rollup.config.mjs b/tools/get-changed-packages/rollup.config.mjs new file mode 100644 index 0000000..44cc164 --- /dev/null +++ b/tools/get-changed-packages/rollup.config.mjs @@ -0,0 +1,8 @@ +/* eslint-disable import/no-anonymous-default-export */ + +import config from "@npcs/rollup-config"; + +/** @type {import('rollup').RollupOptions} */ +export default { + ...config, +}; diff --git a/tools/get-changed-packages/src/pnpm.ts b/tools/get-changed-packages/src/pnpm.ts new file mode 100644 index 0000000..31e635f --- /dev/null +++ b/tools/get-changed-packages/src/pnpm.ts @@ -0,0 +1,20 @@ +import { findWorkspacePackages } from "@pnpm/workspace.find-packages"; +import { filterChangedPkgs, getUnCommitFiles } from "./shared"; + +export const getChangedPackages = async () => { + const pkgs = await getPnpmWorkspacePkgs(process.cwd()); + const unCommitFiles = getUnCommitFiles(); + + return filterChangedPkgs(pkgs, unCommitFiles); +}; + +async function getPnpmWorkspacePkgs(cwd: string) { + return (await findWorkspacePackages(cwd)) + .filter((x) => !!x.manifest.name) + .map(({ rootDir: path, manifest: { name } }) => ({ + path, + name: name!, + })); +} + +export default getChangedPackages; diff --git a/tools/get-changed-packages/src/shared.ts b/tools/get-changed-packages/src/shared.ts new file mode 100644 index 0000000..2e37c8f --- /dev/null +++ b/tools/get-changed-packages/src/shared.ts @@ -0,0 +1,42 @@ +import { execSync } from "child_process"; +import { join } from "path"; + +export interface IPkg { + name: string; + path: string; +} +export function filterChangedPkgs(pkgs: IPkg[], unCommitFiles: string[]) { + const sortedPkgs = pkgs.slice().sort((a, b) => comparePath(a.path, b.path)); + const changedPkgs: string[] = []; + + for (const file of unCommitFiles) { + for (let pi = 0; pi < sortedPkgs.length; pi++) { + const { name, path } = sortedPkgs[pi]; + if (file.startsWith(path)) { + sortedPkgs.splice(pi, 1); + changedPkgs.push(name); + break; + } + } + + if (sortedPkgs.length === 0) { + return changedPkgs; + } + } + + return changedPkgs; +} + +export function comparePath(a: string, b: string) { + return b.length - a.length; +} + +export function getUnCommitFiles() { + const gitRoot = execSync("git rev-parse --show-toplevel").toString().trim(); + const statusString = execSync("git status -s").toString().trim(); + return statusString + .split(/\n\r?/g) + .filter(Boolean) + .map((x) => x.split(/\s+/)[1]) + .map((x) => join(gitRoot, x)); +} diff --git a/tools/get-changed-packages/tests/shared.test.ts b/tools/get-changed-packages/tests/shared.test.ts new file mode 100644 index 0000000..716e09a --- /dev/null +++ b/tools/get-changed-packages/tests/shared.test.ts @@ -0,0 +1,66 @@ +import { comparePath, filterChangedPkgs, IPkg } from "@/shared"; +import { name } from "../package.json"; + +describe(name, () => { + test(comparePath.name, () => { + expect(["a/b", "a/b/c"].sort(comparePath)).toEqual(["a/b/c", "a/b"]); + }); + + describe(filterChangedPkgs.name, () => { + const doTest = ( + pkgs: IPkg[], + unCommitFiles: string[], + expectedResult: string[], + ) => { + expect(filterChangedPkgs(pkgs, unCommitFiles)).toEqual(expectedResult); + }; + + test("should filter pkgs", () => { + const pkgs = [ + { + name: "web", + path: "/web", + }, + { + name: "api", + path: "/api", + }, + ]; + + const unCommitFiles = ["/web/src/index.ts", "/api/src/index.ts"]; + doTest(pkgs, unCommitFiles, ["web", "api"]); + }); + + test("should not include root", () => { + const pkgs = [ + { + name: "root", + path: "/", + }, + { + name: "web", + path: "/web", + }, + ]; + + const unCommitFiles = ["/web/src/index.ts"]; + doTest(pkgs, unCommitFiles, ["web"]); + }); + + test("should include root", () => { + const pkgs = [ + { + name: "root", + path: "/", + }, + { + name: "web", + path: "/web", + }, + ]; + + const unCommitFiles = ["/index.ts"]; + doTest(pkgs, unCommitFiles, ["root"]); + }); + }); +}); diff --git a/tools/get-changed-packages/tsconfig.json b/tools/get-changed-packages/tsconfig.json new file mode 100644 index 0000000..df8ad50 --- /dev/null +++ b/tools/get-changed-packages/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "@npcs/typescript-config", + "compilerOptions": { + "types": ["vitest/globals"], + "paths": { + "@/*": ["./src/*"] + } + }, + "include": ["./**/*"], + "exclude": ["./node_modules", "./esm", "./.rollup.cache"] +} diff --git a/tools/get-changed-packages/vitest.config.mts b/tools/get-changed-packages/vitest.config.mts new file mode 100644 index 0000000..4687ef1 --- /dev/null +++ b/tools/get-changed-packages/vitest.config.mts @@ -0,0 +1,3 @@ +import config from "@npcs/vitest-config"; + +export default config;