<@U8CT0V220> my paths all broke because I bundled ...
# suitescript
e
@jkabot my paths all broke because I bundled my project and the directory depth doesn't match
j
My workaround for this shortcoming has been to resolve the file path of the currently executing script file.
Copy code
export function getScriptFileId(scriptId: string): string {
	const results = search.create({
		type: 'script',
		filters: ['scriptid', 'is', scriptId],
		columns: ['scriptfile']
	}).run().getRange({ start: 0, end: 1 });
	if (results.length === 0) {
		return '';
	}
	return results[0].getValue('scriptfile');
}
Copy code
const scriptFileId = getScriptFileId(runtime.getCurrentScript().id);
const scriptFile = file.load({ id: scriptFileId });
const lastSlash = scriptFile.path.lastIndexOf('/');
const scriptFolderPath = scriptFile.path.substring(0, lastSlash);

const relativeFile = file.load({ id: `${scriptFolderPath}/path/to/relative/file` });
👍 1
e
That's a great idea, thanks for sharing that!
v
So you're loading the file of currently running script & getting a path -- wondering if there's any performance issue ?
j
It's a performance hit but I believe it's the only way to not hard code your file paths.
👍 1
v
@jkabot: export function getScriptFileId(scriptId: string): string --- NS lets you use this format ?
and const ?
How did you make that work ?
j
No I write it in typescript and then compile the script files when they are deployed to NetSuite
👏 1
v
Cool, what tool are you using ? Is the compilation> deployment automated in your setup ?
j
I have a gulpfile that sends files through the typescript compiler and then writes the output to NetSuite using https://github.com/suiteplus/nscabinet
v
Thank you for sharing that, I've been thinking of doing the same with ES6 compiler
j
Np, happy to help simple smile
s
This is one reason I avoid bundles. I expect SDF to take over in this regard.
e
Shouldn't
'../../Templates/my_template.xml'
would be the same as
'SuiteScripts/Traveler/Templates/my_template.xml'
?
j
Yes if the currently executing script is something like
'SuiteScripts/Traveler/somewhere/another-folder/your_script.js'
e
It's in
SuiteScripts/Traveler
.
Templates
is a sub directory under the executing file
j
what's the path of your script
e
SuiteScripts/Traveler/my_suitelet.js
the suitelet is trying to load
suitescripts/traveler/templates/my_template.xml
j
And your goal is to file.load({ id: 'SuiteScripts/Traveler/Templates/my_template.xml' }) ?
e
Yeah
j
So if you use a search strategy like what I shared,
e
I think using your function is the best way to handle it
aye
j
the value of
scriptFile.path
will be
'SuiteScripts/Traveler/my_suitelet.js'
and the value of
scriptFolderPath
will be
'SuiteScripts/Traveler'
so you can get to that xml file with
scriptFolderPath + '/Templates/my_template.xml'
e
I was just wondering why the double
../../
isn't finding the file if it's relative to the root of the cabinet
j
If you're reading the documentation for file.load it is pretty misleading because it uses the word "relative" but in my experience it has no support for "./" and "../"
It requires the full path of the file
e
Ah, damn, I feel like an idiot now lol
that would make sense, heh
j
the documentation would make more sense if it said "The absolute path of the file in the file cabinet"
👍 1
e
Agreed - thanks again for the function
j
np 👍