Skip to content

Commit

Permalink
feat: Config load & save done
Browse files Browse the repository at this point in the history
  • Loading branch information
JUSTIVE committed Jul 31, 2023
1 parent dadafc7 commit e8d9a23
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 47 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@mobily/ts-belt": "4.0.0-rc.5",
"@topcli/prompts": "^1.1.0",
"@topcli/spinner": "^2.1.1",
"@topcli/wcwidth": "^1.0.1",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/App.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Spinner } from "@topcli/spinner";
import * as Stat from "./Stat.mjs";
import * as Decorator from "./Decorator.mjs";
import { pipe } from 'effect';
import * as Config from './Config.mjs'
import * as Config from './Config.js'


type Running = EnvSet.t
Expand Down
42 changes: 0 additions & 42 deletions src/Config.mts

This file was deleted.

119 changes: 119 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { STEPS_KIND, STEPS_VARIANT } from './Step.Preset.mjs';
import {pipe,O,A, G, F, flow, D} from '@mobily/ts-belt'
import { question, select } from '@topcli/prompts';
import * as Decorator from './Decorator.mjs'
import fs from 'fs'
import chalk from 'chalk';

export type t = {
steps: STEPS_VARIANT[];
unSafeBranchList:string[],
sourceDir:string[],
verbose:boolean
}

const CONFIG_PATH = "tstw.config.json"

const fromJSONString = (json:string):O.Option<t> => {
const parsedJSON = JSON.parse(json)
const getArrayField = <T>(fieldName:string):O.Option<T[]> =>
pipe(
parsedJSON,
D.get(fieldName),
O.fromNullable,
O.flatMap(O.fromPredicate(G.isArray)),
)

const steps = pipe(
"steps",
getArrayField,
O.flatMap(O.fromPredicate(A.every((x:STEPS_VARIANT)=>STEPS_KIND.includes(x))))
) as STEPS_VARIANT[]

const unSafeBranchList = getArrayField<string>("unSafeBranchList")
const sourceDir = getArrayField<string>("sourceDir")
const verbose = pipe(
parsedJSON.verbose,
O.fromNullable,
O.flatMap(O.fromPredicate(G.isBoolean)),
O.getWithDefault(false)
)

return pipe(
steps,
O.flatMap(steps=>{
return pipe(
unSafeBranchList,
O.flatMap(unSafeBranchList=>{
return pipe(
sourceDir,
O.map(sourceDir=>{
return ({
steps,
unSafeBranchList,
sourceDir,
verbose
})
})
)
}))
})
)
}

const toJson = (config:t):string => {
return JSON.stringify(config)
}

const writeToFile = async(configJSON:string)=>{
Decorator.Line(`Writing config to ${CONFIG_PATH}`,chalk.yellow)
await fs.promises.writeFile(CONFIG_PATH,configJSON)
}

const readFromFile = async (path:string):Promise<O.Option<t>> => {
try{
const json = await fs.promises.readFile(path)
return fromJSONString(json.toString())
}
catch(e){
return O.None
}
}

const askAndMake = async ():Promise<t> => {
const askSteps = async (prevState:STEPS_VARIANT[]):Promise<STEPS_VARIANT[]> => {
const AVAILABLE_KINDS = (STEPS_KIND as STEPS_VARIANT[]).filter(x=>x!=='_ALWAYS_FAILING_ONLY_FOR_TESTING').filter((kind:STEPS_VARIANT)=>!prevState.includes(kind))
const userResponse = await select("Select steps to run",{
choices:["done",...AVAILABLE_KINDS].map((kind:string)=>({
value:kind,
label:kind
}))
})
if(userResponse === "done")
return prevState
else
return await askSteps([...prevState,userResponse] as STEPS_VARIANT[])
}

const unSafeBranchResponse = (await question("Enter unsafe branch list (space separated, regexes allowed)")).split(' ')
const sourceDirResponse = (await question("Enter source directory list (space separated)")).split(' ')
const stepsResponse = await askSteps([])

return {
steps:stepsResponse,
unSafeBranchList:unSafeBranchResponse,
sourceDir:sourceDirResponse,
verbose:false
}
}

export const load = async(): Promise<t> => {
const loadedConfig = await readFromFile(CONFIG_PATH)
if(O.isSome(loadedConfig))
return loadedConfig
else
return pipe(
await askAndMake(),
F.tap(flow(toJson,writeToFile))
)
}
2 changes: 1 addition & 1 deletion src/EnvSet.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Stat from "./Stat.mjs";
import * as Config from "./Config.mjs"
import * as Config from "./Config.js"
import * as PackageManager from "./PackageManager.mjs";
import { execSync } from 'child_process';
import { osLocale } from 'os-locale';
Expand Down
4 changes: 2 additions & 2 deletions src/PackageManager.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as O from 'effect/Option'
import * as F from 'effect/function'
import * as F from 'effect/Function'
import {select} from '@topcli/prompts'
import {pipe} from 'effect/function'
import {pipe} from 'effect/Function'
import fs from 'fs'
import {match} from 'ts-pattern'
import { VerboseLog } from './Decorator.mjs'
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@
"@/*": ["./src/*"]
}
},
"include": ["src/*.mts"],
"include": ["src/*.mts", "src/Config.ts"],
"exclude": ["js", "./dist/**/*"]
}
12 changes: 12 additions & 0 deletions tstw.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"steps": [
"BRANCH_CHECKING",
"FORMAT_TYPESCRIPT_FILES",
"ESLINT_CHECKING",
"TYPE_CHECKING",
"BUILD"
],
"unSafeBranchList": ["main", "master"],
"sourceDir": ["./src"],
"verbose": false
}

0 comments on commit e8d9a23

Please sign in to comment.