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

Fixed inconsistency for unlinking of pre-symlinked deps, and added option to toggle it #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/src/collectSymlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ export default class SymlinkCollector {
} else if (stats.isSymbolicLink()) {
const packageName = orgPrefix + path.basename(foundPath)
this.callback(packageName, foundPath)

// unlink the symlinked dependency folders prior to `npm install`
// (else, in symlinked folders, it deletes subdeps shared by root project)
console.log(`Unlinking symlinked dependency (protects its subdependencies): ${packageName}`)
fs.unlinkSync(foundPath)
}
}
resolve()
Expand Down
31 changes: 11 additions & 20 deletions app/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,34 @@ const fs = require('fs')
const chalk = require('chalk')

export default class Controller {
constructor (path, args) {
constructor (path, args, keepPrelinked) {
this.path = path
this.newPackages = args !== undefined ? args.join(' ') : undefined
this.keepPrelinked = keepPrelinked
}
run () {
async run () {
let targetPath = this.resolveRoot(this.path)
let packageNames
if (this.checkFile(targetPath, '.nsi.json')) {
console.log(chalk.green('\nFound .nsi.json file. \n Proceeding for safe install..'))
this.readFromNSIFile(targetPath)
packageNames = this.readFromNSIFile(targetPath)
} else {
console.log(chalk.red('\nUnable to find .nsi.json') +
chalk.cyan('\nScanning modules...') +
chalk.green('\nProceeding for safe install...'))
this.collectSymLinksFromNodeModules(targetPath)
packageNames = await this.collectSymLinksFromNodeModules(targetPath)
}
Shell.run(packageNames, targetPath, this.newPackages, this.keepPrelinked)
}

collectSymLinksFromNodeModules (path) {
async collectSymLinksFromNodeModules (path) {
let coll = new SymlinkCollector()
coll.execute(path).then((packages) => {
let packageName = []
packages.forEach(pkg => {
packageName.push(pkg.packageName)
})
this.runShell(packageName.join(' '), path)
})
const packages = await coll.execute(path)
return packages.map(pkg => pkg.packageName)
}

readFromNSIFile (targetPath) {
this.runShell(
JSON.parse(
fs.readFileSync(path.join(targetPath, '.nsi.json'), 'utf8'))
.join(' '), targetPath)
}

runShell (packages, targetPath) {
Shell.run(packages, targetPath, this.newPackages)
return JSON.parse(fs.readFileSync(path.join(targetPath, '.nsi.json'), 'utf8'))
}

checkFile (targetpath, filename) {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const packageData = require('../../package.json')
cli
.version(packageData.version, '-v, --version')
.option('-t, --target <n>', 'Path of the target directory')
.option('-k, --keep-prelinked', 'Keep pre-symlinked deps linked during npm-install (faster, but risks subdep changes)')
.usage('[options] <package> [morePackage ...]')
.parse(process.argv)

let path = cli.target !== undefined ? cli.target : process.cwd()
let keepPrelinked = cli.keepPrelinked !== undefined ? cli.keepPrelinked : false

let controller = new Controller(path, cli.args)
let controller = new Controller(path, cli.args, keepPrelinked)

console.log(chalk.gray('\nKEEP CALM!\nNSI GOT YOU COVERED'))
controller.run()
Expand Down
23 changes: 20 additions & 3 deletions app/src/shell.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { exec } from 'child_process'
const fs = require('fs')
const chalk = require('chalk')
const path = require('path')

export default class Shell {
static async shell (cmd) {
Expand All @@ -14,7 +16,22 @@ export default class Shell {
})
}

static async run (packages, targetdir, newPackages) {
static async run (packages, targetdir, newPackages, keepPrelinked) {
// unlink the symlinked dependency folders prior to `npm install`
// (else, in symlinked folders, it deletes subdeps shared by root project)
if (!keepPrelinked) {
console.log(chalk.greenBright('\nUnlinking symlinked dependencies (protects their subdependencies)'))
for (const packageName of packages) {
const packagePath = path.join(targetdir, 'node_modules', packageName)
if (!fs.existsSync(packagePath)) continue
const stats = fs.lstatSync(packagePath)
if (stats.isSymbolicLink()) {
this.print(packageName)
fs.unlinkSync(packagePath)
}
}
}

console.log(chalk.greenBright('\nInstalling Dependencies'))
let installCmd, log
if (newPackages !== undefined) {
Expand All @@ -24,10 +41,10 @@ export default class Shell {
}
let { stdout } = await this.shell(installCmd)
this.print(stdout)
console.log(chalk.greenBright('Rebuilding Links'))

console.log(chalk.greenBright('Rebuilding Links'))
if (packages.length) {
log = await this.shell(`cd ${targetdir} && npm link ${packages}`)
log = await this.shell(`cd ${targetdir} && npm link ${packages.join(' ')}`)
this.print(log.stdout)
} else {
this.print('No packages needed to relink')
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion out/cli.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.