forked from rvramesh/turbowarp-packager-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (62 loc) · 2.42 KB
/
Copy pathindex.js
File metadata and controls
72 lines (62 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /usr/bin/env node
const fs = require("fs")
const path = require("path")
const AdmZip = require("adm-zip")
const Packager = require("@turbowarp/packager")
const { program } = require("commander")
// Define the command line arguments
program
.requiredOption("-i, --input <inputFile>", "The input Scratch 3.0 project file (Required)")
.requiredOption(
"-o, --output <outputFolder>",
"The output folder for the packaged project (Required)"
)
.option("-s, --settings <settingsFile>", "The settings file for the packager (Optional)")
.parse(process.argv)
const args = program.opts()
// Check that all required arguments are present
if (!args.input || !args.output) {
console.error("Please provide --input, --output, and optionally --settings arguments")
process.exit(1)
}
args.settings = args.settings
? path.join("./", args.settings)
: path.join(__dirname, "turbowarp-packager-settings.json")
// Read the settings from a JSON file
const settings = JSON.parse(fs.readFileSync(args.settings, "utf-8"))
//extractzip method accepts zip file and extracts it to the specified path
const extractzip = (zipPath, outputPath) => {
const zip = new AdmZip(zipPath)
zip.extractAllTo(outputPath, true)
}
const run = async (inputPath, outputPath, settings) => {
const projectData = await fs.promises.readFile(path.join("./", inputPath))
const loadedProject = await Packager.loadProject(projectData)
const packager = new Packager.Packager()
packager.project = loadedProject
packager.options = settings
const result = await packager.package()
let data = result.data
if (data instanceof ArrayBuffer) {
// If packager.options.target wasn't "html", data will be an ArrayBuffer instead of a string.
// Node.js filesystem API doesn't like ArrayBuffers, so we'll convert it to something it understands.
data = new Uint8Array(data)
}
const extension = result.type === "text/html" ? ".html" : ".zip"
const outputDir = path.join("./", outputPath)
if (!fs.existsSync(outputDir)) {
await fs.promises.mkdir(outputDir, { recursive: true })
}
const file = path.join(outputDir, "demo_output" + extension)
await fs.promises.writeFile(file, data)
console.log(`Wrote ${file} (${data.length} bytes)`)
if (extension === ".zip") {
extractzip(file, outputDir)
await fs.promises.unlink(file)
}
}
;(async () =>
run(args.input, args.output, settings).catch((err) => {
console.error(err)
process.exit(1)
}))()