-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd3dd1f
commit 67c597a
Showing
2 changed files
with
84 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,5 @@ | ||
const core = require('@actions/core'); | ||
const github = require('@actions/github'); | ||
const fs = require('fs'); | ||
const getFiles = require('./utils/showFiles'); | ||
const replacer = require('./utils/replacer'); | ||
const Builder = require('./utils/Builder.js'); | ||
const builder = new Builder('./pack'); | ||
|
||
console.log('Build pack.mcmeta'); | ||
let packMcmeta; | ||
|
||
try { | ||
packMcmeta = JSON.parse(fs.readFileSync('./pack/pack.mcmeta')); | ||
} catch (err) { | ||
core.setFailed('Cannot read pack.mcmeta'); | ||
} | ||
|
||
if (packMcmeta.pack.pack_version) { | ||
packMcmeta.pack.description = packMcmeta.pack.description.replace(/\%pack_version\%/g, packMcmeta.pack.pack_version); | ||
core.setOutput("packVersion", packMcmeta.pack.pack_version); | ||
console.log(`Pack version is ${packMcmeta.pack.pack_version}`) | ||
delete packMcmeta.pack.pack_version; | ||
} | ||
|
||
fs.writeFileSync('./pack/pack.mcmeta', JSON.stringify(packMcmeta)); | ||
|
||
core.setOutput("packDescription", packMcmeta.pack.description); | ||
console.log('Minify json File'); | ||
|
||
|
||
const jsonFiles = getFiles('./pack/assets/', name => /^.+\.json$/.test(name)); | ||
|
||
for (const path of jsonFiles) { | ||
const json = JSON.parse(fs.readFileSync(path)); | ||
if (path === 'pack/assets/minecraft/lang/ja_jp.json') continue; | ||
if (path.startsWith('pack/assets/minecraft/models/')) { | ||
if (json.credit) delete json.credit; | ||
if (json.groups) delete json.groups; | ||
if (json.elements) { | ||
for (let i = 0; i < json.elements.length;i++) { | ||
if (json.elements[i].name) delete json.elements[i].name | ||
} | ||
} | ||
} | ||
fs.writeFileSync(path, JSON.stringify(json, replacer)); | ||
} | ||
builder.buildMeta(); | ||
builder.minify(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const core = require('@actions/core'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const getFiles = require('./showFiles'); | ||
const replacer = require('./replacer'); | ||
const replaceArgs = require('./replaceArgs'); | ||
|
||
class Builder { | ||
constructor (root) { | ||
this.root = root; | ||
} | ||
|
||
/** | ||
* return JSON File Paths | ||
* @param {String} p path | ||
* @returns {Array<String>} | ||
*/ | ||
getJSONFiles (p) { | ||
return getFiles(path.join(this.root, p), name => /^.+\.json$/.test(name)).map(name => path.relative('./pack', name)); | ||
} | ||
|
||
/** | ||
* return JSON File contains | ||
* @param {String} p path | ||
* @returns {Any} | ||
*/ | ||
loadJSONFile (p) { | ||
return JSON.parse(fs.readFileSync(path.join(this.root, p), { | ||
encoding: 'utf8', | ||
})); | ||
} | ||
|
||
saveJSONFile (p, data, minify=true) { | ||
if (minify) { | ||
if (p.startsWith('assets/minecraft/models')) { | ||
if (data.credit) delete data.credit; | ||
if (data.groups) delete data.groups; | ||
if (data.elements) for (let i = 0; i < data.elements.length;i++) if (data.elements[i].name) delete data.elements[i].name; | ||
} | ||
} | ||
fs.writeFileSync(path.join(this.root, p), JSON.stringify(data, replacer, minify ? undefined : '\t'), {encoding: 'utf8'}); | ||
} | ||
|
||
buildMeta () { | ||
const meta = this.loadJSONFile('pack.mcmeta'); | ||
meta.pack.description = replaceArgs(meta.pack.description, { | ||
pack_version: meta.pack.pack_version | ||
}); | ||
|
||
core.setOutput('packVersion', meta.pack.pack_version); | ||
core.setOutput("packDescription", meta.pack.description); | ||
|
||
delete meta.pack.pack_version; | ||
|
||
this.saveJSONFile('pack.mcmeta', meta); | ||
|
||
return meta; | ||
} | ||
|
||
minifyFiles (p) { | ||
const files = this.getJSONFiles(p); | ||
for (const file of files) this.minifyFile(file); | ||
} | ||
|
||
minifyFile (p) { | ||
try { | ||
this.saveJSONFile(p, this.loadJSONFile(p)); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
minify () { | ||
this.minifyFile('assets/minecraft/sounds.json'); | ||
this.minifyFiles('assets/minecraft/font'); | ||
this.minifyFiles('assets/minecraft/models'); | ||
} | ||
} | ||
|
||
module.exports = Builder; |