-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat add nuxt / vue detection useful for #144
- Loading branch information
Showing
7 changed files
with
1,079 additions
and
1,004 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,19 @@ | ||
import fs from 'node:fs/promises' | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import type { PackageJson } from '../types/PackageJson' | ||
|
||
export const getPackageJson = async (projectRoot?: string): Promise<PackageJson> => { | ||
let packageJsonPath = '' | ||
if (!projectRoot) { | ||
const filePath = fileURLToPath(import.meta.url) | ||
const dirname = path.dirname(filePath) | ||
const toolRoot = path.resolve(dirname, '..') | ||
packageJsonPath = path.join(toolRoot, 'package.json') | ||
} | ||
if (projectRoot) { | ||
packageJsonPath = path.join(projectRoot, 'package.json') | ||
} | ||
const packageJson: PackageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')) | ||
return packageJson | ||
} |
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,20 +1,23 @@ | ||
import path from 'node:path' | ||
import fs from 'node:fs/promises' | ||
|
||
const getProjectRoot = async () => { | ||
// eslint-disable-next-line node/prefer-global/process | ||
let currentDir = process.cwd() | ||
const getProjectRoot = async (startDir: string) => { | ||
let currentDir = startDir | ||
|
||
// eslint-disable-next-line no-unreachable-loop | ||
while (currentDir !== path.parse(currentDir).root) { | ||
// Check if package.json exists in the current directory | ||
const packageJsonPath = path.join(currentDir, 'package.json') | ||
await fs.access(packageJsonPath) | ||
return currentDir | ||
try { | ||
await fs.access(packageJsonPath) | ||
return currentDir | ||
// eslint-disable-next-line unused-imports/no-unused-vars | ||
} catch (e) { | ||
// File doesn't exist, move up one directory level | ||
currentDir = path.dirname(currentDir) | ||
} | ||
} | ||
|
||
// Move up one directory level | ||
currentDir = path.dirname(currentDir) | ||
throw new Error('Project root not found') | ||
} | ||
|
||
export default getProjectRoot |
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,29 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import getProjectRoot from './getProjectRoot' | ||
import { getPackageJson } from './getPackageJson' | ||
|
||
// eslint-disable-next-line node/prefer-global/process | ||
const projectPath = await getProjectRoot(process.cwd()) || '' | ||
|
||
const checkDependency = async (dependencyName: 'vue' | 'nuxt', projectRoot: string) => { | ||
const packageJsonPath = path.join(projectPath, 'package.json') | ||
if (fs.existsSync(packageJsonPath)) { | ||
const packageJson = await getPackageJson(projectRoot) | ||
return Boolean(packageJson.dependencies[dependencyName]) | ||
} | ||
return false | ||
} | ||
|
||
const isNuxtProject = async (projectRoot: string) => { | ||
const nuxtConfigFiles = ['nuxt.config.js', 'nuxt.config.ts'] | ||
return await checkDependency('nuxt', projectRoot) || nuxtConfigFiles.some(file => fs.existsSync(path.join(projectPath, file))) | ||
} | ||
|
||
const isVueProject = async (projectRoot: string) => { | ||
const vueConfigFiles = ['vue.config.js', 'vue.config.ts'] | ||
const isNuxt = await isNuxtProject(projectRoot) | ||
return !isNuxt && (await checkDependency('vue', projectRoot) || vueConfigFiles.some(file => fs.existsSync(path.join(projectPath, file)))) | ||
} | ||
|
||
export { isNuxtProject, isVueProject } |
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,7 @@ | ||
export interface PackageJson { | ||
version: string | ||
dependencies: { | ||
vue?: string | ||
nuxt?: string | ||
} | ||
} |