0
Fork 0
mirror of https://github.com/penpot/penpot-export.git synced 2025-03-05 20:21:17 -05:00

chore(cli): fail in a more elegant way

This commit is contained in:
Roberto Redradix 2023-09-26 17:35:40 +02:00
parent fe7691e6d1
commit 59dcbfef60

View file

@ -8,57 +8,65 @@ import { parsePenpotUrl } from '../penpot'
const [, , command, ...params] = process.argv
switch (command) {
case 'inspect': {
const [url] = params
try {
switch (command) {
case 'inspect': {
const [url] = params
if (!url) {
throw new Error(
'penpot-export: Missing URL to inspect. Provide an URL to the inspect command.',
)
}
try {
const parsed = parsePenpotUrl(url)
console.log(
[
'The following details are the result of inspecting the provided URL:',
`Penpot instance: ${parsed.instance}`,
`Workspace id: ${parsed.workspaceId}`,
`File id: ${parsed.fileId}`,
`Page id: ${parsed.pageId}`,
].join('\n\t'),
)
} catch (e) {
if (e instanceof TypeError) {
if (!url) {
throw new Error(
`penpot-export: URL inspection failed with the following error: ${e.message}.`,
)
} else {
throw new Error(
'penpot-export: URL inspection failed with an unknown error.',
'Missing URL to inspect. Provide an URL to the inspect command.',
)
}
try {
const parsed = parsePenpotUrl(url)
console.log(
[
'The following details are the result of inspecting the provided URL:',
`Penpot instance: ${parsed.instance}`,
`Workspace id: ${parsed.workspaceId}`,
`File id: ${parsed.fileId}`,
`Page id: ${parsed.pageId}`,
].join('\n\t'),
)
} catch (e) {
if (e instanceof TypeError) {
throw new Error(
`URL inspection failed with the following error: ${e.message}.`,
)
} else {
throw new Error('URL inspection failed with an unknown error.')
}
}
break
}
break
}
default: {
const rootProjectPath = fs.realpathSync(process.cwd())
const configFilePath = path.resolve(
rootProjectPath,
'penpot-export.config.js',
)
const exists = fs.existsSync(configFilePath)
if (!exists) {
throw new Error(
'penpot-export: Config file not found. Check if file penpot-export.config.js exists at root.',
default: {
const rootProjectPath = fs.realpathSync(process.cwd())
const configFilePath = path.resolve(
rootProjectPath,
'penpot-export.config.js',
)
const exists = fs.existsSync(configFilePath)
if (!exists) {
throw new Error(
'Config file not found. Check if file penpot-export.config.js exists at root.',
)
}
const config = require(configFilePath)
penpotExport(config, rootProjectPath)
}
const config = require(configFilePath)
penpotExport(config, rootProjectPath)
}
} catch (e) {
if (e instanceof Error) {
console.error(`penpot-export: ${e.message}`)
} else {
console.error('penpot-export: internal error')
}
process.exit(1)
}