|
|
@ -1,5 +1,47 @@ |
|
|
|
// @flow
|
|
|
|
|
|
|
|
const configDir = (() => { |
|
|
|
const { STORYBOOK_ENV } = process.env |
|
|
|
if (!STORYBOOK_ENV) return '__NOTHING_TO_REPLACE__' |
|
|
|
const { LEDGER_CONFIG_DIRECTORY } = process.env |
|
|
|
if (LEDGER_CONFIG_DIRECTORY) return LEDGER_CONFIG_DIRECTORY |
|
|
|
const electron = require('electron') |
|
|
|
return (electron.app || electron.remote.app).getPath('userData') || '__NOTHING_TO_REPLACE__' |
|
|
|
})() |
|
|
|
|
|
|
|
const cwd = typeof process === 'object' ? process.cwd() || '.' : '__NOTHING_TO_REPLACE__' |
|
|
|
|
|
|
|
function filepathReplace(path: string) { |
|
|
|
if (!cwd) return path |
|
|
|
return path.replace(cwd, '.').replace(configDir, '$USER_DATA') |
|
|
|
} |
|
|
|
|
|
|
|
function filepathRecursiveReplacer(obj: mixed) { |
|
|
|
if (obj && typeof obj === 'object') { |
|
|
|
if (Array.isArray(obj)) { |
|
|
|
for (let i = 0; i < obj.length; i++) { |
|
|
|
const item = obj[i] |
|
|
|
if (typeof item === 'string') { |
|
|
|
obj[i] = filepathReplace(item) |
|
|
|
} else { |
|
|
|
filepathRecursiveReplacer(item) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
for (const k in obj) { |
|
|
|
if (obj.hasOwnProperty(k)) { |
|
|
|
const value = obj[k] |
|
|
|
if (typeof value === 'string') { |
|
|
|
obj[k] = filepathReplace(value) |
|
|
|
} else { |
|
|
|
filepathRecursiveReplacer(obj[k]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export default { |
|
|
|
url: (url: string): string => |
|
|
|
url |
|
|
@ -8,9 +50,7 @@ export default { |
|
|
|
|
|
|
|
appURI: (uri: string): string => uri.replace(/account\/[^/]/g, 'account/<HIDDEN>'), |
|
|
|
|
|
|
|
filepath: (filepath: string): string => { |
|
|
|
const i = filepath.indexOf('/node_modules') |
|
|
|
if (i !== -1) return `.${filepath.slice(i)}` |
|
|
|
return filepath |
|
|
|
}, |
|
|
|
filepath: filepathReplace, |
|
|
|
|
|
|
|
filepathRecursiveReplacer, |
|
|
|
} |
|
|
|