bolt/src/bin/bolt.ts

117 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-02-24 18:30:39 +01:00
#!/usr/bin/env node
2020-02-25 12:26:21 +01:00
import "reflect-metadata"
2020-02-24 18:35:28 +01:00
import "source-map-support/register"
2020-02-24 18:30:39 +01:00
import yargs from "yargs"
2020-03-03 14:53:54 +01:00
import { Program } from "../program"
2020-05-10 15:56:34 +02:00
import { TextFile } from "../text"
2020-03-03 14:53:54 +01:00
global.debug = function (value: any) {
console.error(require('util').inspect(value, { depth: Infinity, colors: true }))
}
2020-02-24 18:30:39 +01:00
2020-02-25 17:55:17 +01:00
function toArray<T>(value: T | T[]): T[] {
2020-02-24 18:30:39 +01:00
if (Array.isArray(value)) {
return value as T[]
}
return value === null || value === undefined ? [] : [value]
}
function pushAll<T>(array: T[], elements: T[]) {
for (const element of elements) {
array.push(element);
}
}
2020-02-25 17:55:17 +01:00
function stripExtension(filepath: string) {
const i = filepath.lastIndexOf('.');
return i !== -1 ? filepath.substring(0, i) : filepath
}
2020-02-24 18:30:39 +01:00
function flatMap<T>(array: T[], proc: (element: T) => T[]) {
let out: T[] = []
for (const element of array) {
pushAll(out, proc(element));
}
return out
}
yargs
2020-02-25 17:55:17 +01:00
2020-02-24 18:30:39 +01:00
.command(
2020-03-03 14:53:54 +01:00
'link [name]',
'Link projects with each other',
2020-05-10 23:50:42 +02:00
2020-03-03 14:53:54 +01:00
yargs => yargs,
2020-02-24 18:30:39 +01:00
args => {
2020-03-03 14:53:54 +01:00
console.log(args.name)
2020-02-24 18:30:39 +01:00
2020-03-03 14:53:54 +01:00
}
2020-02-24 18:30:39 +01:00
2020-03-03 14:53:54 +01:00
)
2020-02-24 19:16:33 +01:00
2020-03-03 14:53:54 +01:00
.command(
2020-02-24 19:16:33 +01:00
2020-03-03 14:53:54 +01:00
'bundle [files..]',
'Compile and optimise a set of Bolt packages/scripts',
2020-02-25 12:26:21 +01:00
2020-03-03 14:53:54 +01:00
yargs => yargs
.string('work-dir')
.describe('work-dir', 'The working directory where files will be resolved against.')
2020-05-10 23:50:42 +02:00
.default('work-dir', '.')
.string('inspect-server'),
2020-03-03 14:53:54 +01:00
args => {
2020-05-10 23:50:42 +02:00
const files = toArray(args.files as string[] | string);
const opts = {};
if (args['inspect-server'] !== undefined) {
opts.inspector = connectToInspector(args['inspect-server'] as string);
}
const program = new Program(files, opts);
program.compile("JS");
2020-02-25 12:26:21 +01:00
2020-02-24 18:30:39 +01:00
})
2020-02-25 17:55:17 +01:00
.command(
'exec [files..]',
2020-03-03 14:53:54 +01:00
'Run the specified Bolt packages/scripts',
2020-02-25 17:55:17 +01:00
2020-03-03 14:53:54 +01:00
yargs => yargs
.string('work-dir')
.describe('work-dir', 'The working directory where files will be resolved against.')
.default('work-dir', '.'),
2020-02-25 17:55:17 +01:00
args => {
2020-03-03 14:53:54 +01:00
const files = toArray(args.files as string | string[]).map(p => new TextFile(p));
2020-02-25 17:55:17 +01:00
2020-03-03 14:53:54 +01:00
if (files.length > 0) {
2020-02-25 17:55:17 +01:00
2020-03-03 14:53:54 +01:00
const program = new Program(files);
2020-03-03 14:53:54 +01:00
for (const file of files) {
program.eval(file)
}
2020-03-03 14:53:54 +01:00
} else {
2020-03-03 14:53:54 +01:00
throw new Error(`Executing packages is not yet supported.`)
2020-03-03 14:53:54 +01:00
}
}
)
2020-02-24 18:30:39 +01:00
.help()
.version()
.argv