Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (temporaryContainer) {
temporaryContainer.cleanup()
}
const addToPrompt = (extra: string): void => {
prompt.value = prompt.value + extra
// make sure the new text is visible
// see https://github.com/IBM/kui/issues/1367
prompt.scrollLeft = prompt.scrollWidth
}
if (dirname) {
// see if we need to add a trailing slash
const filepath = expandHomeDir(join(dirname, match))
isDirectory(filepath)
.then(isDir => {
if (isDir) {
// add a trailing slash if the dirname/match is a directory
debug('complete as directory')
addToPrompt(completion + '/')
} else {
// otherwise, dirname/match is not a directory
debug('complete as scalar')
addToPrompt(completion)
}
})
.catch(err => {
console.error(err)
})
} else {
return new Promise>((resolve, reject) => {
const filepath = argvNoOptions[1]
const { resolved: fullpath, viewer = 'open' } = findFileWithViewer(expandHomeDir(filepath))
debug('fullpath', fullpath, filepath, expandHomeDir(filepath))
const prettyFullPath = fullpath.replace(new RegExp(`^${process.env.HOME}`), '~')
// note: stat not lstat, because we want to follow the link
stat(fullpath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
const error: CodedError = new Error(err.message)
error.stack = err.stack
error.code = 404
reject(error)
} else {
reject(err)
}
} else if (stats.isDirectory() || !parsedOptions['with-data']) {
block: HTMLElement,
prompt: HTMLInputElement,
temporaryContainer: TemporaryContainer,
lastIdx: number
) => {
// dirname will "foo" in the above example; it
// could also be that last is itself the name
// of a directory
const lastIsDir = last.charAt(last.length - 1) === '/'
const dirname = lastIsDir ? last : pathDirname(last)
debug('suggest local file', dirname, last)
if (dirname) {
// then dirname exists! now scan the directory so we can find matches
readdir(expandHomeDir(dirname), (err, files) => {
if (err) {
debug('fs.readdir error', err)
} else {
const partial = basename(last) + (lastIsDir ? '/' : '')
const matches: string[] = files.filter(_f => {
const f = shellescape(_f)
return (lastIsDir || f.indexOf(partial) === 0) && !f.endsWith('~') && f !== '.' && f !== '..'
})
debug('fs.readdir success', partial, matches)
if (matches.length === 1) {
//
// then there is one unique match, so autofill it now;
// completion will be the bit we have to append to the current prompt.value
//
return new Promise>((resolve, reject) => {
const filepath = argvNoOptions[1]
const { resolved: fullpath, viewer = 'open' } = findFileWithViewer(expandHomeDir(filepath))
debug('fullpath', fullpath, filepath, expandHomeDir(filepath))
const prettyFullPath = fullpath.replace(new RegExp(`^${process.env.HOME}`), '~')
// note: stat not lstat, because we want to follow the link
stat(fullpath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
const error: CodedError = new Error(err.message)
error.stack = err.stack
error.code = 404
reject(error)
} else {
reject(err)
}
} else if (stats.isDirectory() || !parsedOptions['with-data']) {
resolve({
const doLs = (cmd: string) => async (opts: Arguments): Promise => {
const semi = await opts.REPL.semicolonInvoke(opts)
if (semi) {
debug('ls with semi', semi)
return semi
}
const { command, execOptions, argvNoOptions: argv } = opts
const filepathAsGiven = argv[argv.indexOf(cmd) + 1]
const filepath = findFile(expandHomeDir(filepathAsGiven), {
safe: true,
keepRelative: true
})
if (filepath.match(/app.asar/) && isSpecialDirectory(filepathAsGiven)) {
// for now, we don't support ls of @ directories
throw new Error('File not found')
}
const rest = command.replace(/^\s*(l)?ls/, '').replace(filepathAsGiven, filepath)
return doExec(
`ls -lh ${rest}`,
Object.assign({}, execOptions, {
nested: true,
raw: true,
env: {
async function open({ tab, argvNoOptions, REPL }: Arguments): Promise {
const filepath = argvNoOptions[argvNoOptions.indexOf('open') + 1]
debug('open', filepath)
const fullpath = findFile(expandHomeDir(filepath))
const suffix = filepath.substring(filepath.lastIndexOf('.') + 1)
if (
suffix === 'js' ||
suffix === 'ts' ||
suffix === 'go' ||
suffix === 'txt' ||
suffix === 'swift' ||
suffix === 'py' ||
suffix === 'json' ||
suffix === 'yaml'
) {
// open json and javascript files in the editor
return REPL.qexec(`edit "${filepath}"`)
} else if (
suffix === 'png' ||