diff --git a/src/commands/solid-edit.ts b/src/commands/solid-edit.ts index 114e78a..24c7936 100644 --- a/src/commands/solid-edit.ts +++ b/src/commands/solid-edit.ts @@ -5,6 +5,8 @@ import fs from 'fs'; import { checkRemoteFileAccess, checkRemoteFileExists, getPodRoot } from "../utils/util"; import type { Logger } from '../logger'; import { ICommandOptions, setOptionDefaults } from './solid-command'; +import touch from "./solid-touch"; +const mime = require('mime-types'); const md5 = require('md5'); const child_process = require('child_process') @@ -12,6 +14,7 @@ const child_process = require('child_process') interface ICommandOptionsEdit extends ICommandOptions { editor?: string, touch?: boolean, + contentType?: string, } export default async function edit(url: string, options?: ICommandOptionsEdit) { @@ -26,7 +29,8 @@ export default async function edit(url: string, options?: ICommandOptionsEdit) { if (!commandOptions.touch) { throw new Error('Could not edit non-existing resource. Please use the --touch flag to create a new resource on edit.') } - await editNewFile(url, commandOptions) + await touch(url, commandOptions); + await editRemoteFile(url, commandOptions) } else { throw new Error(`No access rights for editing resource at ${url}.`) } @@ -97,48 +101,6 @@ async function editRemoteFile(url: string, options: ICommandOptionsEdit) { } } -async function editNewFile(url: string, options: ICommandOptionsEdit) { - const systemTmpDir = os.tmpdir() - const solidTmpDir = path.join(systemTmpDir, '.solid/') - let filename = url.split('/').reverse()[0] - const getRandomizedPrefix = () => (Math.random() + 1).toString(36).substring(7); - filename = getRandomizedPrefix()+"-"+filename - - let tmpFilePath: string | undefined; - try { - let tmpFilePath = path.join(solidTmpDir, filename); - fs.writeFileSync(tmpFilePath, "") - - await new Promise((resolve, reject) => { - var child = child_process.spawn(options.editor, [tmpFilePath], { - stdio: 'inherit' - }); - - child.on('exit', function (e: any, code: any) { - resolve(); - }); - }); - - // Wait for the user to finish editing the - (options.logger || console).log('Press any key to continue'); - await new Promise((resolve, reject) => { - process.stdin.setRawMode(true); - process.stdin.resume(); - process.stdin.on('data', () => resolve()); - }) - - await copy(tmpFilePath, url, options) - if (options.verbose) (options.logger || console).log('Remote file updated!'); - } catch (e) { - throw e - // TODO:: - } finally { - if(tmpFilePath) fs.unlinkSync(tmpFilePath); - if (options.verbose) (options.logger || console).log(`Removing local file file ${tmpFilePath}!`); - } -} - - async function fileMD5(path: string) { return new Promise( (resolve, reject) => { fs.readFile(path, (err,buf) => { diff --git a/src/commands/solid-touch.ts b/src/commands/solid-touch.ts index 72f30bf..e18b2eb 100644 --- a/src/commands/solid-touch.ts +++ b/src/commands/solid-touch.ts @@ -20,8 +20,7 @@ export default async function touch(url: string, options?: ICommandOptionsTouch) if (commandOptions.verbose) commandOptions.logger.log(`Remote file already exists`) } else { - let path = url.replace(/.*\//,'') - let mimetype = mime.lookup(path) + let path = url.replace(/.*\//,'') // todo: remove this? Might be leftover from shell experiment let contentType = options?.contentType diff --git a/src/shell/commands/edit.ts b/src/shell/commands/edit.ts index cab6c0d..224e150 100644 --- a/src/shell/commands/edit.ts +++ b/src/shell/commands/edit.ts @@ -20,6 +20,7 @@ export default class EditCommand extends SolidCommand { .argument('', 'Resource URL') .option('-e, --editor ', 'Use custom editor') .option('-t, --touch', 'Create file if not exists') // Should this be default? + .option('-c, --content-type ', 'Content type of the created resource when using --touch') .option('-v, --verbose', 'Log all operations') // Should this be default? .action(async (url, options) => { let programOpts = addEnvOptions(program.opts() || {});