Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 743902e

Browse files
committed
Add basic functionality
0 parents  commit 743902e

File tree

10 files changed

+1354
-0
lines changed

10 files changed

+1354
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
node_modules/
3+
test.lua
4+
test.p8

8pack.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const assert = require('assert'),
2+
argv = require('minimist')(process.argv.slice(2)),
3+
fs = require('fs'),
4+
path = require('path'),
5+
{VERSIONS, getVersion} = require('./bin/versions'),
6+
removeFileExt = require('./bin/helpers.js'),
7+
writeOutput = (path, src) => {
8+
fs.writeFileSync(path, src);
9+
console.log('writing ' + path);
10+
}
11+
;
12+
13+
if (!argv._.length || argv.h || argv.help) {
14+
//show man page
15+
console.log(require('./bin/help'));
16+
process.exit(0)
17+
}
18+
19+
assert(argv._.length <= 2, 'too many arguments');
20+
21+
const input = path.resolve(argv._[0]); //use input path from arguments
22+
assert.ok(input, 'missing mandatory argument "input"'); //throw when missing input
23+
assert.doesNotThrow(() => fs.existsSync(input), `couldn't locate input file under ${input}`);
24+
25+
const output = argv._[1]; //use output from arguments
26+
if (output) {
27+
assert.doesNotThrow(() => fs.existsSync(output), `couldn't locate output file under ${output}`);
28+
}
29+
30+
const watch = argv.w || argv.watch;
31+
const template = argv.t || argv.template;
32+
33+
//read from input path
34+
const fileInputPath = path.resolve(input);
35+
console.log('reading ' + fileInputPath);
36+
37+
//inject input to inject path
38+
const fileInjectPath = !!output ? path.resolve(output): getVersion(template); //use specified file from output argument or template
39+
40+
//output the result to output path
41+
const fileOutputPath = output ? path.resolve(output) : fileInputPath + '.p8'; //create a new file when not specified the output
42+
console.log(`using ${fileOutputPath} as output`);
43+
44+
45+
const luaSource = fs.readFileSync(fileInputPath, 'utf8'); //contents of input file
46+
const picoSource = fs.readFileSync(fileInjectPath, 'utf8'); //contents of output file
47+
48+
const src = [
49+
picoSource.slice(0, picoSource.indexOf('__lua__') + 7),
50+
luaSource,
51+
picoSource.slice(picoSource.indexOf('__gfx__')),
52+
].join('\n');
53+
54+
if (watch) {
55+
console.log('watching for file changes on ' + fileInputPath);
56+
fs.watch(fileInputPath, {encoding: 'buffer'}, (eventType, filename) => {
57+
if (filename && eventType === 'change') {
58+
console.log("changes detected on " + filename.toString())
59+
writeOutput(fileOutputPath, src);
60+
}
61+
});
62+
}
63+
else {
64+
writeOutput(fileOutputPath, src);
65+
console.log('done');
66+
process.exit(0)
67+
}

bin/help.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const chalk = require('chalk');
2+
const {VERSIONS} = require('./versions');
3+
4+
const help = () =>
5+
`
6+
${chalk.bold('Summary')}: Utility that injects Lua source into a PICO-8 project.
7+
May also watch for file changes and create projects from scratch.
8+
9+
${chalk.bold('Usage')}: node 8pack.js [input] [output] [parameters]
10+
11+
${chalk.bold('Parameters')}:
12+
13+
${chalk.blue('-t, --template')} use pico8 template file as output.
14+
${chalk.blue('-w, --watch')} watch input for changes
15+
${chalk.blue('-h, --help ')} show this page
16+
17+
${chalk.bold('Template Versions')}:
18+
19+
${Object.entries(VERSIONS).reduce(([pk,pv],[nk,nv]) => `* ${pk} => ${pv}\n * ${nk} => ${nv}`)}
20+
21+
${chalk.bold('Examples')}:
22+
23+
node 8pack foo.lua
24+
create foo.p8 from foo.lua using newest PICO-8 template
25+
26+
node 8pack foo.lua bar.p8
27+
inject foo.lua source into bar.p8. May overwrite bar.p8 if already exists.
28+
29+
node 8pack /somepath/foo.lua -w
30+
watch foo.lua for changes and write to /somepath/foo.bar
31+
32+
node 8pack foo.lua bar.p8 -v
33+
create bar.p8 from foo.lua source using newest PICO-8 template and write to bar.p8
34+
`;
35+
36+
module.exports = help();

bin/helpers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* removes last suffix after dot
3+
* @param {String} name - filename
4+
*/
5+
const removeFileExt = name => name.split('.').filter((f, i, a) => i !== a.length - 1).join('.');
6+
7+
module.exports = {
8+
removeFileExt
9+
};

bin/versions.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const assert = require('assert');
4+
5+
const VERSIONS = {
6+
NEWEST: '0.1.10c.p8',
7+
'0.1.10c.p8': '0.1.10c.p8'
8+
};
9+
10+
11+
module.exports = {
12+
VERSIONS,
13+
/**
14+
* get filepath pico8 template for version name
15+
* @param {String|Boolean} version - pico8 project template version. Provide "true" for newest template.
16+
* @return {String} path - of the template file
17+
*/
18+
getVersion: version => {
19+
const _version =
20+
typeof version === 'boolean' ?
21+
VERSIONS.NEWEST :
22+
version === undefined ?
23+
VERSIONS.NEWEST :
24+
version; //use newest version if the template argument is supplied without value
25+
assert(Object.keys(VERSIONS).find(v => v === _version), `version ${_version} does not exist`);
26+
const filePath = path.resolve('./templates', _version);
27+
assert.doesNotThrow(() => fs.existsSync(filePath), `couldn't locate template for version ${_version} allthough it should exist`);
28+
return filePath;
29+
}
30+
};

package-lock.json

Lines changed: 202 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "pico8-packer",
3+
"version": "0.0.0",
4+
"description": "",
5+
"main": "8pack.js",
6+
"start": "8pack.js",
7+
"man": "./src/8pack.man",
8+
"scripts": {
9+
"test": "./node_modules/.bin/jasmine-node spec"
10+
},
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"chalk": "^2.0.1",
15+
"minimist": "^1.2.0"
16+
},
17+
"devDependencies": {
18+
"jasmine-node": "^1.14.5"
19+
}
20+
}

spec/8pack.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe('CLI Tests',()=>{
2+
it('should show the man page on no arguments',()=>{
3+
4+
});
5+
it('should show the man page on -h argument',()=>{
6+
7+
});
8+
it('compile using the -i argument',()=>{
9+
10+
});
11+
});
12+
13+
async function spawnWithArgs(){
14+
15+
16+
};

0 commit comments

Comments
 (0)