1- import { Package , PackageRequirement , Installation , SemVer , semver , utils , hooks } from "libtea"
1+ import { Package , PackageRequirement , SemVer , semver , utils , hooks } from "libtea"
22import { isNumber , isPlainObject , isString , isArray , PlainObject } from "is-what"
3+ import { getScript } from "./usePantry.getScript.ts"
34import useGitLabAPI from "./useGitLabAPI.ts"
45import useGitHubAPI from "./useGitHubAPI.ts"
6+
57const { flatmap, validate } = utils
68const { useMoustaches } = hooks
7- import undent from "outdent"
89
910export interface Interpreter {
1011 project : string
@@ -38,7 +39,10 @@ async function resolve(spec: Package | PackageRequirement): Promise<Package> {
3839 const constraint = "constraint" in spec ? spec . constraint : new semver . Range ( `=${ spec . version } ` )
3940 const versions = await getVersions ( spec )
4041 const version = constraint . max ( versions )
41- if ( ! version ) throw new Error ( `not-found: version: ${ utils . pkg . str ( spec ) } ` )
42+ if ( ! version ) {
43+ console . error ( { versions} )
44+ throw new Error ( `not-found: version: ${ utils . pkg . str ( spec ) } ` )
45+ }
4246 console . debug ( { selected : version } )
4347 return { project : spec . project , version } ;
4448}
@@ -116,90 +120,6 @@ const getDistributable = async (pkg: Package) => {
116120 return { url, ref : undefined , stripComponents, type : 'url' }
117121}
118122
119- const getScript = async ( pkg : Package , key : 'build' | 'test' , deps : Installation [ ] ) => {
120- const yml = await hooks . usePantry ( ) . project ( pkg ) . yaml ( )
121- const node = yml [ key ]
122-
123- const mm = useMoustaches ( )
124- const script = ( input : unknown ) => {
125- const tokens = mm . tokenize . all ( pkg , deps )
126- if ( isArray ( input ) ) input = input . map ( obj => {
127- if ( isPlainObject ( obj ) ) {
128- let run = obj [ 'run' ]
129- if ( ! isString ( run ) ) throw new Error ( 'every node in a script YAML array must contain a `run` key' )
130- let cd = obj [ 'working-directory' ]
131- if ( cd ) {
132- cd = mm . apply ( validate . str ( cd ) , tokens )
133- run = undent `
134- OLDWD="$PWD"
135- mkdir -p "${ cd } "
136- cd "${ cd } "
137- ${ run . trim ( ) }
138- cd "$OLDWD"
139- unset OLDWD
140- `
141- }
142- let fixture_key = key == 'build' ? 'prop' : 'fixture'
143- let fixture = obj [ fixture_key ]
144- if ( fixture ) {
145- fixture_key = fixture_key . toUpperCase ( )
146- fixture = mm . apply ( validate . str ( fixture ) , tokens )
147- run = undent `
148- OLD_${ fixture_key } =$${ fixture_key }
149- ${ fixture_key } =$(mktemp)
150-
151- cat <<XYZ_TEA_EOF > $${ fixture_key }
152- ${ fixture }
153- XYZ_TEA_EOF
154-
155- ${ run }
156-
157- rm -f $${ fixture_key }
158-
159- if test -n "$${ fixture_key } "; then
160- ${ fixture_key } =$OLD_${ fixture_key }
161- else
162- unset ${ fixture_key }
163- fi
164- `
165- }
166-
167- return run . trim ( )
168- } else {
169- return `${ obj } ` . trim ( )
170- }
171- } ) . join ( "\n\n" )
172- return mm . apply ( validate . str ( input ) , tokens )
173- }
174-
175- if ( isPlainObject ( node ) ) {
176- let raw = script ( node . script )
177-
178- let wd = node [ "working-directory" ]
179- if ( wd ) {
180- wd = mm . apply ( wd , [
181- ...mm . tokenize . version ( pkg . version ) ,
182- ...mm . tokenize . host ( ) ,
183- ...mm . tokenize . pkg ( pkg )
184- ] )
185- raw = undent `
186- mkdir -p ${ wd }
187- cd ${ wd }
188-
189- ${ raw }
190- `
191- }
192-
193- const env = node . env
194- if ( isPlainObject ( env ) ) {
195- raw = `${ expand_env ( env , pkg , deps ) } \n\n${ raw } `
196- }
197- return raw
198- } else {
199- return script ( node )
200- }
201- }
202-
203123// deno-lint-ignore no-explicit-any
204124function coerceNumber ( input : any ) {
205125 if ( isNumber ( input ) ) return input
@@ -422,23 +342,10 @@ async function handleURLVersions(versions: PlainObject): Promise<SemVer[]> {
422342 return rv
423343}
424344
425- function expand_env ( env : PlainObject , pkg : Package , deps : Installation [ ] ) : string {
426- const { expand_env_obj } = hooks . usePantry ( )
427- return Object . entries ( expand_env_obj ( env , pkg , deps ) ) . map ( ( [ key , value ] ) => {
428- // weird POSIX string escaping/concat stuff
429- // eg. export FOO="bar ""$baz"" bun"
430- value = `"${ value . trim ( ) . replace ( / " / g, '""' ) } "`
431- while ( value . startsWith ( '""' ) ) value = value . slice ( 1 ) //FIXME lol better pls
432- while ( value . endsWith ( '""' ) ) value = value . slice ( 0 , - 1 ) //FIXME lol better pls
433-
434- return `export ${ key } =${ value } `
435- } ) . join ( "\n" )
436- }
437-
438345//FIXME inefficient, should be in libtea as part of .project()
439346async function filepath ( project : string ) {
440347 for await ( const pkg of hooks . usePantry ( ) . ls ( ) ) {
441348 if ( project == pkg . project ) return pkg . path
442349 }
443350 throw new Error ( `package.yml not found: ${ project } ` )
444- }
351+ }
0 commit comments