-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
145 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |