Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add over-write for Windows and Linux OS #54

Closed
JanMakur opened this issue Jan 28, 2022 · 4 comments
Closed

Add over-write for Windows and Linux OS #54

JanMakur opened this issue Jan 28, 2022 · 4 comments

Comments

@JanMakur
Copy link

I'm creating a software with nodejs and I need to over-write shortcuts in it , I found nothing to read Data from a .lnk file else i would have used fs and did it, Could you please Try adding overwrite for windows or something which gives info of the shortcut.

@TheJaredWilcurt
Copy link
Member

Deleting files is something that each file system sucks at in unique ways. I would recommend pulling in fs-extra and using it to delete the file first, then use create-desktop-shortctuts. fs-extra has tons of code to handle all the weird edgecases you run into when trying to delete a file. Also you don't need to check if the file exists first before using it.

npm install --save fs-extra create-desktop-shortcuts

function createWindowsShortcut () {
  // Because we only specify Windows code in this function,
  // there is no point to run it from other platforms
  if (process.platform !== 'win32') {
    return;
  }

  // Imports
  const path = require('path');
  const createDesktopShortcut = require('create-desktop-shortcuts');
  const fs = require('fs-extra');

  // Variables/settings
  const name = 'My App Name';
  const outputPath = 'C:\\some\\folder';
  const outputFile = path.join(outputPath, name + '.lnk');

  // If the file already exists, it will be deleted first
  fs.removeSync(outputFile);

  // This returns true if the shortcut was created successfully
  const created = createDesktopShortcut({
    windows: {
      filePath: 'C:\\path\\to\\executable.exe',
      outputPath,
      name
    }
  );

  // if there was an issue creating the shortcut, throw
  if (!created) {
    throw 'Shortcut failed to create';
  }
}

// because both the delete and create are synchronous, we use a try/catch
try {
  createWindowsShortcut();
} catch (err) {
  console.log('Failed to either delete the old shortcut or create the new one');
  console.error(err);
}

fs-extra is a pretty bulky dependency for me to pull in to this library, since this is the first time this has been requested since 2019, I'd prefer to not increase the library size if it's something most people don't need.

@JanMakur
Copy link
Author

JanMakur commented Jan 29, 2022 via email

@TheJaredWilcurt
Copy link
Member

So I think what you want is to:

  1. Get properties from a shortcut
  2. Delete the shortcut (that is what fs-extra does, seen above)
  3. Create a new shortcut from the properties (that is what this library does)

So the problem is you are lacking a library to do step 1.

I think I can make that. gimme a day

@TheJaredWilcurt
Copy link
Member

Okay, maybe less than a day. You can use this library now, and pair it with the code above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants