-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for detecting Stimulus Controllers in npm packages
- Loading branch information
Showing
14 changed files
with
449 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import path from "path" | ||
import { glob } from "glob" | ||
import { execSync } from "child_process" | ||
|
||
const fixtures = await glob("test/fixtures/**/package.json", { ignore: "**/**/node_modules/**" }) | ||
|
||
fixtures.forEach(async fixturesPath => { | ||
const fixtureFolder = path.dirname(fixturesPath) | ||
|
||
console.log(`Installing packages for fixture: ${fixtureFolder}`) | ||
|
||
execSync(`cd ${fixtureFolder} && yarn install && cd -`) | ||
}) |
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,7 @@ | ||
import { Project } from '../project' | ||
|
||
import * as tailwindcssStimulusComponents from "./tailwindcss-stimulus-components" | ||
|
||
export async function detectPackages(project: Project) { | ||
await tailwindcssStimulusComponents.analyze(project) | ||
} |
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,27 @@ | ||
import path from "path" | ||
import { glob } from "glob" | ||
|
||
import type { NodeModule } from "../types" | ||
import { Project } from '../project' | ||
import { hasDepedency, findPackagePath } from "./util" | ||
|
||
export async function analyze(project: Project) { | ||
const packageName = "tailwindcss-stimulus-components" | ||
const hasPackage = await hasDepedency(project.projectPath, packageName) | ||
const packagePath = await findPackagePath(project.projectPath, packageName) | ||
|
||
if (!hasPackage || !packagePath) return | ||
|
||
const basePath = path.join(packagePath, "src") | ||
const files = await glob(`${basePath}/**/*.js`) | ||
|
||
const detectedModule: NodeModule = { | ||
name: packageName, | ||
path: packagePath, | ||
controllerRoots: [basePath] | ||
} | ||
|
||
project.detectedNodeModules.push(detectedModule) | ||
|
||
await project.readControllerFiles(files) | ||
} |
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,43 @@ | ||
import path from "path" | ||
|
||
import { folderExists } from "../util" | ||
|
||
export async function findPackagePath(startPath: string, packageName: string): Promise<string|null> { | ||
const nodeModulesPath = await findNodeModulesPath(startPath) | ||
|
||
if (!nodeModulesPath) return null | ||
|
||
return path.join(nodeModulesPath, packageName) | ||
} | ||
|
||
export async function findNodeModulesPath(startPath: string): Promise<string|null> { | ||
const findFolder = async (splits: string[]): Promise<string|null> => { | ||
if (splits.length == 0) return null | ||
|
||
let possiblePath = path.join(...splits, "node_modules") | ||
|
||
if (!possiblePath.startsWith("/")) possiblePath = `/${possiblePath}` | ||
|
||
const exists = await folderExists(possiblePath) | ||
|
||
if (exists) return possiblePath | ||
|
||
return findFolder(splits.slice(0, splits.length - 1)) | ||
} | ||
|
||
return await findFolder(startPath.split("/")) | ||
} | ||
|
||
export function nodeModulesPathFor(nodeModulesPath: string, packageName: string): string { | ||
return path.join(nodeModulesPath, packageName) | ||
} | ||
|
||
export async function hasDepedency(projectPath: string, packageName: string): Promise<boolean> { | ||
const nodeModulesPath = await findNodeModulesPath(projectPath) | ||
|
||
if (!nodeModulesPath) return false | ||
|
||
const packagePath = nodeModulesPathFor(nodeModulesPath, packageName) | ||
|
||
return await folderExists(packagePath) | ||
} |
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
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
9 changes: 9 additions & 0 deletions
9
test/fixtures/packages/tailwindcss-stimulus-components/package.json
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,9 @@ | ||
{ | ||
"name": "tailwindcss-stimulus-components", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"tailwindcss-stimulus-components": "^4.0.4" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
test/fixtures/packages/tailwindcss-stimulus-components/yarn.lock
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 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
tailwindcss-stimulus-components@^4.0.4: | ||
version "4.0.4" | ||
resolved "https://registry.yarnpkg.com/tailwindcss-stimulus-components/-/tailwindcss-stimulus-components-4.0.4.tgz#1df5f2a488aa89365561bb33357095cd59ed831a" | ||
integrity sha512-xNlMs1WufKiTMQtVklwHfrR/iuPVaFA0Mk5uefRnHztmr7w4g6BzKAWHyfte60pjhcQbmlbshHMOZiq/dkXnhw== |
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,38 @@ | ||
import { describe, test, expect } from "vitest" | ||
import { Project } from "../../src" | ||
|
||
const project = new Project(`${process.cwd()}/test/fixtures/packages/tailwindcss-stimulus-components`) | ||
|
||
describe("packages", () => { | ||
describe("tailwindcss-stimulus-components", () => { | ||
test("detects controllers", async () => { | ||
expect(project.controllerDefinitions.length).toEqual(0) | ||
|
||
await project.analyze() | ||
|
||
expect(project.detectedNodeModules.map(module => module.name)).toEqual(["tailwindcss-stimulus-components"]) | ||
expect(project.controllerRoots).toEqual(["node_modules/tailwindcss-stimulus-components/src"]) | ||
expect(project.controllerDefinitions.length).toEqual(11) | ||
expect(project.controllerDefinitions.map(controller => controller.identifier).sort()).toEqual([ | ||
"alert", | ||
"autosave", | ||
"color-preview", | ||
"dropdown", | ||
"index", | ||
"modal", | ||
"popover", | ||
"slideover", | ||
"tabs", | ||
"toggle", | ||
"transition", | ||
]) | ||
|
||
const controller = project.controllerDefinitions.find(controller => controller.identifier === "modal") | ||
|
||
expect(controller.targets).toEqual(["container", "background"]) | ||
expect(Object.keys(controller.values)).toEqual(["open", "restoreScroll"]) | ||
expect(controller.values.open.type).toEqual("Boolean") | ||
expect(controller.values.restoreScroll.type).toEqual("Boolean") | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.