Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions bin/commands/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { mkdirSync, rmdirSync, existsSync, readFileSync, writeFileSync, cpSync } = require('node:fs');
const { join } = require('node:path');
const { execSync } = require('node:child_process');

exports.command = 'build';
exports.desc = 'build from static.json';
exports.builder = function (yargs) {
yargs
.option('out-directory', {
alias: 'out',
type: 'string',
default: './out',
describe: 'path the "out" directory will be saved to'
})
.option('next-base-path', {
type: 'string',
describe: 'Next.js basePath option'
})
}
exports.handler = function (argv) {
const staticExists = existsSync(argv.path);
if (!staticExists) {
throw Error(`Unable to locate ${argv.path}`);
}

const STATIC = JSON.parse(readFileSync(argv.path));

const workspace = join(__dirname, '..', 'workspace');

const exists = existsSync(workspace);
if (exists) {
rmdirSync(workspace, {
recursive: true
});
}

mkdirSync(workspace);

const {
application,
version
} = STATIC['_static'];

execSync(`cd ${workspace} && git clone ${application} . && npm ci`);

writeFileSync(join(workspace, 'static.json'), JSON.stringify(STATIC));

const nextConfiguration = {
output: 'export'
}

if (argv.nextBasePath) {
nextConfiguration.basePath = argv.nextBasePath
}

writeFileSync(
join(workspace, 'next.config.js'),
`module.exports = ${JSON.stringify(nextConfiguration)}`
)

execSync(`cd ${workspace} && npm run build`);

cpSync(join(workspace, 'out'), argv.out, {
recursive: true
});

rmdirSync(workspace, {
recursive: true
});
}
54 changes: 54 additions & 0 deletions bin/commands/lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { existsSync, readFileSync } = require('node:fs');

const Ajv = require("ajv");
const ajv = new Ajv();

exports.command = 'lint';
exports.desc = 'lint a static.json file';
exports.handler = function (argv) {
const staticExists = existsSync(argv.path);
if (!staticExists) {
throw Error(`Unable to locate ${argv.path}`);
}
let STATIC;
try {
STATIC = JSON.parse(readFileSync(argv.path));
} catch (e) {
throw Error(`Unable to parse ${argv.path} as JSON`);
}

const schema = {
type: "object",
properties: {
_static: {
type: "object",
properties: {
application: { type: "string"},
version: { type: "string" }
},
required: ["application"],
additionalProperties: false
},
metadata: {
type: "object",
properties: {
title: { type: "string" },
description: { type: "string" }
}
},
contents: {
type: "object"
}
},
required: ["_static"],
additionalProperties: false
}


const validate = ajv.compile(schema);

const valid = validate(STATIC);
if (!valid) {
console.error(validate.errors);
}
}
73 changes: 6 additions & 67 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,15 @@ const { join } = require('node:path');
const { execSync } = require('node:child_process');

require('yargs/yargs')(process.argv.slice(2))
.command('build', 'build from static.json', (yargs) => {
yargs
.option('path', {
type: 'string',
default: 'static.json',
describe: 'path to the static.json file to processs'
})
.option('out-directory', {
alias: 'out',
type: 'string',
default: './out',
describe: 'path the "out" directory will be saved to'
})
.option('next-base-path', {
type: 'string',
describe: 'Next.js basePath option'
})
}, (argv) => {
const staticExists = existsSync(argv.path);
if (!staticExists) {
throw Error(`Unable to locate ${argv.path}`);
}

const STATIC = JSON.parse(readFileSync(argv.path));

const workspace = join(__dirname, '..', 'workspace');

const exists = existsSync(workspace);
if (exists) {
rmdirSync(workspace, {
recursive: true
});
}

mkdirSync(workspace);

const {
application,
version
} = STATIC['_static'];

execSync(`cd ${workspace} && git clone ${application} . && npm ci`);

writeFileSync(join(workspace, 'static.json'), JSON.stringify(STATIC));

const nextConfiguration = {
output: 'export'
}

if (argv.nextBasePath) {
nextConfiguration.basePath = argv.nextBasePath
}

writeFileSync(
join(workspace, 'next.config.js'),
`module.exports = ${JSON.stringify(nextConfiguration)}`
)

execSync(`cd ${workspace} && npm run build`);

cpSync(join(workspace, 'out'), argv.out, {
recursive: true
});

rmdirSync(workspace, {
recursive: true
});
.option('path', {
type: 'string',
default: 'static.json',
describe: 'path to the static.json file to processs'
})
.commandDir('commands')
.help()
.argv




Expand Down
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"ajv": "^8.12.0",
"yargs": "^17.7.2"
}
}