Skip to content

Commit

Permalink
Add support for unified credentials file
Browse files Browse the repository at this point in the history
Lifted from the original repo
  • Loading branch information
tiennou committed Mar 4, 2025
1 parent 39023ae commit 2d58fab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
32 changes: 29 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,39 @@ export default {

plugins: [
...
screeps({configFile: "./screeps.json"})
screeps()
]
}

```

### Config File
### Yaml Config File

rollup-plugin-screeps needs your screeps username/password and the server to upload to.
rollup-plugin-screeps now uses the [screeps unified credentials file](https://github.com/screepers/screepers-standards/blob/master/SS3-Unified_Credentials_File.md), as used by [screeps-api](https://github.com/screepers/node-screeps-api).

Example `.screeps.yaml` config file:

```
servers:
main:
host: screeps.com
secure: true
token: '00000000-0a0a-0a00-000a-a0000a0000a0'
private:
host: 127.0.0.1
port: 21025
secure: false
username: bob
password: password123
```

Target server default to `main`, it can be selected with `screeps({ server: 'my-server' })` or the environment variable `$SCREEPS_SERVER`.

Branch *(aka the destination folder on screeps server)* default to `auto`, it can be select with `screeps({ branch: 'my-branch' })` or the environment variable `$SCREEPS_BRANCH`.

### JS Config File

rollup-plugin-screeps still support the json config file.

```json
{
Expand All @@ -44,4 +68,6 @@ rollup-plugin-screeps needs your screeps username/password and the server to upl

```

It change be loaded from a file with `screeps({ configFile: './screeps.json' })` or direct as value with `screeps({ config: my_config })`.

If `branch` is set to `"auto"` rollup-plugin-screeps will use your current git branch as the name of the branch on screeps, if you set it to anything else that string will be used as the name of the branch.
43 changes: 20 additions & 23 deletions src/rollup-plugin-screeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export interface ScreepsConfig {
branch: string | "auto"
}

export interface ScreepsOptions{
export interface ScreepsOptions {
configFile?: string
config?: ScreepsConfig
server?: string
branch?: string
dryRun?: boolean
}

Expand Down Expand Up @@ -73,30 +75,25 @@ export function validateConfig(cfg: Partial<ScreepsConfig>): cfg is ScreepsConfi
export function loadConfigFile(configFile: string) {
let data = fs.readFileSync(configFile, 'utf8')
let cfg = JSON.parse(data) as Partial<ScreepsConfig>
if (!validateConfig(cfg)) throw new TypeError("Invalid config")
if(cfg.email && cfg.password && !cfg.token && cfg.hostname === 'screeps.com'){ console.log('Please change your email/password to a token') }
return cfg;
}

export function uploadSource(name: string | ScreepsConfig, options: OutputOptions, bundle: OutputBundle) {
if (!name) {
console.log('screeps() needs a config e.g. screeps({configFile: \'./screeps.json\'}) or screeps({config: { ... }})')
} else {
let config = typeof name === "string" ? loadConfigFile(name) : name;

let code = getFileList(options.file!)
let branch = getBranchName(config.branch)

let api = new ScreepsAPI(config)

if(!config.token){
api.auth(config.email, config.password).then(() => {
runUpload(api, branch!, code)
})
}else{
runUpload(api, branch!, code)
}
export async function loadApi(opts: ScreepsOptions) {
if (opts.config || opts.configFile) {
let config = opts.config || loadConfigFile(opts.configFile!)
if (!validateConfig(config)) throw new TypeError("Invalid config")
const api = new ScreepsAPI(config)
if (!config.token) await api.auth(config.email, config.password)
return api
}
return ScreepsAPI.fromConfig(opts.server ?? process.env.SCREEPS_SERVER ?? 'main', false, { branch: opts.branch ?? process.env.SCREEPS_BRANCH });
}

export function uploadSource(api: ScreepsAPI, options: OutputOptions, bundle: OutputBundle) {
let code = getFileList(options.file!)
let branch = getBranchName((<Record<string, string>>api.opts).branch)
runUpload(api, branch!, code)
}

export function runUpload(api: any, branch: string, code: CodeList){
Expand Down Expand Up @@ -127,8 +124,8 @@ export function getFileList(outputFile: string) {
return code
}

export function getBranchName(branch: string) {
if (branch === 'auto') {
export function getBranchName(branch: string | undefined) {
if (!branch || branch === 'auto') {
return git.branch()
} else {
return branch
Expand All @@ -147,7 +144,7 @@ export function screeps(screepsOptions: ScreepsOptions = {}) {

async writeBundle(options: OutputOptions, bundle: OutputBundle) {
if (!screepsOptions.dryRun) {
uploadSource((screepsOptions.configFile || screepsOptions.config)!, options, bundle);
uploadSource(await loadApi(screepsOptions), options, bundle);
}
}
} as Plugin;
Expand Down

0 comments on commit 2d58fab

Please sign in to comment.