Skip to content

Commit

Permalink
chore(@npcs/get-changed-packages,@npcs/cli,@npcs/rollup-config,@npcs/…
Browse files Browse the repository at this point in the history
…template,npcs): add commit lint
  • Loading branch information
xiaosen7 committed Jul 26, 2024
1 parent 216dd9b commit 02921f8
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
2 changes: 1 addition & 1 deletion apps/template/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
10 changes: 10 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -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,
},
};
19 changes: 9 additions & 10 deletions configs/rollup/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-check

/* eslint-disable import/no-anonymous-default-export */

import resolve from "@rollup/plugin-node-resolve";
Expand All @@ -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",
Expand Down
3 changes: 0 additions & 3 deletions libs/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"private": true,
"packageManager": "[email protected]+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",
Expand Down
55 changes: 46 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ packages:
- "./configs/*"
- "./libs/*"
- "./dbs/*"
- "./tools/*"
3 changes: 3 additions & 0 deletions tools/get-changed-packages/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@npcs/eslint-config"]
}
40 changes: 40 additions & 0 deletions tools/get-changed-packages/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions tools/get-changed-packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @npcs/get-changed-packages

Get the changed packages from the last commit inside a workspace.
35 changes: 35 additions & 0 deletions tools/get-changed-packages/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
8 changes: 8 additions & 0 deletions tools/get-changed-packages/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable import/no-anonymous-default-export */

import config from "@npcs/rollup-config";

/** @type {import('rollup').RollupOptions} */
export default {
...config,
};
20 changes: 20 additions & 0 deletions tools/get-changed-packages/src/pnpm.ts
Original file line number Diff line number Diff line change
@@ -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;
42 changes: 42 additions & 0 deletions tools/get-changed-packages/src/shared.ts
Original file line number Diff line number Diff line change
@@ -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));
}
66 changes: 66 additions & 0 deletions tools/get-changed-packages/tests/shared.test.ts
Original file line number Diff line number Diff line change
@@ -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"]);
});
});
});
Loading

0 comments on commit 02921f8

Please sign in to comment.