-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support nx config for commitlint
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
- Loading branch information
Showing
14 changed files
with
402 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/core/src/install/migrations/20240617094000-config-nx-scopes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { PackageJson } from "../../services/PackageJson"; | ||
import { | ||
createTestProjectDirWithFixtures, | ||
removeTestProjectDir, | ||
restorePackageJson, | ||
} from "../../tests/project"; | ||
import { up } from "./20240617094000-config-nx-scopes"; | ||
|
||
describe("Migration 20240617094000-config-nx-scopes", () => { | ||
let testProjectDir: string; | ||
|
||
beforeAll(() => { | ||
testProjectDir = createTestProjectDirWithFixtures(__filename); | ||
}); | ||
|
||
afterAll(() => { | ||
removeTestProjectDir(__filename); | ||
}); | ||
|
||
describe("Up", () => { | ||
afterEach(() => { | ||
restorePackageJson(__filename); | ||
}); | ||
|
||
it("should apply migration", async () => { | ||
await up(testProjectDir); | ||
|
||
const packageJsonContent = PackageJson.fromDirPath(testProjectDir).getContent(); | ||
|
||
expect(packageJsonContent).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
packages/core/src/install/migrations/20240617094000-config-nx-scopes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { MigrationUpFunction } from "../../services/MigrationsService"; | ||
import { PackageJson } from "../../services/PackageJson"; | ||
import { PackageManagerService } from "../../services/PackageManagerService"; | ||
|
||
export const up: MigrationUpFunction = async (absoluteProjectDir: string): Promise<void> => { | ||
const packageToInstall = "@commitlint/config-nx-scopes"; | ||
const nxDeps = ["@nrwl/workspace", "nx", "lerna"]; | ||
|
||
// Check if project is using nx or lerna | ||
const packageJson = PackageJson.fromDirPath(absoluteProjectDir); | ||
const hasNx = nxDeps.some((dep) => packageJson.hasDependency(dep)); | ||
|
||
if (!hasNx) { | ||
return; | ||
} | ||
|
||
// Ensure that package is installed | ||
if (!(await PackageManagerService.isPackageInstalled(packageToInstall, absoluteProjectDir))) { | ||
await PackageManagerService.addDevPackage(packageToInstall, absoluteProjectDir); | ||
} | ||
|
||
// Update commitlint config | ||
const commitlint = { | ||
extends: [packageToInstall], | ||
}; | ||
|
||
packageJson.merge({ | ||
commitlint, | ||
}); | ||
}; |
8 changes: 8 additions & 0 deletions
8
...es/core/src/install/migrations/__snapshots__/20240617094000-config-nx-scopes.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Migration 20240617094000-config-nx-scopes Up should apply migration 1`] = ` | ||
{ | ||
"license": "MIT", | ||
"version": "1.0.0", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 96 additions & 17 deletions
113
packages/core/src/services/PackageManagerService.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,112 @@ | ||
import { writeFileSync } from "fs"; | ||
import { join } from "path"; | ||
|
||
import { createTestProjectDirWithFixtures, removeTestProjectDir } from "../tests/project"; | ||
import { safeExec } from "../tests/cli"; | ||
import { createTestProjectDir, removeTestProjectDir } from "../tests/project"; | ||
import { PackageJson } from "./PackageJson"; | ||
import { PackageManagerService, PackageManagerType } from "./PackageManagerService"; | ||
|
||
describe("PackageManagerService", () => { | ||
let testProjectDir: string; | ||
|
||
beforeAll(() => { | ||
testProjectDir = createTestProjectDirWithFixtures(__filename); | ||
}); | ||
describe("detectPackageManager", () => { | ||
beforeEach(async () => { | ||
testProjectDir = createTestProjectDir(__filename); | ||
}); | ||
|
||
afterAll(() => { | ||
removeTestProjectDir(__filename); | ||
}); | ||
afterEach(() => { | ||
removeTestProjectDir(__filename); | ||
}); | ||
|
||
describe("detectPackageManager", () => { | ||
it("should retrieve the default package manager when no one is detectable", () => { | ||
const packageManager = PackageManagerService.detectPackageManager(testProjectDir); | ||
|
||
expect(packageManager).toEqual(PackageManagerType.npm); | ||
}); | ||
}); | ||
|
||
it("should retrieve the yarn package manager when yarn.lock file exists", () => { | ||
writeFileSync(join(testProjectDir, "yarn.lock"), "test"); | ||
describe.each([PackageManagerType.npm, PackageManagerType.yarn])( | ||
`with package manager %s`, | ||
(packageManagerType) => { | ||
const packageTypeTestFileName = __filename.replace( | ||
".spec.ts", | ||
`-${packageManagerType}.spec.ts` | ||
); | ||
beforeEach(async () => { | ||
testProjectDir = createTestProjectDir(packageTypeTestFileName); | ||
await safeExec(testProjectDir, `${packageManagerType} init --yes`); | ||
await safeExec(testProjectDir, `${packageManagerType} install --silent`); | ||
}); | ||
|
||
const packageManager = PackageManagerService.detectPackageManager(testProjectDir); | ||
afterEach(() => { | ||
removeTestProjectDir(packageTypeTestFileName); | ||
}); | ||
|
||
expect(packageManager).toEqual(PackageManagerType.yarn); | ||
}); | ||
}); | ||
describe("detectPackageManager", () => { | ||
it("should retrieve the current package manager", () => { | ||
const packageManager = PackageManagerService.detectPackageManager(testProjectDir); | ||
|
||
expect(packageManager).toEqual(packageManagerType); | ||
}); | ||
}); | ||
|
||
describe("isPackageInstalled", () => { | ||
it("should return false if package is not installed", async () => { | ||
const isInstalled = await PackageManagerService.isPackageInstalled( | ||
"test-package", | ||
testProjectDir | ||
); | ||
|
||
expect(isInstalled).toBeFalsy(); | ||
}); | ||
|
||
it("should return true if package is installed", async () => { | ||
await PackageManagerService.addDevPackage("test-package", testProjectDir); | ||
|
||
const isInstalled = await PackageManagerService.isPackageInstalled( | ||
"test-package", | ||
testProjectDir | ||
); | ||
|
||
expect(isInstalled).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe("isMonorepo", () => { | ||
it("should return false if project is not a monorepo", async () => { | ||
const isMonorepo = await PackageManagerService.isMonorepo(testProjectDir); | ||
|
||
expect(isMonorepo).toBeFalsy(); | ||
}); | ||
|
||
it("should return true if project is a monorepo", async () => { | ||
await PackageJson.fromDirPath(testProjectDir).merge({ | ||
private: true, | ||
workspaces: ["packages/*"], | ||
}); | ||
|
||
const testPackageDir = `${testProjectDir}/packages/test-package`; | ||
|
||
await safeExec(testProjectDir, `mkdir -p packages/test-package`); | ||
await safeExec(testPackageDir, `${packageManagerType} init --yes`); | ||
|
||
const isMonorepo = await PackageManagerService.isMonorepo(testProjectDir); | ||
|
||
expect(isMonorepo).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe("addDevPackage", () => { | ||
it("should add a dev package", async () => { | ||
expect( | ||
await PackageManagerService.isPackageInstalled("test-package", testProjectDir) | ||
).toBeFalsy(); | ||
|
||
await PackageManagerService.addDevPackage("test-package", testProjectDir); | ||
|
||
expect( | ||
await PackageManagerService.isPackageInstalled("test-package", testProjectDir) | ||
).toBeTruthy(); | ||
}); | ||
}); | ||
}, | ||
10000 | ||
); | ||
}); |
Oops, something went wrong.