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-25 17:55:17 +01:00
|
|
|
import * as path from "path"
|
|
|
|
import * as fs from "fs-extra"
|
|
|
|
import { spawnSync } from "child_process"
|
2020-02-24 18:30:39 +01:00
|
|
|
|
|
|
|
import yargs from "yargs"
|
|
|
|
|
2020-03-03 14:53:54 +01:00
|
|
|
import { Package, loadPackage } from "../package"
|
|
|
|
import { Program } from "../program"
|
|
|
|
import { TextFile } from "../ast"
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
|
|
|
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.')
|
|
|
|
.default('work-dir', '.'),
|
|
|
|
|
|
|
|
args => {
|
|
|
|
|
|
|
|
const files = toArray(args.path as string[] | string).map(filepath => new TextFile(filepath, args['work-dir']));
|
|
|
|
const program = new Program(files)
|
|
|
|
program.compile({ target: "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-02-26 18:53:28 +01:00
|
|
|
|
2020-03-03 14:53:54 +01:00
|
|
|
for (const file of files) {
|
|
|
|
program.eval(file)
|
|
|
|
}
|
2020-02-26 18:53:28 +01:00
|
|
|
|
2020-03-03 14:53:54 +01:00
|
|
|
} else {
|
2020-02-26 18:53:28 +01:00
|
|
|
|
2020-03-03 14:53:54 +01:00
|
|
|
throw new Error(`Executing packages is not yet supported.`)
|
2020-02-26 18:53:28 +01:00
|
|
|
|
2020-03-03 14:53:54 +01:00
|
|
|
}
|
2020-02-26 18:53:28 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
2020-02-24 18:30:39 +01:00
|
|
|
.help()
|
|
|
|
.version()
|
|
|
|
.argv
|
|
|
|
|