@@ -19,24 +19,30 @@ exports.builder = {
1919exports . handler = async function edit ( { recordPath, resumePath, encoding } ) {
2020 const fs = require ( 'fs' ) ;
2121 const { spawn } = require ( 'child_process' ) ;
22+ const path = require ( 'path' ) ;
23+ const tmp = require ( 'tmp' ) ;
2224 const TOML = require ( '@iarna/toml' ) ;
2325 const Repository = require ( '../lib/Repository.js' ) ;
2426 const Sheet = require ( '../lib/Sheet.js' )
2527 const repo = await Repository . getFromEnvironment ( { working : true } ) ;
2628 const git = await repo . getGit ( ) ;
2729
2830 // open record
29- const recordToml = fs . readFileSync ( resumePath || recordPath , encoding ) ;
31+ let recordToml = fs . readFileSync ( resumePath || recordPath , encoding ) ;
3032
31- // get temp path
32- const tempFilePath = await new Promise ( ( resolve , reject ) => {
33- const mktemp = spawn ( 'mktemp' , [ '-t' , 'gitsheet.XXXXXX.toml' ] ) ;
34-
35- let stdout = '' , stderr = '' ;
36- mktemp . stdout . on ( 'data' , chunk => stdout += chunk ) ;
37- mktemp . stderr . on ( 'data' , chunk => stderr += chunk ) ;
33+ // try to parse and format
34+ try {
35+ const record = TOML . parse ( recordToml ) ;
36+ recordToml = Sheet . stringifyRecord ( record ) ;
37+ } catch ( err ) {
38+ console . warn ( `Failed to parse opened record:\n ${ err } ` ) ;
39+ }
3840
39- mktemp . on ( 'close' , code => code === 0 ? resolve ( stdout . trim ( ) ) : reject ( stderr . trim ( ) ) ) ;
41+ // get temp path
42+ const { name : tempFilePath } = tmp . fileSync ( {
43+ prefix : path . basename ( recordPath , '.toml' ) ,
44+ postfix : '.toml' ,
45+ discardDescriptor : true ,
4046 } ) ;
4147
4248 // populate temp path
@@ -67,7 +73,7 @@ exports.handler = async function edit({ recordPath, resumePath, encoding }) {
6773 try {
6874 editedRecord = TOML . parse ( editedToml ) ;
6975 } catch ( err ) {
70- console . error ( `Failed to parse record:\n${ err } ` ) ;
76+ console . error ( `Failed to parse edited record:\n${ err } ` ) ;
7177 console . error ( `To resume editing, run: git sheet edit ${ recordPath } ${ tempFilePath } ` ) ;
7278 process . exit ( 1 ) ;
7379 }
0 commit comments