Skip to content

Commit faa6c3a

Browse files
committed
feat: Switch to native dialog for OpenTofu export
1 parent 42c3589 commit faa6c3a

File tree

1 file changed

+42
-27
lines changed
  • ocd/packages/react/src/components

1 file changed

+42
-27
lines changed

ocd/packages/react/src/components/Menu.ts

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -220,33 +220,33 @@ export const menuItems: MenuItem[] = [
220220
saveFile(ocdDocument).then((resp) => console.info('Saved', resp))
221221
}
222222
},
223-
{
224-
label: 'OpenTofu (Terraform)',
225-
click: (ocdDocument: OcdDocument, setOcdDocument: Function) => { // Convert to call to Electron API
226-
const writeTerraformFile = async (dirHandle: FileSystemDirectoryHandle, filename: string, contents: string[]) => {
227-
const fileHandle: FileSystemFileHandle = await dirHandle.getFileHandle(filename, {create: true})
228-
// @ts-ignore
229-
const writable = await fileHandle.createWritable()
230-
await writable.write(contents.join('\n'))
231-
await writable.close()
232-
return writable
233-
}
234-
const saveFile = async (ocdDocument: OcdDocument) => {
235-
try {
236-
// @ts-ignore
237-
const handle = await showDirectoryPicker()
238-
// const writable = await handle.createWritable()
239-
const exporter = new OcdTerraformExporter()
240-
const terraform = exporter.export(ocdDocument.design)
241-
const fileWriters = Object.entries(terraform).map(([k, v]) => writeTerraformFile(handle, k, v))
242-
return Promise.all(fileWriters)
243-
} catch (err: any) {
244-
console.error(err.name, err.message);
245-
}
246-
}
247-
saveFile(ocdDocument).then((resp) => console.info('Saved', resp))
248-
}
249-
},
223+
// {
224+
// label: 'OpenTofu (Terraform)',
225+
// click: (ocdDocument: OcdDocument, setOcdDocument: Function) => { // Convert to call to Electron API
226+
// const writeTerraformFile = async (dirHandle: FileSystemDirectoryHandle, filename: string, contents: string[]) => {
227+
// const fileHandle: FileSystemFileHandle = await dirHandle.getFileHandle(filename, {create: true})
228+
// // @ts-ignore
229+
// const writable = await fileHandle.createWritable()
230+
// await writable.write(contents.join('\n'))
231+
// await writable.close()
232+
// return writable
233+
// }
234+
// const saveFile = async (ocdDocument: OcdDocument) => {
235+
// try {
236+
// // @ts-ignore
237+
// const handle = await showDirectoryPicker()
238+
// // const writable = await handle.createWritable()
239+
// const exporter = new OcdTerraformExporter()
240+
// const terraform = exporter.export(ocdDocument.design)
241+
// const fileWriters = Object.entries(terraform).map(([k, v]) => writeTerraformFile(handle, k, v))
242+
// return Promise.all(fileWriters)
243+
// } catch (err: any) {
244+
// console.error(err.name, err.message);
245+
// }
246+
// }
247+
// saveFile(ocdDocument).then((resp) => console.info('Saved', resp))
248+
// }
249+
// },
250250
{
251251
label: 'Resource Manager',
252252
click: (ocdDocument: OcdDocument, setOcdDocument: Function, ocdConsoleConfig: OcdConsoleConfig, setOcdConsoleConfig: Function, activeFile: Record<string, any>, setActiveFile: Function) => {
@@ -256,6 +256,21 @@ export const menuItems: MenuItem[] = [
256256
setOcdDocument(clone)
257257
}
258258
},
259+
{
260+
label: 'OpenTofu (Terraform)',
261+
click: (ocdDocument: OcdDocument, setOcdDocument: Function, ocdConsoleConfig: OcdConsoleConfig, setOcdConsoleConfig: Function, activeFile: Record<string, any>, setActiveFile: Function) => {
262+
const suggestedFilename = activeFile.name.replaceAll('.okit', '.xlsx')
263+
const design = JSON.parse(JSON.stringify(ocdDocument.design)) // Resolve cloning issue when design changed
264+
console.debug('Export Excel Design:', design)
265+
OcdDesignFacade.exportToTerraform(design, suggestedFilename).then((results) => {
266+
if (!results.canceled) {
267+
console.debug('Design Exported to OpenTofu')
268+
} else {
269+
console.debug('Design Exported to OpenTofu Cancelled')
270+
}
271+
}).catch((resp) => {console.warn('Save Design Failed with', resp)})
272+
}
273+
},
259274
{
260275
label: 'Excel',
261276
click: (ocdDocument: OcdDocument, setOcdDocument: Function, ocdConsoleConfig: OcdConsoleConfig, setOcdConsoleConfig: Function, activeFile: Record<string, any>, setActiveFile: Function) => {

0 commit comments

Comments
 (0)