Skip to content

Commit

Permalink
fix(postinstall): more obvious error messages on symlink creation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Sokolov committed Apr 16, 2019
1 parent 542df64 commit 0e348c1
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions commands/postinstall/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,32 @@ const to = path.relative(from, cwd);
// Symlink file
const symlink = `${from}/${symlinkname}`;

function existsAndIsSymlink(path) {
const exists = fs.existsSync(path);
if (!exists) {
throw new Error(`${path} doesn't exist`);
}
const stats = fs.lstatSync(path);
if (stats.isDirectory()) {
throw new Error(`${path} is an existing directory`);
}
else if (stats.isFile()) {
throw new Error(`${path} is an existing file`);
}
else if (!stats.isSymbolicLink()) {
throw new Error(`${path} is neither a file, directory or symlink, but it exists somehow`);
}
}

try {
// Add symlink to the project root folder
fs.symlinkSync(to, symlink);
if (!fs.existsSync(symlink)) {
fs.symlinkSync(to, symlink);
console.log(`Symlink from ${symlink} to ${to} successfully created`);
}
existsAndIsSymlink(symlink);
} catch (err) {
// Show error message
console.log(`Error while creating symlink to ${to}\n`, err);
} finally {
// Check symlink file existance, otherwise fail
if (fs.existsSync(symlink)) {
console.log(`Symlink from ${symlink} to ${to} successfully created`);
} else {
process.exit(-1);
}
process.exit(-1);
}

0 comments on commit 0e348c1

Please sign in to comment.