feat(cli): Implement checkNPM utility and integrate it into module/locale publishing and registry functions

Should help #86 and #87
This commit is contained in:
melvinchia3636
2026-01-29 16:27:46 +08:00
parent a0b63d32dc
commit b07d19e12a
3 changed files with 26 additions and 4 deletions

View File

@@ -8,11 +8,13 @@ import executeCommand from '@/utils/commands'
import logger from '@/utils/logger'
import normalizePackage from '@/utils/normalizePackage'
import { getRegistryUrl } from '../../../utils/registry'
import { checkNPM, getRegistryUrl } from '../../../utils/registry'
import validateLocalesAuthor from '../functions/validateLocalesAuthor'
import { validateLocaleStructureHandler } from './validateLocaleStructure'
export async function publishLocaleHandler(langCode: string): Promise<void> {
checkNPM()
const { fullName, targetDir } = normalizePackage(langCode, 'locale')
if (!fs.existsSync(targetDir)) {

View File

@@ -9,7 +9,7 @@ import logger from '@/utils/logger'
import bumpPackageVersion, {
revertPackageVersion
} from '../../../utils/bumpPackageVersion'
import { getRegistryUrl } from '../../../utils/registry'
import { checkNPM, getRegistryUrl } from '../../../utils/registry'
import validateModuleAuthor from '../functions/validateModuleAuthor'
import validateModuleStructure from '../functions/validateModuleStructure'
@@ -57,6 +57,8 @@ function restoreGitignoreAfterPublish(modulePath: string): void {
* 7. Reverts version on failure
*/
export async function publishModuleHandler(moduleName: string): Promise<void> {
checkNPM()
const modulePath = path.join(ROOT_DIR, 'apps', moduleName)
if (!fs.existsSync(modulePath)) {

View File

@@ -6,6 +6,21 @@ import logger from '@/utils/logger'
import executeCommand from './commands'
export function checkNPM(): void {
try {
executeCommand('npm --version', { cwd: ROOT_DIR })
} catch (err) {
const errorMsg = err instanceof Error ? err.message : String(err)
if (errorMsg.includes('not found')) {
logger.error(
'npm not found. Please make sure npm is installed and accessible.'
)
process.exit(1)
}
}
}
/**
* Gets the registry URL for the @lifeforge scope from bunfig.toml.
*
@@ -36,6 +51,8 @@ export function getRegistryUrl(): string {
export async function checkPackageExists(
packageName: string
): Promise<boolean> {
checkNPM()
const registry = getRegistryUrl()
try {
@@ -60,6 +77,8 @@ export async function checkAuth(): Promise<{
authenticated: boolean
username?: string
}> {
checkNPM()
const registry = getRegistryUrl()
try {
@@ -79,8 +98,7 @@ export async function checkAuth(): Promise<{
throw new Error('Not authenticated')
} catch {
logger.warn('Not authenticated. Please login first.')
logger.error('Not authenticated. Please login first.')
process.exit(1)
}
}