Skip to content

Commit

Permalink
Fix: change touch option on edit by call to touch command and editing…
Browse files Browse the repository at this point in the history
… newly created file to fix content type issues
  • Loading branch information
Dexagod committed Jan 8, 2025
1 parent 637990a commit 097d232
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 45 deletions.
48 changes: 5 additions & 43 deletions src/commands/solid-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ 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')

interface ICommandOptionsEdit extends ICommandOptions {
editor?: string,
touch?: boolean,
contentType?: string,
}

export default async function edit(url: string, options?: ICommandOptionsEdit) {
Expand All @@ -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}.`)
}
Expand Down Expand Up @@ -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<void>((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<void>((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) => {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/solid-touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/shell/commands/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class EditCommand extends SolidCommand {
.argument('<url>', 'Resource URL')
.option('-e, --editor <path_to_editor_executable>', 'Use custom editor')
.option('-t, --touch', 'Create file if not exists') // Should this be default?
.option('-c, --content-type <string>', '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() || {});
Expand Down

0 comments on commit 097d232

Please sign in to comment.