Skip to content

Commit

Permalink
fix([email protected]): replace flimsy shell script with JS entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Aug 16, 2023
1 parent 877bdc9 commit 0d39064
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 59 deletions.
4 changes: 2 additions & 2 deletions schema/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@fadroma/schema",
"version": "1.0.5",
"version": "1.0.6",
"type": "module",
"main": "schema.mjs",
"description": "Documentation generator for cosmwasm-schema",
"bin": {
"fadroma-schema": "./schema.sh"
"fadroma-schema": "./schema.cli.mjs"
},
"scripts": {
"start": "schema.mjs",
Expand Down
36 changes: 36 additions & 0 deletions schema/schema.cli.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node
import {argv, env, cwd} from 'node:process'
import {dirname, basename, resolve, relative} from 'node:path'
import {existsSync, realpathSync, readFileSync} from 'node:fs'
import {SchemaToMarkdown} from './schema.mjs'

if (argv.length > 2) {
process.stderr.write(`Converting schema: ${argv[2]}\n`)
const source = readFileSync(argv[2], 'utf8')
const parsed = JSON.parse(source)
const schema = new SchemaToMarkdown(argv[2], parsed)
process.stdout.write(schema.toMd())
} else {
// Shorten paths for usage
const paths = (env.PATH||'').split(':')
if (paths.includes(dirname(argv[0]))) {
argv[0] = basename(argv[0])
} else {
const resolvedPaths = paths
.map(path=>resolve(path, basename(argv[0])))
.filter(path=>existsSync(path))
.map(path=>realpathSync(path))
if (resolvedPaths.includes(argv[0])) {
argv[0] = basename(argv[0])
}
}
argv[1] = relative(cwd(), argv[1])
// Print usage
process.stderr.write([
`Fadroma Schema Renderer (https://fadroma.tech)`,
`Usage:`,
` ${process.argv.join(' ')} CONTRACT_SCHEMA.json`,
''
].join('\n'))
process.exit(1)
}
54 changes: 0 additions & 54 deletions schema/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -289,57 +289,3 @@ export class SchemaToMarkdown extends Schema {
return '(unsupported)'
}
}

// CLI entry point when running as a standalone script:
Promise.all([
import('node:process'),
import('node:url'),
import('node:path'),
import('node:fs'),
])
.then(
([
{argv, env, cwd},
{fileURLToPath},
{dirname, basename, resolve, relative},
{existsSync, realpathSync, readFileSync}
])=>{
const main = realpathSync(fileURLToPath(import.meta.url))
const self = realpathSync(argv[1])
if (main === self) {
if (argv.length > 2) {
process.stderr.write(`Converting schema: ${argv[2]}\n`)
const source = readFileSync(argv[2], 'utf8')
const parsed = JSON.parse(source)
const schema = new SchemaToMarkdown(argv[2], parsed)
process.stdout.write(schema.toMd())
} else {
// Shorten paths for usage
const paths = (env.PATH||'').split(':')
if (paths.includes(dirname(argv[0]))) {
argv[0] = basename(argv[0])
} else {
const resolvedPaths = paths
.map(path=>resolve(path, basename(argv[0])))
.filter(path=>existsSync(path))
.map(path=>realpathSync(path))
if (resolvedPaths.includes(argv[0])) {
argv[0] = basename(argv[0])
}
}
argv[1] = relative(cwd(), argv[1])
// Print usage
process.stderr.write([
`Fadroma Schema Renderer (https://fadroma.tech)`,
`Usage:`,
` ${process.argv.join(' ')} CONTRACT_SCHEMA.json`,
''
].join('\n'))
process.exit(1)
}
}
},
(e)=>{
console.info('Not running in Node.js')
}
)
3 changes: 0 additions & 3 deletions schema/schema.sh

This file was deleted.

0 comments on commit 0d39064

Please sign in to comment.