Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const loadComposition = (inputFile: string, originalCode?: string, localCodePath?: string) => {
if (Capabilities.inBrowser() && originalCode) {
debug('loadComposition for webpack', originalCode)
return originalCode
}
const localSourcePath = localCodePath || Util.findFile(Util.expandHomeDir(inputFile))
debug('load source code from', localSourcePath)
try {
let errorMessage = ''
let logMessage = ''
debug('using require to process composition', localSourcePath)
// we'll override these temporarily
const log = console.log
const err = console.error
const exit = process.exit
const furlSequenceComponent = (parentActionName: string) => (component: string, idx: number): Promise => {
const match = component.match(patterns.action.expr.inline)
if (match && match.length === 3) {
// then this component is an inline function
debug('sequence component is inline function', match[0])
const body = `let main = ${match[0]}`
const candidateName = `${parentActionName}-${idx + 1}`
return createWithRetryOnName(body, parentActionName, execOptions, idx, currentIter, candidateName)
} else {
if (!Capabilities.inBrowser() && existsSync(Util.expandHomeDir(component))) {
debug('sequence component is local file', component)
// then we assume that the component identifies a local file
// note: the first step reserves a name
return createWithRetryOnName(
'let main=x=>x',
parentActionName,
execOptions,
idx,
currentIter,
basename(component.replace(/\..*$/, ''))
)
.then(reservedAction => reservedAction.name)
.then(reservedName =>
maybeComponentIsFile(reservedName, undefined, component, 'let', {}, args, { nested: true })
)
} else {
export const injectContent = () => {
if (Capabilities.inBrowser()) {
UI.injectCSS({
css: require('@kui-shell/plugin-grid/web/css/table.css'),
key: 'grid-visualization.table.css'
})
} else {
const root = dirname(require.resolve('@kui-shell/plugin-grid/package.json'))
UI.injectCSS(join(root, 'web/css/table.css'))
}
UI.injectCSS('https://cdnjs.cloudflare.com/ajax/libs/balloon-css/0.5.0/balloon.min.css') // tooltips
}
export const owOpts = (options = {}): WskOpts => {
if (isLinux) {
// options.forever = true
options['timeout'] = 5000
options['agent'] = agent()
}
if (Settings.theme.userAgent && !process.env.TEST_SPACE && !process.env.TRAVIS) {
// install a User-Agent header, except when running tests
debug('setting User-Agent', Settings.theme.userAgent)
options['User-Agent'] = Settings.theme.userAgent
}
if (Capabilities.inBrowser()) {
options['noUserAgent'] = true
}
return options
}
async function needle(method: 'get', url: string): Promise<{ statusCode: number; body: string }> {
if (Capabilities.inBrowser()) {
debug('fetch via xhr')
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open(method, url)
xhr.addEventListener('error', () => {
console.error('error in xhr', xhr)
reject(xhr.response || 'Internal Error')
})
xhr.addEventListener('load', () => {
resolve({
statusCode: xhr.status,
body: xhr.response.response
})
})
xhr.send()
})
}
const readViaFilesystem = () => {
debug('readViaFilesystem')
require('fs').readFile(Util.findFile(template), (err, data) => {
if (err) {
reject(err)
} else {
resolve(data.toString())
}
})
}
try {
debug('attempting to read template', template)
if (Capabilities.inBrowser()) {
if (template.indexOf('@') >= 0) {
readViaImport()
} else {
reject(new Error('Unable to read the given template'))
}
} else {
readViaFilesystem()
}
} catch (err) {
console.error('error with readViaImport', err)
readViaFilesystem()
}
})
}
export const injectCSS = async () => {
if (Capabilities.inBrowser()) {
await Promise.all([
UI.injectCSS({
css: require('diff2html/dist/diff2html.min.css'),
key: 'diff2html.css'
}),
UI.injectCSS({
css: require('@kui-shell/plugin-bash-like/web/css/my-diff2html.css'),
key: 'mydiff2html.css'
})
])
} else {
const root = dirname(require.resolve('diff2html/package.json'))
const ourRoot = dirname(require.resolve('@kui-shell/plugin-bash-like/package.json'))
await Promise.all([
UI.injectCSS(join(root, 'dist/diff2html.min.css')),
UI.injectCSS(join(ourRoot, 'web/css/my-diff2html.css'))
const updateLocalWskProps = (auth?: string, subject?: string): Promise => {
if (!Capabilities.inBrowser()) {
return readFromLocalWskProps(auth, subject).then(writeToLocalWskProps)
} else {
return Promise.resolve(auth)
}
}
new Promise((resolve, reject) => {
const ENOENT = () => {
const error = new Error('The specified file does not exist')
error['code'] = 404
return error
}
if (!Capabilities.inBrowser()) {
stat(input, err => {
if (err) {
reject(ENOENT())
} else {
resolve()
}
})
} else {
if (input.indexOf('@') >= 0) {
resolve()
} else {
reject(ENOENT())
}
}
})
new Promise(async (resolve, reject) => {
if (!Capabilities.inBrowser()) {
debug('readFile in headless mode or for electron')
fs.readFile(localCodePath, (err, data) => {
if (err) {
reject(err)
} else {
resolve(data.toString())
}
})
} else {
debug('readFile for webpack', localCodePath)
try {
const data = await import(
'@kui-shell/plugin-apache-composer/samples' +
localCodePath.replace(/^.*plugin-apache-composer\/samples(.*)$/, '$1')
)
debug('readFile for webpack done', data)