Skip to content

Commit

Permalink
fix: Check module exists on finding requires
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanpaudel93 committed Mar 23, 2024
1 parent e0fa992 commit 78aa460
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
13 changes: 12 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import process from 'node:process'
import process, { emitWarning } from 'node:process'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import fs from 'node:fs'
Expand All @@ -10,6 +10,17 @@ import { type Tag, deployContract } from './lib/deploy'

const PKG_ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '../')

process.emitWarning = (warning, ...args) => {
if (args[0] === 'ExperimentalWarning')
return

if (args[0] && typeof args[0] === 'object' && args[0].type === 'ExperimentalWarning')
return

// @ts-expect-error "experimental warning"
return emitWarning(warning, ...args)
}

function getVersion() {
const packageJsonPath = path.join(PKG_ROOT, 'package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString())
Expand Down
24 changes: 13 additions & 11 deletions src/lib/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ interface Module { name: string, path: string, content?: string }

async function fileExists(path: string): Promise<boolean> {
try {
await fs.access(path, constants.R_OK)
return true // The file exists and is readable
await fs.access(path, constants.F_OK | constants.R_OK)
return true
}
catch {
return false // The file doesn't exist, isn't readable, or another error occurred
return false
}
}

async function getModulePath(module: string, cwd: string) {
try {
if (['json', '.base64', '.pretty', '.utils', '.crypto'].includes(module))
return

const modPath = path.join(cwd, `${module.replace(/\./g, '/')}.lua`)
if (await fileExists(modPath))
return modPath
Expand All @@ -44,10 +41,15 @@ async function getModulePath(module: string, cwd: string) {
const command = `lua -e "${luaCode}"`

const { stdout, stderr } = await execAsync(command)

if (stderr)
return

return stdout.trim()
if (stdout) {
const potentialPath = stdout.trim()
if (await fileExists(potentialPath))
return potentialPath
}
}
catch (error) {}
}
Expand Down Expand Up @@ -76,7 +78,7 @@ export async function createProjectStructure(mainFile: string, cwd: string) {
let orderedModNames = modules.map(m => m.name)

for (let i = 0; i < modules.length; i++) {
if (modules[i].content || !(await fs.stat(modules[i].path)).isFile())
if (modules[i].content)
continue

modules[i].content = (await fs.readFile(modules[i].path, 'utf-8'))
Expand Down Expand Up @@ -123,7 +125,7 @@ async function findRequires(data: string, cwd: string): Promise<Module[]> {
: null
})

return (await Promise.all(requiredModules)).filter(m => !!m)
return (await Promise.all(requiredModules)).filter(m => !!m) as Module[]
}

export async function loadContract(contractPath: string) {
Expand All @@ -133,9 +135,9 @@ export async function loadContract(contractPath: string) {
filePath = path.resolve(path.join(process.cwd(), contractPath))

if (!(await fileExists(filePath)))
throw new Error(chalk.red('ERROR: file not found.'))
throw new Error(chalk.red(`ERROR: ${filePath} file not found.`))

console.log(chalk.green('Loading... ', contractPath))
console.log(chalk.green('Deploying: ', contractPath))
let line = await fs.readFile(filePath, 'utf-8')

const spinner = ora({
Expand Down

0 comments on commit 78aa460

Please sign in to comment.