Skip to content

Commit

Permalink
Run actions from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mordonez-me committed May 30, 2023
1 parent 2d51a51 commit 834758e
Show file tree
Hide file tree
Showing 16 changed files with 605 additions and 196 deletions.
Binary file modified .DS_Store
Binary file not shown.
70 changes: 70 additions & 0 deletions args/airdrop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import commandLineUsage from 'command-line-usage'
import commandLineArgs from 'command-line-args'
import { checkRequiredKeys, clusterCommandLine, convertOptionsToCamelCase, privateKeyCommandLine, validateFilesExistenceFromKeys } from './utils'
import _ from 'lodash'
import chalk from 'chalk'
import { AirdropParams, init } from '../src/airdrop'

export default (argv: any) => {
const createCollectionOptionList = [
privateKeyCommandLine,
clusterCommandLine,
{
name: 'candy-machine-address',
alias: 'm',
type: String,
description: 'Candy machine address.',
typeLabel: '{underline string}',
group: 'required'
},
{
name: 'distribution-list',
alias: 'd',
type: String,
description: 'List of address (one by line) to airdrop an NFT.',
typeLabel: '{underline string}',
group: 'required'
}
]
const createCollectionOptions = commandLineArgs(createCollectionOptionList, { argv })

const createCollectionDefinitions = [
{
header: 'NFT Airdrop for POAPs',
content: 'Send NFTs from a candy machine.'
},
{
header: 'Required options',
optionList: createCollectionOptionList,
group: ['required']
},
{
header: 'Optional (with default values)',
optionList: createCollectionOptionList,
group: ['_none']
}
]

const requiredKeysPresent = checkRequiredKeys(createCollectionOptionList, createCollectionOptions.required)

if (!requiredKeysPresent) {
console.log(commandLineUsage(createCollectionDefinitions))
} else {

const filesExistResult = validateFilesExistenceFromKeys(
['private-key', 'distribution-list'],
createCollectionOptions._all
)

if (filesExistResult.length > 0) {
_.forEach(filesExistResult, x => {
console.log(chalk.red(chalk.bold(`ERROR ====> ${x}`)))
})
return
}

const data = convertOptionsToCamelCase(createCollectionOptions._all)

init(data as AirdropParams)
}
}
102 changes: 102 additions & 0 deletions args/candy-machine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import commandLineUsage from 'command-line-usage'
import commandLineArgs from 'command-line-args'
import { checkRequiredKeys, clusterCommandLine, convertOptionsToCamelCase, privateKeyCommandLine, validateFilesExistenceFromKeys } from './utils'
import _ from 'lodash'
import chalk from 'chalk'
import { CreateCandyMachineParams, init } from '../src/candy-machine'

export default (argv: any) => {
const createCollectionOptionList = [
privateKeyCommandLine,
clusterCommandLine,
{
name: 'collection-address',
alias: 'a',
type: String,
description: 'Collection address.',
typeLabel: '{underline string}',
group: 'required'
},
{
name: 'quantity',
alias: 'q',
type: Number,
description: 'Quantity of items in the candy machine (items to be minted).',
typeLabel: '{underline number}',
group: 'required'
},
{
name: 'image-path',
alias: 'P',
type: String,
description: 'NFT image path',
typeLabel: '{underline file}',
group: 'required'
},
{
name: 'image-name',
alias: 'N',
type: String,
defaultValue: '',
description: 'NFT image name. Defaults to \'\'',
typeLabel: '{underline string}'
},
{
name: 'image-description',
alias: 'D',
type: String,
defaultValue: '',
description: 'NFT image description. Defaults to \'\'',
typeLabel: '{underline string}'
},
{
name: 'attributes',
alias: 'A',
type: String,
defaultValue: '',
description: 'NFT attributes with json format. Defaults to null',
typeLabel: '{underline string}'
},
]
const createCollectionOptions = commandLineArgs(createCollectionOptionList, { argv })

const createCollectionDefinitions = [
{
header: 'NFT Candy Machine for POAPs',
content: 'Creates a candy machine from a collection.'
},
{
header: 'Required options',
optionList: createCollectionOptionList,
group: ['required']
},
{
header: 'Optional (with default values)',
optionList: createCollectionOptionList,
group: ['_none']
}
]

const requiredKeysPresent = checkRequiredKeys(createCollectionOptionList, createCollectionOptions.required)

if (!requiredKeysPresent) {
console.log(commandLineUsage(createCollectionDefinitions))
} else {

const filesExistResult = validateFilesExistenceFromKeys(
['private-key', 'image-path'],
createCollectionOptions._all
)

if (filesExistResult.length > 0) {
_.forEach(filesExistResult, x => {
console.log(chalk.red(chalk.bold(`ERROR ====> ${x}`)))
})
return
}

const data = convertOptionsToCamelCase(createCollectionOptions._all)

init(data as CreateCandyMachineParams)
}
}
91 changes: 91 additions & 0 deletions args/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { clusterApiUrl } from '@solana/web3.js'
import commandLineArgs from 'command-line-args'
import commandLineUsage from 'command-line-usage'
import _ from 'lodash'
import { CreateParams, init } from '../src/create'
import { checkRequiredKeys, clusterCommandLine, convertOptionsToCamelCase, privateKeyCommandLine, validateFilesExistenceFromKeys } from './utils'
import chalk from 'chalk'

export default (argv: any) => {

const cluster = clusterApiUrl("devnet")

const createCollectionOptionList = [
privateKeyCommandLine,
{
name: 'collection-name',
alias: 'n',
type: String,
description: 'Collection name.',
typeLabel: '{underline string}',
group: 'required'
},
{
name: 'image-path',
alias: 'P',
type: String,
description: 'Collection image path',
typeLabel: '{underline file}',
group: 'required'
},
clusterCommandLine,
{
name: 'image-name',
alias: 'N',
type: String,
defaultValue: '',
description: 'Collection image name.\n{italic Defaults to \'\'.}\n',
typeLabel: '{underline string}'
},
{
name: 'image-description',
alias: 'D',
type: String,
defaultValue: '',
description: 'Collection image description.\n{italic Defaults to \'\'.}\n',
typeLabel: '{underline string}'
},
]

const createCollectionOptions = commandLineArgs(createCollectionOptionList, { argv })

const createCollectionDefinitions = [
{
header: 'NFT Collection for POAPs',
content: 'Creates a collection for POAP.'
},
{
header: 'Required options',
optionList: createCollectionOptionList,
group: ['required']
},
{
header: 'Optional (with default values)',
optionList: createCollectionOptionList,
group: ['_none']
}
]

const requiredKeysPresent = checkRequiredKeys(createCollectionOptionList, createCollectionOptions.required)

if (!requiredKeysPresent) {
console.log(commandLineUsage(createCollectionDefinitions))
} else {

const filesExistResult = validateFilesExistenceFromKeys(
['private-key', 'image-path'],
createCollectionOptions._all
)

if (filesExistResult.length > 0) {
_.forEach(filesExistResult, x => {
console.log(chalk.red(chalk.bold(`ERROR ====> ${x}`)))
})
return
}

const data = convertOptionsToCamelCase(createCollectionOptions._all)

init(data as CreateParams)
}
}
63 changes: 63 additions & 0 deletions args/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { clusterApiUrl } from "@solana/web3.js"
import commandLineUsage from 'command-line-usage'
import commandLineArgs from 'command-line-args'
import runCreateCollection from './create'
import runCreateCandyMachine from './candy-machine'
import runAirdrop from './airdrop'
import _ from 'lodash'

const commands = [
{
name: 'command', defaultOption: true,
description: 'create-collection | create-candy-machine | airdrop'
},
]

const mainDefinitions = [
{
header: 'NFT Collection for POAPs',
content: 'Creates a collection, candy machine and airdrop NFTs.'
},
{
header: 'Synopsis',
content: [
'$ create-collection --args',
'$ create-candy-machine --args',
'$ airdrop --args'
]
},
{
header: 'Options',
optionList: commands
}
]

const mainOptions = commandLineArgs(commands, { stopAtFirstUnknown: true })
const argv = mainOptions._unknown || []

// console.log('mainOptions\n===========')
// console.log(mainOptions)

const CREATE_COLLECTION = 'create-collection'
const CREATE_CANDY_MACHINE = 'create-candy-machine'
const AIRDROP = 'airdrop'

const allowedCommands = [CREATE_CANDY_MACHINE, CREATE_COLLECTION, AIRDROP]

const commandDetected = _.filter(_.map(
allowedCommands, command => mainOptions.command == command
), x => x == false)

// console.log('commandDetected', commandDetected)
if (commandDetected.length == allowedCommands.length) {
const mainUsage = commandLineUsage(mainDefinitions)
console.log(mainUsage)
}

if (mainOptions.command === CREATE_COLLECTION) {
runCreateCollection(argv)
} else if (mainOptions.command === CREATE_CANDY_MACHINE) {
runCreateCandyMachine(argv)
} else if (mainOptions.command === AIRDROP) {
runAirdrop(argv)
}
56 changes: 56 additions & 0 deletions args/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import _, { Dictionary } from 'lodash'
import * as fs from 'fs'
import * as path from 'path'
import { clusterApiUrl } from '@solana/web3.js'

// const fs = require('fs')
// const path = require('path')

export const checkFileExistence = (filePath: string) => {
const file = path.resolve(process.cwd(), filePath)
return fs.existsSync(file)
}

export const validateFilesExistenceFromKeys = (keys: string[], data: object) => {
const filePaths = _.values(_.pick(data, keys));
const filesExistResult = _.map(filePaths, x => {
const exist = checkFileExistence(x)
if (!exist) {
return `File ${x} doesn't exist`
} else {
return true
}
})
return _.filter(filesExistResult, x => typeof x == 'string')
}

export const checkRequiredKeys = (optionList: object, requiredOptionList: object) => {
const requiredKeys = _.map(_.filter(optionList, x => _.get(x, 'defaultValue') == undefined), 'name')
const passedKeys = _.keys(requiredOptionList)
return _.intersection(requiredKeys, passedKeys).length == requiredKeys.length
}

export const convertOptionsToCamelCase = (options: object): Dictionary<any> => {
return _.mapKeys(options, function (value, key) {
return _.camelCase(key);
});
}

export const privateKeyCommandLine = {
name: 'private-key',
alias: 'k',
type: String,
description: 'Path to a private key',
typeLabel: '{underline file}',
group: 'required'
}

const cluster = clusterApiUrl("devnet")
export const clusterCommandLine = {
name: 'cluster',
alias: 'c',
type: String,
defaultValue: cluster,
description: 'Cluster to be used.\n{italic Defaults to devnet.}\n',
typeLabel: '{underline url}'
}
2 changes: 2 additions & 0 deletions create-collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarn run v1.22.19
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Loading

0 comments on commit 834758e

Please sign in to comment.