Skip to content

Commit

Permalink
feat: basic init command working
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao committed Aug 10, 2023
1 parent 30903cd commit cc27102
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ build/
*.pem

# debug
tmp/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Expand Down
15 changes: 15 additions & 0 deletions bin/gemforge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node
import { spawnSync } from "node:child_process"
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from "node:path"

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

// find argument string
const index = process.argv.findIndex((arg) => arg === __filename)
const args = process.argv.slice(index + 1)

// Say our original entrance script is `app.js`
const cmd = `node --no-warnings ${resolve(__dirname, "../build/src/cli.js")}`
spawnSync(cmd, args, { stdio: "inherit", shell: true })
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@
"version": "1.0.0",
"description": "CLI for designing, building, deploying and upgrading Diamond Standard contracts on EVM chains.",
"bin": {
"gemforge": "./build/cli.js"
},
"exports": {
".": {
"types": "./build/index.d.ts",
"import": {
"development": "./src",
"default": "./build/index.js"
}
}
"gemforge": "./bin/gemforge.js"
},
"type": "module",
"types": "./build/src/index.d.ts",
"exports": "./build/src/index.js",
"author": "Ramesh Nair <ram@hiddentao.com>",
"license": "MIT",
"scripts": {
"prepare": "husky install && npx husky add .husky/commit-msg 'npx commitlint --edit $1'",
"prettify": "prettier --write .",
"dev": "tsc -b -w",
"build": "tsc -b"
"dev": "tsc -b -w tsconfig.json",
"build": "rm -fr build/* && tsc -p tsconfig.json"
},
"devDependencies": {
"@commitlint/cli": "^17.2.0",
Expand All @@ -41,5 +35,10 @@
"extends": [
"@commitlint/config-conventional"
]
},
"dependencies": {
"chalk": "^5.3.0",
"commander": "^11.0.0",
"execa": "^7.2.0"
}
}
84 changes: 75 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Command } from 'commander'

import packageJson from '../package.json' assert { "type": "json" }
import { command as init } from './cli/init.js'
import { command as build } from './cli/build.js'

const { version } = packageJson

const cli = new Command()

cli
.version(version)
.addCommand(init())
.addCommand(build())

cli.parseAsync(process.argv)

11 changes: 11 additions & 0 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Command } from 'commander'

export const command = () =>
new Command('init')
.description('Build a project.')
.option('-f, --folder <folder>', 'folder to run the build in', '.')
.option('-c, --config <config>', 'gemforge config file', 'gemforge.config.js')
.action(async (folder: string) => {
console.log(typeof folder, folder)
throw new Error('test')
})
34 changes: 34 additions & 0 deletions src/cli/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { $ } from 'execa'
import chalk from 'chalk'
import path, { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

export const $$ = $({ stdio: 'inherit' })

export const getContext = (args: Record<string, any>) => {
const context = {
folder: args.folder,
}

if (context.folder != '.') {
log(`Using folder ${context.folder}`)
context.folder = path.resolve(process.cwd(), context.folder)
} else {
context.folder = process.cwd()
}

return context
}

export const template = (file: string) => {
return path.resolve(__dirname, '../templates', file)
}

export const log = (message: string) => console.log(chalk.gray(message))
export const info = (message: string) => console.log(chalk.whiteBright(message))
export const success = (message: string) => console.log(chalk.greenBright(message))
export const error = (message: string) => console.log(chalk.redBright(message))
export const warn = (message: string) => console.log(chalk.yellowBright(message))
14 changes: 14 additions & 0 deletions src/cli/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Command } from 'commander'
import { $$, getContext, info, template } from './common.js'

export const command = () =>
new Command('init')
.description('Initialize a new project, generating all necessary scaffolding.')
.option('-f, --folder <folder>', 'folder to create the scaffold in', '.')
.action(async (args) => {
const ctx = getContext(args)

// write config file
info('Writing config file...')
await $$`cp ${template('gemforge.config.js')} ${ctx.folder}/gemforge.config.js`
})
Empty file removed src/index.ts
Empty file.
Loading

0 comments on commit cc27102

Please sign in to comment.