@travetto/base
Advanced tools
Comparing version 3.0.0-rc.7 to 3.0.0-rc.8
@@ -16,3 +16,2 @@ export * from './src/console'; | ||
export * from './src/object'; | ||
export * from './src/util'; | ||
export * from './src/watch'; | ||
export * from './src/util'; |
{ | ||
"name": "@travetto/base", | ||
"version": "3.0.0-rc.7", | ||
"version": "3.0.0-rc.8", | ||
"description": "Environment config and common utilities for travetto applications.", | ||
@@ -29,3 +29,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/manifest": "^3.0.0-rc.6", | ||
"@travetto/manifest": "^3.0.0-rc.7", | ||
"@types/source-map-support": "^0.5.6", | ||
@@ -35,4 +35,3 @@ "source-map-support": "^0.5.21" | ||
"peerDependencies": { | ||
"@parcel/watcher": "^2.1.0", | ||
"@travetto/transformer": "^3.0.0-rc.9" | ||
"@travetto/transformer": "^3.0.0-rc.10" | ||
}, | ||
@@ -42,5 +41,2 @@ "peerDependenciesMeta": { | ||
"optional": true | ||
}, | ||
"@parcel/watcher": { | ||
"optional": true | ||
} | ||
@@ -47,0 +43,0 @@ }, |
@@ -90,4 +90,5 @@ <!-- This file was generated by @travetto/doc and should not be modified directly --> | ||
const ᚕ_c = tslib_1.__importStar(require("@travetto/base/src/console.js")); | ||
var ᚕf = "@travetto/base/doc/transpile.js"; | ||
function work() { | ||
ᚕ_c.log({ level: "debug", source: __filename, line: 2, scope: "work", args: ['Start Work'] }); | ||
ᚕ_c.log({ level: "debug", source: ᚕf, line: 2, scope: "work", args: ['Start Work'] }); | ||
try { | ||
@@ -97,5 +98,5 @@ 1 / 0; | ||
catch (err) { | ||
ᚕ_c.log({ level: "error", source: __filename, line: 7, scope: "work", args: ['Divide by zero', { error: err }] }); | ||
ᚕ_c.log({ level: "error", source: ᚕf, line: 7, scope: "work", args: ['Divide by zero', { error: err }] }); | ||
} | ||
ᚕ_c.log({ level: "debug", source: __filename, line: 9, scope: "work", args: ['End Work'] }); | ||
ᚕ_c.log({ level: "debug", source: ᚕf, line: 9, scope: "work", args: ['End Work'] }); | ||
} | ||
@@ -102,0 +103,0 @@ exports.work = work; |
@@ -109,3 +109,3 @@ import util from 'util'; | ||
// Resolve input to source file | ||
const source = ev.source ? RootIndex.getSourceFile(ev.source) : RootIndex.mainModule.output; | ||
const source = ev.source ? RootIndex.getSourceFile(ev.source) : RootIndex.mainModule.outputPath; | ||
const mod = RootIndex.getModuleFromSource(source); | ||
@@ -117,3 +117,3 @@ const outEv = { | ||
module: ev.module ?? mod?.name, | ||
modulePath: ev.modulePath ?? (mod ? source.split(`${mod.source}/`)[1] : '') | ||
modulePath: ev.modulePath ?? (mod ? source.split(`${mod.sourceFolder}/`)[1] : '') | ||
}; | ||
@@ -120,0 +120,0 @@ |
@@ -87,5 +87,5 @@ /** | ||
*/ | ||
static export(filter: RegExp): Record<string, string | undefined> { | ||
static export(filter: RegExp, exclude?: RegExp): Record<string, string | undefined> { | ||
return Object.fromEntries(Object.entries(process.env) | ||
.filter(([k]) => filter.test(k)) | ||
.filter(([k]) => filter.test(k) && (!exclude || !(exclude?.test(k)))) | ||
.sort((a, b) => a[0].localeCompare(b[0])) | ||
@@ -92,0 +92,0 @@ ); |
import rl from 'readline'; | ||
import { ChildProcess, spawn, SpawnOptions } from 'child_process'; | ||
import { Readable } from 'stream'; | ||
import { SHARE_ENV, Worker, WorkerOptions, parentPort } from 'worker_threads'; | ||
import { SHARE_ENV, Worker, WorkerOptions } from 'worker_threads'; | ||
@@ -233,3 +233,2 @@ import { path } from '@travetto/manifest'; | ||
...process.env, | ||
TRV_MAIN: '', | ||
...((options.env !== SHARE_ENV ? options.env : {}) || {}), | ||
@@ -295,16 +294,2 @@ }; | ||
} | ||
/** | ||
* Run operation and send response across worker/fork/spawn/root | ||
*/ | ||
static returnResponse(res?: unknown, failure = false): void { | ||
if (parentPort) { | ||
parentPort.postMessage(res); | ||
} else { | ||
process.send?.(res); | ||
if (res) { | ||
process[!failure ? 'stdout' : 'stderr'].write(`${typeof res === 'string' ? res : JSON.stringify(res)}\n`); | ||
} | ||
} | ||
} | ||
} |
@@ -33,5 +33,2 @@ import { Env } from './env'; | ||
/** Get main value */ | ||
get main(): string | undefined { return Env.get('TRV_MAIN'); }, | ||
/** Is test */ | ||
@@ -63,3 +60,3 @@ get test(): boolean { return this.profiles.includes('test'); }, | ||
const env = cfg.envName ?? GlobalEnv.envName; | ||
const profiles = new Set([GlobalEnv.profiles, ...(cfg.profiles ?? [])]); | ||
const profiles = new Set([...GlobalEnv.profiles, ...(cfg.profiles ?? [])]); | ||
const isProd = /^prod/i.test(env); | ||
@@ -74,6 +71,2 @@ | ||
if ('main' in cfg) { | ||
set.TRV_MAIN = cfg.main; | ||
} | ||
for (const [k, v] of Object.entries(set)) { | ||
@@ -80,0 +73,0 @@ (v === undefined || v === null) ? delete process.env[k] : process.env[k] = `${v}`; |
import timers from 'timers/promises'; | ||
import { RootIndex } from '@travetto/manifest'; | ||
import { RootIndex, WatchEvent, watchFolders } from '@travetto/manifest'; | ||
import { ObjectUtil } from '../object'; | ||
import { FileWatchEvent, WatchUtil } from '../watch'; | ||
import { ShutdownManager } from '../shutdown'; | ||
type WatchHandler = (ev: FileWatchEvent) => (void | Promise<void>); | ||
type ManualWatchEvent = { type: 'trigger-watch' } & FileWatchEvent; | ||
type WatchHandler = (ev: WatchEvent) => (void | Promise<void>); | ||
type ManualWatchEvent = { trigger?: boolean } & WatchEvent; | ||
interface ModuleLoader { | ||
@@ -17,6 +16,7 @@ init?(): Promise<void>; | ||
function isEvent(ev: unknown): ev is ManualWatchEvent { | ||
function isTriggerEvent(ev: unknown): ev is ManualWatchEvent { | ||
return ObjectUtil.isPlainObject(ev) && | ||
'type' in ev && typeof ev.type === 'string' && ev.type === 'trigger-watch' && | ||
'action' in ev && 'file' in ev; | ||
('action' in ev && typeof ev.action === 'string') && | ||
('file' in ev && typeof ev.file === 'string') && | ||
('trigger' in ev && typeof ev.trigger === 'boolean'); | ||
} | ||
@@ -32,3 +32,3 @@ | ||
async dispatch(ev: FileWatchEvent): Promise<void> { | ||
async dispatch(ev: WatchEvent): Promise<void> { | ||
if (ev.action !== 'create') { | ||
@@ -67,6 +67,6 @@ await this.#loader.unload(ev.file); | ||
process.on('message', async ev => { | ||
if (isEvent(ev)) { | ||
if (isTriggerEvent(ev)) { | ||
const found = RootIndex.getFromSource(ev.file); | ||
if (found) { | ||
this.dispatch({ action: ev.action, file: found.output }); | ||
this.dispatch({ action: ev.action, file: found.outputFile }); | ||
} | ||
@@ -83,3 +83,8 @@ } | ||
await WatchUtil.buildOutputWatcher(ev => this.dispatch(ev)); | ||
// Watch all output | ||
await watchFolders( | ||
RootIndex.getLocalOutputFolders(), | ||
ev => this.dispatch(ev), | ||
{ filter: ev => ev.file.endsWith('.js') } | ||
); | ||
} | ||
@@ -86,0 +91,0 @@ } |
import fs from 'fs/promises'; | ||
import { path } from '@travetto/manifest'; | ||
import { watchFolders, WatchEvent, path } from '@travetto/manifest'; | ||
@@ -57,2 +57,15 @@ import { FileResourceConfig, FileResourceProvider } from './resource'; | ||
} | ||
/** | ||
* Watch resource files | ||
* @param onEvent | ||
* @param filter | ||
*/ | ||
watchFiles( | ||
onEvent: (ev: WatchEvent) => void, | ||
filter?: (ev: WatchEvent) => boolean | ||
): Promise<() => Promise<void>> { | ||
console.log('Watching', this.getAllPaths()); | ||
return watchFolders(this.getAllPaths(), onEvent, { filter }); | ||
} | ||
} |
@@ -55,9 +55,16 @@ import { Readable } from 'stream'; | ||
} | ||
const found = new Set(); | ||
return paths.map(pth => { | ||
const [base, sub] = pth.replace(/^@$/, main).replace(/^@#/, `${main}#`).split('#'); | ||
const rel = sub ?? (base !== main ? cfg.moduleFolder : undefined) ?? cfg.mainFolder; | ||
return RootIndex.hasModule(base) ? | ||
path.resolve(RootIndex.getModule(base)!.source, rel ?? '') : | ||
const value = RootIndex.hasModule(base) ? | ||
path.resolve(RootIndex.getModule(base)!.sourcePath, rel ?? '') : | ||
path.resolve(base, sub ?? cfg.mainFolder ?? ''); | ||
}); | ||
if (found.has(value)) { | ||
return undefined; | ||
} else { | ||
found.add(value); | ||
return value; | ||
} | ||
}).filter((x): x is string => !!x); | ||
} | ||
@@ -64,0 +71,0 @@ |
import { setTimeout } from 'timers/promises'; | ||
import { parentPort } from 'worker_threads'; | ||
@@ -187,2 +188,5 @@ import { RootIndex } from '@travetto/manifest'; | ||
/** | ||
* Trigger exit based on a code or a passed in error | ||
*/ | ||
exit(codeOrError: number | Error & { code?: number }): Promise<void> { | ||
@@ -192,4 +196,17 @@ const code = typeof codeOrError === 'number' ? codeOrError : (codeOrError?.code ?? 1); | ||
} | ||
/** | ||
* Trigger shutdown, and return response as requested | ||
*/ | ||
exitWithResponse(res: unknown, failure = false): Promise<void> { | ||
parentPort?.postMessage(res); | ||
process.send?.(res); | ||
if (res !== undefined) { | ||
const msg = typeof res === 'string' ? res : (res instanceof Error ? res.stack : JSON.stringify(res)); | ||
process[!failure ? 'stdout' : 'stderr'].write(`${msg}\n`); | ||
} | ||
return this.exit(!failure ? 0 : (res && res instanceof Error ? res : 1)); | ||
} | ||
} | ||
export const ShutdownManager = new $ShutdownManager(); |
@@ -89,2 +89,9 @@ import timers from 'timers/promises'; | ||
/** | ||
* Pretty print a delta between now and `time`, with auto-detection of largest unit | ||
*/ | ||
static prettyDeltaSinceTime(time: number, unit?: TimeUnit): string { | ||
return this.prettyDelta(Date.now() - time, unit); | ||
} | ||
/** | ||
* Pretty print a delta, with auto-detection of largest unit | ||
@@ -91,0 +98,0 @@ */ |
4
201
86316
24
2105
- Removed@parcel/watcher@2.5.0(transitive)
- Removed@parcel/watcher-android-arm64@2.5.0(transitive)
- Removed@parcel/watcher-darwin-arm64@2.5.0(transitive)
- Removed@parcel/watcher-darwin-x64@2.5.0(transitive)
- Removed@parcel/watcher-freebsd-x64@2.5.0(transitive)
- Removed@parcel/watcher-linux-arm-glibc@2.5.0(transitive)
- Removed@parcel/watcher-linux-arm-musl@2.5.0(transitive)
- Removed@parcel/watcher-linux-arm64-glibc@2.5.0(transitive)
- Removed@parcel/watcher-linux-arm64-musl@2.5.0(transitive)
- Removed@parcel/watcher-linux-x64-glibc@2.5.0(transitive)
- Removed@parcel/watcher-linux-x64-musl@2.5.0(transitive)
- Removed@parcel/watcher-win32-arm64@2.5.0(transitive)
- Removed@parcel/watcher-win32-ia32@2.5.0(transitive)
- Removed@parcel/watcher-win32-x64@2.5.0(transitive)
- Removedbraces@3.0.3(transitive)
- Removeddetect-libc@1.0.3(transitive)
- Removedfill-range@7.1.1(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-number@7.0.0(transitive)
- Removedmicromatch@4.0.8(transitive)
- Removednode-addon-api@7.1.1(transitive)
- Removedpicomatch@2.3.1(transitive)
- Removedto-regex-range@5.0.1(transitive)