Skip to content

Commit

Permalink
Moving away from tweenode.config.ts, since it bugs out on node and de…
Browse files Browse the repository at this point in the history
…no, may return later
  • Loading branch information
greatsquare0 committed Nov 4, 2024
1 parent 2adbc38 commit dd9ac54
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 25 deletions.
2 changes: 1 addition & 1 deletion __tests__/handle_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Tweenode Configuration', () => {
version: 'v2.2.0',
},
storyFormats: {
useTweegoBuiltin: false,
cleanTweegoBuiltins: false,
formats: [
{
name: 'Example',
Expand Down
1 change: 1 addition & 0 deletions build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ await Bun.build({
outdir: './dist',
sourcemap: 'external',
target: 'node',
format: 'esm',
minify: false,
external: ['adm-zip', 'fs-extra'],
plugins: [dts()],
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
"dependencies": {
"adm-zip": "^0.5.16",
"fs-extra": "^11.2.0"
}
},
"publishConfig": {

}
}
43 changes: 30 additions & 13 deletions src/download_tweego.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { existsSync } from 'node:fs'
import { mkdir, rm } from 'node:fs/promises'
import { resolve } from 'node:path'

import { loadConfig } from './handle_config'
import { config } from './state'
import { downloadFile, extract } from './utils'
import { getTweegoUrl } from './get_tweego_url'
import { TweenodeSetupConfig } from './handle_config'
import { emptyDir } from 'fs-extra'

export const getTweenodeFolderPath = () => {
return resolve(process.cwd(), './.tweenode/')
}

const config = await loadConfig()

// interface FileStat {
// name: string
// hash: any
Expand Down Expand Up @@ -82,15 +82,18 @@ const config = await loadConfig()

// }

export const downloadTweego = async () => {
export const downloadTweego = async (options?: TweenodeSetupConfig) => {
let usedConfig = config.setup!.tweegoBinaries!
if (options?.tweegoBinaries) {
usedConfig = options!.tweegoBinaries
}

if (!existsSync(getTweenodeFolderPath())) {
await mkdir(getTweenodeFolderPath(), { recursive: true })
}

const url =
config.setup!.tweegoBinaries!.customUrl !== ''
? getTweegoUrl()
: config.setup!.tweegoBinaries!.customUrl
usedConfig!.customUrl !== '' ? getTweegoUrl() : usedConfig!.customUrl
await downloadFile(
url!,
resolve(getTweenodeFolderPath(), url!.split('/').pop()!)
Expand All @@ -105,8 +108,19 @@ export const extractTweego = async () => {
await rm(archivePath)
}

export const downloadCustomStoryFormats = async () => {
for await (const format of config.setup!.storyFormats!.formats!) {
export const downloadCustomStoryFormats = async (
options?: TweenodeSetupConfig
) => {
let usedConfig = config.setup!.storyFormats
if (options?.storyFormats) {
usedConfig = options?.storyFormats
}

if (usedConfig!.cleanTweegoBuiltins) {
await emptyDir(resolve(getTweenodeFolderPath(), './storyformats/'))
}

for await (const format of usedConfig!.formats!) {
const archiveName = format.src!.split('/').pop()

let path = ''
Expand Down Expand Up @@ -140,13 +154,16 @@ export const downloadCustomStoryFormats = async () => {
}
}

export const setupTweego = async () => {
export const setupTweego = async (options?: TweenodeSetupConfig) => {
if (!existsSync(getTweenodeFolderPath())) {
await downloadTweego()
await downloadTweego(options)
await extractTweego()

if (config.setup!.storyFormats!.formats!.length > 0) {
await downloadCustomStoryFormats()
if (
config.setup!.storyFormats!.formats!.length > 0 ||
options!.storyFormats!.formats!.length > 0
) {
await downloadCustomStoryFormats(options)
}
}
}
3 changes: 1 addition & 2 deletions src/get_tweego_url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { loadConfig } from './handle_config'
const config = await loadConfig()
import { config } from './state'

const ARCH_MAPPING = {
ia32: 'x86',
Expand Down
13 changes: 11 additions & 2 deletions src/handle_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ export interface TweenodeSetupConfig {
storyFormats?: {
/**
* When `false`, it will delete all formats shipped with Tweego
* @deprecated use cleanTweegoBuiltins
*/
useTweegoBuiltin: boolean
useTweegoBuiltin?: boolean
/**
* When `true`, it will delete all formats shipped with Tweego
*/
cleanTweegoBuiltins: boolean
/**
* Array of custom formats to be downloaded
*/
Expand All @@ -93,6 +98,7 @@ export interface TweenodeSetupConfig {

export interface TweenodeDebugConfig {
writeToLog: boolean
detachProcess: boolean
}

export interface TweenodeConfig {
Expand Down Expand Up @@ -143,6 +149,7 @@ export const loadConfig = async (
export const defaultConfig: Partial<TweenodeConfig> = {
debug: {
writeToLog: false,
detachProcess: false,
},
build: {
output: {
Expand All @@ -157,7 +164,7 @@ export const defaultConfig: Partial<TweenodeConfig> = {
version: '2.1.1',
},
storyFormats: {
useTweegoBuiltin: false,
cleanTweegoBuiltins: false,
formats: [
{
name: 'sugarcube-2',
Expand All @@ -179,6 +186,8 @@ export const defaultConfig: Partial<TweenodeConfig> = {

/**
* Defines configs for use in Tweenode
* # This may get removed
* # Currently, only works as intended on Bun
* @example
* // ./tweenode.config.ts (Can be .js)
*import { defineConfig, type TweenodeConfig } from 'tweenode'
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from './handle_config'
import { defineConfig, defaultConfig } from './handle_config'
import { Tweenode } from './run_tweego'
import { setupTweego } from './download_tweego'

export { Tweenode, setupTweego, defineConfig }
export { Tweenode, setupTweego, defineConfig, defaultConfig }
8 changes: 4 additions & 4 deletions src/run_tweego.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'
import { resolve } from 'node:path'
import { appendFileSync } from 'node:fs'
import { outputFile, removeSync } from 'fs-extra/esm'
import {
loadConfig,
import type {
TweenodeBuildConfig,
TweenodeDebugConfig,
TweenodeSetupConfig,
} from './handle_config'

const loadedConfig = await loadConfig()
import { config as loadedConfig } from './state'

import { verifyBinarie } from './verify_tweego'
import { getTweenodeFolderPath } from './download_tweego'
Expand Down Expand Up @@ -48,8 +47,9 @@ export class Tweenode {
const args = getArgs(this.buildConfig)

try {
console.log(args)
this.childProcess = spawn(this.tweegoBinariePath, args, {
detached: true,
detached: this.debugConfig.detachProcess,
env: {
...process.env,
TWEEGO_PATH: resolve(getTweenodeFolderPath(), 'storyformats'),
Expand Down
5 changes: 5 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { loadConfig } from './handle_config'

let config = await loadConfig()

export { config }

0 comments on commit dd9ac54

Please sign in to comment.