Skip to content

Commit 097d232

Browse files
committed
Fix: change touch option on edit by call to touch command and editing newly created file to fix content type issues
1 parent 637990a commit 097d232

File tree

3 files changed

+7
-45
lines changed

3 files changed

+7
-45
lines changed

src/commands/solid-edit.ts

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import fs from 'fs';
55
import { checkRemoteFileAccess, checkRemoteFileExists, getPodRoot } from "../utils/util";
66
import type { Logger } from '../logger';
77
import { ICommandOptions, setOptionDefaults } from './solid-command';
8+
import touch from "./solid-touch";
9+
const mime = require('mime-types');
810

911
const md5 = require('md5');
1012
const child_process = require('child_process')
1113

1214
interface ICommandOptionsEdit extends ICommandOptions {
1315
editor?: string,
1416
touch?: boolean,
17+
contentType?: string,
1518
}
1619

1720
export default async function edit(url: string, options?: ICommandOptionsEdit) {
@@ -26,7 +29,8 @@ export default async function edit(url: string, options?: ICommandOptionsEdit) {
2629
if (!commandOptions.touch) {
2730
throw new Error('Could not edit non-existing resource. Please use the --touch flag to create a new resource on edit.')
2831
}
29-
await editNewFile(url, commandOptions)
32+
await touch(url, commandOptions);
33+
await editRemoteFile(url, commandOptions)
3034
} else {
3135
throw new Error(`No access rights for editing resource at ${url}.`)
3236
}
@@ -97,48 +101,6 @@ async function editRemoteFile(url: string, options: ICommandOptionsEdit) {
97101
}
98102
}
99103

100-
async function editNewFile(url: string, options: ICommandOptionsEdit) {
101-
const systemTmpDir = os.tmpdir()
102-
const solidTmpDir = path.join(systemTmpDir, '.solid/')
103-
let filename = url.split('/').reverse()[0]
104-
const getRandomizedPrefix = () => (Math.random() + 1).toString(36).substring(7);
105-
filename = getRandomizedPrefix()+"-"+filename
106-
107-
let tmpFilePath: string | undefined;
108-
try {
109-
let tmpFilePath = path.join(solidTmpDir, filename);
110-
fs.writeFileSync(tmpFilePath, "")
111-
112-
await new Promise<void>((resolve, reject) => {
113-
var child = child_process.spawn(options.editor, [tmpFilePath], {
114-
stdio: 'inherit'
115-
});
116-
117-
child.on('exit', function (e: any, code: any) {
118-
resolve();
119-
});
120-
});
121-
122-
// Wait for the user to finish editing the
123-
(options.logger || console).log('Press any key to continue');
124-
await new Promise<void>((resolve, reject) => {
125-
process.stdin.setRawMode(true);
126-
process.stdin.resume();
127-
process.stdin.on('data', () => resolve());
128-
})
129-
130-
await copy(tmpFilePath, url, options)
131-
if (options.verbose) (options.logger || console).log('Remote file updated!');
132-
} catch (e) {
133-
throw e
134-
// TODO::
135-
} finally {
136-
if(tmpFilePath) fs.unlinkSync(tmpFilePath);
137-
if (options.verbose) (options.logger || console).log(`Removing local file file ${tmpFilePath}!`);
138-
}
139-
}
140-
141-
142104
async function fileMD5(path: string) {
143105
return new Promise( (resolve, reject) => {
144106
fs.readFile(path, (err,buf) => {

src/commands/solid-touch.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export default async function touch(url: string, options?: ICommandOptionsTouch)
2020
if (commandOptions.verbose) commandOptions.logger.log(`Remote file already exists`)
2121
}
2222
else {
23-
let path = url.replace(/.*\//,'')
24-
let mimetype = mime.lookup(path)
23+
let path = url.replace(/.*\//,'') // todo: remove this? Might be leftover from shell experiment
2524

2625
let contentType = options?.contentType
2726

src/shell/commands/edit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class EditCommand extends SolidCommand {
2020
.argument('<url>', 'Resource URL')
2121
.option('-e, --editor <path_to_editor_executable>', 'Use custom editor')
2222
.option('-t, --touch', 'Create file if not exists') // Should this be default?
23+
.option('-c, --content-type <string>', 'Content type of the created resource when using --touch')
2324
.option('-v, --verbose', 'Log all operations') // Should this be default?
2425
.action(async (url, options) => {
2526
let programOpts = addEnvOptions(program.opts() || {});

0 commit comments

Comments
 (0)