Solo funcionan las rutas absolutas. Este código funciona. Si uso ../../../SYSTEM.USER/DOCUMENTS/DOCUMENTS.NFO'
no funcionará.
EDITAR: ¿Cómo puedo usar una variable como /%APPLICATION%/SYSTEM.USER/DOCUMENTS/DOCUMENTS.NFO
? ¿Es eso posible de esta manera?
Main.js
function OpenDocuments() { const path = require('path'); nw.Shell.showItemInFolder(path.resolve('D:/PROTON DRIVE/PROTON.USER/DOCUMENTS/DOCUMENTS.NFO')); }
Default.html
<div name="FolderMenuContainer" id="FolderMenuContainer" class="FolderMenuContainer"> <span name="FolderIcon" id="FolderIcon" class="material-icons"> description </span> <span name="FolderText" id="FolderText" class="FolderText" onclick="OpenDocuments()"> Documents </span> </div>
File Structure
Una forma que uso es usar path.join
y process.cwd()
. cwd
significa "Directorio de trabajo actual".
Ejemplo:
const path = require('path'); const place = path.join(process.cwd(), '..', '..', 'my-folder');
También puede usar __dirname
que es relativo al archivo desde el que se ejecuta el código, que puede ser diferente del directorio de trabajo actual.
const path = require('path'); const place = path.join(__dirname, '..', '..', 'my-folder');
Si desea utilizar la resolución de variables de entorno de Windows, le recomendaría crear una función auxiliar como esta:
/** * Replaces all environment variables with their actual value. * Keeps intact non-environment variables using '%'. * * @example * // 'C:\Users\bob\Desktop\AMD64' * resolveWindowsEnvironmentVariables('C:\Users\%USERNAME%\Desktop\%PROCESSOR_ARCHITECTURE%'); * * @param {string} filePath The input file path with percents. * @return {string} The resolved file path. */ function resolveWindowsEnvironmentVariables (filePath) { if (process.platform !== 'win32') { return filePath; } if (!filePath || typeof(filePath) !== 'string') { return undefined; } /** * Returns the value stored in the process.env for a given * environment variable. Or the original '%ASDF%' string if * not found. * * @example * replaceEnvironmentVariable('%USERNAME%', 'USERNAME'); * * @param {string} withPercents '%USERNAME%' * @param {string} withoutPercents 'USERNAME' * @return {string} 'bob' || '%USERNAME%' */ function replaceEnvironmentVariable (withPercents, withoutPercents) { let found = process.env[withoutPercents]; // 'C:\Users\%USERNAME%\Desktop\%asdf%' => 'C:\Users\bob\Desktop\%asdf%' return found || withPercents; } // 'C:\Users\%USERNAME%\Desktop\%PROCESSOR_ARCHITECTURE%' => 'C:\Users\bob\Desktop\AMD64' filePath = filePath.replace(/%([^%]+)%/g, replaceEnvironmentVariable); return filePath; }
Es posible que también desee resolver rutas que no sean de Windows
/** * Resolves paths that start with a tilde to the user's * home directory. * * @example * // '/home/bob/GitHub/Repo/file.png' * resolveTilde('~/GitHub/Repo/file.png'); * * @param {string} filePath '~/GitHub/Repo/file.png' * @return {string} '/home/bob/GitHub/Repo/file.png' */ function resolveTilde (filePath) { if (process.platform === 'win32') { return filePath; } if (!filePath || typeof(filePath) !== 'string') { return undefined; } const os = require('os'); // '~/folder/path' or '~' not '~alias/folder/path' if (filePath.startsWith('~/') || filePath === '~') { return filePath.replace('~', os.homedir()); } return filePath; }
¡Pude resolverlo gracias a Jaredcheeda! La solución específica a mi problema es esta:
function RunNotePad() { const path = require('path'); nw.Shell.openItem(path.join(process.cwd(), '..', '..', '..', 'PROTON.FILES/PROGRAM.FILES/V8.4.2_NotePad++', 'notepad++.exe')); }
Cada '..' retrocede un directorio. Intenté usar ../../../ en la ruta de mi archivo en mi código original y no funcionó. ¡Usar el método que Jerod explicó está funcionando!