Skip to content

Commit

Permalink
Added crx init command to create plugin project
Browse files Browse the repository at this point in the history
  • Loading branch information
connerdouglass committed Nov 11, 2021
1 parent b11cc4a commit 643c66e
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/crx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/customrealms/cli/lib"
)
Expand All @@ -25,14 +26,32 @@ func main() {
case "version":
fmt.Printf("customrealms-cli (crx) v%s\n", VERSION)
case "init":
fmt.Println("crx build ... is not yet implemented")
os.Exit(1)
crxInit()
case "build":
crxBuild()
}

}

func crxInit() {

var projectDir string

cwd, err := os.Getwd()
if err != nil {
panic(err)
}

flag.StringVar(&projectDir, "p", cwd, "plugin project directory")

flag.CommandLine.Parse(os.Args[2:])

if err := lib.InitDir(projectDir, filepath.Base(projectDir)); err != nil {
panic(err)
}

}

func crxBuild() {

var projectDir string
Expand Down
157 changes: 157 additions & 0 deletions lib/init_dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package lib

import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

//go:embed initial/main.ts
var mainTs string

//go:embed initial/tsconfig.json
var tsconfigJson string

//go:embed initial/webpack.config.js
var webpackConfigJs string

type PackageJSONForInit struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
Scripts map[string]string `json:"scripts"`
Keywords []string `json:"keywords"`
Author string `json:"author"`
License string `json:"license"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
}

func InitDir(dir, name string) error {

// Setup the files
if err := initDirFiles(dir, name); err != nil {
return err
}

// Run npm install
cmd := exec.Command("npm", "install")
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}

// Initialize the git repo
cmd = exec.Command("git", "init")
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}

return nil
}

func initDirFiles(dir, name string) error {

// If there are already files in the dir
entries, err := os.ReadDir(dir)
if err != nil && !os.IsNotExist(err) {
return err
}
if len(entries) > 0 {
return errors.New("directory already contains files")
}

// Make the directory
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}

// Write the .gitignore file

gitignore := []string{
"/node_modules",
"/dist",
".DS_Store",
"",
}
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte(strings.Join(gitignore, "\n")), 0777); err != nil {
return err
}

// Write the package.json file

packageJson := PackageJSONForInit{
Name: name,
Version: "1.0.0",
Description: "",
Scripts: map[string]string{
"build:jar": fmt.Sprintf("crx build -o ./dist/%s.jar", name),
"build": "webpack --mode=production",
"clean": "rm -rf ./dist",
},
Keywords: []string{},
Author: "",
License: "ISC",
Dependencies: map[string]string{
"@customrealms/core": "^0.1.0",
},
DevDependencies: map[string]string{
"@customrealms/cli": "^0.1.0",
"ts-loader": "^9.2.6",
"tslib": "^2.3.1",
"typescript": "^4.4.4",
"webpack": "^5.63.0",
"webpack-cli": "^4.9.1",
},
}
jsonBytes, err := json.MarshalIndent(packageJson, "", "\t")
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(dir, "package.json"), jsonBytes, 0777); err != nil {
return err
}

// Write the tsconfig.json

if err := os.WriteFile(filepath.Join(dir, "tsconfig.json"), []byte(tsconfigJson), 0777); err != nil {
return err
}

// Write the webpack config

if err := os.WriteFile(
filepath.Join(dir, "webpack.config.js"),
[]byte(webpackConfigJs),
0777,
); err != nil {
return err
}

// Write the main.ts file

if err := os.MkdirAll(filepath.Join(dir, "src"), 0777); err != nil {
return err
}

if err := os.WriteFile(
filepath.Join(dir, "src", "main.ts"),
[]byte(mainTs),
0777,
); err != nil {
return err
}

return nil

}
24 changes: 24 additions & 0 deletions lib/initial/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PlayerChatEvent, ServerCommands, ServerEvents } from '@customrealms/core';

// Listen for player chat events
ServerEvents.register(PlayerChatEvent, event => {

const player = event.getPlayer();
const message = event.getMessage();

// Log something to the server console
console.log(`Player "${player.getName()}" said "${message}"`);

// Send a response to the player who chatted
player.sendMessage(`I see that you said "${message}"`)

});

// Listen for /title commands
ServerCommands.register('/title {message}...', (player, call) => {

const message = call.getPlaceholder('message')!;

player.sendTitle(message, null, 20, 20, 20);

});
28 changes: 28 additions & 0 deletions lib/initial/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compileOnSave": false,
"compilerOptions": {
"strict": true,
"outDir": "./dist",
"sourceMap": false,
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"importHelpers": true,
"target": "es3",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"files": [
"src/main.ts"
]
}
21 changes: 21 additions & 0 deletions lib/initial/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('path');

module.exports = {
entry: './src/main.ts',
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};

0 comments on commit 643c66e

Please sign in to comment.