Skip to content

Commit

Permalink
refactor: refactor run-foreman back to js and add allowjs to ts config
Browse files Browse the repository at this point in the history
  • Loading branch information
k80bowman committed Feb 6, 2024
1 parent beb4f3c commit 6930891
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ program.option('-p, --port <PORT>', 'start indexing ports at number PORT', 0

// Foreman Event Bus/Emitter //

const emitter = new events.EventEmitter()
emitter.once('killall', function (signal: any) {
var emitter = new events.EventEmitter()
emitter.once('killall', function (signal) {
display.Done('Killing all processes with signal ', signal)
})
emitter.setMaxListeners(50)

const start = _proc.start
const once = _proc.once
var start = _proc.start
var once = _proc.once

const loadProc = _procfile.loadProc
var loadProc = _procfile.loadProc

const loadEnvs = _envs.loadEnvs
var loadEnvs = _envs.loadEnvs

const getreqs = _requirements.getreqs
const calculatePadding = _requirements.calculatePadding
var getreqs = _requirements.getreqs
var calculatePadding = _requirements.calculatePadding

// Kill All Child Processes on SIGINT
process.once('SIGINT', function () {
Expand All @@ -69,29 +69,29 @@ program
.option('-t, --trim <N>', 'trim logs to N characters', 0)
.option('-w, --wrap', 'wrap logs (negates trim)')
.description('Start the jobs in the Procfile')
.action(function (this:any, args: any) {
const envs = loadEnvs(program.env)
.action(function (args) {
var envs = loadEnvs(program.env)

const proc = loadProc(program.procfile)
var proc = loadProc(program.procfile)

if (!proc) {
return
}

if (this.showenvs) {
for (const key in envs) {
for (var key in envs) {
display.Alert('env %s=%s', key, envs[key])
}
}

const reqs = getreqs(args, proc)
var reqs = getreqs(args, proc)

display.padding = calculatePadding(reqs)

display.raw = this.raw

if (this.wrap) {
display.wrapline = process.stdout.columns ? process.stdout.columns - display.padding - 7 : 0
display.wrapline = process.stdout.columns - display.padding - 7
display.trimline = 0
display.Alert('Wrapping display Output to %d Columns', display.wrapline)
} else {
Expand All @@ -116,26 +116,26 @@ program
.usage('[Options]')
.option('-s, --showenvs', 'show ENV variables on start', false)
.description('Run a one off process using the ENV variables')
.action(function (this: any, args: string | any[]) {
const envs = loadEnvs(program.env)
.action(function (args) {
var envs = loadEnvs(program.env)

const callback = function (code: number | undefined) {
var callback = function (code) {
process.exit(code)
}

if (!args || args.length === 0) {
return
}

const input = quote(args)
var input = quote(args)

if (this.showenvs) {
for (const key in envs) {
for (var key in envs) {
display.Alert('env %s=%s', key, envs[key])
}
}

display.trimline = process.stdout.columns ? process.stdout.columns - 5 : 0
display.trimline = process.stdout.columns - 5

once(input, envs, callback)
})
Expand All @@ -151,31 +151,31 @@ program
.option('-t, --type <TYPE>', 'export file to TYPE (default upstart)', 'upstart')
.option('-m, --template <DIR>', 'use template folder')
.description('Export to an upstart job independent of foreman')
.action(function (this: any, procArgs: any) {
const envs = loadEnvs(program.env)
.action(function (procArgs) {
var envs = loadEnvs(program.env)

const procs = loadProc(program.procfile)
var procs = loadProc(program.procfile)

if (!procs) {
return
}

const req = getreqs(procArgs, procs)
var req = getreqs(procArgs, procs)

// Variables for Upstart Template
const config = {
var config = {
application: this.app,
cwd: path.resolve(process.cwd(), this.cwd || ''),
user: this.user,
logs: this.log,
envs: envs,
group: this.gid || this.user,
template: this.template,
processes: [{}],
envfile: path.resolve(program.env),
}

let writeout
config.envfile = path.resolve(program.env)

var writeout
if (exporters[this.type]) {
writeout = exporters[this.type]
} else {
Expand All @@ -185,9 +185,9 @@ program

// Check for Upstart User
// friendly warning - does not stop export
let userExists = false
var userExists = false
fs.readFileSync('/etc/passwd')
.toString().split(/\n/).forEach(function (line: { match: (arg0: RegExp) => any[] }) {
.toString().split(/\n/).forEach(function (line) {
if (line.match(/^[^:]*/)[0] === config.user) {
userExists = true
}
Expand All @@ -198,39 +198,39 @@ program
}

// using port 5006 because it is not known to be used by other common software
const baseport = Number.parseInt(program.port || envs.PORT || process.env.PORT || 5006)
let baseport_i = 0
let baseport_j = 0
let envl = []
var baseport = Number.parseInt(program.port || envs.PORT || process.env.PORT || 5006)
var baseport_i = 0
var baseport_j = 0
var envl = []

config.processes = []

// This is ugly because of shitty support for array copying
// Cleanup is definitely required
for (let key in req) {
const cmd = procs[key]
for (var key in req) {
var c = {}
var cmd = procs[key]

if (!cmd) {
display.Warn("Required Key '%s' Does Not Exist in Procfile Definition", key)
continue
}

const n = req[key]
var n = req[key]

config.processes.push({process: key, n: n})
const c = {
...config,
process: key,
command: cmd,
numbers: [{}],
c.process = key
c.command = cmd

for (var _ in config) {
c[_] = config[_]
}

for (let i = 1; i <= n; i++) {
const conf = {
...c,
number: i,
port: (baseport + baseport_i + baseport_j) * 100,
}
c.numbers = []
for (var i = 1; i <= n; i++) {
const port = (baseport + baseport_i + baseport_j) * 100

envl = []
const envl = []
for (key in envs) {
envl.push({
key: key,
Expand All @@ -244,6 +244,13 @@ program

conf.envs = envl

const conf = {
...c,
number: i,
port,
envs: envl,
}

// Write the APP-PROCESS-N.conf File
writeout.foreman_app_n(conf, this.out)

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"declaration": false,
"extends": "../tsconfig",
"compilerOptions": {
"sourceMap": true
"sourceMap": true,
"rootDirs": ["./", "../src"]
},
"include": [
"./**/*",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"allowJs": true, // allowed to support run-foreman.js. Should be removed when that funcitonality is refactored.
"declaration": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
Expand Down

0 comments on commit 6930891

Please sign in to comment.