Skip to content

Commit

Permalink
verifyFile
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Aug 11, 2024
1 parent e9d1dee commit 7ce0e5e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exit, fileExists } from '~/utils'
import { exit, verifyFile } from '~/utils'
import { run, capture } from '~/shell'
import Container from '~/container'

Expand All @@ -11,8 +11,7 @@ async function compose(contextName: string, command: string, path: string, optio
if (Container.find(contextName, path))
return run(`${base} ${command}${options.append ? ` ${path}` : ''}`, options)

if (options.exit && !fileExists(path))
exit(1, `👿 '${path}' must point to a valid docker-compose yaml file`)
options.exit && verifyFile(path)

return run(`${base} -f ${path} ${command}`, options)
}
Expand Down
5 changes: 2 additions & 3 deletions src/staxfile/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs'
import { exit, fileExists, makeTempFile } from '~/utils'
import { exit, makeTempFile, verifyFile } from '~/utils'
import path from 'path'
import yaml from 'js-yaml'
import DockerfileCompiler from './dockerfile_compiler'
Expand All @@ -11,8 +11,7 @@ export default class Compiler {
public baseDir: string

constructor(staxfile: string) {
if (!fileExists(staxfile))
exit(1, `Staxfile not found: ${staxfile}`)
verifyFile(staxfile, 'Staxfile not found')

this.staxfile = path.resolve(staxfile)
this.config = yaml.load(readFileSync(this.staxfile, 'utf-8'))
Expand Down
9 changes: 3 additions & 6 deletions src/staxfile/dockerfile_compiler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { readFileSync, writeFileSync } from 'fs'
import { exit, fileExists } from '~/utils'
import { exit, verifyFile } from '~/utils'

interface BuildOptions {
dockerfile: string;
Expand All @@ -19,9 +19,7 @@ export default class DockerfileCompiler {
const modules = this.loadModules()
let text = ""

if (!fileExists(this.build.dockerfile))
exit(1, `File not found: ${this.build.dockerfile}`)

verifyFile(this.build.dockerfile, 'Dockerfile not found')
readFileSync(this.build.dockerfile, 'utf-8').split("\n").forEach((line) => {
const matches = line.trim().match(/# \$stax\.section +(.*?)$/)

Expand Down Expand Up @@ -55,8 +53,7 @@ export default class DockerfileCompiler {
}

private parseModuleFile(file: string, modules: Record<string, string>) {
if (!fileExists(file))
exit(1, `Module file not found: ${file}`)
verifyFile(file, 'Module file not found')

const contents = readFileSync(file, 'utf-8')
let sectionName: string
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export function fileExists(file) {
return existsSync(file)
}

export function verifyFile(file: string, message: string = undefined): boolean {
if (!fileExists(file))
exit(1, (message || 'File not found') + `: ${file}`)
return true
}

export function directoryExists(path: string): boolean {
return existsSync(path) && statSync(path).isDirectory();
}
Expand Down

0 comments on commit 7ce0e5e

Please sign in to comment.