@@ -11,12 +11,10 @@ import * as pty from 'node-pty'
1111import tmp from 'tmp'
1212import yaml from 'js-yaml'
1313
14+ import { home } from './boot'
1415import spawn from './spawn'
1516import * as nix from './nix'
1617
17- // The home directory for environments
18- let home = path . join ( path . dirname ( __dirname ) , 'envs' )
19-
2018export enum Platform {
2119 UNIX , WIN , DOCKER
2220}
@@ -103,21 +101,11 @@ export default class Environment {
103101 if ( read ) this . read ( )
104102 }
105103
106- /**
107- * Get or set the ome directory for environments
108- *
109- * @param value Value for home directory
110- */
111- static home ( value ?: string ) : string {
112- if ( value ) home = value
113- return home
114- }
115-
116104 /**
117105 * Path to the environment specification files
118106 */
119107 path ( ) : string {
120- return path . join ( Environment . home ( ) , this . name ) + '.yaml'
108+ return path . join ( home , 'envs' , this . name + '.yaml' )
121109 }
122110
123111 /**
@@ -137,7 +125,7 @@ export default class Environment {
137125 if ( this . adds && this . adds . length === 0 ) this . adds = undefined
138126 if ( this . removes && this . removes . length === 0 ) this . removes = undefined
139127
140- mkdirp . sync ( Environment . home ( ) )
128+ mkdirp . sync ( path . join ( home , 'envs' ) )
141129 const yml = yaml . safeDump ( this , { skipInvalid : true } )
142130 fs . writeFileSync ( this . path ( ) , yml )
143131 return this
@@ -180,13 +168,13 @@ export default class Environment {
180168 * List the environments on this machine
181169 */
182170 static async envs ( ) : Promise < Array < any > > {
183- const names = glob . sync ( '*.yaml' , { cwd : Environment . home ( ) } ) . map ( file => file . slice ( 0 , - 5 ) )
171+ const names = glob . sync ( '*.yaml' , { cwd : path . join ( home , 'envs' ) } ) . map ( file => file . slice ( 0 , - 5 ) )
184172 const envs = [ ]
185173 for ( let name of names ) {
186174 const env = new Environment ( name )
187175 envs . push ( Object . assign ( { } , env , {
188176 path : env . path ( ) ,
189- built : await nix . built ( name ) ,
177+ built : await env . built ( ) ,
190178 location : await nix . location ( name )
191179 } ) )
192180 }
@@ -313,8 +301,8 @@ export default class Environment {
313301 *
314302 * @param docker Also build a Docker container for the environment?
315303 */
316- async build ( docker : boolean = false ) : Promise < Environment > {
317- await nix . install ( this . name , this . pkgs ( ) , true )
304+ async build ( store ?: string , docker : boolean = false ) : Promise < Environment > {
305+ await nix . install ( this . name , this . pkgs ( ) , true , store )
318306
319307 if ( docker ) {
320308 // TODO: complete implementation of this, using these files...
@@ -337,6 +325,18 @@ export default class Environment {
337325 return this . write ( )
338326 }
339327
328+ /**
329+ * Has this environment been built?
330+ */
331+ async built ( ) : Promise < boolean > {
332+ try {
333+ await nix . location ( this . name )
334+ return true
335+ } catch {
336+ return false
337+ }
338+ }
339+
340340 /**
341341 * Upgrade all packages in the environment
342342 *
@@ -358,7 +358,7 @@ export default class Environment {
358358 *
359359 * @param pure Should the shell that this command is executed in be 'pure'?
360360 */
361- async vars ( pure : boolean = false ) : Promise < { [ key : string ] : string } > {
361+ async vars ( pure : boolean = false ) : Promise < { [ key : string ] : string } > {
362362 const location = await nix . location ( this . name )
363363
364364 let PATH = `${ location } /bin:${ location } /sbin`
@@ -379,6 +379,8 @@ export default class Environment {
379379 * @param pure Should the shell that this command is executed in be 'pure'?
380380 */
381381 async within ( command : string , pure : boolean = false ) {
382+ if ( ! this . built ( ) ) throw new Error ( `Environment "${ this . name } " has not be built yet.` )
383+
382384 // Get the path to bash because it may not be available in
383385 // the PATH of a pure shell
384386 let shell = await spawn ( 'which' , [ 'bash' ] )
@@ -395,6 +397,8 @@ export default class Environment {
395397 * @param sessionParameters Parameters of the session
396398 */
397399 async enter ( sessionParameters : SessionParameters ) {
400+ if ( ! this . built ( ) ) throw new Error ( `Environment "${ this . name } " has not be built yet.` )
401+
398402 let { command, platform, pure, stdin, stdout } = sessionParameters
399403 const location = await nix . location ( this . name )
400404
0 commit comments