6 Commits

Author SHA1 Message Date
J H
87d3e83eb4 chores 2024-02-13 15:49:31 -07:00
J H
599da8f52c chore: Update some things to make a single export? 2024-02-12 15:15:25 -07:00
J H
17ec714277 Convert back to old common js 2024-02-08 13:35:56 -07:00
Aiden McClelland
dc7a86a8e8 fixes from testing 2024-02-08 13:30:32 -07:00
J H
4b1834a490 test 2024-02-02 08:51:56 -07:00
J H
dae4748659 chore: Beta 7 2024-02-02 08:42:57 -07:00
24 changed files with 178 additions and 115 deletions

View File

@@ -13,8 +13,16 @@ buildOutput: lib/test/output.ts fmt
echo 'done'
bundle: clean $(TS_FILES) package.json .FORCE node_modules test fmt
bundle: $(TS_FILES) package.json .FORCE node_modules test fmt
npx tsc
npx tsc --project tsconfig-cjs.json
cp package.json dist/package.json
cp README.md dist/README.md
cp LICENSE dist/LICENSE
full-bundle:
make clean
make bundle
cp package.json dist/package.json
cp README.md dist/README.md
cp LICENSE dist/LICENSE

View File

@@ -14,6 +14,7 @@ export type ValueType =
| "union"
export type ValueSpec = ValueSpecOf<ValueType>
/** core spec types. These types provide the metadata for performing validations */
// prettier-ignore
export type ValueSpecOf<T extends ValueType> = T extends "text"
? ValueSpecText
: T extends "textarea"

View File

@@ -2,7 +2,7 @@ import { Effects, ExpectedExports } from "../types"
import { SDKManifest } from "../manifest/ManifestTypes"
import * as D from "./configDependencies"
import { Config, ExtractConfigType } from "./builder/config"
import { Utils, utils } from "../util/utils"
import { Utils, createUtils } from "../util/utils"
import nullIfEmpty from "../util/nullIfEmpty"
import { InterfaceReceipt } from "../interfaces/interfaceReceipt"
import { InterfacesReceipt as InterfacesReceipt } from "../interfaces/setupInterfaces"
@@ -72,7 +72,7 @@ export function setupConfig<
const { restart } = await write({
input: JSON.parse(JSON.stringify(input)),
effects,
utils: utils(effects),
utils: createUtils(effects),
dependencies: D.configDependenciesSet<Manifest>(),
})
if (restart) {
@@ -80,7 +80,7 @@ export function setupConfig<
}
}) as ExpectedExports.setConfig,
getConfig: (async ({ effects }) => {
const myUtils = utils<Manifest, Store>(effects)
const myUtils = createUtils<Manifest, Store>(effects)
const configValue = nullIfEmpty(
(await read({ effects, utils: myUtils })) || null,
)

View File

@@ -3,7 +3,7 @@ import {
DeepPartial,
Effects,
} from "../types"
import { Utils, utils } from "../util/utils"
import { Utils, createUtils } from "../util/utils"
import { deepEqual } from "../util/deepEqual"
import { deepMerge } from "../util/deepMerge"
import { SDKManifest } from "../manifest/ManifestTypes"
@@ -41,7 +41,7 @@ export class DependencyConfig<
return this.dependencyConfig({
localConfig: options.localConfig as Input,
effects: options.effects,
utils: utils<Manifest, Store>(options.effects),
utils: createUtils<Manifest, Store>(options.effects),
})
}
}

View File

@@ -1,20 +1,22 @@
import "./backup"
import "./config"
import "./config/builder"
import "./config/configTypes"
import "./health"
import "./health/checkFns"
import "./mainFn"
import "ts-matches"
import "./types"
import "@iarna/toml"
import "./types"
import "./util"
import "yaml"
import "./dependencyConfig"
import "./actions"
import "./manifest"
import "./inits"
export { Daemons } from "./mainFn/Daemons"
export { EmVer } from "./emverLite/mod"
export { Overlay } from "./util/Overlay"
export { Utils } from "./util/utils"
export * as actions from "./actions"
export * as backup from "./backup"
export * as config from "./config"
export * as configBuilder from "./config/builder"
export * as configTypes from "./config/configTypes"
export * as dependencyConfig from "./dependencyConfig"
export * as health from "./health"
export * as healthFns from "./health/checkFns"
export * as inits from "./inits"
export * as mainFn from "./mainFn"
export * as manifest from "./manifest"
export * as toml from "@iarna/toml"
export * as types from "./types"
export * as util from "./util"
export * as yaml from "yaml"
export * as matches from "ts-matches"
export * as YAML from "yaml"

View File

@@ -3,7 +3,7 @@ import { Utils } from "../util/utils"
export type SetupExports<Store> = (opts: {
effects: Effects
utils: Utils<Store>
utils: Utils<any, Store>
}) =>
| {
ui: ExposeUiPaths<Store>

View File

@@ -1,6 +1,6 @@
import { SDKManifest } from "../manifest/ManifestTypes"
import { Effects, ExpectedExports } from "../types"
import { Utils, utils } from "../util/utils"
import { Utils, createUtils } from "../util/utils"
export type InstallFn<Manifest extends SDKManifest, Store> = (opts: {
effects: Effects
@@ -21,7 +21,7 @@ export class Install<Manifest extends SDKManifest, Store> {
if (!previousVersion)
await this.fn({
effects,
utils: utils(effects),
utils: createUtils(effects),
})
}
}

View File

@@ -1,6 +1,6 @@
import { SDKManifest } from "../manifest/ManifestTypes"
import { Effects, ExpectedExports } from "../types"
import { Utils, utils } from "../util/utils"
import { Utils, createUtils } from "../util/utils"
export type UninstallFn<Manifest extends SDKManifest, Store> = (opts: {
effects: Effects
@@ -21,7 +21,7 @@ export class Uninstall<Manifest extends SDKManifest, Store> {
if (!nextVersion)
await this.fn({
effects,
utils: utils(effects),
utils: createUtils(effects),
})
}
}

View File

@@ -52,7 +52,7 @@ Daemons.of({
export class Daemons<Manifest extends SDKManifest, Ids extends string> {
private constructor(
readonly effects: Effects,
readonly started: (onTerm: () => void) => null,
readonly started: (onTerm: () => PromiseLike<void>) => PromiseLike<void>,
readonly daemons?: Daemon<Manifest, Ids, "command", Ids>[],
) {}
/**
@@ -67,7 +67,7 @@ export class Daemons<Manifest extends SDKManifest, Ids extends string> {
*/
static of<Manifest extends SDKManifest>(config: {
effects: Effects
started: (onTerm: () => void) => null
started: (onTerm: () => PromiseLike<void>) => PromiseLike<void>
healthReceipts: HealthReceipt[]
}) {
return new Daemons<Manifest, never>(config.effects, config.started)

View File

@@ -1,6 +1,6 @@
import { Effects, ExpectedExports } from "../types"
import { createMainUtils } from "../util"
import { Utils, utils } from "../util/utils"
import { Utils, createUtils } from "../util/utils"
import { Daemons } from "./Daemons"
import "../interfaces/NetworkInterfaceBuilder"
import "../interfaces/Origin"

View File

@@ -9,11 +9,8 @@ import * as _I from "../index"
import { camelCase } from "../../scripts/oldSpecToBuilder"
import { deepMerge } from "../util/deepMerge"
export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T
? 1
: 2) extends <G>() => G extends U ? 1 : 2
? Y
: N
export type IfEquals<T, U, Y = unknown, N = never> =
(<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? Y : N
export function testOutput<A, B>(): (c: IfEquals<A, B>) => null {
return () => null
}

View File

@@ -1,6 +1,6 @@
import { Effects } from "../types"
import { createMainUtils } from "../util"
import { utils } from "../util/utils"
import { createUtils } from "../util/utils"
type Store = {
config: {
@@ -15,23 +15,23 @@ const noop = () => {}
describe("Store", () => {
test("types", async () => {
;async () => {
utils<Manifest, Store>(todo<Effects>()).store.setOwn("/config", {
createUtils<Manifest, Store>(todo<Effects>()).store.setOwn("/config", {
someValue: "a",
})
utils<Manifest, Store>(todo<Effects>()).store.setOwn(
createUtils<Manifest, Store>(todo<Effects>()).store.setOwn(
"/config/someValue",
"b",
)
utils<Manifest, Store>(todo<Effects>()).store.setOwn("", {
createUtils<Manifest, Store>(todo<Effects>()).store.setOwn("", {
config: { someValue: "b" },
})
utils<Manifest, Store>(todo<Effects>()).store.setOwn(
createUtils<Manifest, Store>(todo<Effects>()).store.setOwn(
"/config/someValue",
// @ts-expect-error Type is wrong for the setting value
5,
)
utils(todo<Effects>()).store.setOwn(
createUtils(todo<Effects>()).store.setOwn(
// @ts-expect-error Path is wrong
"/config/someVae3lue",
"someValue",
@@ -64,31 +64,31 @@ describe("Store", () => {
.const()
/// ----------------- ERRORS -----------------
utils<Manifest, Store>(todo<Effects>()).store.setOwn("", {
createUtils<Manifest, Store>(todo<Effects>()).store.setOwn("", {
// @ts-expect-error Type is wrong for the setting value
config: { someValue: "notInAOrB" },
})
utils<Manifest, Store>(todo<Effects>()).store.setOwn(
createUtils<Manifest, Store>(todo<Effects>()).store.setOwn(
"/config/someValue",
// @ts-expect-error Type is wrong for the setting value
"notInAOrB",
)
;(await utils<Manifest, Store>(todo<Effects>())
;(await createUtils<Manifest, Store>(todo<Effects>())
.store.getOwn("/config/someValue")
// @ts-expect-error Const should normally not be callable
.const()) satisfies string
;(await utils<Manifest, Store>(todo<Effects>())
;(await createUtils<Manifest, Store>(todo<Effects>())
.store.getOwn("/config")
// @ts-expect-error Const should normally not be callable
.const()) satisfies Store["config"]
await utils<Manifest, Store>(todo<Effects>())
await createUtils<Manifest, Store>(todo<Effects>())
// @ts-expect-error Path is wrong
.store.getOwn("/config/somdsfeValue")
// @ts-expect-error Const should normally not be callable
.const()
///
;(await utils<Manifest, Store>(todo<Effects>())
;(await createUtils<Manifest, Store>(todo<Effects>())
.store.getOwn("/config/someValue")
// @ts-expect-error satisfies type is wrong
.const()) satisfies number

View File

@@ -114,8 +114,8 @@ export type ValidIfNoStupidEscape<A> = A extends
| `${string}\\"${string}`
? never
: "" extends A & ""
? never
: A
? never
: A
export type ConfigRes = {
/** This should be the previous config, that way during set config we start with the previous */

View File

@@ -1,6 +1,6 @@
import fs from "fs/promises"
import * as fs from "fs/promises"
import * as T from "../types"
import cp from "child_process"
import * as cp from "child_process"
import { promisify } from "util"
import { Buffer } from "node:buffer"
export const execFile = promisify(cp.execFile)
@@ -8,18 +8,22 @@ export const execFile = promisify(cp.execFile)
export class Overlay {
private constructor(
readonly effects: T.Effects,
readonly imageId: string,
readonly rootfs: string,
) {}
static async of(effects: T.Effects, imageId: string) {
const rootfs = await effects.createOverlayedImage({ imageId })
for (const dirPart of ["dev", "sys", "proc", "run"] as const) {
const dir = await fs.mkdir(`${rootfs}/${dirPart}`, { recursive: true })
if (!dir) break
await execFile("mount", ["--bind", `/${dirPart}`, dir])
await fs.mkdir(`${rootfs}/${dirPart}`, { recursive: true })
await execFile("mount", [
"--rbind",
`/${dirPart}`,
`${rootfs}/${dirPart}`,
])
}
return new Overlay(effects, rootfs)
return new Overlay(effects, imageId, rootfs)
}
async mount(options: MountOptions, path: string): Promise<Overlay> {
@@ -55,20 +59,59 @@ export class Overlay {
command: string[],
options?: CommandOptions,
): Promise<{ stdout: string | Buffer; stderr: string | Buffer }> {
return await execFile("chroot", [this.rootfs, ...command], options)
let extra: string[] = []
if (options?.cwd) {
extra.push(`--workdir=${options.cwd}`)
delete options.cwd
}
if (options?.user) {
extra.push(`--user=${options.user}`)
delete options.user
}
return await execFile(
"start-cli",
[
"chroot",
`--env=/media/startos/env/${this.imageId}.env`,
...extra,
this.rootfs,
...command,
],
options,
)
}
spawn(
command: string[],
options?: CommandOptions,
): cp.ChildProcessWithoutNullStreams {
return cp.spawn("chroot", [this.rootfs, ...command], options)
let extra: string[] = []
if (options?.cwd) {
extra.push(`--workdir=${options.cwd}`)
delete options.cwd
}
if (options?.user) {
extra.push(`--user=${options.user}`)
delete options.user
}
return cp.spawn(
"start-cli",
[
"chroot",
`--env=/media/startos/env/${this.imageId}.env`,
...extra,
this.rootfs,
...command,
],
options,
)
}
}
export type CommandOptions = {
env?: { [variable: string]: string }
cwd?: string
user?: string
}
export type MountOptions =

View File

@@ -2,7 +2,7 @@ import * as matches from "ts-matches"
import * as YAML from "yaml"
import * as TOML from "@iarna/toml"
import * as T from "../types"
import fs from "fs"
import * as fs from "fs"
const previousPath = /(.+?)\/([^/]*)$/
@@ -60,12 +60,12 @@ export class FileHelper<A> {
async write(data: A, effects: T.Effects) {
if (previousPath.exec(this.path)) {
await new Promise((resolve, reject) =>
fs.mkdir(this.path, (err) => (!err ? resolve(null) : reject(err))),
fs.mkdir(this.path, (err: any) => (!err ? resolve(null) : reject(err))),
)
}
await new Promise((resolve, reject) =>
fs.writeFile(this.path, this.writeData(data), (err) =>
fs.writeFile(this.path, this.writeData(data), (err: any) =>
!err ? resolve(null) : reject(err),
),
)
@@ -76,7 +76,7 @@ export class FileHelper<A> {
}
return this.readData(
await new Promise((resolve, reject) =>
fs.readFile(this.path, (err, data) =>
fs.readFile(this.path, (err: any, data: any) =>
!err ? resolve(data.toString("utf-8")) : reject(err),
),
),

View File

@@ -1,6 +1,6 @@
// a,g,h,A-Z,,,,-
import crypto from "crypto"
import * as crypto from "crypto"
export function getRandomCharInSet(charset: string): string {
const set = stringToCharSet(charset)
let charIdx = crypto.randomInt(0, set.len)

View File

@@ -7,7 +7,7 @@ import "./deepEqual"
import "./deepMerge"
import "./Overlay"
import "./once"
import { utils } from "./utils"
import * as utils from "./utils"
import { SDKManifest } from "../manifest/ManifestTypes"
// prettier-ignore
@@ -23,7 +23,7 @@ export const isKnownError = (e: unknown): e is T.KnownError =>
declare const affine: unique symbol
export const createUtils = utils
export const createUtils = utils.createUtils
export const createMainUtils = <Manifest extends SDKManifest, Store>(
effects: T.Effects,
) => createUtils<Manifest, Store, {}>(effects)

View File

@@ -125,6 +125,7 @@ export type Utils<
command: ValidIfNoStupidEscape<A> | [string, ...string[]],
options: CommandOptions & {
mounts?: { path: string; options: MountOptions }[]
overlay?: Overlay
},
) => Promise<DaemonReturned>
store: {
@@ -141,7 +142,7 @@ export type Utils<
) => Promise<void>
}
}
export const utils = <
export const createUtils = <
Manifest extends SDKManifest,
Store = never,
WrapperOverWrite = { const: never },
@@ -224,10 +225,11 @@ export const utils = <
command: ValidIfNoStupidEscape<A> | [string, ...string[]],
options: CommandOptions & {
mounts?: { path: string; options: MountOptions }[]
overlay?: Overlay
},
): Promise<DaemonReturned> => {
const commands = splitCommand(command)
const overlay = await Overlay.of(effects, imageId)
const overlay = options.overlay || (await Overlay.of(effects, imageId))
for (let mount of options.mounts || []) {
await overlay.mount(mount.options, mount.path)
}
@@ -235,14 +237,14 @@ export const utils = <
env: options.env,
})
const answer = new Promise<null>((resolve, reject) => {
childProcess.stdout.on("data", (data) => {
childProcess.stdout.on("data", (data: any) => {
console.log(data.toString())
})
childProcess.stderr.on("data", (data) => {
childProcess.stderr.on("data", (data: any) => {
console.error(data.toString())
})
childProcess.on("close", (code) => {
childProcess.on("close", (code: any) => {
if (code === 0) {
return resolve(null)
}

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@start9labs/start-sdk",
"version": "0.4.0-rev0.lib0.rc8.beta2",
"version": "0.4.0-rev0.lib0.rc8.beta7",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@start9labs/start-sdk",
"version": "0.4.0-rev0.lib0.rc8.beta2",
"version": "0.4.0-rev0.lib0.rc8.beta7",
"license": "MIT",
"dependencies": {
"@iarna/toml": "^2.2.5",

View File

@@ -1,12 +1,28 @@
{
"name": "@start9labs/start-sdk",
"version": "0.4.0-rev0.lib0.rc8.beta2",
"version": "0.4.0-rev0.lib0.rc8.beta7",
"description": "Software development kit to facilitate packaging services for StartOS",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"main": "./cjs/lib/index.js",
"types": "./cjs/lib/index.d.ts",
"module": "./mjs/lib/index.js",
"sideEffects": true,
"exports": {
".": {
"import": "./mjs/lib/index.js",
"require": "./cjs/lib/index.js",
"types": "./cjs/lib/index.d.ts"
}
},
"typesVersion": {
">=3.1": {
"*": [
"cjs/lib/*"
]
}
},
"scripts": {
"test": "jest -c ./jest.config.js --coverage",
"buildOutput": "ts-node --esm ./lib/test/makeOutput.ts && npx prettier --write '**/*.ts'",
"buildOutput": "ts-node ./lib/test/makeOutput.ts && npx prettier --write '**/*.ts'",
"check": "tsc --noEmit"
},
"repository": {
@@ -40,6 +56,5 @@
"tsconfig-paths": "^3.14.2",
"typescript": "^5.0.4",
"vitest": "^0.29.2"
},
"declaration": true
}
}

View File

@@ -1,15 +0,0 @@
{
"targets": [
{
"extname": ".cjs",
"module": "commonjs"
},
{
"extname": ".mjs",
"module": "esnext"
}
],
"projects": [
"./tsconfig.json"
]
}

19
tsconfig-base.json Normal file
View File

@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "esnext",
"strict": true,
"outDir": "dist",
"preserveConstEnums": true,
"sourceMap": true,
"target": "es2017",
"pretty": true,
"declaration": true,
"noImplicitAny": true,
"esModuleInterop": true,
"types": ["node", "jest"],
"moduleResolution": "node",
"skipLibCheck": true
},
"include": ["lib/**/*"],
"exclude": ["lib/**/*.spec.ts", "lib/**/*.gen.ts", "list", "node_modules"]
}

8
tsconfig-cjs.json Normal file
View File

@@ -0,0 +1,8 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/cjs",
"target": "es2018"
}
}

View File

@@ -1,25 +1,8 @@
{
"include": [
"./lib/**/*.ts",
"scripts/oldSpecToBuilder.ts"
],
"inputs": [
"./lib/index.ts"
],
"extends": "./tsconfig-base.json",
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"moduleResolution": "node",
"declaration": true,
"outDir": "./dist/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
"module": "esnext",
"outDir": "dist/mjs",
"target": "esnext"
}
}
}